blob: 9f59b1270a7c68da3086d22db2930d041cbaaec8 [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.
Siva Reddy Kallam5a8bae92017-12-22 16:05:27 +05307 * Copyright (C) 2005-2016 Broadcom Corporation.
8 * Copyright (C) 2016-2017 Broadcom Limited.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070011 * Derived from proprietary unpublished source code,
Siva Reddy Kallam5a8bae92017-12-22 16:05:27 +053012 * Copyright (C) 2000-2016 Broadcom Corporation.
13 * Copyright (C) 2016-2017 Broadcom Ltd.
Michael Chan49cabf42005-06-06 15:15:17 -070014 *
15 * Permission is hereby granted for the distribution of this firmware
16 * data in hexadecimal or equivalent format, provided this copyright
17 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/module.h>
22#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000023#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/compiler.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020030#include <linux/in.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000031#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/ioport.h>
33#include <linux/pci.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000038#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070040#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070041#include <linux/brcmphy.h>
Michael Chane565eec2014-01-03 10:09:12 -080042#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/if_vlan.h>
44#include <linux/ip.h>
45#include <linux/tcp.h>
46#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070047#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020048#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080049#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000050#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000051#include <linux/hwmon.h>
52#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030055#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000057#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Matt Carlsonbe947302012-12-03 19:36:57 +000061#include <uapi/linux/net_tstamp.h>
62#include <linux/ptp_clock_kernel.h>
63
David S. Miller49b6e95f2007-03-29 01:38:42 -070064#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070066#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68
Matt Carlson63532392008-11-03 16:49:57 -080069#define BAR_0 0
70#define BAR_2 2
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include "tg3.h"
73
Joe Perches63c3a662011-04-26 08:12:10 +000074/* Functions & macros to verify TG3_FLAGS types */
75
76static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
77{
78 return test_bit(flag, bits);
79}
80
81static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
82{
83 set_bit(flag, bits);
84}
85
86static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
87{
88 clear_bit(flag, bits);
89}
90
91#define tg3_flag(tp, flag) \
92 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
93#define tg3_flag_set(tp, flag) \
94 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
95#define tg3_flag_clear(tp, flag) \
96 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000099#define TG3_MAJ_NUM 3
Michael Chande750e42014-05-11 20:22:55 -0700100#define TG3_MIN_NUM 137
Matt Carlson6867c842010-07-11 09:31:44 +0000101#define DRV_MODULE_VERSION \
102 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chande750e42014-05-11 20:22:55 -0700103#define DRV_MODULE_RELDATE "May 11, 2014"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000105#define RESET_KIND_SHUTDOWN 0
106#define RESET_KIND_INIT 1
107#define RESET_KIND_SUSPEND 2
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define TG3_DEF_RX_MODE 0
110#define TG3_DEF_TX_MODE 0
111#define TG3_DEF_MSG_ENABLE \
112 (NETIF_MSG_DRV | \
113 NETIF_MSG_PROBE | \
114 NETIF_MSG_LINK | \
115 NETIF_MSG_TIMER | \
116 NETIF_MSG_IFDOWN | \
117 NETIF_MSG_IFUP | \
118 NETIF_MSG_RX_ERR | \
119 NETIF_MSG_TX_ERR)
120
Matt Carlson520b2752011-06-13 13:39:02 +0000121#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* length of time before we decide the hardware is borked,
124 * and dev->tx_timeout() should be called to fix the problem
125 */
Joe Perches63c3a662011-04-26 08:12:10 +0000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define TG3_TX_TIMEOUT (5 * HZ)
128
129/* hardware minimum and maximum for a single frame's data payload */
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -0400130#define TG3_MIN_MTU ETH_ZLEN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000132 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* These numbers seem to be hard coded in the NIC firmware somehow.
135 * You can't change the ring sizes, but you can change where you place
136 * them in the NIC onboard memory.
137 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000138#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000139 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000140 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000142#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000143 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000144 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define TG3_DEF_RX_JUMBO_RING_PENDING 100
146
147/* Do not place this n-ring entries value into the tp struct itself,
148 * we really want to expose these constants to GCC so that modulo et
149 * al. operations are done with shifts and masks instead of with
150 * hw multiply/modulo instructions. Another solution would be to
151 * replace things like '% foo' with '& (foo - 1)'.
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154#define TG3_TX_RING_SIZE 512
155#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
156
Matt Carlson2c49a442010-09-30 10:34:35 +0000157#define TG3_RX_STD_RING_BYTES(tp) \
158 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
159#define TG3_RX_JMB_RING_BYTES(tp) \
160 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
161#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000162 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
164 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
166
Matt Carlson287be122009-08-28 13:58:46 +0000167#define TG3_DMA_BYTE_ENAB 64
168
169#define TG3_RX_STD_DMA_SZ 1536
170#define TG3_RX_JMB_DMA_SZ 9046
171
172#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
173
174#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
175#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlson2c49a442010-09-30 10:34:35 +0000180#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
181 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000182
Matt Carlsond2757fc2010-04-12 06:58:27 +0000183/* Due to a hardware bug, the 5701 can only DMA to memory addresses
184 * that are at least dword aligned when used in PCIX mode. The driver
185 * works around this bug by double copying the packet. This workaround
186 * is built into the normal double copy length check for efficiency.
187 *
188 * However, the double copy is only necessary on those architectures
189 * where unaligned memory accesses are inefficient. For those architectures
190 * where unaligned memory accesses incur little penalty, we can reintegrate
191 * the 5701 in the normal rx path. Doing so saves a device structure
192 * dereference by hardcoding the double copy threshold in place.
193 */
194#define TG3_RX_COPY_THRESHOLD 256
195#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
196 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
197#else
198 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
199#endif
200
Matt Carlson81389f52011-08-31 11:44:49 +0000201#if (NET_IP_ALIGN != 0)
202#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
203#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000204#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000205#endif
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000208#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000209#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000210#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Matt Carlsonad829262008-11-21 17:16:16 -0800212#define TG3_RAW_IP_ALIGN 2
213
Michael Chane565eec2014-01-03 10:09:12 -0800214#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
215#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
216
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000217#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000218#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000219
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800220#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000221#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800222#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
223#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
224
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500225static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000226 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
229MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
230MODULE_LICENSE("GPL");
231MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800232MODULE_FIRMWARE(FIRMWARE_TG3);
233MODULE_FIRMWARE(FIRMWARE_TG3TSO);
234MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
237module_param(tg3_debug, int, 0);
238MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
239
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000240#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
241#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
242
Benoit Taine9baa3c32014-08-08 15:56:03 +0200243static const struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
266 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
267 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
270 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
271 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
278 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
284 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000292 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
293 PCI_VENDOR_ID_LENOVO,
294 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
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_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
298 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000317 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
318 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
320 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
321 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
322 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
326 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
336 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
338 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
348 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
349 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
350 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700351 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
352 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
353 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
354 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
355 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
356 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
357 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000358 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700359 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
362MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
363
Andreas Mohr50da8592006-08-14 23:54:30 -0700364static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000366} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 { "rx_octets" },
368 { "rx_fragments" },
369 { "rx_ucast_packets" },
370 { "rx_mcast_packets" },
371 { "rx_bcast_packets" },
372 { "rx_fcs_errors" },
373 { "rx_align_errors" },
374 { "rx_xon_pause_rcvd" },
375 { "rx_xoff_pause_rcvd" },
376 { "rx_mac_ctrl_rcvd" },
377 { "rx_xoff_entered" },
378 { "rx_frame_too_long_errors" },
379 { "rx_jabbers" },
380 { "rx_undersize_packets" },
381 { "rx_in_length_errors" },
382 { "rx_out_length_errors" },
383 { "rx_64_or_less_octet_packets" },
384 { "rx_65_to_127_octet_packets" },
385 { "rx_128_to_255_octet_packets" },
386 { "rx_256_to_511_octet_packets" },
387 { "rx_512_to_1023_octet_packets" },
388 { "rx_1024_to_1522_octet_packets" },
389 { "rx_1523_to_2047_octet_packets" },
390 { "rx_2048_to_4095_octet_packets" },
391 { "rx_4096_to_8191_octet_packets" },
392 { "rx_8192_to_9022_octet_packets" },
393
394 { "tx_octets" },
395 { "tx_collisions" },
396
397 { "tx_xon_sent" },
398 { "tx_xoff_sent" },
399 { "tx_flow_control" },
400 { "tx_mac_errors" },
401 { "tx_single_collisions" },
402 { "tx_mult_collisions" },
403 { "tx_deferred" },
404 { "tx_excessive_collisions" },
405 { "tx_late_collisions" },
406 { "tx_collide_2times" },
407 { "tx_collide_3times" },
408 { "tx_collide_4times" },
409 { "tx_collide_5times" },
410 { "tx_collide_6times" },
411 { "tx_collide_7times" },
412 { "tx_collide_8times" },
413 { "tx_collide_9times" },
414 { "tx_collide_10times" },
415 { "tx_collide_11times" },
416 { "tx_collide_12times" },
417 { "tx_collide_13times" },
418 { "tx_collide_14times" },
419 { "tx_collide_15times" },
420 { "tx_ucast_packets" },
421 { "tx_mcast_packets" },
422 { "tx_bcast_packets" },
423 { "tx_carrier_sense_errors" },
424 { "tx_discards" },
425 { "tx_errors" },
426
427 { "dma_writeq_full" },
428 { "dma_write_prioq_full" },
429 { "rxbds_empty" },
430 { "rx_discards" },
431 { "rx_errors" },
432 { "rx_threshold_hit" },
433
434 { "dma_readq_full" },
435 { "dma_read_prioq_full" },
436 { "tx_comp_queue_full" },
437
438 { "ring_set_send_prod_index" },
439 { "ring_status_update" },
440 { "nic_irqs" },
441 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000442 { "nic_tx_threshold_hit" },
443
444 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Matt Carlson48fa55a2011-04-13 11:05:06 +0000447#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000448#define TG3_NVRAM_TEST 0
449#define TG3_LINK_TEST 1
450#define TG3_REGISTER_TEST 2
451#define TG3_MEMORY_TEST 3
452#define TG3_MAC_LOOPB_TEST 4
453#define TG3_PHY_LOOPB_TEST 5
454#define TG3_EXT_LOOPB_TEST 6
455#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000456
457
Andreas Mohr50da8592006-08-14 23:54:30 -0700458static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700459 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000460} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000461 [TG3_NVRAM_TEST] = { "nvram test (online) " },
462 [TG3_LINK_TEST] = { "link test (online) " },
463 [TG3_REGISTER_TEST] = { "register test (offline)" },
464 [TG3_MEMORY_TEST] = { "memory test (offline)" },
465 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
466 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
467 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
468 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700469};
470
Matt Carlson48fa55a2011-04-13 11:05:06 +0000471#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
472
473
Michael Chanb401e9e2005-12-19 16:27:04 -0800474static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
475{
476 writel(val, tp->regs + off);
477}
478
479static u32 tg3_read32(struct tg3 *tp, u32 off)
480{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000481 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800482}
483
Matt Carlson0d3031d2007-10-10 18:02:43 -0700484static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
485{
486 writel(val, tp->aperegs + off);
487}
488
489static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
490{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000491 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
495{
Michael Chan68929142005-08-09 20:17:14 -0700496 unsigned long flags;
497
498 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700499 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700501 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700502}
503
504static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
505{
506 writel(val, tp->regs + off);
507 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Michael Chan68929142005-08-09 20:17:14 -0700510static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
511{
512 unsigned long flags;
513 u32 val;
514
515 spin_lock_irqsave(&tp->indirect_lock, flags);
516 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
517 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
518 spin_unlock_irqrestore(&tp->indirect_lock, flags);
519 return val;
520}
521
522static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
523{
524 unsigned long flags;
525
526 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
527 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
528 TG3_64BIT_REG_LOW, val);
529 return;
530 }
Matt Carlson66711e62009-11-13 13:03:49 +0000531 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700532 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
533 TG3_64BIT_REG_LOW, val);
534 return;
535 }
536
537 spin_lock_irqsave(&tp->indirect_lock, flags);
538 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
539 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
540 spin_unlock_irqrestore(&tp->indirect_lock, flags);
541
542 /* In indirect mode when disabling interrupts, we also need
543 * to clear the interrupt bit in the GRC local ctrl register.
544 */
545 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
546 (val == 0x1)) {
547 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
548 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
549 }
550}
551
552static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
553{
554 unsigned long flags;
555 u32 val;
556
557 spin_lock_irqsave(&tp->indirect_lock, flags);
558 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
559 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
560 spin_unlock_irqrestore(&tp->indirect_lock, flags);
561 return val;
562}
563
Michael Chanb401e9e2005-12-19 16:27:04 -0800564/* usec_wait specifies the wait time in usec when writing to certain registers
565 * where it is unsafe to read back the register without some delay.
566 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
567 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
568 */
569static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Joe Perches63c3a662011-04-26 08:12:10 +0000571 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800572 /* Non-posted methods */
573 tp->write32(tp, off, val);
574 else {
575 /* Posted method */
576 tg3_write32(tp, off, val);
577 if (usec_wait)
578 udelay(usec_wait);
579 tp->read32(tp, off);
580 }
581 /* Wait again after the read for the posted method to guarantee that
582 * the wait time is met.
583 */
584 if (usec_wait)
585 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Michael Chan09ee9292005-08-09 20:17:00 -0700588static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
589{
590 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000591 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
592 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
593 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700594 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700595}
596
Michael Chan20094932005-08-09 20:16:32 -0700597static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 void __iomem *mbox = tp->regs + off;
600 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000601 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000603 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
604 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 readl(mbox);
606}
607
Michael Chanb5d37722006-09-27 16:06:21 -0700608static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
609{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000610 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700611}
612
613static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
614{
615 writel(val, tp->regs + off + GRCMBOX_BASE);
616}
617
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000618#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700619#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000620#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
621#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
622#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700623
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000624#define tw32(reg, val) tp->write32(tp, reg, val)
625#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
626#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
627#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
630{
Michael Chan68929142005-08-09 20:17:14 -0700631 unsigned long flags;
632
Joe Perches41535772013-02-16 11:20:04 +0000633 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700634 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
635 return;
636
Michael Chan68929142005-08-09 20:17:14 -0700637 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000638 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700639 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
640 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Michael Chanbbadf502006-04-06 21:46:34 -0700642 /* Always leave this as zero. */
643 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
644 } else {
645 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
646 tw32_f(TG3PCI_MEM_WIN_DATA, val);
647
648 /* Always leave this as zero. */
649 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
650 }
Michael Chan68929142005-08-09 20:17:14 -0700651 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
655{
Michael Chan68929142005-08-09 20:17:14 -0700656 unsigned long flags;
657
Joe Perches41535772013-02-16 11:20:04 +0000658 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700659 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
660 *val = 0;
661 return;
662 }
663
Michael Chan68929142005-08-09 20:17:14 -0700664 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000665 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700666 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
667 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Michael Chanbbadf502006-04-06 21:46:34 -0700669 /* Always leave this as zero. */
670 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
671 } else {
672 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
673 *val = tr32(TG3PCI_MEM_WIN_DATA);
674
675 /* Always leave this as zero. */
676 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
677 }
Michael Chan68929142005-08-09 20:17:14 -0700678 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Matt Carlson0d3031d2007-10-10 18:02:43 -0700681static void tg3_ape_lock_init(struct tg3 *tp)
682{
683 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000685
Joe Perches41535772013-02-16 11:20:04 +0000686 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000687 regbase = TG3_APE_LOCK_GRANT;
688 else
689 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690
691 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000692 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
693 switch (i) {
694 case TG3_APE_LOCK_PHY0:
695 case TG3_APE_LOCK_PHY1:
696 case TG3_APE_LOCK_PHY2:
697 case TG3_APE_LOCK_PHY3:
698 bit = APE_LOCK_GRANT_DRIVER;
699 break;
700 default:
701 if (!tp->pci_fn)
702 bit = APE_LOCK_GRANT_DRIVER;
703 else
704 bit = 1 << tp->pci_fn;
705 }
706 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000707 }
708
Matt Carlson0d3031d2007-10-10 18:02:43 -0700709}
710
711static int tg3_ape_lock(struct tg3 *tp, int locknum)
712{
713 int i, off;
714 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000715 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700716
Joe Perches63c3a662011-04-26 08:12:10 +0000717 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700718 return 0;
719
720 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000721 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000722 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000723 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000724 case TG3_APE_LOCK_GRC:
725 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000726 if (!tp->pci_fn)
727 bit = APE_LOCK_REQ_DRIVER;
728 else
729 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000730 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000731 case TG3_APE_LOCK_PHY0:
732 case TG3_APE_LOCK_PHY1:
733 case TG3_APE_LOCK_PHY2:
734 case TG3_APE_LOCK_PHY3:
735 bit = APE_LOCK_REQ_DRIVER;
736 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000737 default:
738 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700739 }
740
Joe Perches41535772013-02-16 11:20:04 +0000741 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000742 req = TG3_APE_LOCK_REQ;
743 gnt = TG3_APE_LOCK_GRANT;
744 } else {
745 req = TG3_APE_PER_LOCK_REQ;
746 gnt = TG3_APE_PER_LOCK_GRANT;
747 }
748
Matt Carlson0d3031d2007-10-10 18:02:43 -0700749 off = 4 * locknum;
750
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000751 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700752
753 /* Wait for up to 1 millisecond to acquire lock. */
754 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000755 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000756 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700757 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800758 if (pci_channel_offline(tp->pdev))
759 break;
760
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761 udelay(10);
762 }
763
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000764 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700765 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000766 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700767 ret = -EBUSY;
768 }
769
770 return ret;
771}
772
773static void tg3_ape_unlock(struct tg3 *tp, int locknum)
774{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000775 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700776
Joe Perches63c3a662011-04-26 08:12:10 +0000777 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700778 return;
779
780 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000781 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000782 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000783 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000784 case TG3_APE_LOCK_GRC:
785 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000786 if (!tp->pci_fn)
787 bit = APE_LOCK_GRANT_DRIVER;
788 else
789 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000790 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000791 case TG3_APE_LOCK_PHY0:
792 case TG3_APE_LOCK_PHY1:
793 case TG3_APE_LOCK_PHY2:
794 case TG3_APE_LOCK_PHY3:
795 bit = APE_LOCK_GRANT_DRIVER;
796 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000797 default:
798 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700799 }
800
Joe Perches41535772013-02-16 11:20:04 +0000801 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000802 gnt = TG3_APE_LOCK_GRANT;
803 else
804 gnt = TG3_APE_PER_LOCK_GRANT;
805
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000806 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700807}
808
Matt Carlsonb65a3722012-07-16 16:24:00 +0000809static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000810{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 u32 apedata;
812
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000814 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000815 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000816
817 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000818 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
819 break;
820
Matt Carlsonb65a3722012-07-16 16:24:00 +0000821 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
822
Jonathan Toppinsea91df62018-03-14 12:36:25 -0400823 udelay(10);
Matt Carlsonb65a3722012-07-16 16:24:00 +0000824 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000825 }
826
Matt Carlsonb65a3722012-07-16 16:24:00 +0000827 return timeout_us ? 0 : -EBUSY;
828}
829
Florian Fainelli038e8932017-03-06 12:56:02 -0800830#ifdef CONFIG_TIGON3_HWMON
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000831static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
832{
833 u32 i, apedata;
834
835 for (i = 0; i < timeout_us / 10; i++) {
836 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
837
838 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
839 break;
840
841 udelay(10);
842 }
843
844 return i == timeout_us / 10;
845}
846
Michael Chan86449942012-10-02 20:31:14 -0700847static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
848 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000849{
850 int err;
851 u32 i, bufoff, msgoff, maxlen, apedata;
852
853 if (!tg3_flag(tp, APE_HAS_NCSI))
854 return 0;
855
856 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
857 if (apedata != APE_SEG_SIG_MAGIC)
858 return -ENODEV;
859
860 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
861 if (!(apedata & APE_FW_STATUS_READY))
862 return -EAGAIN;
863
864 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
865 TG3_APE_SHMEM_BASE;
866 msgoff = bufoff + 2 * sizeof(u32);
867 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
868
869 while (len) {
870 u32 length;
871
872 /* Cap xfer sizes to scratchpad limits. */
873 length = (len > maxlen) ? maxlen : len;
874 len -= length;
875
876 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
877 if (!(apedata & APE_FW_STATUS_READY))
878 return -EAGAIN;
879
880 /* Wait for up to 1 msec for APE to service previous event. */
881 err = tg3_ape_event_lock(tp, 1000);
882 if (err)
883 return err;
884
885 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
886 APE_EVENT_STATUS_SCRTCHPD_READ |
887 APE_EVENT_STATUS_EVENT_PENDING;
888 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
889
890 tg3_ape_write32(tp, bufoff, base_off);
891 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
892
893 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
894 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
895
896 base_off += length;
897
898 if (tg3_ape_wait_for_event(tp, 30000))
899 return -EAGAIN;
900
901 for (i = 0; length; i += 4, length -= 4) {
902 u32 val = tg3_ape_read32(tp, msgoff + i);
903 memcpy(data, &val, sizeof(u32));
904 data++;
905 }
906 }
907
908 return 0;
909}
Florian Fainelli038e8932017-03-06 12:56:02 -0800910#endif
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000911
Matt Carlsonb65a3722012-07-16 16:24:00 +0000912static int tg3_ape_send_event(struct tg3 *tp, u32 event)
913{
914 int err;
915 u32 apedata;
916
917 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
918 if (apedata != APE_SEG_SIG_MAGIC)
919 return -EAGAIN;
920
921 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
922 if (!(apedata & APE_FW_STATUS_READY))
923 return -EAGAIN;
924
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530925 /* Wait for up to 20 millisecond for APE to service previous event. */
926 err = tg3_ape_event_lock(tp, 20000);
Matt Carlsonb65a3722012-07-16 16:24:00 +0000927 if (err)
928 return err;
929
930 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
931 event | APE_EVENT_STATUS_EVENT_PENDING);
932
933 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
934 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
935
936 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000937}
938
939static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
940{
941 u32 event;
942 u32 apedata;
943
944 if (!tg3_flag(tp, ENABLE_APE))
945 return;
946
947 switch (kind) {
948 case RESET_KIND_INIT:
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530949 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000950 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
951 APE_HOST_SEG_SIG_MAGIC);
952 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
953 APE_HOST_SEG_LEN_MAGIC);
954 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
955 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
956 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
957 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
958 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
959 APE_HOST_BEHAV_NO_PHYLOCK);
960 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
961 TG3_APE_HOST_DRVR_STATE_START);
962
963 event = APE_EVENT_STATUS_STATE_START;
964 break;
965 case RESET_KIND_SHUTDOWN:
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000966 if (device_may_wakeup(&tp->pdev->dev) &&
967 tg3_flag(tp, WOL_ENABLE)) {
968 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
969 TG3_APE_HOST_WOL_SPEED_AUTO);
970 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
971 } else
972 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
973
974 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
975
976 event = APE_EVENT_STATUS_STATE_UNLOAD;
977 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000978 default:
979 return;
980 }
981
982 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
983
984 tg3_ape_send_event(tp, event);
985}
986
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530987static void tg3_send_ape_heartbeat(struct tg3 *tp,
988 unsigned long interval)
989{
990 /* Check if hb interval has exceeded */
991 if (!tg3_flag(tp, ENABLE_APE) ||
992 time_before(jiffies, tp->ape_hb_jiffies + interval))
993 return;
994
995 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
996 tp->ape_hb_jiffies = jiffies;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static void tg3_disable_ints(struct tg3 *tp)
1000{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 int i;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 tw32(TG3PCI_MISC_HOST_CTRL,
1004 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001005 for (i = 0; i < tp->irq_max; i++)
1006 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009static void tg3_enable_ints(struct tg3 *tp)
1010{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001011 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001012
Michael Chanbbe832c2005-06-24 20:20:04 -07001013 tp->irq_sync = 0;
1014 wmb();
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 tw32(TG3PCI_MISC_HOST_CTRL,
1017 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001019 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001020 for (i = 0; i < tp->irq_cnt; i++) {
1021 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001022
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001023 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001024 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001025 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1026
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001027 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001028 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001029
1030 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001031 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001032 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1033 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1034 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001035 tw32(HOSTCC_MODE, tp->coal_now);
1036
1037 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Matt Carlson17375d22009-08-28 14:02:18 +00001040static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001041{
Matt Carlson17375d22009-08-28 14:02:18 +00001042 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001043 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001044 unsigned int work_exists = 0;
1045
1046 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001047 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001048 if (sblk->status & SD_STATUS_LINK_CHG)
1049 work_exists = 1;
1050 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001051
1052 /* check for TX work to do */
1053 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1054 work_exists = 1;
1055
1056 /* check for RX work to do */
1057 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001058 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001059 work_exists = 1;
1060
1061 return work_exists;
1062}
1063
Matt Carlson17375d22009-08-28 14:02:18 +00001064/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001065 * similar to tg3_enable_ints, but it accurately determines whether there
1066 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001067 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Matt Carlson17375d22009-08-28 14:02:18 +00001069static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Matt Carlson17375d22009-08-28 14:02:18 +00001071 struct tg3 *tp = tnapi->tp;
1072
Matt Carlson898a56f2009-08-28 14:02:40 +00001073 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 mmiowb();
1075
David S. Millerfac9b832005-05-18 22:46:34 -07001076 /* When doing tagged status, this work check is unnecessary.
1077 * The last_tag we write above tells the chip which piece of
1078 * work we've completed.
1079 */
Joe Perches63c3a662011-04-26 08:12:10 +00001080 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001081 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001082 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void tg3_switch_clocks(struct tg3 *tp)
1086{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001087 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 u32 orig_clock_ctrl;
1089
Joe Perches63c3a662011-04-26 08:12:10 +00001090 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001091 return;
1092
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001093 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 orig_clock_ctrl = clock_ctrl;
1096 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1097 CLOCK_CTRL_CLKRUN_OENABLE |
1098 0x1f);
1099 tp->pci_clock_ctrl = clock_ctrl;
1100
Joe Perches63c3a662011-04-26 08:12:10 +00001101 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001103 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1104 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001107 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1108 clock_ctrl |
1109 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1110 40);
1111 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1112 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1113 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001115 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118#define PHY_BUSY_LOOPS 5000
1119
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001120static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1121 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 u32 frame_val;
1124 unsigned int loops;
1125 int ret;
1126
1127 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1128 tw32_f(MAC_MI_MODE,
1129 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1130 udelay(80);
1131 }
1132
Michael Chan8151ad52012-07-29 19:15:41 +00001133 tg3_ape_lock(tp, tp->phy_ape_lock);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *val = 0x0;
1136
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001137 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 MI_COM_PHY_ADDR_MASK);
1139 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1140 MI_COM_REG_ADDR_MASK);
1141 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 tw32_f(MAC_MI_COM, frame_val);
1144
1145 loops = PHY_BUSY_LOOPS;
1146 while (loops != 0) {
1147 udelay(10);
1148 frame_val = tr32(MAC_MI_COM);
1149
1150 if ((frame_val & MI_COM_BUSY) == 0) {
1151 udelay(5);
1152 frame_val = tr32(MAC_MI_COM);
1153 break;
1154 }
1155 loops -= 1;
1156 }
1157
1158 ret = -EBUSY;
1159 if (loops != 0) {
1160 *val = frame_val & MI_COM_DATA_MASK;
1161 ret = 0;
1162 }
1163
1164 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1165 tw32_f(MAC_MI_MODE, tp->mi_mode);
1166 udelay(80);
1167 }
1168
Michael Chan8151ad52012-07-29 19:15:41 +00001169 tg3_ape_unlock(tp, tp->phy_ape_lock);
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return ret;
1172}
1173
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001174static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1175{
1176 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1177}
1178
1179static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1180 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 u32 frame_val;
1183 unsigned int loops;
1184 int ret;
1185
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001186 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001187 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001188 return 0;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1191 tw32_f(MAC_MI_MODE,
1192 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1193 udelay(80);
1194 }
1195
Michael Chan8151ad52012-07-29 19:15:41 +00001196 tg3_ape_lock(tp, tp->phy_ape_lock);
1197
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001198 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 MI_COM_PHY_ADDR_MASK);
1200 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1201 MI_COM_REG_ADDR_MASK);
1202 frame_val |= (val & MI_COM_DATA_MASK);
1203 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 tw32_f(MAC_MI_COM, frame_val);
1206
1207 loops = PHY_BUSY_LOOPS;
1208 while (loops != 0) {
1209 udelay(10);
1210 frame_val = tr32(MAC_MI_COM);
1211 if ((frame_val & MI_COM_BUSY) == 0) {
1212 udelay(5);
1213 frame_val = tr32(MAC_MI_COM);
1214 break;
1215 }
1216 loops -= 1;
1217 }
1218
1219 ret = -EBUSY;
1220 if (loops != 0)
1221 ret = 0;
1222
1223 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1224 tw32_f(MAC_MI_MODE, tp->mi_mode);
1225 udelay(80);
1226 }
1227
Michael Chan8151ad52012-07-29 19:15:41 +00001228 tg3_ape_unlock(tp, tp->phy_ape_lock);
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return ret;
1231}
1232
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001233static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1234{
1235 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1236}
1237
Matt Carlsonb0988c12011-04-20 07:57:39 +00001238static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1239{
1240 int err;
1241
1242 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1243 if (err)
1244 goto done;
1245
1246 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1247 if (err)
1248 goto done;
1249
1250 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1251 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1252 if (err)
1253 goto done;
1254
1255 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1256
1257done:
1258 return err;
1259}
1260
1261static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1262{
1263 int err;
1264
1265 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1266 if (err)
1267 goto done;
1268
1269 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1270 if (err)
1271 goto done;
1272
1273 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1274 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1275 if (err)
1276 goto done;
1277
1278 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1279
1280done:
1281 return err;
1282}
1283
1284static int tg3_phydsp_read(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_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1291
1292 return err;
1293}
1294
1295static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1296{
1297 int err;
1298
1299 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1300 if (!err)
1301 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1302
1303 return err;
1304}
1305
Matt Carlson15ee95c2011-04-20 07:57:40 +00001306static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1307{
1308 int err;
1309
1310 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1311 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1312 MII_TG3_AUXCTL_SHDWSEL_MISC);
1313 if (!err)
1314 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1315
1316 return err;
1317}
1318
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001319static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1320{
1321 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1322 set |= MII_TG3_AUXCTL_MISC_WREN;
1323
1324 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1325}
1326
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001327static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1328{
1329 u32 val;
1330 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001331
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001332 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1333
1334 if (err)
1335 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001336
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001337 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001338 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1339 else
1340 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1341
1342 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1343 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1344
1345 return err;
1346}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001347
Nithin Sujir3ab71072013-09-20 16:46:55 -07001348static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1349{
1350 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1351 reg | val | MII_TG3_MISC_SHDW_WREN);
1352}
1353
Matt Carlson95e28692008-05-25 23:44:14 -07001354static int tg3_bmcr_reset(struct tg3 *tp)
1355{
1356 u32 phy_control;
1357 int limit, err;
1358
1359 /* OK, reset it, and poll the BMCR_RESET bit until it
1360 * clears or we time out.
1361 */
1362 phy_control = BMCR_RESET;
1363 err = tg3_writephy(tp, MII_BMCR, phy_control);
1364 if (err != 0)
1365 return -EBUSY;
1366
1367 limit = 5000;
1368 while (limit--) {
1369 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1370 if (err != 0)
1371 return -EBUSY;
1372
1373 if ((phy_control & BMCR_RESET) == 0) {
1374 udelay(40);
1375 break;
1376 }
1377 udelay(10);
1378 }
Roel Kluind4675b52009-02-12 16:33:27 -08001379 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001380 return -EBUSY;
1381
1382 return 0;
1383}
1384
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1386{
Francois Romieu3d165432009-01-19 16:56:50 -08001387 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001388 u32 val;
1389
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001390 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001391
Hauke Mehrtensead24022013-09-28 23:15:26 +02001392 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001393 val = -EIO;
1394
1395 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001396
1397 return val;
1398}
1399
1400static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1401{
Francois Romieu3d165432009-01-19 16:56:50 -08001402 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001403 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001404
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001405 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001406
Hauke Mehrtensead24022013-09-28 23:15:26 +02001407 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001408 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001409
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001410 spin_unlock_bh(&tp->lock);
1411
1412 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001413}
1414
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001415static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001416{
1417 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001419
Andrew Lunn7f854422016-01-06 20:11:18 +01001420 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001421 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001422 case PHY_ID_BCM50610:
1423 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001424 val = MAC_PHYCFG2_50610_LED_MODES;
1425 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001426 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001427 val = MAC_PHYCFG2_AC131_LED_MODES;
1428 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001429 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001430 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1431 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001432 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001433 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1434 break;
1435 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001436 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001437 }
1438
1439 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1440 tw32(MAC_PHYCFG2, val);
1441
1442 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001443 val &= ~(MAC_PHYCFG1_RGMII_INT |
1444 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1445 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001446 tw32(MAC_PHYCFG1, val);
1447
1448 return;
1449 }
1450
Joe Perches63c3a662011-04-26 08:12:10 +00001451 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001452 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1453 MAC_PHYCFG2_FMODE_MASK_MASK |
1454 MAC_PHYCFG2_GMODE_MASK_MASK |
1455 MAC_PHYCFG2_ACT_MASK_MASK |
1456 MAC_PHYCFG2_QUAL_MASK_MASK |
1457 MAC_PHYCFG2_INBAND_ENABLE;
1458
1459 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001460
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001461 val = tr32(MAC_PHYCFG1);
1462 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1463 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001464 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1465 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001466 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001467 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1469 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001470 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1471 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1472 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001473
Matt Carlsona9daf362008-05-25 23:49:44 -07001474 val = tr32(MAC_EXT_RGMII_MODE);
1475 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1476 MAC_RGMII_MODE_RX_QUALITY |
1477 MAC_RGMII_MODE_RX_ACTIVITY |
1478 MAC_RGMII_MODE_RX_ENG_DET |
1479 MAC_RGMII_MODE_TX_ENABLE |
1480 MAC_RGMII_MODE_TX_LOWPWR |
1481 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1483 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001484 val |= MAC_RGMII_MODE_RX_INT_B |
1485 MAC_RGMII_MODE_RX_QUALITY |
1486 MAC_RGMII_MODE_RX_ACTIVITY |
1487 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001488 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001489 val |= MAC_RGMII_MODE_TX_ENABLE |
1490 MAC_RGMII_MODE_TX_LOWPWR |
1491 MAC_RGMII_MODE_TX_RESET;
1492 }
1493 tw32(MAC_EXT_RGMII_MODE, val);
1494}
1495
Matt Carlson158d7ab2008-05-29 01:37:54 -07001496static void tg3_mdio_start(struct tg3 *tp)
1497{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001498 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1499 tw32_f(MAC_MI_MODE, tp->mi_mode);
1500 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001501
Joe Perches63c3a662011-04-26 08:12:10 +00001502 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001503 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001504 tg3_mdio_config_5785(tp);
1505}
1506
1507static int tg3_mdio_init(struct tg3 *tp)
1508{
1509 int i;
1510 u32 reg;
1511 struct phy_device *phydev;
1512
Joe Perches63c3a662011-04-26 08:12:10 +00001513 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001514 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001515
Matt Carlson69f11c92011-07-13 09:27:30 +00001516 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001517
Joe Perches41535772013-02-16 11:20:04 +00001518 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001519 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1520 else
1521 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1522 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001523 if (is_serdes)
1524 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001525 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1526 int addr;
1527
1528 addr = ssb_gige_get_phyaddr(tp->pdev);
1529 if (addr < 0)
1530 return addr;
1531 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001532 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001533 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001534
Matt Carlson158d7ab2008-05-29 01:37:54 -07001535 tg3_mdio_start(tp);
1536
Joe Perches63c3a662011-04-26 08:12:10 +00001537 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001538 return 0;
1539
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001540 tp->mdio_bus = mdiobus_alloc();
1541 if (tp->mdio_bus == NULL)
1542 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001543
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001544 tp->mdio_bus->name = "tg3 mdio bus";
1545 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001546 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001547 tp->mdio_bus->priv = tp;
1548 tp->mdio_bus->parent = &tp->pdev->dev;
1549 tp->mdio_bus->read = &tg3_mdio_read;
1550 tp->mdio_bus->write = &tg3_mdio_write;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001551 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001552
1553 /* The bus registration will look for all the PHYs on the mdio bus.
1554 * Unfortunately, it does not ensure the PHY is powered up before
1555 * accessing the PHY ID registers. A chip reset is the
1556 * quickest way to bring the device back to an operational state..
1557 */
1558 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1559 tg3_bmcr_reset(tp);
1560
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001561 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001562 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001563 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001564 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001565 return i;
1566 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001567
Andrew Lunn7f854422016-01-06 20:11:18 +01001568 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsona9daf362008-05-25 23:49:44 -07001569
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001570 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001571 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001572 mdiobus_unregister(tp->mdio_bus);
1573 mdiobus_free(tp->mdio_bus);
1574 return -ENODEV;
1575 }
1576
1577 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001578 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001579 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001580 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001581 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001582 case PHY_ID_BCM50610:
1583 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001584 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001585 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001586 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001587 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001588 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001589 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001590 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001591 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001592 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001593 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001594 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001595 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001596 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001597 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001598 case PHY_ID_RTL8201E:
1599 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001600 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001601 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001602 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001603 break;
1604 }
1605
Joe Perches63c3a662011-04-26 08:12:10 +00001606 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001607
Joe Perches41535772013-02-16 11:20:04 +00001608 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001609 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001610
1611 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001612}
1613
1614static void tg3_mdio_fini(struct tg3 *tp)
1615{
Joe Perches63c3a662011-04-26 08:12:10 +00001616 if (tg3_flag(tp, MDIOBUS_INITED)) {
1617 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001618 mdiobus_unregister(tp->mdio_bus);
1619 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001620 }
1621}
1622
Matt Carlson95e28692008-05-25 23:44:14 -07001623/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001624static inline void tg3_generate_fw_event(struct tg3 *tp)
1625{
1626 u32 val;
1627
1628 val = tr32(GRC_RX_CPU_EVENT);
1629 val |= GRC_RX_CPU_DRIVER_EVENT;
1630 tw32_f(GRC_RX_CPU_EVENT, val);
1631
1632 tp->last_event_jiffies = jiffies;
1633}
1634
1635#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1636
1637/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001638static void tg3_wait_for_event_ack(struct tg3 *tp)
1639{
1640 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001641 unsigned int delay_cnt;
1642 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001643
Matt Carlson4ba526c2008-08-15 14:10:04 -07001644 /* If enough time has passed, no wait is necessary. */
1645 time_remain = (long)(tp->last_event_jiffies + 1 +
1646 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1647 (long)jiffies;
1648 if (time_remain < 0)
1649 return;
1650
1651 /* Check if we can shorten the wait time. */
1652 delay_cnt = jiffies_to_usecs(time_remain);
1653 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1654 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1655 delay_cnt = (delay_cnt >> 3) + 1;
1656
1657 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001658 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1659 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001660 if (pci_channel_offline(tp->pdev))
1661 break;
1662
Matt Carlson4ba526c2008-08-15 14:10:04 -07001663 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001664 }
1665}
1666
1667/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001668static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001669{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001670 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001671
1672 val = 0;
1673 if (!tg3_readphy(tp, MII_BMCR, &reg))
1674 val = reg << 16;
1675 if (!tg3_readphy(tp, MII_BMSR, &reg))
1676 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001677 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001678
1679 val = 0;
1680 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1681 val = reg << 16;
1682 if (!tg3_readphy(tp, MII_LPA, &reg))
1683 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001684 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001685
1686 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001687 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001688 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1689 val = reg << 16;
1690 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1691 val |= (reg & 0xffff);
1692 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001693 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001694
1695 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1696 val = reg << 16;
1697 else
1698 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001699 *data++ = val;
1700}
1701
1702/* tp->lock is held. */
1703static void tg3_ump_link_report(struct tg3 *tp)
1704{
1705 u32 data[4];
1706
1707 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1708 return;
1709
1710 tg3_phy_gather_ump_data(tp, data);
1711
1712 tg3_wait_for_event_ack(tp);
1713
1714 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1715 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1716 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1717 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1718 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1719 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001720
Matt Carlson4ba526c2008-08-15 14:10:04 -07001721 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001722}
1723
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001724/* tp->lock is held. */
1725static void tg3_stop_fw(struct tg3 *tp)
1726{
1727 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1728 /* Wait for RX cpu to ACK the previous event. */
1729 tg3_wait_for_event_ack(tp);
1730
1731 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1732
1733 tg3_generate_fw_event(tp);
1734
1735 /* Wait for RX cpu to ACK this event. */
1736 tg3_wait_for_event_ack(tp);
1737 }
1738}
1739
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001740/* tp->lock is held. */
1741static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1742{
1743 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1744 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1745
1746 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1747 switch (kind) {
1748 case RESET_KIND_INIT:
1749 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1750 DRV_STATE_START);
1751 break;
1752
1753 case RESET_KIND_SHUTDOWN:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_UNLOAD);
1756 break;
1757
1758 case RESET_KIND_SUSPEND:
1759 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1760 DRV_STATE_SUSPEND);
1761 break;
1762
1763 default:
1764 break;
1765 }
1766 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001767}
1768
1769/* tp->lock is held. */
1770static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1771{
1772 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1773 switch (kind) {
1774 case RESET_KIND_INIT:
1775 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1776 DRV_STATE_START_DONE);
1777 break;
1778
1779 case RESET_KIND_SHUTDOWN:
1780 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1781 DRV_STATE_UNLOAD_DONE);
1782 break;
1783
1784 default:
1785 break;
1786 }
1787 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001788}
1789
1790/* tp->lock is held. */
1791static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1792{
1793 if (tg3_flag(tp, ENABLE_ASF)) {
1794 switch (kind) {
1795 case RESET_KIND_INIT:
1796 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1797 DRV_STATE_START);
1798 break;
1799
1800 case RESET_KIND_SHUTDOWN:
1801 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1802 DRV_STATE_UNLOAD);
1803 break;
1804
1805 case RESET_KIND_SUSPEND:
1806 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1807 DRV_STATE_SUSPEND);
1808 break;
1809
1810 default:
1811 break;
1812 }
1813 }
1814}
1815
1816static int tg3_poll_fw(struct tg3 *tp)
1817{
1818 int i;
1819 u32 val;
1820
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001821 if (tg3_flag(tp, NO_FWARE_REPORTED))
1822 return 0;
1823
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001824 if (tg3_flag(tp, IS_SSB_CORE)) {
1825 /* We don't use firmware. */
1826 return 0;
1827 }
1828
Joe Perches41535772013-02-16 11:20:04 +00001829 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001830 /* Wait up to 20ms for init done. */
1831 for (i = 0; i < 200; i++) {
1832 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1833 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001834 if (pci_channel_offline(tp->pdev))
1835 return -ENODEV;
1836
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001837 udelay(100);
1838 }
1839 return -ENODEV;
1840 }
1841
1842 /* Wait for firmware initialization to complete. */
1843 for (i = 0; i < 100000; i++) {
1844 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1845 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1846 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001847 if (pci_channel_offline(tp->pdev)) {
1848 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1849 tg3_flag_set(tp, NO_FWARE_REPORTED);
1850 netdev_info(tp->dev, "No firmware running\n");
1851 }
1852
1853 break;
1854 }
1855
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001856 udelay(10);
1857 }
1858
1859 /* Chip might not be fitted with firmware. Some Sun onboard
1860 * parts are configured like that. So don't signal the timeout
1861 * of the above loop as an error, but do report the lack of
1862 * running firmware once.
1863 */
1864 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1865 tg3_flag_set(tp, NO_FWARE_REPORTED);
1866
1867 netdev_info(tp->dev, "No firmware running\n");
1868 }
1869
Joe Perches41535772013-02-16 11:20:04 +00001870 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001871 /* The 57765 A0 needs a little more
1872 * time to do some important work.
1873 */
1874 mdelay(10);
1875 }
1876
1877 return 0;
1878}
1879
Matt Carlson95e28692008-05-25 23:44:14 -07001880static void tg3_link_report(struct tg3 *tp)
1881{
1882 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001883 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001884 tg3_ump_link_report(tp);
1885 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001886 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1887 (tp->link_config.active_speed == SPEED_1000 ?
1888 1000 :
1889 (tp->link_config.active_speed == SPEED_100 ?
1890 100 : 10)),
1891 (tp->link_config.active_duplex == DUPLEX_FULL ?
1892 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001893
Joe Perches05dbe002010-02-17 19:44:19 +00001894 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1895 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1896 "on" : "off",
1897 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1898 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001899
1900 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1901 netdev_info(tp->dev, "EEE is %s\n",
1902 tp->setlpicnt ? "enabled" : "disabled");
1903
Matt Carlson95e28692008-05-25 23:44:14 -07001904 tg3_ump_link_report(tp);
1905 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001906
1907 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001908}
1909
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001910static u32 tg3_decode_flowctrl_1000T(u32 adv)
1911{
1912 u32 flowctrl = 0;
1913
1914 if (adv & ADVERTISE_PAUSE_CAP) {
1915 flowctrl |= FLOW_CTRL_RX;
1916 if (!(adv & ADVERTISE_PAUSE_ASYM))
1917 flowctrl |= FLOW_CTRL_TX;
1918 } else if (adv & ADVERTISE_PAUSE_ASYM)
1919 flowctrl |= FLOW_CTRL_TX;
1920
1921 return flowctrl;
1922}
1923
Matt Carlson95e28692008-05-25 23:44:14 -07001924static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1925{
1926 u16 miireg;
1927
Steve Glendinninge18ce342008-12-16 02:00:00 -08001928 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001929 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001930 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001931 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001932 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001933 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1934 else
1935 miireg = 0;
1936
1937 return miireg;
1938}
1939
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001940static u32 tg3_decode_flowctrl_1000X(u32 adv)
1941{
1942 u32 flowctrl = 0;
1943
1944 if (adv & ADVERTISE_1000XPAUSE) {
1945 flowctrl |= FLOW_CTRL_RX;
1946 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1947 flowctrl |= FLOW_CTRL_TX;
1948 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1949 flowctrl |= FLOW_CTRL_TX;
1950
1951 return flowctrl;
1952}
1953
Matt Carlson95e28692008-05-25 23:44:14 -07001954static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1955{
1956 u8 cap = 0;
1957
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001958 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1959 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1960 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1961 if (lcladv & ADVERTISE_1000XPAUSE)
1962 cap = FLOW_CTRL_RX;
1963 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001964 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001965 }
1966
1967 return cap;
1968}
1969
Matt Carlsonf51f3562008-05-25 23:45:08 -07001970static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001971{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001972 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001973 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001974 u32 old_rx_mode = tp->rx_mode;
1975 u32 old_tx_mode = tp->tx_mode;
1976
Joe Perches63c3a662011-04-26 08:12:10 +00001977 if (tg3_flag(tp, USE_PHYLIB))
Andrew Lunn7f854422016-01-06 20:11:18 +01001978 autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001979 else
1980 autoneg = tp->link_config.autoneg;
1981
Joe Perches63c3a662011-04-26 08:12:10 +00001982 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001983 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001984 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001985 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001986 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001987 } else
1988 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001989
Matt Carlsonf51f3562008-05-25 23:45:08 -07001990 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001991
Steve Glendinninge18ce342008-12-16 02:00:00 -08001992 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001993 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1994 else
1995 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1996
Matt Carlsonf51f3562008-05-25 23:45:08 -07001997 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001998 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001999
Steve Glendinninge18ce342008-12-16 02:00:00 -08002000 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07002001 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
2002 else
2003 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
2004
Matt Carlsonf51f3562008-05-25 23:45:08 -07002005 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002006 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002007}
2008
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002009static void tg3_adjust_link(struct net_device *dev)
2010{
2011 u8 oldflowctrl, linkmesg = 0;
2012 u32 mac_mode, lcl_adv, rmt_adv;
2013 struct tg3 *tp = netdev_priv(dev);
Andrew Lunn7f854422016-01-06 20:11:18 +01002014 struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002015
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002016 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002017
2018 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2019 MAC_MODE_HALF_DUPLEX);
2020
2021 oldflowctrl = tp->link_config.active_flowctrl;
2022
2023 if (phydev->link) {
2024 lcl_adv = 0;
2025 rmt_adv = 0;
2026
2027 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2028 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002029 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002030 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002031 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002032 else
2033 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002034
2035 if (phydev->duplex == DUPLEX_HALF)
2036 mac_mode |= MAC_MODE_HALF_DUPLEX;
2037 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002038 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002039 tp->link_config.flowctrl);
2040
2041 if (phydev->pause)
2042 rmt_adv = LPA_PAUSE_CAP;
2043 if (phydev->asym_pause)
2044 rmt_adv |= LPA_PAUSE_ASYM;
2045 }
2046
2047 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2048 } else
2049 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2050
2051 if (mac_mode != tp->mac_mode) {
2052 tp->mac_mode = mac_mode;
2053 tw32_f(MAC_MODE, tp->mac_mode);
2054 udelay(40);
2055 }
2056
Joe Perches41535772013-02-16 11:20:04 +00002057 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002058 if (phydev->speed == SPEED_10)
2059 tw32(MAC_MI_STAT,
2060 MAC_MI_STAT_10MBPS_MODE |
2061 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2062 else
2063 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2064 }
2065
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002066 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2067 tw32(MAC_TX_LENGTHS,
2068 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2069 (6 << TX_LENGTHS_IPG_SHIFT) |
2070 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2071 else
2072 tw32(MAC_TX_LENGTHS,
2073 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2074 (6 << TX_LENGTHS_IPG_SHIFT) |
2075 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2076
Matt Carlson34655ad2012-02-22 12:35:18 +00002077 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002078 phydev->speed != tp->link_config.active_speed ||
2079 phydev->duplex != tp->link_config.active_duplex ||
2080 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002081 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082
Matt Carlson34655ad2012-02-22 12:35:18 +00002083 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002084 tp->link_config.active_speed = phydev->speed;
2085 tp->link_config.active_duplex = phydev->duplex;
2086
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002087 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002088
2089 if (linkmesg)
2090 tg3_link_report(tp);
2091}
2092
2093static int tg3_phy_init(struct tg3 *tp)
2094{
2095 struct phy_device *phydev;
2096
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002097 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002098 return 0;
2099
2100 /* Bring the PHY back to a known state. */
2101 tg3_bmcr_reset(tp);
2102
Andrew Lunn7f854422016-01-06 20:11:18 +01002103 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002104
2105 /* Attach the MAC to the PHY. */
Andrew Lunn84eff6d2016-01-06 20:11:10 +01002106 phydev = phy_connect(tp->dev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +00002107 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002108 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002109 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002110 return PTR_ERR(phydev);
2111 }
2112
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002114 switch (phydev->interface) {
2115 case PHY_INTERFACE_MODE_GMII:
2116 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002117 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002118 phydev->supported &= (PHY_GBIT_FEATURES |
2119 SUPPORTED_Pause |
2120 SUPPORTED_Asym_Pause);
2121 break;
2122 }
2123 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002124 case PHY_INTERFACE_MODE_MII:
2125 phydev->supported &= (PHY_BASIC_FEATURES |
2126 SUPPORTED_Pause |
2127 SUPPORTED_Asym_Pause);
2128 break;
2129 default:
Andrew Lunn7f854422016-01-06 20:11:18 +01002130 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002131 return -EINVAL;
2132 }
2133
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002134 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002135
2136 phydev->advertising = phydev->supported;
2137
Andrew Lunn22209432016-01-06 20:11:13 +01002138 phy_attached_info(phydev);
2139
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002140 return 0;
2141}
2142
2143static void tg3_phy_start(struct tg3 *tp)
2144{
2145 struct phy_device *phydev;
2146
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002147 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002148 return;
2149
Andrew Lunn7f854422016-01-06 20:11:18 +01002150 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002151
Matt Carlson80096062010-08-02 11:26:06 +00002152 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2153 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002154 phydev->speed = tp->link_config.speed;
2155 phydev->duplex = tp->link_config.duplex;
2156 phydev->autoneg = tp->link_config.autoneg;
2157 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002158 }
2159
2160 phy_start(phydev);
2161
2162 phy_start_aneg(phydev);
2163}
2164
2165static void tg3_phy_stop(struct tg3 *tp)
2166{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002167 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002168 return;
2169
Andrew Lunn7f854422016-01-06 20:11:18 +01002170 phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002171}
2172
2173static void tg3_phy_fini(struct tg3 *tp)
2174{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002175 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Andrew Lunn7f854422016-01-06 20:11:18 +01002176 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002177 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002178 }
2179}
2180
Matt Carlson941ec902011-08-19 13:58:23 +00002181static int tg3_phy_set_extloopbk(struct tg3 *tp)
2182{
2183 int err;
2184 u32 val;
2185
2186 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2187 return 0;
2188
2189 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2190 /* Cannot do read-modify-write on 5401 */
2191 err = tg3_phy_auxctl_write(tp,
2192 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2193 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2194 0x4c20);
2195 goto done;
2196 }
2197
2198 err = tg3_phy_auxctl_read(tp,
2199 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2200 if (err)
2201 return err;
2202
2203 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2204 err = tg3_phy_auxctl_write(tp,
2205 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2206
2207done:
2208 return err;
2209}
2210
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002211static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2212{
2213 u32 phytest;
2214
2215 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2216 u32 phy;
2217
2218 tg3_writephy(tp, MII_TG3_FET_TEST,
2219 phytest | MII_TG3_FET_SHADOW_EN);
2220 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2221 if (enable)
2222 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2223 else
2224 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2225 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2226 }
2227 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2228 }
2229}
2230
Matt Carlson6833c042008-11-21 17:18:59 -08002231static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2232{
2233 u32 reg;
2234
Joe Perches63c3a662011-04-26 08:12:10 +00002235 if (!tg3_flag(tp, 5705_PLUS) ||
2236 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002237 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002238 return;
2239
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002240 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002241 tg3_phy_fet_toggle_apd(tp, enable);
2242 return;
2243 }
2244
Nithin Sujir3ab71072013-09-20 16:46:55 -07002245 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002246 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2247 MII_TG3_MISC_SHDW_SCR5_SDTL |
2248 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002249 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002250 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2251
Nithin Sujir3ab71072013-09-20 16:46:55 -07002252 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002253
2254
Nithin Sujir3ab71072013-09-20 16:46:55 -07002255 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002256 if (enable)
2257 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2258
Nithin Sujir3ab71072013-09-20 16:46:55 -07002259 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002260}
2261
Joe Perches953c96e2013-04-09 10:18:14 +00002262static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002263{
2264 u32 phy;
2265
Joe Perches63c3a662011-04-26 08:12:10 +00002266 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002267 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 return;
2269
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002270 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002271 u32 ephy;
2272
Matt Carlson535ef6e2009-08-25 10:09:36 +00002273 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2274 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2275
2276 tg3_writephy(tp, MII_TG3_FET_TEST,
2277 ephy | MII_TG3_FET_SHADOW_EN);
2278 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002279 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002280 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002281 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002282 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2283 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002284 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002285 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002286 }
2287 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002288 int ret;
2289
2290 ret = tg3_phy_auxctl_read(tp,
2291 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2292 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002293 if (enable)
2294 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2295 else
2296 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002297 tg3_phy_auxctl_write(tp,
2298 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002299 }
2300 }
2301}
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303static void tg3_phy_set_wirespeed(struct tg3 *tp)
2304{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002305 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 u32 val;
2307
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002308 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return;
2310
Matt Carlson15ee95c2011-04-20 07:57:40 +00002311 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2312 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002313 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2314 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002317static void tg3_phy_apply_otp(struct tg3 *tp)
2318{
2319 u32 otp, phy;
2320
2321 if (!tp->phy_otp)
2322 return;
2323
2324 otp = tp->phy_otp;
2325
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002326 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002327 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002328
2329 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2330 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2331 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2332
2333 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2334 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2335 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2336
2337 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2338 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2339 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2340
2341 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2342 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2343
2344 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2345 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2346
2347 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2348 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2349 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2350
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002351 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002352}
2353
Nithin Sujir400dfba2013-05-18 06:26:53 +00002354static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2355{
2356 u32 val;
2357 struct ethtool_eee *dest = &tp->eee;
2358
2359 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2360 return;
2361
2362 if (eee)
2363 dest = eee;
2364
2365 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2366 return;
2367
2368 /* Pull eee_active */
2369 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2370 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2371 dest->eee_active = 1;
2372 } else
2373 dest->eee_active = 0;
2374
2375 /* Pull lp advertised settings */
2376 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2377 return;
2378 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2379
2380 /* Pull advertised and eee_enabled settings */
2381 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2382 return;
2383 dest->eee_enabled = !!val;
2384 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2385
2386 /* Pull tx_lpi_enabled */
2387 val = tr32(TG3_CPMU_EEE_MODE);
2388 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2389
2390 /* Pull lpi timer value */
2391 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2392}
2393
Joe Perches953c96e2013-04-09 10:18:14 +00002394static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002395{
2396 u32 val;
2397
2398 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2399 return;
2400
2401 tp->setlpicnt = 0;
2402
2403 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002404 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002405 tp->link_config.active_duplex == DUPLEX_FULL &&
2406 (tp->link_config.active_speed == SPEED_100 ||
2407 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002408 u32 eeectl;
2409
2410 if (tp->link_config.active_speed == SPEED_1000)
2411 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2412 else
2413 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2414
2415 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2416
Nithin Sujir400dfba2013-05-18 06:26:53 +00002417 tg3_eee_pull_config(tp, NULL);
2418 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002419 tp->setlpicnt = 2;
2420 }
2421
2422 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002423 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002424 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002425 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002426 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002427 }
2428
Matt Carlson52b02d02010-10-14 10:37:41 +00002429 val = tr32(TG3_CPMU_EEE_MODE);
2430 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2431 }
2432}
2433
Matt Carlsonb0c59432011-05-19 12:12:48 +00002434static void tg3_phy_eee_enable(struct tg3 *tp)
2435{
2436 u32 val;
2437
2438 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002439 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2440 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002441 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002442 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002443 val = MII_TG3_DSP_TAP26_ALNOKO |
2444 MII_TG3_DSP_TAP26_RMRXSTO;
2445 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002446 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002447 }
2448
2449 val = tr32(TG3_CPMU_EEE_MODE);
2450 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453static int tg3_wait_macro_done(struct tg3 *tp)
2454{
2455 int limit = 100;
2456
2457 while (limit--) {
2458 u32 tmp32;
2459
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002460 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if ((tmp32 & 0x1000) == 0)
2462 break;
2463 }
2464 }
Roel Kluind4675b52009-02-12 16:33:27 -08002465 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return -EBUSY;
2467
2468 return 0;
2469}
2470
2471static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2472{
2473 static const u32 test_pat[4][6] = {
2474 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2475 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2476 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2477 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2478 };
2479 int chan;
2480
2481 for (chan = 0; chan < 4; chan++) {
2482 int i;
2483
2484 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2485 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002486 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 for (i = 0; i < 6; i++)
2489 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2490 test_pat[chan][i]);
2491
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002492 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (tg3_wait_macro_done(tp)) {
2494 *resetp = 1;
2495 return -EBUSY;
2496 }
2497
2498 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2499 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002500 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (tg3_wait_macro_done(tp)) {
2502 *resetp = 1;
2503 return -EBUSY;
2504 }
2505
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002506 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (tg3_wait_macro_done(tp)) {
2508 *resetp = 1;
2509 return -EBUSY;
2510 }
2511
2512 for (i = 0; i < 6; i += 2) {
2513 u32 low, high;
2514
2515 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2516 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2517 tg3_wait_macro_done(tp)) {
2518 *resetp = 1;
2519 return -EBUSY;
2520 }
2521 low &= 0x7fff;
2522 high &= 0x000f;
2523 if (low != test_pat[chan][i] ||
2524 high != test_pat[chan][i+1]) {
2525 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2526 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2527 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2528
2529 return -EBUSY;
2530 }
2531 }
2532 }
2533
2534 return 0;
2535}
2536
2537static int tg3_phy_reset_chanpat(struct tg3 *tp)
2538{
2539 int chan;
2540
2541 for (chan = 0; chan < 4; chan++) {
2542 int i;
2543
2544 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2545 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002546 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 for (i = 0; i < 6; i++)
2548 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002549 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (tg3_wait_macro_done(tp))
2551 return -EBUSY;
2552 }
2553
2554 return 0;
2555}
2556
2557static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2558{
2559 u32 reg32, phy9_orig;
2560 int retries, do_phy_reset, err;
2561
2562 retries = 10;
2563 do_phy_reset = 1;
2564 do {
2565 if (do_phy_reset) {
2566 err = tg3_bmcr_reset(tp);
2567 if (err)
2568 return err;
2569 do_phy_reset = 0;
2570 }
2571
2572 /* Disable transmitter and interrupt. */
2573 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2574 continue;
2575
2576 reg32 |= 0x3000;
2577 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2578
2579 /* Set full-duplex, 1000 mbps. */
2580 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002581 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002584 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 continue;
2586
Matt Carlson221c5632011-06-13 13:39:01 +00002587 tg3_writephy(tp, MII_CTRL1000,
2588 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002590 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002591 if (err)
2592 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002595 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2598 if (!err)
2599 break;
2600 } while (--retries);
2601
2602 err = tg3_phy_reset_chanpat(tp);
2603 if (err)
2604 return err;
2605
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002606 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002609 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002611 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Matt Carlson221c5632011-06-13 13:39:01 +00002613 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002615 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2616 if (err)
2617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002619 reg32 &= ~0x3000;
2620 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2621
2622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002625static void tg3_carrier_off(struct tg3 *tp)
2626{
2627 netif_carrier_off(tp->dev);
2628 tp->link_up = false;
2629}
2630
Nithin Sujirce20f162013-04-09 08:48:04 +00002631static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2632{
2633 if (tg3_flag(tp, ENABLE_ASF))
2634 netdev_warn(tp->dev,
2635 "Management side-band traffic will be interrupted during phy settings change\n");
2636}
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638/* This will reset the tigon3 PHY if there is no valid
2639 * link unless the FORCE argument is non-zero.
2640 */
2641static int tg3_phy_reset(struct tg3 *tp)
2642{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002643 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 int err;
2645
Joe Perches41535772013-02-16 11:20:04 +00002646 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002647 val = tr32(GRC_MISC_CFG);
2648 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2649 udelay(40);
2650 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002651 err = tg3_readphy(tp, MII_BMSR, &val);
2652 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (err != 0)
2654 return -EBUSY;
2655
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002656 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002657 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002658 tg3_link_report(tp);
2659 }
2660
Joe Perches41535772013-02-16 11:20:04 +00002661 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2662 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2663 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 err = tg3_phy_reset_5703_4_5(tp);
2665 if (err)
2666 return err;
2667 goto out;
2668 }
2669
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002670 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002671 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2672 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002673 cpmuctrl = tr32(TG3_CPMU_CTRL);
2674 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2675 tw32(TG3_CPMU_CTRL,
2676 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2677 }
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 err = tg3_bmcr_reset(tp);
2680 if (err)
2681 return err;
2682
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002683 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002684 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2685 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002686
2687 tw32(TG3_CPMU_CTRL, cpmuctrl);
2688 }
2689
Joe Perches41535772013-02-16 11:20:04 +00002690 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2691 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002692 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2693 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2694 CPMU_LSPD_1000MB_MACCLK_12_5) {
2695 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2696 udelay(40);
2697 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2698 }
2699 }
2700
Joe Perches63c3a662011-04-26 08:12:10 +00002701 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002702 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002703 return 0;
2704
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002705 tg3_phy_apply_otp(tp);
2706
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002707 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002708 tg3_phy_toggle_apd(tp, true);
2709 else
2710 tg3_phy_toggle_apd(tp, false);
2711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002714 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002715 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2716 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002717 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002719
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002720 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002721 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2722 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002724
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002725 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002726 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002727 tg3_phydsp_write(tp, 0x000a, 0x310b);
2728 tg3_phydsp_write(tp, 0x201f, 0x9506);
2729 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002730 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002731 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002732 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002733 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002734 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2735 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2736 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2737 tg3_writephy(tp, MII_TG3_TEST1,
2738 MII_TG3_TEST1_TRIM_EN | 0x4);
2739 } else
2740 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2741
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002742 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002743 }
Michael Chanc424cb22006-04-29 18:56:34 -07002744 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Set Extended packet length bit (bit 14) on all chips that */
2747 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002748 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002750 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002751 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002753 err = tg3_phy_auxctl_read(tp,
2754 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2755 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002756 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2757 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759
2760 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2761 * jumbo frames transmission.
2762 */
Joe Perches63c3a662011-04-26 08:12:10 +00002763 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002764 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002765 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002766 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768
Joe Perches41535772013-02-16 11:20:04 +00002769 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002770 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002771 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002772 }
2773
Joe Perches41535772013-02-16 11:20:04 +00002774 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002775 tg3_phydsp_write(tp, 0xffb, 0x4000);
2776
Joe Perches953c96e2013-04-09 10:18:14 +00002777 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 tg3_phy_set_wirespeed(tp);
2779 return 0;
2780}
2781
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002782#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2783#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2784#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2785 TG3_GPIO_MSG_NEED_VAUX)
2786#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2787 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2788 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2789 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2790 (TG3_GPIO_MSG_DRVR_PRES << 12))
2791
2792#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2793 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2794 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2795 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2796 (TG3_GPIO_MSG_NEED_VAUX << 12))
2797
2798static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2799{
2800 u32 status, shift;
2801
Joe Perches41535772013-02-16 11:20:04 +00002802 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2803 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002804 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2805 else
2806 status = tr32(TG3_CPMU_DRV_STATUS);
2807
2808 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2809 status &= ~(TG3_GPIO_MSG_MASK << shift);
2810 status |= (newstat << shift);
2811
Joe Perches41535772013-02-16 11:20:04 +00002812 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2813 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002814 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2815 else
2816 tw32(TG3_CPMU_DRV_STATUS, status);
2817
2818 return status >> TG3_APE_GPIO_MSG_SHIFT;
2819}
2820
Matt Carlson520b2752011-06-13 13:39:02 +00002821static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2822{
2823 if (!tg3_flag(tp, IS_NIC))
2824 return 0;
2825
Joe Perches41535772013-02-16 11:20:04 +00002826 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2827 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2828 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002829 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2830 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002831
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002832 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2833
2834 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2835 TG3_GRC_LCLCTL_PWRSW_DELAY);
2836
2837 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2838 } else {
2839 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2840 TG3_GRC_LCLCTL_PWRSW_DELAY);
2841 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002842
Matt Carlson520b2752011-06-13 13:39:02 +00002843 return 0;
2844}
2845
2846static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2847{
2848 u32 grc_local_ctrl;
2849
2850 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002851 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2852 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002853 return;
2854
2855 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2856
2857 tw32_wait_f(GRC_LOCAL_CTRL,
2858 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2859 TG3_GRC_LCLCTL_PWRSW_DELAY);
2860
2861 tw32_wait_f(GRC_LOCAL_CTRL,
2862 grc_local_ctrl,
2863 TG3_GRC_LCLCTL_PWRSW_DELAY);
2864
2865 tw32_wait_f(GRC_LOCAL_CTRL,
2866 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2867 TG3_GRC_LCLCTL_PWRSW_DELAY);
2868}
2869
2870static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2871{
2872 if (!tg3_flag(tp, IS_NIC))
2873 return;
2874
Joe Perches41535772013-02-16 11:20:04 +00002875 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2876 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002877 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2878 (GRC_LCLCTRL_GPIO_OE0 |
2879 GRC_LCLCTRL_GPIO_OE1 |
2880 GRC_LCLCTRL_GPIO_OE2 |
2881 GRC_LCLCTRL_GPIO_OUTPUT0 |
2882 GRC_LCLCTRL_GPIO_OUTPUT1),
2883 TG3_GRC_LCLCTL_PWRSW_DELAY);
2884 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2885 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2886 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2887 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2888 GRC_LCLCTRL_GPIO_OE1 |
2889 GRC_LCLCTRL_GPIO_OE2 |
2890 GRC_LCLCTRL_GPIO_OUTPUT0 |
2891 GRC_LCLCTRL_GPIO_OUTPUT1 |
2892 tp->grc_local_ctrl;
2893 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2894 TG3_GRC_LCLCTL_PWRSW_DELAY);
2895
2896 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2897 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2898 TG3_GRC_LCLCTL_PWRSW_DELAY);
2899
2900 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2901 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2902 TG3_GRC_LCLCTL_PWRSW_DELAY);
2903 } else {
2904 u32 no_gpio2;
2905 u32 grc_local_ctrl = 0;
2906
2907 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002908 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002909 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2910 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2911 grc_local_ctrl,
2912 TG3_GRC_LCLCTL_PWRSW_DELAY);
2913 }
2914
2915 /* On 5753 and variants, GPIO2 cannot be used. */
2916 no_gpio2 = tp->nic_sram_data_cfg &
2917 NIC_SRAM_DATA_CFG_NO_GPIO2;
2918
2919 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2920 GRC_LCLCTRL_GPIO_OE1 |
2921 GRC_LCLCTRL_GPIO_OE2 |
2922 GRC_LCLCTRL_GPIO_OUTPUT1 |
2923 GRC_LCLCTRL_GPIO_OUTPUT2;
2924 if (no_gpio2) {
2925 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2926 GRC_LCLCTRL_GPIO_OUTPUT2);
2927 }
2928 tw32_wait_f(GRC_LOCAL_CTRL,
2929 tp->grc_local_ctrl | grc_local_ctrl,
2930 TG3_GRC_LCLCTL_PWRSW_DELAY);
2931
2932 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2933
2934 tw32_wait_f(GRC_LOCAL_CTRL,
2935 tp->grc_local_ctrl | grc_local_ctrl,
2936 TG3_GRC_LCLCTL_PWRSW_DELAY);
2937
2938 if (!no_gpio2) {
2939 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2940 tw32_wait_f(GRC_LOCAL_CTRL,
2941 tp->grc_local_ctrl | grc_local_ctrl,
2942 TG3_GRC_LCLCTL_PWRSW_DELAY);
2943 }
2944 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002945}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002946
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002947static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002948{
2949 u32 msg = 0;
2950
2951 /* Serialize power state transitions */
2952 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2953 return;
2954
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002955 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002956 msg = TG3_GPIO_MSG_NEED_VAUX;
2957
2958 msg = tg3_set_function_status(tp, msg);
2959
2960 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2961 goto done;
2962
2963 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2964 tg3_pwrsrc_switch_to_vaux(tp);
2965 else
2966 tg3_pwrsrc_die_with_vmain(tp);
2967
2968done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002969 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002970}
2971
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002972static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Matt Carlson683644b2011-03-09 16:58:23 +00002974 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Matt Carlson334355a2010-01-20 16:58:10 +00002976 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002977 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return;
2979
Joe Perches41535772013-02-16 11:20:04 +00002980 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2981 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2982 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002983 tg3_frob_aux_power_5717(tp, include_wol ?
2984 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002985 return;
2986 }
2987
2988 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002989 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002991 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002992
Michael Chanbc1c7562006-03-20 17:48:03 -08002993 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002994 if (dev_peer) {
2995 struct tg3 *tp_peer = netdev_priv(dev_peer);
2996
Joe Perches63c3a662011-04-26 08:12:10 +00002997 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002998 return;
2999
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003000 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003001 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003002 need_vaux = true;
3003 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08003004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003006 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3007 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003008 need_vaux = true;
3009
Matt Carlson520b2752011-06-13 13:39:02 +00003010 if (need_vaux)
3011 tg3_pwrsrc_switch_to_vaux(tp);
3012 else
3013 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003016static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3017{
3018 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3019 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003020 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003021 if (speed != SPEED_10)
3022 return 1;
3023 } else if (speed == SPEED_10)
3024 return 1;
3025
3026 return 0;
3027}
3028
Nithin Sujir44f3b502013-05-13 11:04:15 +00003029static bool tg3_phy_power_bug(struct tg3 *tp)
3030{
3031 switch (tg3_asic_rev(tp)) {
3032 case ASIC_REV_5700:
3033 case ASIC_REV_5704:
3034 return true;
3035 case ASIC_REV_5780:
3036 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3037 return true;
3038 return false;
3039 case ASIC_REV_5717:
3040 if (!tp->pci_fn)
3041 return true;
3042 return false;
3043 case ASIC_REV_5719:
3044 case ASIC_REV_5720:
3045 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3046 !tp->pci_fn)
3047 return true;
3048 return false;
3049 }
3050
3051 return false;
3052}
3053
Nithin Sujir989038e2013-08-30 17:01:36 -07003054static bool tg3_phy_led_bug(struct tg3 *tp)
3055{
3056 switch (tg3_asic_rev(tp)) {
3057 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003058 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003059 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3060 !tp->pci_fn)
3061 return true;
3062 return false;
3063 }
3064
3065 return false;
3066}
3067
Matt Carlson0a459aa2008-11-03 16:54:15 -08003068static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003069{
Matt Carlsonce057f02007-11-12 21:08:03 -08003070 u32 val;
3071
Nithin Sujir942d1af2013-04-09 08:48:07 +00003072 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3073 return;
3074
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003075 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003076 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003077 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3078 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3079
3080 sg_dig_ctrl |=
3081 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3082 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3083 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3084 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003085 return;
Michael Chan51297242007-02-13 12:17:57 -08003086 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003087
Joe Perches41535772013-02-16 11:20:04 +00003088 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003089 tg3_bmcr_reset(tp);
3090 val = tr32(GRC_MISC_CFG);
3091 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3092 udelay(40);
3093 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003094 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003095 u32 phytest;
3096 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3097 u32 phy;
3098
3099 tg3_writephy(tp, MII_ADVERTISE, 0);
3100 tg3_writephy(tp, MII_BMCR,
3101 BMCR_ANENABLE | BMCR_ANRESTART);
3102
3103 tg3_writephy(tp, MII_TG3_FET_TEST,
3104 phytest | MII_TG3_FET_SHADOW_EN);
3105 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3106 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3107 tg3_writephy(tp,
3108 MII_TG3_FET_SHDW_AUXMODE4,
3109 phy);
3110 }
3111 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3112 }
3113 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003114 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003115 if (!tg3_phy_led_bug(tp))
3116 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3117 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003118
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003119 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3120 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3121 MII_TG3_AUXCTL_PCTL_VREG_11V;
3122 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003123 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003124
Michael Chan15c3b692006-03-22 01:06:52 -08003125 /* The PHY should not be powered down on some chips because
3126 * of bugs.
3127 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003128 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003129 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003130
Joe Perches41535772013-02-16 11:20:04 +00003131 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3132 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003133 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3134 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3135 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3136 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3137 }
3138
Michael Chan15c3b692006-03-22 01:06:52 -08003139 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3140}
3141
Matt Carlson3f007892008-11-03 16:51:36 -08003142/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003143static int tg3_nvram_lock(struct tg3 *tp)
3144{
Joe Perches63c3a662011-04-26 08:12:10 +00003145 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003146 int i;
3147
3148 if (tp->nvram_lock_cnt == 0) {
3149 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3150 for (i = 0; i < 8000; i++) {
3151 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3152 break;
3153 udelay(20);
3154 }
3155 if (i == 8000) {
3156 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3157 return -ENODEV;
3158 }
3159 }
3160 tp->nvram_lock_cnt++;
3161 }
3162 return 0;
3163}
3164
3165/* tp->lock is held. */
3166static void tg3_nvram_unlock(struct tg3 *tp)
3167{
Joe Perches63c3a662011-04-26 08:12:10 +00003168 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003169 if (tp->nvram_lock_cnt > 0)
3170 tp->nvram_lock_cnt--;
3171 if (tp->nvram_lock_cnt == 0)
3172 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3173 }
3174}
3175
3176/* tp->lock is held. */
3177static void tg3_enable_nvram_access(struct tg3 *tp)
3178{
Joe Perches63c3a662011-04-26 08:12:10 +00003179 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003180 u32 nvaccess = tr32(NVRAM_ACCESS);
3181
3182 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3183 }
3184}
3185
3186/* tp->lock is held. */
3187static void tg3_disable_nvram_access(struct tg3 *tp)
3188{
Joe Perches63c3a662011-04-26 08:12:10 +00003189 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003190 u32 nvaccess = tr32(NVRAM_ACCESS);
3191
3192 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3193 }
3194}
3195
3196static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3197 u32 offset, u32 *val)
3198{
3199 u32 tmp;
3200 int i;
3201
3202 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3203 return -EINVAL;
3204
3205 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3206 EEPROM_ADDR_DEVID_MASK |
3207 EEPROM_ADDR_READ);
3208 tw32(GRC_EEPROM_ADDR,
3209 tmp |
3210 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3211 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3212 EEPROM_ADDR_ADDR_MASK) |
3213 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3214
3215 for (i = 0; i < 1000; i++) {
3216 tmp = tr32(GRC_EEPROM_ADDR);
3217
3218 if (tmp & EEPROM_ADDR_COMPLETE)
3219 break;
3220 msleep(1);
3221 }
3222 if (!(tmp & EEPROM_ADDR_COMPLETE))
3223 return -EBUSY;
3224
Matt Carlson62cedd12009-04-20 14:52:29 -07003225 tmp = tr32(GRC_EEPROM_DATA);
3226
3227 /*
3228 * The data will always be opposite the native endian
3229 * format. Perform a blind byteswap to compensate.
3230 */
3231 *val = swab32(tmp);
3232
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003233 return 0;
3234}
3235
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +05303236#define NVRAM_CMD_TIMEOUT 10000
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003237
3238static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3239{
3240 int i;
3241
3242 tw32(NVRAM_CMD, nvram_cmd);
3243 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003244 usleep_range(10, 40);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003245 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3246 udelay(10);
3247 break;
3248 }
3249 }
3250
3251 if (i == NVRAM_CMD_TIMEOUT)
3252 return -EBUSY;
3253
3254 return 0;
3255}
3256
3257static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3258{
Joe Perches63c3a662011-04-26 08:12:10 +00003259 if (tg3_flag(tp, NVRAM) &&
3260 tg3_flag(tp, NVRAM_BUFFERED) &&
3261 tg3_flag(tp, FLASH) &&
3262 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003263 (tp->nvram_jedecnum == JEDEC_ATMEL))
3264
3265 addr = ((addr / tp->nvram_pagesize) <<
3266 ATMEL_AT45DB0X1B_PAGE_POS) +
3267 (addr % tp->nvram_pagesize);
3268
3269 return addr;
3270}
3271
3272static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3273{
Joe Perches63c3a662011-04-26 08:12:10 +00003274 if (tg3_flag(tp, NVRAM) &&
3275 tg3_flag(tp, NVRAM_BUFFERED) &&
3276 tg3_flag(tp, FLASH) &&
3277 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003278 (tp->nvram_jedecnum == JEDEC_ATMEL))
3279
3280 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3281 tp->nvram_pagesize) +
3282 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3283
3284 return addr;
3285}
3286
Matt Carlsone4f34112009-02-25 14:25:00 +00003287/* NOTE: Data read in from NVRAM is byteswapped according to
3288 * the byteswapping settings for all other register accesses.
3289 * tg3 devices are BE devices, so on a BE machine, the data
3290 * returned will be exactly as it is seen in NVRAM. On a LE
3291 * machine, the 32-bit value will be byteswapped.
3292 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003293static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3294{
3295 int ret;
3296
Joe Perches63c3a662011-04-26 08:12:10 +00003297 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003298 return tg3_nvram_read_using_eeprom(tp, offset, val);
3299
3300 offset = tg3_nvram_phys_addr(tp, offset);
3301
3302 if (offset > NVRAM_ADDR_MSK)
3303 return -EINVAL;
3304
3305 ret = tg3_nvram_lock(tp);
3306 if (ret)
3307 return ret;
3308
3309 tg3_enable_nvram_access(tp);
3310
3311 tw32(NVRAM_ADDR, offset);
3312 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3313 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3314
3315 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003316 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003317
3318 tg3_disable_nvram_access(tp);
3319
3320 tg3_nvram_unlock(tp);
3321
3322 return ret;
3323}
3324
Matt Carlsona9dc5292009-02-25 14:25:30 +00003325/* Ensures NVRAM data is in bytestream format. */
3326static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003327{
3328 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003329 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003330 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003331 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003332 return res;
3333}
3334
Matt Carlsondbe9b922012-02-13 10:20:09 +00003335static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3336 u32 offset, u32 len, u8 *buf)
3337{
3338 int i, j, rc = 0;
3339 u32 val;
3340
3341 for (i = 0; i < len; i += 4) {
3342 u32 addr;
3343 __be32 data;
3344
3345 addr = offset + i;
3346
3347 memcpy(&data, buf + i, 4);
3348
3349 /*
3350 * The SEEPROM interface expects the data to always be opposite
3351 * the native endian format. We accomplish this by reversing
3352 * all the operations that would have been performed on the
3353 * data from a call to tg3_nvram_read_be32().
3354 */
3355 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3356
3357 val = tr32(GRC_EEPROM_ADDR);
3358 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3359
3360 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3361 EEPROM_ADDR_READ);
3362 tw32(GRC_EEPROM_ADDR, val |
3363 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3364 (addr & EEPROM_ADDR_ADDR_MASK) |
3365 EEPROM_ADDR_START |
3366 EEPROM_ADDR_WRITE);
3367
3368 for (j = 0; j < 1000; j++) {
3369 val = tr32(GRC_EEPROM_ADDR);
3370
3371 if (val & EEPROM_ADDR_COMPLETE)
3372 break;
3373 msleep(1);
3374 }
3375 if (!(val & EEPROM_ADDR_COMPLETE)) {
3376 rc = -EBUSY;
3377 break;
3378 }
3379 }
3380
3381 return rc;
3382}
3383
3384/* offset and length are dword aligned */
3385static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3386 u8 *buf)
3387{
3388 int ret = 0;
3389 u32 pagesize = tp->nvram_pagesize;
3390 u32 pagemask = pagesize - 1;
3391 u32 nvram_cmd;
3392 u8 *tmp;
3393
3394 tmp = kmalloc(pagesize, GFP_KERNEL);
3395 if (tmp == NULL)
3396 return -ENOMEM;
3397
3398 while (len) {
3399 int j;
3400 u32 phy_addr, page_off, size;
3401
3402 phy_addr = offset & ~pagemask;
3403
3404 for (j = 0; j < pagesize; j += 4) {
3405 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3406 (__be32 *) (tmp + j));
3407 if (ret)
3408 break;
3409 }
3410 if (ret)
3411 break;
3412
3413 page_off = offset & pagemask;
3414 size = pagesize;
3415 if (len < size)
3416 size = len;
3417
3418 len -= size;
3419
3420 memcpy(tmp + page_off, buf, size);
3421
3422 offset = offset + (pagesize - page_off);
3423
3424 tg3_enable_nvram_access(tp);
3425
3426 /*
3427 * Before we can erase the flash page, we need
3428 * to issue a special "write enable" command.
3429 */
3430 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3431
3432 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3433 break;
3434
3435 /* Erase the target page */
3436 tw32(NVRAM_ADDR, phy_addr);
3437
3438 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3439 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3440
3441 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3442 break;
3443
3444 /* Issue another write enable to start the write. */
3445 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3446
3447 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3448 break;
3449
3450 for (j = 0; j < pagesize; j += 4) {
3451 __be32 data;
3452
3453 data = *((__be32 *) (tmp + j));
3454
3455 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3456
3457 tw32(NVRAM_ADDR, phy_addr + j);
3458
3459 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3460 NVRAM_CMD_WR;
3461
3462 if (j == 0)
3463 nvram_cmd |= NVRAM_CMD_FIRST;
3464 else if (j == (pagesize - 4))
3465 nvram_cmd |= NVRAM_CMD_LAST;
3466
3467 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3468 if (ret)
3469 break;
3470 }
3471 if (ret)
3472 break;
3473 }
3474
3475 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3476 tg3_nvram_exec_cmd(tp, nvram_cmd);
3477
3478 kfree(tmp);
3479
3480 return ret;
3481}
3482
3483/* offset and length are dword aligned */
3484static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3485 u8 *buf)
3486{
3487 int i, ret = 0;
3488
3489 for (i = 0; i < len; i += 4, offset += 4) {
3490 u32 page_off, phy_addr, nvram_cmd;
3491 __be32 data;
3492
3493 memcpy(&data, buf + i, 4);
3494 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3495
3496 page_off = offset % tp->nvram_pagesize;
3497
3498 phy_addr = tg3_nvram_phys_addr(tp, offset);
3499
Matt Carlsondbe9b922012-02-13 10:20:09 +00003500 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3501
3502 if (page_off == 0 || i == 0)
3503 nvram_cmd |= NVRAM_CMD_FIRST;
3504 if (page_off == (tp->nvram_pagesize - 4))
3505 nvram_cmd |= NVRAM_CMD_LAST;
3506
3507 if (i == (len - 4))
3508 nvram_cmd |= NVRAM_CMD_LAST;
3509
Matt Carlson42278222012-02-13 15:20:11 +00003510 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3511 !tg3_flag(tp, FLASH) ||
3512 !tg3_flag(tp, 57765_PLUS))
3513 tw32(NVRAM_ADDR, phy_addr);
3514
Joe Perches41535772013-02-16 11:20:04 +00003515 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003516 !tg3_flag(tp, 5755_PLUS) &&
3517 (tp->nvram_jedecnum == JEDEC_ST) &&
3518 (nvram_cmd & NVRAM_CMD_FIRST)) {
3519 u32 cmd;
3520
3521 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3522 ret = tg3_nvram_exec_cmd(tp, cmd);
3523 if (ret)
3524 break;
3525 }
3526 if (!tg3_flag(tp, FLASH)) {
3527 /* We always do complete word writes to eeprom. */
3528 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3529 }
3530
3531 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3532 if (ret)
3533 break;
3534 }
3535 return ret;
3536}
3537
3538/* offset and length are dword aligned */
3539static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3540{
3541 int ret;
3542
3543 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3544 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3545 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3546 udelay(40);
3547 }
3548
3549 if (!tg3_flag(tp, NVRAM)) {
3550 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3551 } else {
3552 u32 grc_mode;
3553
3554 ret = tg3_nvram_lock(tp);
3555 if (ret)
3556 return ret;
3557
3558 tg3_enable_nvram_access(tp);
3559 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3560 tw32(NVRAM_WRITE1, 0x406);
3561
3562 grc_mode = tr32(GRC_MODE);
3563 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3564
3565 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3566 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3567 buf);
3568 } else {
3569 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3570 buf);
3571 }
3572
3573 grc_mode = tr32(GRC_MODE);
3574 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3575
3576 tg3_disable_nvram_access(tp);
3577 tg3_nvram_unlock(tp);
3578 }
3579
3580 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3581 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3582 udelay(40);
3583 }
3584
3585 return ret;
3586}
3587
Matt Carlson997b4f12011-08-31 11:44:53 +00003588#define RX_CPU_SCRATCH_BASE 0x30000
3589#define RX_CPU_SCRATCH_SIZE 0x04000
3590#define TX_CPU_SCRATCH_BASE 0x34000
3591#define TX_CPU_SCRATCH_SIZE 0x04000
3592
3593/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003594static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003595{
3596 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003597 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003598
Nithin Sujir837c45b2013-03-06 17:02:30 +00003599 for (i = 0; i < iters; i++) {
3600 tw32(cpu_base + CPU_STATE, 0xffffffff);
3601 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3602 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3603 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003604 if (pci_channel_offline(tp->pdev))
3605 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003606 }
3607
3608 return (i == iters) ? -EBUSY : 0;
3609}
3610
3611/* tp->lock is held. */
3612static int tg3_rxcpu_pause(struct tg3 *tp)
3613{
3614 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3615
3616 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3617 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3618 udelay(10);
3619
3620 return rc;
3621}
3622
3623/* tp->lock is held. */
3624static int tg3_txcpu_pause(struct tg3 *tp)
3625{
3626 return tg3_pause_cpu(tp, TX_CPU_BASE);
3627}
3628
3629/* tp->lock is held. */
3630static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3631{
3632 tw32(cpu_base + CPU_STATE, 0xffffffff);
3633 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3634}
3635
3636/* tp->lock is held. */
3637static void tg3_rxcpu_resume(struct tg3 *tp)
3638{
3639 tg3_resume_cpu(tp, RX_CPU_BASE);
3640}
3641
3642/* tp->lock is held. */
3643static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3644{
3645 int rc;
3646
3647 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003648
Joe Perches41535772013-02-16 11:20:04 +00003649 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003650 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3651
3652 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3653 return 0;
3654 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003655 if (cpu_base == RX_CPU_BASE) {
3656 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003657 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003658 /*
3659 * There is only an Rx CPU for the 5750 derivative in the
3660 * BCM4785.
3661 */
3662 if (tg3_flag(tp, IS_SSB_CORE))
3663 return 0;
3664
Nithin Sujir837c45b2013-03-06 17:02:30 +00003665 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003666 }
3667
Nithin Sujir837c45b2013-03-06 17:02:30 +00003668 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003669 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003670 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003671 return -ENODEV;
3672 }
3673
3674 /* Clear firmware's nvram arbitration. */
3675 if (tg3_flag(tp, NVRAM))
3676 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3677 return 0;
3678}
3679
Nithin Sujir31f11a92013-03-06 17:02:33 +00003680static int tg3_fw_data_len(struct tg3 *tp,
3681 const struct tg3_firmware_hdr *fw_hdr)
3682{
3683 int fw_len;
3684
3685 /* Non fragmented firmware have one firmware header followed by a
3686 * contiguous chunk of data to be written. The length field in that
3687 * header is not the length of data to be written but the complete
3688 * length of the bss. The data length is determined based on
3689 * tp->fw->size minus headers.
3690 *
3691 * Fragmented firmware have a main header followed by multiple
3692 * fragments. Each fragment is identical to non fragmented firmware
3693 * with a firmware header followed by a contiguous chunk of data. In
3694 * the main header, the length field is unused and set to 0xffffffff.
3695 * In each fragment header the length is the entire size of that
3696 * fragment i.e. fragment data + header length. Data length is
3697 * therefore length field in the header minus TG3_FW_HDR_LEN.
3698 */
3699 if (tp->fw_len == 0xffffffff)
3700 fw_len = be32_to_cpu(fw_hdr->len);
3701 else
3702 fw_len = tp->fw->size;
3703
3704 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3705}
3706
Matt Carlson997b4f12011-08-31 11:44:53 +00003707/* tp->lock is held. */
3708static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3709 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003710 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003711{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003712 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003713 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003714 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003715
3716 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3717 netdev_err(tp->dev,
3718 "%s: Trying to load TX cpu firmware which is 5705\n",
3719 __func__);
3720 return -EINVAL;
3721 }
3722
Nithin Sujirc4dab502013-03-06 17:02:34 +00003723 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003724 write_op = tg3_write_mem;
3725 else
3726 write_op = tg3_write_indirect_reg32;
3727
Nithin Sujirc4dab502013-03-06 17:02:34 +00003728 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3729 /* It is possible that bootcode is still loading at this point.
3730 * Get the nvram lock first before halting the cpu.
3731 */
3732 int lock_err = tg3_nvram_lock(tp);
3733 err = tg3_halt_cpu(tp, cpu_base);
3734 if (!lock_err)
3735 tg3_nvram_unlock(tp);
3736 if (err)
3737 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003738
Nithin Sujirc4dab502013-03-06 17:02:34 +00003739 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3740 write_op(tp, cpu_scratch_base + i, 0);
3741 tw32(cpu_base + CPU_STATE, 0xffffffff);
3742 tw32(cpu_base + CPU_MODE,
3743 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3744 } else {
3745 /* Subtract additional main header for fragmented firmware and
3746 * advance to the first fragment
3747 */
3748 total_len -= TG3_FW_HDR_LEN;
3749 fw_hdr++;
3750 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003751
Nithin Sujir31f11a92013-03-06 17:02:33 +00003752 do {
3753 u32 *fw_data = (u32 *)(fw_hdr + 1);
3754 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3755 write_op(tp, cpu_scratch_base +
3756 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3757 (i * sizeof(u32)),
3758 be32_to_cpu(fw_data[i]));
3759
3760 total_len -= be32_to_cpu(fw_hdr->len);
3761
3762 /* Advance to next fragment */
3763 fw_hdr = (struct tg3_firmware_hdr *)
3764 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3765 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003766
3767 err = 0;
3768
3769out:
3770 return err;
3771}
3772
3773/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003774static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3775{
3776 int i;
3777 const int iters = 5;
3778
3779 tw32(cpu_base + CPU_STATE, 0xffffffff);
3780 tw32_f(cpu_base + CPU_PC, pc);
3781
3782 for (i = 0; i < iters; i++) {
3783 if (tr32(cpu_base + CPU_PC) == pc)
3784 break;
3785 tw32(cpu_base + CPU_STATE, 0xffffffff);
3786 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3787 tw32_f(cpu_base + CPU_PC, pc);
3788 udelay(1000);
3789 }
3790
3791 return (i == iters) ? -EBUSY : 0;
3792}
3793
3794/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003795static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3796{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003797 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003798 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003799
Nithin Sujir77997ea2013-03-06 17:02:32 +00003800 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003801
3802 /* Firmware blob starts with version numbers, followed by
3803 start address and length. We are setting complete length.
3804 length = end_address_of_bss - start_address_of_text.
3805 Remainder is the blob to be loaded contiguously
3806 from start address. */
3807
Matt Carlson997b4f12011-08-31 11:44:53 +00003808 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3809 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003810 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003811 if (err)
3812 return err;
3813
3814 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3815 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003816 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003817 if (err)
3818 return err;
3819
3820 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003821 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3822 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003823 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003824 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3825 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003826 tr32(RX_CPU_BASE + CPU_PC),
3827 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003828 return -ENODEV;
3829 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003830
3831 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003832
3833 return 0;
3834}
3835
Nithin Sujirc4dab502013-03-06 17:02:34 +00003836static int tg3_validate_rxcpu_state(struct tg3 *tp)
3837{
3838 const int iters = 1000;
3839 int i;
3840 u32 val;
3841
3842 /* Wait for boot code to complete initialization and enter service
3843 * loop. It is then safe to download service patches
3844 */
3845 for (i = 0; i < iters; i++) {
3846 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3847 break;
3848
3849 udelay(10);
3850 }
3851
3852 if (i == iters) {
3853 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3854 return -EBUSY;
3855 }
3856
3857 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3858 if (val & 0xff) {
3859 netdev_warn(tp->dev,
3860 "Other patches exist. Not downloading EEE patch\n");
3861 return -EEXIST;
3862 }
3863
3864 return 0;
3865}
3866
3867/* tp->lock is held. */
3868static void tg3_load_57766_firmware(struct tg3 *tp)
3869{
3870 struct tg3_firmware_hdr *fw_hdr;
3871
3872 if (!tg3_flag(tp, NO_NVRAM))
3873 return;
3874
3875 if (tg3_validate_rxcpu_state(tp))
3876 return;
3877
3878 if (!tp->fw)
3879 return;
3880
3881 /* This firmware blob has a different format than older firmware
3882 * releases as given below. The main difference is we have fragmented
3883 * data to be written to non-contiguous locations.
3884 *
3885 * In the beginning we have a firmware header identical to other
3886 * firmware which consists of version, base addr and length. The length
3887 * here is unused and set to 0xffffffff.
3888 *
3889 * This is followed by a series of firmware fragments which are
3890 * individually identical to previous firmware. i.e. they have the
3891 * firmware header and followed by data for that fragment. The version
3892 * field of the individual fragment header is unused.
3893 */
3894
3895 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3896 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3897 return;
3898
3899 if (tg3_rxcpu_pause(tp))
3900 return;
3901
3902 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3903 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3904
3905 tg3_rxcpu_resume(tp);
3906}
3907
Matt Carlson997b4f12011-08-31 11:44:53 +00003908/* tp->lock is held. */
3909static int tg3_load_tso_firmware(struct tg3 *tp)
3910{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003911 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003912 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003913 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003914
Matt Carlson1caf13e2013-03-06 17:02:29 +00003915 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003916 return 0;
3917
Nithin Sujir77997ea2013-03-06 17:02:32 +00003918 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003919
3920 /* Firmware blob starts with version numbers, followed by
3921 start address and length. We are setting complete length.
3922 length = end_address_of_bss - start_address_of_text.
3923 Remainder is the blob to be loaded contiguously
3924 from start address. */
3925
Matt Carlson997b4f12011-08-31 11:44:53 +00003926 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003927
Joe Perches41535772013-02-16 11:20:04 +00003928 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003929 cpu_base = RX_CPU_BASE;
3930 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3931 } else {
3932 cpu_base = TX_CPU_BASE;
3933 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3934 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3935 }
3936
3937 err = tg3_load_firmware_cpu(tp, cpu_base,
3938 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003939 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003940 if (err)
3941 return err;
3942
3943 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003944 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3945 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003946 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003947 netdev_err(tp->dev,
3948 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003949 __func__, tr32(cpu_base + CPU_PC),
3950 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003951 return -ENODEV;
3952 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003953
3954 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003955 return 0;
3956}
3957
Michael Chanf022ae62014-01-03 10:09:11 -08003958/* tp->lock is held. */
3959static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3960{
3961 u32 addr_high, addr_low;
3962
3963 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3964 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3965 (mac_addr[4] << 8) | mac_addr[5]);
3966
3967 if (index < 4) {
3968 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3969 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3970 } else {
3971 index -= 4;
3972 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3973 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3974 }
3975}
Matt Carlson997b4f12011-08-31 11:44:53 +00003976
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003977/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003978static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003979{
Michael Chanf022ae62014-01-03 10:09:11 -08003980 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003981 int i;
3982
Matt Carlson3f007892008-11-03 16:51:36 -08003983 for (i = 0; i < 4; i++) {
3984 if (i == 1 && skip_mac_1)
3985 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003986 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003987 }
3988
Joe Perches41535772013-02-16 11:20:04 +00003989 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3990 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003991 for (i = 4; i < 16; i++)
3992 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003993 }
3994
3995 addr_high = (tp->dev->dev_addr[0] +
3996 tp->dev->dev_addr[1] +
3997 tp->dev->dev_addr[2] +
3998 tp->dev->dev_addr[3] +
3999 tp->dev->dev_addr[4] +
4000 tp->dev->dev_addr[5]) &
4001 TX_BACKOFF_SEED_MASK;
4002 tw32(MAC_TX_BACKOFF_SEED, addr_high);
4003}
4004
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004005static void tg3_enable_register_access(struct tg3 *tp)
4006{
4007 /*
4008 * Make sure register accesses (indirect or otherwise) will function
4009 * correctly.
4010 */
4011 pci_write_config_dword(tp->pdev,
4012 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4013}
4014
4015static int tg3_power_up(struct tg3 *tp)
4016{
Matt Carlsonbed98292011-07-13 09:27:29 +00004017 int err;
4018
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004019 tg3_enable_register_access(tp);
4020
Matt Carlsonbed98292011-07-13 09:27:29 +00004021 err = pci_set_power_state(tp->pdev, PCI_D0);
4022 if (!err) {
4023 /* Switch out of Vaux if it is a NIC */
4024 tg3_pwrsrc_switch_to_vmain(tp);
4025 } else {
4026 netdev_err(tp->dev, "Transition to D0 failed\n");
4027 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004028
Matt Carlsonbed98292011-07-13 09:27:29 +00004029 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004030}
4031
Joe Perches953c96e2013-04-09 10:18:14 +00004032static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004033
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004034static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
4036 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004037 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004039 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004040
4041 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004042 if (tg3_flag(tp, CLKREQ_BUG))
4043 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4044 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4047 tw32(TG3PCI_MISC_HOST_CTRL,
4048 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4049
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004050 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004051 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004052
Joe Perches63c3a662011-04-26 08:12:10 +00004053 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004054 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004055 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004056 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004057 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004058 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004059
Andrew Lunn7f854422016-01-06 20:11:18 +01004060 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004061
Matt Carlson80096062010-08-02 11:26:06 +00004062 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004063
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004064 tp->link_config.speed = phydev->speed;
4065 tp->link_config.duplex = phydev->duplex;
4066 tp->link_config.autoneg = phydev->autoneg;
4067 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004068
4069 advertising = ADVERTISED_TP |
4070 ADVERTISED_Pause |
4071 ADVERTISED_Autoneg |
4072 ADVERTISED_10baseT_Half;
4073
Joe Perches63c3a662011-04-26 08:12:10 +00004074 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4075 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004076 advertising |=
4077 ADVERTISED_100baseT_Half |
4078 ADVERTISED_100baseT_Full |
4079 ADVERTISED_10baseT_Full;
4080 else
4081 advertising |= ADVERTISED_10baseT_Full;
4082 }
4083
4084 phydev->advertising = advertising;
4085
4086 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004087
4088 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004089 if (phyid != PHY_ID_BCMAC131) {
4090 phyid &= PHY_BCM_OUI_MASK;
4091 if (phyid == PHY_BCM_OUI_1 ||
4092 phyid == PHY_BCM_OUI_2 ||
4093 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004094 do_low_power = true;
4095 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004096 }
Matt Carlsondd477002008-05-25 23:45:58 -07004097 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004098 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004099
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004100 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004101 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Matt Carlson2855b9f2012-02-13 15:20:14 +00004103 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004104 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
4106
Joe Perches41535772013-02-16 11:20:04 +00004107 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004108 u32 val;
4109
4110 val = tr32(GRC_VCPU_EXT_CTRL);
4111 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004112 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004113 int i;
4114 u32 val;
4115
4116 for (i = 0; i < 200; i++) {
4117 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4118 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4119 break;
4120 msleep(1);
4121 }
4122 }
Joe Perches63c3a662011-04-26 08:12:10 +00004123 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004124 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4125 WOL_DRV_STATE_SHUTDOWN |
4126 WOL_DRV_WOL |
4127 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004128
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004129 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 u32 mac_mode;
4131
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004132 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004133 if (do_low_power &&
4134 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4135 tg3_phy_auxctl_write(tp,
4136 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4137 MII_TG3_AUXCTL_PCTL_WOL_EN |
4138 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4139 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004140 udelay(40);
4141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004143 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004144 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004145 else if (tp->phy_flags &
4146 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4147 if (tp->link_config.active_speed == SPEED_1000)
4148 mac_mode = MAC_MODE_PORT_MODE_GMII;
4149 else
4150 mac_mode = MAC_MODE_PORT_MODE_MII;
4151 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004152 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004154 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004155 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004156 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004157 SPEED_100 : SPEED_10;
4158 if (tg3_5700_link_polarity(tp, speed))
4159 mac_mode |= MAC_MODE_LINK_POLARITY;
4160 else
4161 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 } else {
4164 mac_mode = MAC_MODE_PORT_MODE_TBI;
4165 }
4166
Joe Perches63c3a662011-04-26 08:12:10 +00004167 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 tw32(MAC_LED_CTRL, tp->led_ctrl);
4169
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004170 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004171 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4172 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004173 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Joe Perches63c3a662011-04-26 08:12:10 +00004175 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004176 mac_mode |= MAC_MODE_APE_TX_EN |
4177 MAC_MODE_APE_RX_EN |
4178 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 tw32_f(MAC_MODE, mac_mode);
4181 udelay(100);
4182
4183 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4184 udelay(10);
4185 }
4186
Joe Perches63c3a662011-04-26 08:12:10 +00004187 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004188 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4189 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 u32 base_val;
4191
4192 base_val = tp->pci_clock_ctrl;
4193 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4194 CLOCK_CTRL_TXCLK_DISABLE);
4195
Michael Chanb401e9e2005-12-19 16:27:04 -08004196 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4197 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004198 } else if (tg3_flag(tp, 5780_CLASS) ||
4199 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004200 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004201 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004202 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 u32 newbits1, newbits2;
4204
Joe Perches41535772013-02-16 11:20:04 +00004205 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4206 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4208 CLOCK_CTRL_TXCLK_DISABLE |
4209 CLOCK_CTRL_ALTCLK);
4210 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004211 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 newbits1 = CLOCK_CTRL_625_CORE;
4213 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4214 } else {
4215 newbits1 = CLOCK_CTRL_ALTCLK;
4216 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4217 }
4218
Michael Chanb401e9e2005-12-19 16:27:04 -08004219 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4220 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Michael Chanb401e9e2005-12-19 16:27:04 -08004222 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4223 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Joe Perches63c3a662011-04-26 08:12:10 +00004225 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 u32 newbits3;
4227
Joe Perches41535772013-02-16 11:20:04 +00004228 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4229 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4231 CLOCK_CTRL_TXCLK_DISABLE |
4232 CLOCK_CTRL_44MHZ_CORE);
4233 } else {
4234 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4235 }
4236
Michael Chanb401e9e2005-12-19 16:27:04 -08004237 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4238 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240 }
4241
Joe Perches63c3a662011-04-26 08:12:10 +00004242 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004243 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004244
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004245 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
4247 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004248 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004249 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4250 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 u32 val = tr32(0x7d00);
4252
4253 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4254 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004255 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004256 int err;
4257
4258 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004260 if (!err)
4261 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264
Michael Chanbbadf502006-04-06 21:46:34 -07004265 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4266
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004267 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return 0;
4270}
4271
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004272static void tg3_power_down(struct tg3 *tp)
4273{
Joe Perches63c3a662011-04-26 08:12:10 +00004274 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004275 pci_set_power_state(tp->pdev, PCI_D3hot);
4276}
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4279{
4280 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4281 case MII_TG3_AUX_STAT_10HALF:
4282 *speed = SPEED_10;
4283 *duplex = DUPLEX_HALF;
4284 break;
4285
4286 case MII_TG3_AUX_STAT_10FULL:
4287 *speed = SPEED_10;
4288 *duplex = DUPLEX_FULL;
4289 break;
4290
4291 case MII_TG3_AUX_STAT_100HALF:
4292 *speed = SPEED_100;
4293 *duplex = DUPLEX_HALF;
4294 break;
4295
4296 case MII_TG3_AUX_STAT_100FULL:
4297 *speed = SPEED_100;
4298 *duplex = DUPLEX_FULL;
4299 break;
4300
4301 case MII_TG3_AUX_STAT_1000HALF:
4302 *speed = SPEED_1000;
4303 *duplex = DUPLEX_HALF;
4304 break;
4305
4306 case MII_TG3_AUX_STAT_1000FULL:
4307 *speed = SPEED_1000;
4308 *duplex = DUPLEX_FULL;
4309 break;
4310
4311 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004312 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004313 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4314 SPEED_10;
4315 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4316 DUPLEX_HALF;
4317 break;
4318 }
Matt Carlsone7405222012-02-13 15:20:16 +00004319 *speed = SPEED_UNKNOWN;
4320 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
Matt Carlson42b64a42011-05-19 12:12:49 +00004325static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Matt Carlson42b64a42011-05-19 12:12:49 +00004327 int err = 0;
4328 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Matt Carlson42b64a42011-05-19 12:12:49 +00004330 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004331 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004332 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Matt Carlson42b64a42011-05-19 12:12:49 +00004334 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4335 if (err)
4336 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Matt Carlson4f272092011-12-14 11:09:57 +00004338 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4339 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004340
Joe Perches41535772013-02-16 11:20:04 +00004341 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4342 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004343 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004344
Matt Carlson4f272092011-12-14 11:09:57 +00004345 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4346 if (err)
4347 goto done;
4348 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004349
Matt Carlson42b64a42011-05-19 12:12:49 +00004350 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4351 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Matt Carlson42b64a42011-05-19 12:12:49 +00004353 tw32(TG3_CPMU_EEE_MODE,
4354 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004355
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004356 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004357 if (!err) {
4358 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004359
Matt Carlsona6b68da2010-12-06 08:28:52 +00004360 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004361 /* Advertise 100-BaseTX EEE ability */
4362 if (advertise & ADVERTISED_100baseT_Full)
4363 val |= MDIO_AN_EEE_ADV_100TX;
4364 /* Advertise 1000-BaseT EEE ability */
4365 if (advertise & ADVERTISED_1000baseT_Full)
4366 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004367
4368 if (!tp->eee.eee_enabled) {
4369 val = 0;
4370 tp->eee.advertised = 0;
4371 } else {
4372 tp->eee.advertised = advertise &
4373 (ADVERTISED_100baseT_Full |
4374 ADVERTISED_1000baseT_Full);
4375 }
4376
Matt Carlson42b64a42011-05-19 12:12:49 +00004377 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004378 if (err)
4379 val = 0;
4380
Joe Perches41535772013-02-16 11:20:04 +00004381 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004382 case ASIC_REV_5717:
4383 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004384 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004385 case ASIC_REV_5719:
4386 /* If we advertised any eee advertisements above... */
4387 if (val)
4388 val = MII_TG3_DSP_TAP26_ALNOKO |
4389 MII_TG3_DSP_TAP26_RMRXSTO |
4390 MII_TG3_DSP_TAP26_OPCSINPT;
4391 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4392 /* Fall through */
4393 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004394 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004395 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4396 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4397 MII_TG3_DSP_CH34TP2_HIBW01);
4398 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004399
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004400 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004401 if (!err)
4402 err = err2;
4403 }
4404
4405done:
4406 return err;
4407}
4408
4409static void tg3_phy_copper_begin(struct tg3 *tp)
4410{
Matt Carlsond13ba512012-02-22 12:35:19 +00004411 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4412 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4413 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004414
Nithin Sujir942d1af2013-04-09 08:48:07 +00004415 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4416 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004417 adv = ADVERTISED_10baseT_Half |
4418 ADVERTISED_10baseT_Full;
4419 if (tg3_flag(tp, WOL_SPEED_100MB))
4420 adv |= ADVERTISED_100baseT_Half |
4421 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004422 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4423 if (!(tp->phy_flags &
4424 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4425 adv |= ADVERTISED_1000baseT_Half;
4426 adv |= ADVERTISED_1000baseT_Full;
4427 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004428
Matt Carlsond13ba512012-02-22 12:35:19 +00004429 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004430 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004431 adv = tp->link_config.advertising;
4432 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4433 adv &= ~(ADVERTISED_1000baseT_Half |
4434 ADVERTISED_1000baseT_Full);
4435
4436 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004437 }
4438
Matt Carlsond13ba512012-02-22 12:35:19 +00004439 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004440
Nithin Sujir942d1af2013-04-09 08:48:07 +00004441 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4442 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4443 /* Normally during power down we want to autonegotiate
4444 * the lowest possible speed for WOL. However, to avoid
4445 * link flap, we leave it untouched.
4446 */
4447 return;
4448 }
4449
Matt Carlsond13ba512012-02-22 12:35:19 +00004450 tg3_writephy(tp, MII_BMCR,
4451 BMCR_ANENABLE | BMCR_ANRESTART);
4452 } else {
4453 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 u32 bmcr, orig_bmcr;
4455
4456 tp->link_config.active_speed = tp->link_config.speed;
4457 tp->link_config.active_duplex = tp->link_config.duplex;
4458
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004459 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4460 /* With autoneg disabled, 5715 only links up when the
4461 * advertisement register has the configured speed
4462 * enabled.
4463 */
4464 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4465 }
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 bmcr = 0;
4468 switch (tp->link_config.speed) {
4469 default:
4470 case SPEED_10:
4471 break;
4472
4473 case SPEED_100:
4474 bmcr |= BMCR_SPEED100;
4475 break;
4476
4477 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004478 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 if (tp->link_config.duplex == DUPLEX_FULL)
4483 bmcr |= BMCR_FULLDPLX;
4484
4485 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4486 (bmcr != orig_bmcr)) {
4487 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4488 for (i = 0; i < 1500; i++) {
4489 u32 tmp;
4490
4491 udelay(10);
4492 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4493 tg3_readphy(tp, MII_BMSR, &tmp))
4494 continue;
4495 if (!(tmp & BMSR_LSTATUS)) {
4496 udelay(40);
4497 break;
4498 }
4499 }
4500 tg3_writephy(tp, MII_BMCR, bmcr);
4501 udelay(40);
4502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 }
4504}
4505
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004506static int tg3_phy_pull_config(struct tg3 *tp)
4507{
4508 int err;
4509 u32 val;
4510
4511 err = tg3_readphy(tp, MII_BMCR, &val);
4512 if (err)
4513 goto done;
4514
4515 if (!(val & BMCR_ANENABLE)) {
4516 tp->link_config.autoneg = AUTONEG_DISABLE;
4517 tp->link_config.advertising = 0;
4518 tg3_flag_clear(tp, PAUSE_AUTONEG);
4519
4520 err = -EIO;
4521
4522 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4523 case 0:
4524 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4525 goto done;
4526
4527 tp->link_config.speed = SPEED_10;
4528 break;
4529 case BMCR_SPEED100:
4530 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4531 goto done;
4532
4533 tp->link_config.speed = SPEED_100;
4534 break;
4535 case BMCR_SPEED1000:
4536 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4537 tp->link_config.speed = SPEED_1000;
4538 break;
4539 }
4540 /* Fall through */
4541 default:
4542 goto done;
4543 }
4544
4545 if (val & BMCR_FULLDPLX)
4546 tp->link_config.duplex = DUPLEX_FULL;
4547 else
4548 tp->link_config.duplex = DUPLEX_HALF;
4549
4550 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4551
4552 err = 0;
4553 goto done;
4554 }
4555
4556 tp->link_config.autoneg = AUTONEG_ENABLE;
4557 tp->link_config.advertising = ADVERTISED_Autoneg;
4558 tg3_flag_set(tp, PAUSE_AUTONEG);
4559
4560 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4561 u32 adv;
4562
4563 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4564 if (err)
4565 goto done;
4566
4567 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4568 tp->link_config.advertising |= adv | ADVERTISED_TP;
4569
4570 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4571 } else {
4572 tp->link_config.advertising |= ADVERTISED_FIBRE;
4573 }
4574
4575 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4576 u32 adv;
4577
4578 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4579 err = tg3_readphy(tp, MII_CTRL1000, &val);
4580 if (err)
4581 goto done;
4582
4583 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4584 } else {
4585 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4586 if (err)
4587 goto done;
4588
4589 adv = tg3_decode_flowctrl_1000X(val);
4590 tp->link_config.flowctrl = adv;
4591
4592 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4593 adv = mii_adv_to_ethtool_adv_x(val);
4594 }
4595
4596 tp->link_config.advertising |= adv;
4597 }
4598
4599done:
4600 return err;
4601}
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603static int tg3_init_5401phy_dsp(struct tg3 *tp)
4604{
4605 int err;
4606
4607 /* Turn off tap power management. */
4608 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004609 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004611 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4612 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4613 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4614 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4615 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 udelay(40);
4618
4619 return err;
4620}
4621
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004622static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4623{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004624 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004625
4626 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4627 return true;
4628
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004629 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004630
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004631 if (tp->eee.eee_enabled) {
4632 if (tp->eee.advertised != eee.advertised ||
4633 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4634 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4635 return false;
4636 } else {
4637 /* EEE is disabled but we're advertising */
4638 if (eee.advertised)
4639 return false;
4640 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004641
4642 return true;
4643}
4644
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004645static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004647 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004648
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649 advertising = tp->link_config.advertising;
4650 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004652 advmsk = ADVERTISE_ALL;
4653 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004654 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004655 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004658 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4659 return false;
4660
4661 if ((*lcladv & advmsk) != tgtadv)
4662 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004663
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004664 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 u32 tg3_ctrl;
4666
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004667 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004668
Matt Carlson221c5632011-06-13 13:39:01 +00004669 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004670 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Matt Carlson3198e072012-02-13 15:20:10 +00004672 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004673 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4674 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004675 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4676 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4677 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4678 } else {
4679 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4680 }
4681
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004682 if (tg3_ctrl != tgtadv)
4683 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004685
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004686 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004687}
4688
Matt Carlson859edb22011-12-08 14:40:16 +00004689static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4690{
4691 u32 lpeth = 0;
4692
4693 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4694 u32 val;
4695
4696 if (tg3_readphy(tp, MII_STAT1000, &val))
4697 return false;
4698
4699 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4700 }
4701
4702 if (tg3_readphy(tp, MII_LPA, rmtadv))
4703 return false;
4704
4705 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4706 tp->link_config.rmt_adv = lpeth;
4707
4708 return true;
4709}
4710
Joe Perches953c96e2013-04-09 10:18:14 +00004711static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004712{
4713 if (curr_link_up != tp->link_up) {
4714 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004715 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004716 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004717 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004718 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4719 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4720 }
4721
4722 tg3_link_report(tp);
4723 return true;
4724 }
4725
4726 return false;
4727}
4728
Michael Chan3310e242013-04-09 08:48:05 +00004729static void tg3_clear_mac_status(struct tg3 *tp)
4730{
4731 tw32(MAC_EVENT, 0);
4732
4733 tw32_f(MAC_STATUS,
4734 MAC_STATUS_SYNC_CHANGED |
4735 MAC_STATUS_CFG_CHANGED |
4736 MAC_STATUS_MI_COMPLETION |
4737 MAC_STATUS_LNKSTATE_CHANGED);
4738 udelay(40);
4739}
4740
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004741static void tg3_setup_eee(struct tg3 *tp)
4742{
4743 u32 val;
4744
4745 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4746 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4747 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4748 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4749
4750 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4751
4752 tw32_f(TG3_CPMU_EEE_CTRL,
4753 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4754
4755 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4756 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4757 TG3_CPMU_EEEMD_LPI_IN_RX |
4758 TG3_CPMU_EEEMD_EEE_ENABLE;
4759
4760 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4761 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4762
4763 if (tg3_flag(tp, ENABLE_APE))
4764 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4765
4766 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4767
4768 tw32_f(TG3_CPMU_EEE_DBTMR1,
4769 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4770 (tp->eee.tx_lpi_timer & 0xffff));
4771
4772 tw32_f(TG3_CPMU_EEE_DBTMR2,
4773 TG3_CPMU_DBTMR2_APE_TX_2047US |
4774 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4775}
4776
Joe Perches953c96e2013-04-09 10:18:14 +00004777static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Joe Perches953c96e2013-04-09 10:18:14 +00004779 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004780 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004781 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 u16 current_speed;
4783 u8 current_duplex;
4784 int i, err;
4785
Michael Chan3310e242013-04-09 08:48:05 +00004786 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Matt Carlson8ef21422008-05-02 16:47:53 -07004788 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4789 tw32_f(MAC_MI_MODE,
4790 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4791 udelay(80);
4792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004794 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 /* Some third-party PHYs need to be reset on link going
4797 * down.
4798 */
Joe Perches41535772013-02-16 11:20:04 +00004799 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4800 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4801 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004802 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 tg3_readphy(tp, MII_BMSR, &bmsr);
4804 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4805 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004806 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
4808 if (force_reset)
4809 tg3_phy_reset(tp);
4810
Matt Carlson79eb6902010-02-17 15:17:03 +00004811 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 tg3_readphy(tp, MII_BMSR, &bmsr);
4813 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004814 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 bmsr = 0;
4816
4817 if (!(bmsr & BMSR_LSTATUS)) {
4818 err = tg3_init_5401phy_dsp(tp);
4819 if (err)
4820 return err;
4821
4822 tg3_readphy(tp, MII_BMSR, &bmsr);
4823 for (i = 0; i < 1000; i++) {
4824 udelay(10);
4825 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4826 (bmsr & BMSR_LSTATUS)) {
4827 udelay(40);
4828 break;
4829 }
4830 }
4831
Matt Carlson79eb6902010-02-17 15:17:03 +00004832 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4833 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 !(bmsr & BMSR_LSTATUS) &&
4835 tp->link_config.active_speed == SPEED_1000) {
4836 err = tg3_phy_reset(tp);
4837 if (!err)
4838 err = tg3_init_5401phy_dsp(tp);
4839 if (err)
4840 return err;
4841 }
4842 }
Joe Perches41535772013-02-16 11:20:04 +00004843 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4844 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* 5701 {A0,B0} CRC bug workaround */
4846 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004847 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4848 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4849 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
4851
4852 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004853 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4854 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004856 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004858 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4860
Joe Perches41535772013-02-16 11:20:04 +00004861 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4862 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4864 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4865 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4866 else
4867 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4868 }
4869
Joe Perches953c96e2013-04-09 10:18:14 +00004870 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004871 current_speed = SPEED_UNKNOWN;
4872 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004873 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004874 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004876 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004877 err = tg3_phy_auxctl_read(tp,
4878 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4879 &val);
4880 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004881 tg3_phy_auxctl_write(tp,
4882 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4883 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 goto relink;
4885 }
4886 }
4887
4888 bmsr = 0;
4889 for (i = 0; i < 100; i++) {
4890 tg3_readphy(tp, MII_BMSR, &bmsr);
4891 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4892 (bmsr & BMSR_LSTATUS))
4893 break;
4894 udelay(40);
4895 }
4896
4897 if (bmsr & BMSR_LSTATUS) {
4898 u32 aux_stat, bmcr;
4899
4900 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4901 for (i = 0; i < 2000; i++) {
4902 udelay(10);
4903 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4904 aux_stat)
4905 break;
4906 }
4907
4908 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4909 &current_speed,
4910 &current_duplex);
4911
4912 bmcr = 0;
4913 for (i = 0; i < 200; i++) {
4914 tg3_readphy(tp, MII_BMCR, &bmcr);
4915 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4916 continue;
4917 if (bmcr && bmcr != 0x7fff)
4918 break;
4919 udelay(10);
4920 }
4921
Matt Carlsonef167e22007-12-20 20:10:01 -08004922 lcl_adv = 0;
4923 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Matt Carlsonef167e22007-12-20 20:10:01 -08004925 tp->link_config.active_speed = current_speed;
4926 tp->link_config.active_duplex = current_duplex;
4927
4928 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004929 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4930
Matt Carlsonef167e22007-12-20 20:10:01 -08004931 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004932 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004933 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004934 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004935 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004936
4937 /* EEE settings changes take effect only after a phy
4938 * reset. If we have skipped a reset due to Link Flap
4939 * Avoidance being enabled, do it now.
4940 */
4941 if (!eee_config_ok &&
4942 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004943 !force_reset) {
4944 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004945 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 } else {
4948 if (!(bmcr & BMCR_ANENABLE) &&
4949 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004950 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004951 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
4953 }
4954
Joe Perches953c96e2013-04-09 10:18:14 +00004955 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004956 tp->link_config.active_duplex == DUPLEX_FULL) {
4957 u32 reg, bit;
4958
4959 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4960 reg = MII_TG3_FET_GEN_STAT;
4961 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4962 } else {
4963 reg = MII_TG3_EXT_STAT;
4964 bit = MII_TG3_EXT_STAT_MDIX;
4965 }
4966
4967 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4968 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4969
Matt Carlsonef167e22007-12-20 20:10:01 -08004970 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 }
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004975 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 tg3_phy_copper_begin(tp);
4977
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004978 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004979 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004980 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4981 current_speed = SPEED_1000;
4982 current_duplex = DUPLEX_FULL;
4983 tp->link_config.active_speed = current_speed;
4984 tp->link_config.active_duplex = current_duplex;
4985 }
4986
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004987 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004988 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4989 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004990 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
4992
4993 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004994 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (tp->link_config.active_speed == SPEED_100 ||
4996 tp->link_config.active_speed == SPEED_10)
4997 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4998 else
4999 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005000 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00005001 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5002 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5004
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00005005 /* In order for the 5750 core in BCM4785 chip to work properly
5006 * in RGMII mode, the Led Control Register must be set up.
5007 */
5008 if (tg3_flag(tp, RGMII_MODE)) {
5009 u32 led_ctrl = tr32(MAC_LED_CTRL);
5010 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5011
5012 if (tp->link_config.active_speed == SPEED_10)
5013 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5014 else if (tp->link_config.active_speed == SPEED_100)
5015 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5016 LED_CTRL_100MBPS_ON);
5017 else if (tp->link_config.active_speed == SPEED_1000)
5018 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5019 LED_CTRL_1000MBPS_ON);
5020
5021 tw32(MAC_LED_CTRL, led_ctrl);
5022 udelay(40);
5023 }
5024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5026 if (tp->link_config.active_duplex == DUPLEX_HALF)
5027 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5028
Joe Perches41535772013-02-16 11:20:04 +00005029 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005030 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005031 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005033 else
5034 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
5036
5037 /* ??? Without this setting Netgear GA302T PHY does not
5038 * ??? send/receive packets...
5039 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005040 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005041 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5043 tw32_f(MAC_MI_MODE, tp->mi_mode);
5044 udelay(80);
5045 }
5046
5047 tw32_f(MAC_MODE, tp->mac_mode);
5048 udelay(40);
5049
Matt Carlson52b02d02010-10-14 10:37:41 +00005050 tg3_phy_eee_adjust(tp, current_link_up);
5051
Joe Perches63c3a662011-04-26 08:12:10 +00005052 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 /* Polled via timer. */
5054 tw32_f(MAC_EVENT, 0);
5055 } else {
5056 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5057 }
5058 udelay(40);
5059
Joe Perches41535772013-02-16 11:20:04 +00005060 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005061 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005063 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 udelay(120);
5065 tw32_f(MAC_STATUS,
5066 (MAC_STATUS_SYNC_CHANGED |
5067 MAC_STATUS_CFG_CHANGED));
5068 udelay(40);
5069 tg3_write_mem(tp,
5070 NIC_SRAM_FIRMWARE_MBOX,
5071 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5072 }
5073
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005074 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005075 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005076 if (tp->link_config.active_speed == SPEED_100 ||
5077 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005078 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5079 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005080 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005081 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5082 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005083 }
5084
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005085 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 return 0;
5088}
5089
5090struct tg3_fiber_aneginfo {
5091 int state;
5092#define ANEG_STATE_UNKNOWN 0
5093#define ANEG_STATE_AN_ENABLE 1
5094#define ANEG_STATE_RESTART_INIT 2
5095#define ANEG_STATE_RESTART 3
5096#define ANEG_STATE_DISABLE_LINK_OK 4
5097#define ANEG_STATE_ABILITY_DETECT_INIT 5
5098#define ANEG_STATE_ABILITY_DETECT 6
5099#define ANEG_STATE_ACK_DETECT_INIT 7
5100#define ANEG_STATE_ACK_DETECT 8
5101#define ANEG_STATE_COMPLETE_ACK_INIT 9
5102#define ANEG_STATE_COMPLETE_ACK 10
5103#define ANEG_STATE_IDLE_DETECT_INIT 11
5104#define ANEG_STATE_IDLE_DETECT 12
5105#define ANEG_STATE_LINK_OK 13
5106#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5107#define ANEG_STATE_NEXT_PAGE_WAIT 15
5108
5109 u32 flags;
5110#define MR_AN_ENABLE 0x00000001
5111#define MR_RESTART_AN 0x00000002
5112#define MR_AN_COMPLETE 0x00000004
5113#define MR_PAGE_RX 0x00000008
5114#define MR_NP_LOADED 0x00000010
5115#define MR_TOGGLE_TX 0x00000020
5116#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5117#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5118#define MR_LP_ADV_SYM_PAUSE 0x00000100
5119#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5120#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5121#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5122#define MR_LP_ADV_NEXT_PAGE 0x00001000
5123#define MR_TOGGLE_RX 0x00002000
5124#define MR_NP_RX 0x00004000
5125
5126#define MR_LINK_OK 0x80000000
5127
5128 unsigned long link_time, cur_time;
5129
5130 u32 ability_match_cfg;
5131 int ability_match_count;
5132
5133 char ability_match, idle_match, ack_match;
5134
5135 u32 txconfig, rxconfig;
5136#define ANEG_CFG_NP 0x00000080
5137#define ANEG_CFG_ACK 0x00000040
5138#define ANEG_CFG_RF2 0x00000020
5139#define ANEG_CFG_RF1 0x00000010
5140#define ANEG_CFG_PS2 0x00000001
5141#define ANEG_CFG_PS1 0x00008000
5142#define ANEG_CFG_HD 0x00004000
5143#define ANEG_CFG_FD 0x00002000
5144#define ANEG_CFG_INVAL 0x00001f06
5145
5146};
5147#define ANEG_OK 0
5148#define ANEG_DONE 1
5149#define ANEG_TIMER_ENAB 2
5150#define ANEG_FAILED -1
5151
5152#define ANEG_STATE_SETTLE_TIME 10000
5153
5154static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5155 struct tg3_fiber_aneginfo *ap)
5156{
Matt Carlson5be73b42007-12-20 20:09:29 -08005157 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 unsigned long delta;
5159 u32 rx_cfg_reg;
5160 int ret;
5161
5162 if (ap->state == ANEG_STATE_UNKNOWN) {
5163 ap->rxconfig = 0;
5164 ap->link_time = 0;
5165 ap->cur_time = 0;
5166 ap->ability_match_cfg = 0;
5167 ap->ability_match_count = 0;
5168 ap->ability_match = 0;
5169 ap->idle_match = 0;
5170 ap->ack_match = 0;
5171 }
5172 ap->cur_time++;
5173
5174 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5175 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5176
5177 if (rx_cfg_reg != ap->ability_match_cfg) {
5178 ap->ability_match_cfg = rx_cfg_reg;
5179 ap->ability_match = 0;
5180 ap->ability_match_count = 0;
5181 } else {
5182 if (++ap->ability_match_count > 1) {
5183 ap->ability_match = 1;
5184 ap->ability_match_cfg = rx_cfg_reg;
5185 }
5186 }
5187 if (rx_cfg_reg & ANEG_CFG_ACK)
5188 ap->ack_match = 1;
5189 else
5190 ap->ack_match = 0;
5191
5192 ap->idle_match = 0;
5193 } else {
5194 ap->idle_match = 1;
5195 ap->ability_match_cfg = 0;
5196 ap->ability_match_count = 0;
5197 ap->ability_match = 0;
5198 ap->ack_match = 0;
5199
5200 rx_cfg_reg = 0;
5201 }
5202
5203 ap->rxconfig = rx_cfg_reg;
5204 ret = ANEG_OK;
5205
Matt Carlson33f401a2010-04-05 10:19:27 +00005206 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 case ANEG_STATE_UNKNOWN:
5208 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5209 ap->state = ANEG_STATE_AN_ENABLE;
5210
5211 /* fallthru */
5212 case ANEG_STATE_AN_ENABLE:
5213 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5214 if (ap->flags & MR_AN_ENABLE) {
5215 ap->link_time = 0;
5216 ap->cur_time = 0;
5217 ap->ability_match_cfg = 0;
5218 ap->ability_match_count = 0;
5219 ap->ability_match = 0;
5220 ap->idle_match = 0;
5221 ap->ack_match = 0;
5222
5223 ap->state = ANEG_STATE_RESTART_INIT;
5224 } else {
5225 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5226 }
5227 break;
5228
5229 case ANEG_STATE_RESTART_INIT:
5230 ap->link_time = ap->cur_time;
5231 ap->flags &= ~(MR_NP_LOADED);
5232 ap->txconfig = 0;
5233 tw32(MAC_TX_AUTO_NEG, 0);
5234 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5235 tw32_f(MAC_MODE, tp->mac_mode);
5236 udelay(40);
5237
5238 ret = ANEG_TIMER_ENAB;
5239 ap->state = ANEG_STATE_RESTART;
5240
5241 /* fallthru */
5242 case ANEG_STATE_RESTART:
5243 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005244 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005246 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 break;
5249
5250 case ANEG_STATE_DISABLE_LINK_OK:
5251 ret = ANEG_DONE;
5252 break;
5253
5254 case ANEG_STATE_ABILITY_DETECT_INIT:
5255 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005256 ap->txconfig = ANEG_CFG_FD;
5257 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5258 if (flowctrl & ADVERTISE_1000XPAUSE)
5259 ap->txconfig |= ANEG_CFG_PS1;
5260 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5261 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5263 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5264 tw32_f(MAC_MODE, tp->mac_mode);
5265 udelay(40);
5266
5267 ap->state = ANEG_STATE_ABILITY_DETECT;
5268 break;
5269
5270 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005271 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 break;
5274
5275 case ANEG_STATE_ACK_DETECT_INIT:
5276 ap->txconfig |= ANEG_CFG_ACK;
5277 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5278 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5279 tw32_f(MAC_MODE, tp->mac_mode);
5280 udelay(40);
5281
5282 ap->state = ANEG_STATE_ACK_DETECT;
5283
5284 /* fallthru */
5285 case ANEG_STATE_ACK_DETECT:
5286 if (ap->ack_match != 0) {
5287 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5288 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5289 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5290 } else {
5291 ap->state = ANEG_STATE_AN_ENABLE;
5292 }
5293 } else if (ap->ability_match != 0 &&
5294 ap->rxconfig == 0) {
5295 ap->state = ANEG_STATE_AN_ENABLE;
5296 }
5297 break;
5298
5299 case ANEG_STATE_COMPLETE_ACK_INIT:
5300 if (ap->rxconfig & ANEG_CFG_INVAL) {
5301 ret = ANEG_FAILED;
5302 break;
5303 }
5304 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5305 MR_LP_ADV_HALF_DUPLEX |
5306 MR_LP_ADV_SYM_PAUSE |
5307 MR_LP_ADV_ASYM_PAUSE |
5308 MR_LP_ADV_REMOTE_FAULT1 |
5309 MR_LP_ADV_REMOTE_FAULT2 |
5310 MR_LP_ADV_NEXT_PAGE |
5311 MR_TOGGLE_RX |
5312 MR_NP_RX);
5313 if (ap->rxconfig & ANEG_CFG_FD)
5314 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5315 if (ap->rxconfig & ANEG_CFG_HD)
5316 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5317 if (ap->rxconfig & ANEG_CFG_PS1)
5318 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5319 if (ap->rxconfig & ANEG_CFG_PS2)
5320 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5321 if (ap->rxconfig & ANEG_CFG_RF1)
5322 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5323 if (ap->rxconfig & ANEG_CFG_RF2)
5324 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5325 if (ap->rxconfig & ANEG_CFG_NP)
5326 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5327
5328 ap->link_time = ap->cur_time;
5329
5330 ap->flags ^= (MR_TOGGLE_TX);
5331 if (ap->rxconfig & 0x0008)
5332 ap->flags |= MR_TOGGLE_RX;
5333 if (ap->rxconfig & ANEG_CFG_NP)
5334 ap->flags |= MR_NP_RX;
5335 ap->flags |= MR_PAGE_RX;
5336
5337 ap->state = ANEG_STATE_COMPLETE_ACK;
5338 ret = ANEG_TIMER_ENAB;
5339 break;
5340
5341 case ANEG_STATE_COMPLETE_ACK:
5342 if (ap->ability_match != 0 &&
5343 ap->rxconfig == 0) {
5344 ap->state = ANEG_STATE_AN_ENABLE;
5345 break;
5346 }
5347 delta = ap->cur_time - ap->link_time;
5348 if (delta > ANEG_STATE_SETTLE_TIME) {
5349 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5350 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5351 } else {
5352 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5353 !(ap->flags & MR_NP_RX)) {
5354 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5355 } else {
5356 ret = ANEG_FAILED;
5357 }
5358 }
5359 }
5360 break;
5361
5362 case ANEG_STATE_IDLE_DETECT_INIT:
5363 ap->link_time = ap->cur_time;
5364 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5365 tw32_f(MAC_MODE, tp->mac_mode);
5366 udelay(40);
5367
5368 ap->state = ANEG_STATE_IDLE_DETECT;
5369 ret = ANEG_TIMER_ENAB;
5370 break;
5371
5372 case ANEG_STATE_IDLE_DETECT:
5373 if (ap->ability_match != 0 &&
5374 ap->rxconfig == 0) {
5375 ap->state = ANEG_STATE_AN_ENABLE;
5376 break;
5377 }
5378 delta = ap->cur_time - ap->link_time;
5379 if (delta > ANEG_STATE_SETTLE_TIME) {
5380 /* XXX another gem from the Broadcom driver :( */
5381 ap->state = ANEG_STATE_LINK_OK;
5382 }
5383 break;
5384
5385 case ANEG_STATE_LINK_OK:
5386 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5387 ret = ANEG_DONE;
5388 break;
5389
5390 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5391 /* ??? unimplemented */
5392 break;
5393
5394 case ANEG_STATE_NEXT_PAGE_WAIT:
5395 /* ??? unimplemented */
5396 break;
5397
5398 default:
5399 ret = ANEG_FAILED;
5400 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
5403 return ret;
5404}
5405
Matt Carlson5be73b42007-12-20 20:09:29 -08005406static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407{
5408 int res = 0;
5409 struct tg3_fiber_aneginfo aninfo;
5410 int status = ANEG_FAILED;
5411 unsigned int tick;
5412 u32 tmp;
5413
5414 tw32_f(MAC_TX_AUTO_NEG, 0);
5415
5416 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5417 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5418 udelay(40);
5419
5420 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5421 udelay(40);
5422
5423 memset(&aninfo, 0, sizeof(aninfo));
5424 aninfo.flags |= MR_AN_ENABLE;
5425 aninfo.state = ANEG_STATE_UNKNOWN;
5426 aninfo.cur_time = 0;
5427 tick = 0;
5428 while (++tick < 195000) {
5429 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5430 if (status == ANEG_DONE || status == ANEG_FAILED)
5431 break;
5432
5433 udelay(1);
5434 }
5435
5436 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5437 tw32_f(MAC_MODE, tp->mac_mode);
5438 udelay(40);
5439
Matt Carlson5be73b42007-12-20 20:09:29 -08005440 *txflags = aninfo.txconfig;
5441 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 if (status == ANEG_DONE &&
5444 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5445 MR_LP_ADV_FULL_DUPLEX)))
5446 res = 1;
5447
5448 return res;
5449}
5450
5451static void tg3_init_bcm8002(struct tg3 *tp)
5452{
5453 u32 mac_status = tr32(MAC_STATUS);
5454 int i;
5455
5456 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005457 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 !(mac_status & MAC_STATUS_PCS_SYNCED))
5459 return;
5460
5461 /* Set PLL lock range. */
5462 tg3_writephy(tp, 0x16, 0x8007);
5463
5464 /* SW reset */
5465 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5466
5467 /* Wait for reset to complete. */
5468 /* XXX schedule_timeout() ... */
5469 for (i = 0; i < 500; i++)
5470 udelay(10);
5471
5472 /* Config mode; select PMA/Ch 1 regs. */
5473 tg3_writephy(tp, 0x10, 0x8411);
5474
5475 /* Enable auto-lock and comdet, select txclk for tx. */
5476 tg3_writephy(tp, 0x11, 0x0a10);
5477
5478 tg3_writephy(tp, 0x18, 0x00a0);
5479 tg3_writephy(tp, 0x16, 0x41ff);
5480
5481 /* Assert and deassert POR. */
5482 tg3_writephy(tp, 0x13, 0x0400);
5483 udelay(40);
5484 tg3_writephy(tp, 0x13, 0x0000);
5485
5486 tg3_writephy(tp, 0x11, 0x0a50);
5487 udelay(40);
5488 tg3_writephy(tp, 0x11, 0x0a10);
5489
5490 /* Wait for signal to stabilize */
5491 /* XXX schedule_timeout() ... */
5492 for (i = 0; i < 15000; i++)
5493 udelay(10);
5494
5495 /* Deselect the channel register so we can read the PHYID
5496 * later.
5497 */
5498 tg3_writephy(tp, 0x10, 0x8011);
5499}
5500
Joe Perches953c96e2013-04-09 10:18:14 +00005501static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005503 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005504 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 u32 sg_dig_ctrl, sg_dig_status;
5506 u32 serdes_cfg, expected_sg_dig_ctrl;
5507 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
5509 serdes_cfg = 0;
5510 expected_sg_dig_ctrl = 0;
5511 workaround = 0;
5512 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005513 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Joe Perches41535772013-02-16 11:20:04 +00005515 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5516 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 workaround = 1;
5518 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5519 port_a = 0;
5520
5521 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5522 /* preserve bits 20-23 for voltage regulator */
5523 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5524 }
5525
5526 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5527
5528 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005529 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 if (workaround) {
5531 u32 val = serdes_cfg;
5532
5533 if (port_a)
5534 val |= 0xc010000;
5535 else
5536 val |= 0x4010000;
5537 tw32_f(MAC_SERDES_CFG, val);
5538 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005539
5540 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
5542 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5543 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005544 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546 goto out;
5547 }
5548
5549 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005550 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
Matt Carlson82cd3d12007-12-20 20:09:00 -08005552 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5553 if (flowctrl & ADVERTISE_1000XPAUSE)
5554 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5555 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5556 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005559 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005560 tp->serdes_counter &&
5561 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5562 MAC_STATUS_RCVD_CFG)) ==
5563 MAC_STATUS_PCS_SYNCED)) {
5564 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005565 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005566 goto out;
5567 }
5568restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 if (workaround)
5570 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005571 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 udelay(5);
5573 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5574
Michael Chan3d3ebe72006-09-27 15:59:15 -07005575 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005576 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5578 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005579 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 mac_status = tr32(MAC_STATUS);
5581
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005582 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005584 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Matt Carlson82cd3d12007-12-20 20:09:00 -08005586 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5587 local_adv |= ADVERTISE_1000XPAUSE;
5588 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5589 local_adv |= ADVERTISE_1000XPSE_ASYM;
5590
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005591 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005592 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005593 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005594 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Matt Carlson859edb22011-12-08 14:40:16 +00005596 tp->link_config.rmt_adv =
5597 mii_adv_to_ethtool_adv_x(remote_adv);
5598
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005600 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005601 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005602 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005603 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005604 if (tp->serdes_counter)
5605 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 else {
5607 if (workaround) {
5608 u32 val = serdes_cfg;
5609
5610 if (port_a)
5611 val |= 0xc010000;
5612 else
5613 val |= 0x4010000;
5614
5615 tw32_f(MAC_SERDES_CFG, val);
5616 }
5617
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005618 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 udelay(40);
5620
5621 /* Link parallel detection - link is up */
5622 /* only if we have PCS_SYNC and not */
5623 /* receiving config code words */
5624 mac_status = tr32(MAC_STATUS);
5625 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5626 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5627 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005628 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005629 tp->phy_flags |=
5630 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005631 tp->serdes_counter =
5632 SERDES_PARALLEL_DET_TIMEOUT;
5633 } else
5634 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 }
5636 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005637 } else {
5638 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005639 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 }
5641
5642out:
5643 return current_link_up;
5644}
5645
Joe Perches953c96e2013-04-09 10:18:14 +00005646static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647{
Joe Perches953c96e2013-04-09 10:18:14 +00005648 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005650 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005654 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005656
Matt Carlson5be73b42007-12-20 20:09:29 -08005657 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5658 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Matt Carlson5be73b42007-12-20 20:09:29 -08005660 if (txflags & ANEG_CFG_PS1)
5661 local_adv |= ADVERTISE_1000XPAUSE;
5662 if (txflags & ANEG_CFG_PS2)
5663 local_adv |= ADVERTISE_1000XPSE_ASYM;
5664
5665 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5666 remote_adv |= LPA_1000XPAUSE;
5667 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5668 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Matt Carlson859edb22011-12-08 14:40:16 +00005670 tp->link_config.rmt_adv =
5671 mii_adv_to_ethtool_adv_x(remote_adv);
5672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 tg3_setup_flow_control(tp, local_adv, remote_adv);
5674
Joe Perches953c96e2013-04-09 10:18:14 +00005675 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 }
5677 for (i = 0; i < 30; i++) {
5678 udelay(20);
5679 tw32_f(MAC_STATUS,
5680 (MAC_STATUS_SYNC_CHANGED |
5681 MAC_STATUS_CFG_CHANGED));
5682 udelay(40);
5683 if ((tr32(MAC_STATUS) &
5684 (MAC_STATUS_SYNC_CHANGED |
5685 MAC_STATUS_CFG_CHANGED)) == 0)
5686 break;
5687 }
5688
5689 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005690 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5692 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005693 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005695 tg3_setup_flow_control(tp, 0, 0);
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005698 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5701 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005702
5703 tw32_f(MAC_MODE, tp->mac_mode);
5704 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 }
5706
5707out:
5708 return current_link_up;
5709}
5710
Joe Perches953c96e2013-04-09 10:18:14 +00005711static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712{
5713 u32 orig_pause_cfg;
5714 u16 orig_active_speed;
5715 u8 orig_active_duplex;
5716 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005717 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 int i;
5719
Matt Carlson8d018622007-12-20 20:05:44 -08005720 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 orig_active_speed = tp->link_config.active_speed;
5722 orig_active_duplex = tp->link_config.active_duplex;
5723
Joe Perches63c3a662011-04-26 08:12:10 +00005724 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005725 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005726 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 mac_status = tr32(MAC_STATUS);
5728 mac_status &= (MAC_STATUS_PCS_SYNCED |
5729 MAC_STATUS_SIGNAL_DET |
5730 MAC_STATUS_CFG_CHANGED |
5731 MAC_STATUS_RCVD_CFG);
5732 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5733 MAC_STATUS_SIGNAL_DET)) {
5734 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5735 MAC_STATUS_CFG_CHANGED));
5736 return 0;
5737 }
5738 }
5739
5740 tw32_f(MAC_TX_AUTO_NEG, 0);
5741
5742 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5743 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5744 tw32_f(MAC_MODE, tp->mac_mode);
5745 udelay(40);
5746
Matt Carlson79eb6902010-02-17 15:17:03 +00005747 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 tg3_init_bcm8002(tp);
5749
5750 /* Enable link change event even when serdes polling. */
5751 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5752 udelay(40);
5753
Joe Perches953c96e2013-04-09 10:18:14 +00005754 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005755 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 mac_status = tr32(MAC_STATUS);
5757
Joe Perches63c3a662011-04-26 08:12:10 +00005758 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5760 else
5761 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5762
Matt Carlson898a56f2009-08-28 14:02:40 +00005763 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005765 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 for (i = 0; i < 100; i++) {
5768 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5769 MAC_STATUS_CFG_CHANGED));
5770 udelay(5);
5771 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005772 MAC_STATUS_CFG_CHANGED |
5773 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 break;
5775 }
5776
5777 mac_status = tr32(MAC_STATUS);
5778 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005779 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005780 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5781 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 tw32_f(MAC_MODE, (tp->mac_mode |
5783 MAC_MODE_SEND_CONFIGS));
5784 udelay(1);
5785 tw32_f(MAC_MODE, tp->mac_mode);
5786 }
5787 }
5788
Joe Perches953c96e2013-04-09 10:18:14 +00005789 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 tp->link_config.active_speed = SPEED_1000;
5791 tp->link_config.active_duplex = DUPLEX_FULL;
5792 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5793 LED_CTRL_LNKLED_OVERRIDE |
5794 LED_CTRL_1000MBPS_ON));
5795 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005796 tp->link_config.active_speed = SPEED_UNKNOWN;
5797 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5799 LED_CTRL_LNKLED_OVERRIDE |
5800 LED_CTRL_TRAFFIC_OVERRIDE));
5801 }
5802
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005803 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005804 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (orig_pause_cfg != now_pause_cfg ||
5806 orig_active_speed != tp->link_config.active_speed ||
5807 orig_active_duplex != tp->link_config.active_duplex)
5808 tg3_link_report(tp);
5809 }
5810
5811 return 0;
5812}
5813
Joe Perches953c96e2013-04-09 10:18:14 +00005814static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005815{
Joe Perches953c96e2013-04-09 10:18:14 +00005816 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005817 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005818 u16 current_speed = SPEED_UNKNOWN;
5819 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005820 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005821 u32 local_adv, remote_adv, sgsr;
5822
5823 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5824 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5825 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5826 (sgsr & SERDES_TG3_SGMII_MODE)) {
5827
5828 if (force_reset)
5829 tg3_phy_reset(tp);
5830
5831 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5832
5833 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5834 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5835 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005836 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005837 if (sgsr & SERDES_TG3_SPEED_1000) {
5838 current_speed = SPEED_1000;
5839 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5840 } else if (sgsr & SERDES_TG3_SPEED_100) {
5841 current_speed = SPEED_100;
5842 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5843 } else {
5844 current_speed = SPEED_10;
5845 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5846 }
5847
5848 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5849 current_duplex = DUPLEX_FULL;
5850 else
5851 current_duplex = DUPLEX_HALF;
5852 }
5853
5854 tw32_f(MAC_MODE, tp->mac_mode);
5855 udelay(40);
5856
5857 tg3_clear_mac_status(tp);
5858
5859 goto fiber_setup_done;
5860 }
Michael Chan747e8f82005-07-25 12:33:22 -07005861
5862 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5863 tw32_f(MAC_MODE, tp->mac_mode);
5864 udelay(40);
5865
Michael Chan3310e242013-04-09 08:48:05 +00005866 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005867
5868 if (force_reset)
5869 tg3_phy_reset(tp);
5870
Matt Carlson859edb22011-12-08 14:40:16 +00005871 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005872
5873 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5874 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005875 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005876 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5877 bmsr |= BMSR_LSTATUS;
5878 else
5879 bmsr &= ~BMSR_LSTATUS;
5880 }
Michael Chan747e8f82005-07-25 12:33:22 -07005881
5882 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5883
5884 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005885 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005886 /* do nothing, just check for link up at the end */
5887 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005888 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005889
5890 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005891 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5892 ADVERTISE_1000XPAUSE |
5893 ADVERTISE_1000XPSE_ASYM |
5894 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005895
Matt Carlson28011cf2011-11-16 18:36:59 -05005896 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005897 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005898
Matt Carlson28011cf2011-11-16 18:36:59 -05005899 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5900 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005901 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5902 tg3_writephy(tp, MII_BMCR, bmcr);
5903
5904 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005905 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005906 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005907
5908 return err;
5909 }
5910 } else {
5911 u32 new_bmcr;
5912
5913 bmcr &= ~BMCR_SPEED1000;
5914 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5915
5916 if (tp->link_config.duplex == DUPLEX_FULL)
5917 new_bmcr |= BMCR_FULLDPLX;
5918
5919 if (new_bmcr != bmcr) {
5920 /* BMCR_SPEED1000 is a reserved bit that needs
5921 * to be set on write.
5922 */
5923 new_bmcr |= BMCR_SPEED1000;
5924
5925 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005926 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005927 u32 adv;
5928
5929 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5930 adv &= ~(ADVERTISE_1000XFULL |
5931 ADVERTISE_1000XHALF |
5932 ADVERTISE_SLCT);
5933 tg3_writephy(tp, MII_ADVERTISE, adv);
5934 tg3_writephy(tp, MII_BMCR, bmcr |
5935 BMCR_ANRESTART |
5936 BMCR_ANENABLE);
5937 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005938 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005939 }
5940 tg3_writephy(tp, MII_BMCR, new_bmcr);
5941 bmcr = new_bmcr;
5942 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5943 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005944 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005945 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5946 bmsr |= BMSR_LSTATUS;
5947 else
5948 bmsr &= ~BMSR_LSTATUS;
5949 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005950 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005951 }
5952 }
5953
5954 if (bmsr & BMSR_LSTATUS) {
5955 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005956 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005957 if (bmcr & BMCR_FULLDPLX)
5958 current_duplex = DUPLEX_FULL;
5959 else
5960 current_duplex = DUPLEX_HALF;
5961
Matt Carlsonef167e22007-12-20 20:10:01 -08005962 local_adv = 0;
5963 remote_adv = 0;
5964
Michael Chan747e8f82005-07-25 12:33:22 -07005965 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005966 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005967
5968 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5969 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5970 common = local_adv & remote_adv;
5971 if (common & (ADVERTISE_1000XHALF |
5972 ADVERTISE_1000XFULL)) {
5973 if (common & ADVERTISE_1000XFULL)
5974 current_duplex = DUPLEX_FULL;
5975 else
5976 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005977
5978 tp->link_config.rmt_adv =
5979 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005980 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005981 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005982 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005983 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005984 }
Michael Chan747e8f82005-07-25 12:33:22 -07005985 }
5986 }
5987
Michael Chan85730a62013-04-09 08:48:06 +00005988fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005989 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005990 tg3_setup_flow_control(tp, local_adv, remote_adv);
5991
Michael Chan747e8f82005-07-25 12:33:22 -07005992 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5993 if (tp->link_config.active_duplex == DUPLEX_HALF)
5994 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5995
5996 tw32_f(MAC_MODE, tp->mac_mode);
5997 udelay(40);
5998
5999 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
6000
6001 tp->link_config.active_speed = current_speed;
6002 tp->link_config.active_duplex = current_duplex;
6003
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006004 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07006005 return err;
6006}
6007
6008static void tg3_serdes_parallel_detect(struct tg3 *tp)
6009{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006010 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006011 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006012 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006013 return;
6014 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006015
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006016 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006017 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6018 u32 bmcr;
6019
6020 tg3_readphy(tp, MII_BMCR, &bmcr);
6021 if (bmcr & BMCR_ANENABLE) {
6022 u32 phy1, phy2;
6023
6024 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006025 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6026 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006027
6028 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006029 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6030 MII_TG3_DSP_EXP1_INT_STAT);
6031 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6032 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006033
6034 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6035 /* We have signal detect and not receiving
6036 * config code words, link is up by parallel
6037 * detection.
6038 */
6039
6040 bmcr &= ~BMCR_ANENABLE;
6041 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6042 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006043 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006044 }
6045 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006046 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006047 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006048 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006049 u32 phy2;
6050
6051 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006052 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6053 MII_TG3_DSP_EXP1_INT_STAT);
6054 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006055 if (phy2 & 0x20) {
6056 u32 bmcr;
6057
6058 /* Config code words received, turn on autoneg. */
6059 tg3_readphy(tp, MII_BMCR, &bmcr);
6060 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6061
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006062 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006063
6064 }
6065 }
6066}
6067
Joe Perches953c96e2013-04-09 10:18:14 +00006068static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006070 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 int err;
6072
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006073 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006075 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006076 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006077 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Joe Perches41535772013-02-16 11:20:04 +00006080 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006081 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006082
6083 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6084 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6085 scale = 65;
6086 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6087 scale = 6;
6088 else
6089 scale = 12;
6090
6091 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6092 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6093 tw32(GRC_MISC_CFG, val);
6094 }
6095
Matt Carlsonf2096f92011-04-05 14:22:48 +00006096 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6097 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006098 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6099 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006100 val |= tr32(MAC_TX_LENGTHS) &
6101 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6102 TX_LENGTHS_CNT_DWN_VAL_MSK);
6103
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 if (tp->link_config.active_speed == SPEED_1000 &&
6105 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006106 tw32(MAC_TX_LENGTHS, val |
6107 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006109 tw32(MAC_TX_LENGTHS, val |
6110 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
Joe Perches63c3a662011-04-26 08:12:10 +00006112 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006113 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006115 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 } else {
6117 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6118 }
6119 }
6120
Joe Perches63c3a662011-04-26 08:12:10 +00006121 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006122 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006123 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006124 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6125 tp->pwrmgmt_thresh;
6126 else
6127 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6128 tw32(PCIE_PWR_MGMT_THRESH, val);
6129 }
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 return err;
6132}
6133
Matt Carlsonbe947302012-12-03 19:36:57 +00006134/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006135static u64 tg3_refclk_read(struct tg3 *tp)
6136{
6137 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6138 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6139}
6140
6141/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006142static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6143{
Nithin Sujir92e64572013-07-29 13:58:38 -07006144 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6145
6146 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006147 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6148 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006149 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006150}
6151
Matt Carlson7d41e492012-12-03 19:36:58 +00006152static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6153static inline void tg3_full_unlock(struct tg3 *tp);
6154static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6155{
6156 struct tg3 *tp = netdev_priv(dev);
6157
6158 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6159 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006160 SOF_TIMESTAMPING_SOFTWARE;
6161
6162 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006163 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006164 SOF_TIMESTAMPING_RX_HARDWARE |
6165 SOF_TIMESTAMPING_RAW_HARDWARE;
6166 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006167
6168 if (tp->ptp_clock)
6169 info->phc_index = ptp_clock_index(tp->ptp_clock);
6170 else
6171 info->phc_index = -1;
6172
6173 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6174
6175 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6176 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6177 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6178 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6179 return 0;
6180}
6181
6182static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6183{
6184 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6185 bool neg_adj = false;
6186 u32 correction = 0;
6187
6188 if (ppb < 0) {
6189 neg_adj = true;
6190 ppb = -ppb;
6191 }
6192
6193 /* Frequency adjustment is performed using hardware with a 24 bit
6194 * accumulator and a programmable correction value. On each clk, the
6195 * correction value gets added to the accumulator and when it
6196 * overflows, the time counter is incremented/decremented.
6197 *
6198 * So conversion from ppb to correction value is
6199 * ppb * (1 << 24) / 1000000000
6200 */
6201 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6202 TG3_EAV_REF_CLK_CORRECT_MASK;
6203
6204 tg3_full_lock(tp, 0);
6205
6206 if (correction)
6207 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6208 TG3_EAV_REF_CLK_CORRECT_EN |
6209 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6210 else
6211 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6212
6213 tg3_full_unlock(tp);
6214
6215 return 0;
6216}
6217
6218static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6219{
6220 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6221
6222 tg3_full_lock(tp, 0);
6223 tp->ptp_adjust += delta;
6224 tg3_full_unlock(tp);
6225
6226 return 0;
6227}
6228
Richard Cochranf578b412015-03-29 23:11:57 +02006229static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006230{
6231 u64 ns;
Matt Carlson7d41e492012-12-03 19:36:58 +00006232 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6233
6234 tg3_full_lock(tp, 0);
6235 ns = tg3_refclk_read(tp);
6236 ns += tp->ptp_adjust;
6237 tg3_full_unlock(tp);
6238
Richard Cochran7a20efb2015-03-31 23:08:08 +02006239 *ts = ns_to_timespec64(ns);
Matt Carlson7d41e492012-12-03 19:36:58 +00006240
6241 return 0;
6242}
6243
6244static int tg3_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochranf578b412015-03-29 23:11:57 +02006245 const struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006246{
6247 u64 ns;
6248 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6249
Richard Cochranf578b412015-03-29 23:11:57 +02006250 ns = timespec64_to_ns(ts);
Matt Carlson7d41e492012-12-03 19:36:58 +00006251
6252 tg3_full_lock(tp, 0);
6253 tg3_refclk_write(tp, ns);
6254 tp->ptp_adjust = 0;
6255 tg3_full_unlock(tp);
6256
6257 return 0;
6258}
6259
6260static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6261 struct ptp_clock_request *rq, int on)
6262{
Nithin Sujir92e64572013-07-29 13:58:38 -07006263 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6264 u32 clock_ctl;
6265 int rval = 0;
6266
6267 switch (rq->type) {
6268 case PTP_CLK_REQ_PEROUT:
6269 if (rq->perout.index != 0)
6270 return -EINVAL;
6271
6272 tg3_full_lock(tp, 0);
6273 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6274 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6275
6276 if (on) {
6277 u64 nsec;
6278
6279 nsec = rq->perout.start.sec * 1000000000ULL +
6280 rq->perout.start.nsec;
6281
6282 if (rq->perout.period.sec || rq->perout.period.nsec) {
6283 netdev_warn(tp->dev,
6284 "Device supports only a one-shot timesync output, period must be 0\n");
6285 rval = -EINVAL;
6286 goto err_out;
6287 }
6288
6289 if (nsec & (1ULL << 63)) {
6290 netdev_warn(tp->dev,
6291 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6292 rval = -EINVAL;
6293 goto err_out;
6294 }
6295
6296 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6297 tw32(TG3_EAV_WATCHDOG0_MSB,
6298 TG3_EAV_WATCHDOG0_EN |
6299 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6300
6301 tw32(TG3_EAV_REF_CLCK_CTL,
6302 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6303 } else {
6304 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6305 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6306 }
6307
6308err_out:
6309 tg3_full_unlock(tp);
6310 return rval;
6311
6312 default:
6313 break;
6314 }
6315
Matt Carlson7d41e492012-12-03 19:36:58 +00006316 return -EOPNOTSUPP;
6317}
6318
6319static const struct ptp_clock_info tg3_ptp_caps = {
6320 .owner = THIS_MODULE,
6321 .name = "tg3 clock",
6322 .max_adj = 250000000,
6323 .n_alarm = 0,
6324 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006325 .n_per_out = 1,
Richard Cochran4986b4f02014-03-20 22:21:55 +01006326 .n_pins = 0,
Matt Carlson7d41e492012-12-03 19:36:58 +00006327 .pps = 0,
6328 .adjfreq = tg3_ptp_adjfreq,
6329 .adjtime = tg3_ptp_adjtime,
Richard Cochranf578b412015-03-29 23:11:57 +02006330 .gettime64 = tg3_ptp_gettime,
6331 .settime64 = tg3_ptp_settime,
Matt Carlson7d41e492012-12-03 19:36:58 +00006332 .enable = tg3_ptp_enable,
6333};
6334
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006335static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6336 struct skb_shared_hwtstamps *timestamp)
6337{
6338 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6339 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6340 tp->ptp_adjust);
6341}
6342
Matt Carlsonbe947302012-12-03 19:36:57 +00006343/* tp->lock must be held */
6344static void tg3_ptp_init(struct tg3 *tp)
6345{
6346 if (!tg3_flag(tp, PTP_CAPABLE))
6347 return;
6348
6349 /* Initialize the hardware clock to the system time. */
6350 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6351 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006352 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006353}
6354
6355/* tp->lock must be held */
6356static void tg3_ptp_resume(struct tg3 *tp)
6357{
6358 if (!tg3_flag(tp, PTP_CAPABLE))
6359 return;
6360
6361 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6362 tp->ptp_adjust = 0;
6363}
6364
6365static void tg3_ptp_fini(struct tg3 *tp)
6366{
6367 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6368 return;
6369
Matt Carlson7d41e492012-12-03 19:36:58 +00006370 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006371 tp->ptp_clock = NULL;
6372 tp->ptp_adjust = 0;
6373}
6374
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006375static inline int tg3_irq_sync(struct tg3 *tp)
6376{
6377 return tp->irq_sync;
6378}
6379
Matt Carlson97bd8e42011-04-13 11:05:04 +00006380static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6381{
6382 int i;
6383
6384 dst = (u32 *)((u8 *)dst + off);
6385 for (i = 0; i < len; i += sizeof(u32))
6386 *dst++ = tr32(off + i);
6387}
6388
6389static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6390{
6391 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6392 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6393 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6394 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6395 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6396 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6397 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6398 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6399 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6400 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6401 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6402 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6403 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6404 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6405 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6406 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6407 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6408 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6409 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6410
Joe Perches63c3a662011-04-26 08:12:10 +00006411 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006412 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6413
6414 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6415 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6416 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6417 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6418 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6419 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6420 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6421 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6422
Joe Perches63c3a662011-04-26 08:12:10 +00006423 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006424 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6425 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6426 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6427 }
6428
6429 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6430 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6431 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6432 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6433 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6434
Joe Perches63c3a662011-04-26 08:12:10 +00006435 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006436 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6437}
6438
6439static void tg3_dump_state(struct tg3 *tp)
6440{
6441 int i;
6442 u32 *regs;
6443
6444 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006445 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006446 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006447
Joe Perches63c3a662011-04-26 08:12:10 +00006448 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006449 /* Read up to but not including private PCI registers */
6450 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6451 regs[i / sizeof(u32)] = tr32(i);
6452 } else
6453 tg3_dump_legacy_regs(tp, regs);
6454
6455 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6456 if (!regs[i + 0] && !regs[i + 1] &&
6457 !regs[i + 2] && !regs[i + 3])
6458 continue;
6459
6460 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6461 i * 4,
6462 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6463 }
6464
6465 kfree(regs);
6466
6467 for (i = 0; i < tp->irq_cnt; i++) {
6468 struct tg3_napi *tnapi = &tp->napi[i];
6469
6470 /* SW status block */
6471 netdev_err(tp->dev,
6472 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6473 i,
6474 tnapi->hw_status->status,
6475 tnapi->hw_status->status_tag,
6476 tnapi->hw_status->rx_jumbo_consumer,
6477 tnapi->hw_status->rx_consumer,
6478 tnapi->hw_status->rx_mini_consumer,
6479 tnapi->hw_status->idx[0].rx_producer,
6480 tnapi->hw_status->idx[0].tx_consumer);
6481
6482 netdev_err(tp->dev,
6483 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6484 i,
6485 tnapi->last_tag, tnapi->last_irq_tag,
6486 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6487 tnapi->rx_rcb_ptr,
6488 tnapi->prodring.rx_std_prod_idx,
6489 tnapi->prodring.rx_std_cons_idx,
6490 tnapi->prodring.rx_jmb_prod_idx,
6491 tnapi->prodring.rx_jmb_cons_idx);
6492 }
6493}
6494
Michael Chandf3e6542006-05-26 17:48:07 -07006495/* This is called whenever we suspect that the system chipset is re-
6496 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6497 * is bogus tx completions. We try to recover by setting the
6498 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6499 * in the workqueue.
6500 */
6501static void tg3_tx_recover(struct tg3 *tp)
6502{
Joe Perches63c3a662011-04-26 08:12:10 +00006503 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006504 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6505
Matt Carlson5129c3a2010-04-05 10:19:23 +00006506 netdev_warn(tp->dev,
6507 "The system may be re-ordering memory-mapped I/O "
6508 "cycles to the network device, attempting to recover. "
6509 "Please report the problem to the driver maintainer "
6510 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006511
Joe Perches63c3a662011-04-26 08:12:10 +00006512 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006513}
6514
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006515static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006516{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006517 /* Tell compiler to fetch tx indices from memory. */
6518 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006519 return tnapi->tx_pending -
6520 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006521}
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523/* Tigon3 never reports partial packet sends. So we do not
6524 * need special logic to handle SKBs that have not had all
6525 * of their frags sent yet, like SunGEM does.
6526 */
Matt Carlson17375d22009-08-28 14:02:18 +00006527static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
Matt Carlson17375d22009-08-28 14:02:18 +00006529 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006530 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006531 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006532 struct netdev_queue *txq;
6533 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006534 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006535
Joe Perches63c3a662011-04-26 08:12:10 +00006536 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006537 index--;
6538
6539 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
6541 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006542 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006544 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
Michael Chandf3e6542006-05-26 17:48:07 -07006546 if (unlikely(skb == NULL)) {
6547 tg3_tx_recover(tp);
6548 return;
6549 }
6550
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006551 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6552 struct skb_shared_hwtstamps timestamp;
6553 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6554 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6555
6556 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6557
6558 skb_tstamp_tx(skb, &timestamp);
6559 }
6560
Alexander Duyckf4188d82009-12-02 16:48:38 +00006561 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006562 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006563 skb_headlen(skb),
6564 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565
6566 ri->skb = NULL;
6567
Matt Carlsone01ee142011-07-27 14:20:50 +00006568 while (ri->fragmented) {
6569 ri->fragmented = false;
6570 sw_idx = NEXT_TX(sw_idx);
6571 ri = &tnapi->tx_buffers[sw_idx];
6572 }
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 sw_idx = NEXT_TX(sw_idx);
6575
6576 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006577 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006578 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6579 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006580
6581 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006582 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006583 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006584 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006585
6586 while (ri->fragmented) {
6587 ri->fragmented = false;
6588 sw_idx = NEXT_TX(sw_idx);
6589 ri = &tnapi->tx_buffers[sw_idx];
6590 }
6591
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 sw_idx = NEXT_TX(sw_idx);
6593 }
6594
Tom Herbert298376d2011-11-28 16:33:30 +00006595 pkts_compl++;
6596 bytes_compl += skb->len;
6597
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07006598 dev_consume_skb_any(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006599
6600 if (unlikely(tx_bug)) {
6601 tg3_tx_recover(tp);
6602 return;
6603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 }
6605
Tom Herbert5cb917b2012-03-05 19:53:50 +00006606 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006607
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006608 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
Michael Chan1b2a7202006-08-07 21:46:02 -07006610 /* Need to make the tx_cons update visible to tg3_start_xmit()
6611 * before checking for netif_queue_stopped(). Without the
6612 * memory barrier, there is a small possibility that tg3_start_xmit()
6613 * will miss it and cause the queue to be stopped forever.
6614 */
6615 smp_mb();
6616
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006617 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006618 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006619 __netif_tx_lock(txq, smp_processor_id());
6620 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006621 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006622 netif_tx_wake_queue(txq);
6623 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625}
6626
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006627static void tg3_frag_free(bool is_frag, void *data)
6628{
6629 if (is_frag)
Alexander Duycke51423d2015-05-06 21:12:31 -07006630 skb_free_frag(data);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006631 else
6632 kfree(data);
6633}
6634
Eric Dumazet9205fd92011-11-18 06:47:01 +00006635static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006636{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006637 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6638 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6639
Eric Dumazet9205fd92011-11-18 06:47:01 +00006640 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006641 return;
6642
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006643 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006644 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006645 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006646 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006647}
6648
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006649
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650/* Returns size of skb allocated or < 0 on error.
6651 *
6652 * We only need to fill in the address because the other members
6653 * of the RX descriptor are invariant, see tg3_init_rings.
6654 *
6655 * Note the purposeful assymetry of cpu vs. chip accesses. For
6656 * posting buffers we only dirty the first cache line of the RX
6657 * descriptor (containing the address). Whereas for the RX status
6658 * buffers the cpu only reads the last cacheline of the RX descriptor
6659 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6660 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006661static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006662 u32 opaque_key, u32 dest_idx_unmasked,
6663 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664{
6665 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006666 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006667 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006669 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 switch (opaque_key) {
6672 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006673 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006674 desc = &tpr->rx_std[dest_idx];
6675 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006676 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 break;
6678
6679 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006680 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006681 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006682 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006683 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 break;
6685
6686 default:
6687 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689
6690 /* Do not overwrite any of the map or rp information
6691 * until we are sure we can commit to a new buffer.
6692 *
6693 * Callers depend upon this behavior and assume that
6694 * we leave everything unchanged if we fail.
6695 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006696 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6697 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006698 if (skb_size <= PAGE_SIZE) {
6699 data = netdev_alloc_frag(skb_size);
6700 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006701 } else {
6702 data = kmalloc(skb_size, GFP_ATOMIC);
6703 *frag_size = 0;
6704 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006705 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 return -ENOMEM;
6707
Eric Dumazet9205fd92011-11-18 06:47:01 +00006708 mapping = pci_map_single(tp->pdev,
6709 data + TG3_RX_OFFSET(tp),
6710 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006712 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006713 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006714 return -EIO;
6715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Eric Dumazet9205fd92011-11-18 06:47:01 +00006717 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006718 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 desc->addr_hi = ((u64)mapping >> 32);
6721 desc->addr_lo = ((u64)mapping & 0xffffffff);
6722
Eric Dumazet9205fd92011-11-18 06:47:01 +00006723 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724}
6725
6726/* We only need to move over in the address because the other
6727 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006728 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 */
Matt Carlsona3896162009-11-13 13:03:44 +00006730static void tg3_recycle_rx(struct tg3_napi *tnapi,
6731 struct tg3_rx_prodring_set *dpr,
6732 u32 opaque_key, int src_idx,
6733 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734{
Matt Carlson17375d22009-08-28 14:02:18 +00006735 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6737 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006738 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006739 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
6741 switch (opaque_key) {
6742 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006743 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006744 dest_desc = &dpr->rx_std[dest_idx];
6745 dest_map = &dpr->rx_std_buffers[dest_idx];
6746 src_desc = &spr->rx_std[src_idx];
6747 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 break;
6749
6750 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006751 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006752 dest_desc = &dpr->rx_jmb[dest_idx].std;
6753 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6754 src_desc = &spr->rx_jmb[src_idx].std;
6755 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 break;
6757
6758 default:
6759 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Eric Dumazet9205fd92011-11-18 06:47:01 +00006762 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006763 dma_unmap_addr_set(dest_map, mapping,
6764 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 dest_desc->addr_hi = src_desc->addr_hi;
6766 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006767
6768 /* Ensure that the update to the skb happens after the physical
6769 * addresses have been transferred to the new BD location.
6770 */
6771 smp_wmb();
6772
Eric Dumazet9205fd92011-11-18 06:47:01 +00006773 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774}
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776/* The RX ring scheme is composed of multiple rings which post fresh
6777 * buffers to the chip, and one special ring the chip uses to report
6778 * status back to the host.
6779 *
6780 * The special ring reports the status of received packets to the
6781 * host. The chip does not write into the original descriptor the
6782 * RX buffer was obtained from. The chip simply takes the original
6783 * descriptor as provided by the host, updates the status and length
6784 * field, then writes this into the next status ring entry.
6785 *
6786 * Each ring the host uses to post buffers to the chip is described
6787 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6788 * it is first placed into the on-chip ram. When the packet's length
6789 * is known, it walks down the TG3_BDINFO entries to select the ring.
6790 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6791 * which is within the range of the new packet's length is chosen.
6792 *
6793 * The "separate ring for rx status" scheme may sound queer, but it makes
6794 * sense from a cache coherency perspective. If only the host writes
6795 * to the buffer post rings, and only the chip writes to the rx status
6796 * rings, then cache lines never move beyond shared-modified state.
6797 * If both the host and chip were to write into the same ring, cache line
6798 * eviction could occur since both entities want it in an exclusive state.
6799 */
Matt Carlson17375d22009-08-28 14:02:18 +00006800static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801{
Matt Carlson17375d22009-08-28 14:02:18 +00006802 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006803 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006804 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006805 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006806 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006808 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006810 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 /*
6812 * We need to order the read of hw_idx and the read of
6813 * the opaque cookie.
6814 */
6815 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 work_mask = 0;
6817 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006818 std_prod_idx = tpr->rx_std_prod_idx;
6819 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006821 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006822 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 unsigned int len;
6824 struct sk_buff *skb;
6825 dma_addr_t dma_addr;
6826 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006827 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006828 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829
6830 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6831 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6832 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006833 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006834 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006835 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006836 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006837 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006839 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006840 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006841 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006842 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006843 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
6846 work_mask |= opaque_key;
6847
Michael Chand7b95312014-02-28 15:05:10 -08006848 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006850 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 desc_idx, *post_ptr);
6852 drop_it_no_recycle:
6853 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006854 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 goto next_pkt;
6856 }
6857
Eric Dumazet9205fd92011-11-18 06:47:01 +00006858 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006859 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6860 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006862 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6863 RXD_FLAG_PTPSTAT_PTPV1 ||
6864 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6865 RXD_FLAG_PTPSTAT_PTPV2) {
6866 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6867 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6868 }
6869
Matt Carlsond2757fc2010-04-12 06:58:27 +00006870 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006872 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Eric Dumazet9205fd92011-11-18 06:47:01 +00006874 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006875 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 if (skb_size < 0)
6877 goto drop_it;
6878
Matt Carlson287be122009-08-28 13:58:46 +00006879 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 PCI_DMA_FROMDEVICE);
6881
Eric Dumazet9205fd92011-11-18 06:47:01 +00006882 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006883 * after the usage of the old DMA mapping.
6884 */
6885 smp_wmb();
6886
Eric Dumazet9205fd92011-11-18 06:47:01 +00006887 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006888
Ivan Vecera85aec732013-11-06 14:02:36 +01006889 skb = build_skb(data, frag_size);
6890 if (!skb) {
6891 tg3_frag_free(frag_size != 0, data);
6892 goto drop_it_no_recycle;
6893 }
6894 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006896 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 desc_idx, *post_ptr);
6898
Eric Dumazet9205fd92011-11-18 06:47:01 +00006899 skb = netdev_alloc_skb(tp->dev,
6900 len + TG3_RAW_IP_ALIGN);
6901 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 goto drop_it_no_recycle;
6903
Eric Dumazet9205fd92011-11-18 06:47:01 +00006904 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006906 memcpy(skb->data,
6907 data + TG3_RX_OFFSET(tp),
6908 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 }
6911
Eric Dumazet9205fd92011-11-18 06:47:01 +00006912 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006913 if (tstamp)
6914 tg3_hwclock_to_timestamp(tp, tstamp,
6915 skb_hwtstamps(skb));
6916
Michał Mirosławdc668912011-04-07 03:35:07 +00006917 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6919 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6920 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6921 skb->ip_summed = CHECKSUM_UNNECESSARY;
6922 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006923 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
6925 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006926
6927 if (len > (tp->dev->mtu + ETH_HLEN) &&
Vlad Yasevich7d3083e2014-09-30 19:39:36 -04006928 skb->protocol != htons(ETH_P_8021Q) &&
6929 skb->protocol != htons(ETH_P_8021AD)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006930 dev_kfree_skb_any(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006931 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006932 }
6933
Matt Carlson9dc7a112010-04-12 06:58:28 +00006934 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006935 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006936 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006937 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006938
Matt Carlsonbf933c82011-01-25 15:58:49 +00006939 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 received++;
6942 budget--;
6943
6944next_pkt:
6945 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006946
6947 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006948 tpr->rx_std_prod_idx = std_prod_idx &
6949 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006950 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6951 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006952 work_mask &= ~RXD_OPAQUE_RING_STD;
6953 rx_std_posted = 0;
6954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006956 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006957 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006958
6959 /* Refresh hw_idx to see if there is new work */
6960 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006961 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006962 rmb();
6963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 }
6965
6966 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006967 tnapi->rx_rcb_ptr = sw_idx;
6968 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969
6970 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006971 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006972 /* Sync BD data before updating mailbox */
6973 wmb();
6974
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006975 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006976 tpr->rx_std_prod_idx = std_prod_idx &
6977 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006978 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6979 tpr->rx_std_prod_idx);
6980 }
6981 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006982 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6983 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006984 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6985 tpr->rx_jmb_prod_idx);
6986 }
6987 mmiowb();
6988 } else if (work_mask) {
6989 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6990 * updated before the producer indices can be updated.
6991 */
6992 smp_wmb();
6993
Matt Carlson2c49a442010-09-30 10:34:35 +00006994 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6995 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006996
Michael Chan7ae52892012-03-21 15:38:33 +00006997 if (tnapi != &tp->napi[1]) {
6998 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006999 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00007000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
7003 return received;
7004}
7005
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007006static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007009 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007010 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7011
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 if (sblk->status & SD_STATUS_LINK_CHG) {
7013 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007014 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007015 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007016 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007017 tw32_f(MAC_STATUS,
7018 (MAC_STATUS_SYNC_CHANGED |
7019 MAC_STATUS_CFG_CHANGED |
7020 MAC_STATUS_MI_COMPLETION |
7021 MAC_STATUS_LNKSTATE_CHANGED));
7022 udelay(40);
7023 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007024 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007025 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 }
7027 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007028}
7029
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007030static int tg3_rx_prodring_xfer(struct tg3 *tp,
7031 struct tg3_rx_prodring_set *dpr,
7032 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007033{
7034 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007035 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007036
7037 while (1) {
7038 src_prod_idx = spr->rx_std_prod_idx;
7039
7040 /* Make sure updates to the rx_std_buffers[] entries and the
7041 * standard producer index are seen in the correct order.
7042 */
7043 smp_rmb();
7044
7045 if (spr->rx_std_cons_idx == src_prod_idx)
7046 break;
7047
7048 if (spr->rx_std_cons_idx < src_prod_idx)
7049 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7050 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007051 cpycnt = tp->rx_std_ring_mask + 1 -
7052 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007053
Matt Carlson2c49a442010-09-30 10:34:35 +00007054 cpycnt = min(cpycnt,
7055 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007056
7057 si = spr->rx_std_cons_idx;
7058 di = dpr->rx_std_prod_idx;
7059
Matt Carlsone92967b2010-02-12 14:47:06 +00007060 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007061 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007062 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007063 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007064 break;
7065 }
7066 }
7067
7068 if (!cpycnt)
7069 break;
7070
7071 /* Ensure that updates to the rx_std_buffers ring and the
7072 * shadowed hardware producer ring from tg3_recycle_skb() are
7073 * ordered correctly WRT the skb check above.
7074 */
7075 smp_rmb();
7076
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007077 memcpy(&dpr->rx_std_buffers[di],
7078 &spr->rx_std_buffers[si],
7079 cpycnt * sizeof(struct ring_info));
7080
7081 for (i = 0; i < cpycnt; i++, di++, si++) {
7082 struct tg3_rx_buffer_desc *sbd, *dbd;
7083 sbd = &spr->rx_std[si];
7084 dbd = &dpr->rx_std[di];
7085 dbd->addr_hi = sbd->addr_hi;
7086 dbd->addr_lo = sbd->addr_lo;
7087 }
7088
Matt Carlson2c49a442010-09-30 10:34:35 +00007089 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7090 tp->rx_std_ring_mask;
7091 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7092 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007093 }
7094
7095 while (1) {
7096 src_prod_idx = spr->rx_jmb_prod_idx;
7097
7098 /* Make sure updates to the rx_jmb_buffers[] entries and
7099 * the jumbo producer index are seen in the correct order.
7100 */
7101 smp_rmb();
7102
7103 if (spr->rx_jmb_cons_idx == src_prod_idx)
7104 break;
7105
7106 if (spr->rx_jmb_cons_idx < src_prod_idx)
7107 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7108 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007109 cpycnt = tp->rx_jmb_ring_mask + 1 -
7110 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007111
7112 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007113 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007114
7115 si = spr->rx_jmb_cons_idx;
7116 di = dpr->rx_jmb_prod_idx;
7117
Matt Carlsone92967b2010-02-12 14:47:06 +00007118 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007119 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007120 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007121 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007122 break;
7123 }
7124 }
7125
7126 if (!cpycnt)
7127 break;
7128
7129 /* Ensure that updates to the rx_jmb_buffers ring and the
7130 * shadowed hardware producer ring from tg3_recycle_skb() are
7131 * ordered correctly WRT the skb check above.
7132 */
7133 smp_rmb();
7134
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007135 memcpy(&dpr->rx_jmb_buffers[di],
7136 &spr->rx_jmb_buffers[si],
7137 cpycnt * sizeof(struct ring_info));
7138
7139 for (i = 0; i < cpycnt; i++, di++, si++) {
7140 struct tg3_rx_buffer_desc *sbd, *dbd;
7141 sbd = &spr->rx_jmb[si].std;
7142 dbd = &dpr->rx_jmb[di].std;
7143 dbd->addr_hi = sbd->addr_hi;
7144 dbd->addr_lo = sbd->addr_lo;
7145 }
7146
Matt Carlson2c49a442010-09-30 10:34:35 +00007147 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7148 tp->rx_jmb_ring_mask;
7149 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7150 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007151 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007152
7153 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007154}
7155
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007156static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7157{
7158 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
7160 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007161 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007162 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007163 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007164 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 }
7166
Matt Carlsonf891ea12012-04-24 13:37:01 +00007167 if (!tnapi->rx_rcb_prod_idx)
7168 return work_done;
7169
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 /* run RX thread, within the bounds set by NAPI.
7171 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007172 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007174 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007175 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
Joe Perches63c3a662011-04-26 08:12:10 +00007177 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007178 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007179 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007180 u32 std_prod_idx = dpr->rx_std_prod_idx;
7181 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007182
Michael Chan7ae52892012-03-21 15:38:33 +00007183 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007184 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007185 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007186 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007187
7188 wmb();
7189
Matt Carlsone4af1af2010-02-12 14:47:05 +00007190 if (std_prod_idx != dpr->rx_std_prod_idx)
7191 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7192 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007193
Matt Carlsone4af1af2010-02-12 14:47:05 +00007194 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7195 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7196 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007197
7198 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007199
7200 if (err)
7201 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007202 }
7203
David S. Miller6f535762007-10-11 18:08:29 -07007204 return work_done;
7205}
David S. Millerf7383c22005-05-18 22:50:53 -07007206
Matt Carlsondb219972011-11-04 09:15:03 +00007207static inline void tg3_reset_task_schedule(struct tg3 *tp)
7208{
7209 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7210 schedule_work(&tp->reset_task);
7211}
7212
7213static inline void tg3_reset_task_cancel(struct tg3 *tp)
7214{
7215 cancel_work_sync(&tp->reset_task);
7216 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007217 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007218}
7219
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007220static int tg3_poll_msix(struct napi_struct *napi, int budget)
7221{
7222 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7223 struct tg3 *tp = tnapi->tp;
7224 int work_done = 0;
7225 struct tg3_hw_status *sblk = tnapi->hw_status;
7226
7227 while (1) {
7228 work_done = tg3_poll_work(tnapi, work_done, budget);
7229
Joe Perches63c3a662011-04-26 08:12:10 +00007230 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007231 goto tx_recovery;
7232
7233 if (unlikely(work_done >= budget))
7234 break;
7235
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007236 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007237 * to tell the hw how much work has been processed,
7238 * so we must read it before checking for more work.
7239 */
7240 tnapi->last_tag = sblk->status_tag;
7241 tnapi->last_irq_tag = tnapi->last_tag;
7242 rmb();
7243
7244 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007245 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7246 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007247
7248 /* This test here is not race free, but will reduce
7249 * the number of interrupts by looping again.
7250 */
7251 if (tnapi == &tp->napi[1] && tp->rx_refill)
7252 continue;
7253
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007254 napi_complete_done(napi, work_done);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007255 /* Reenable interrupts. */
7256 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007257
7258 /* This test here is synchronized by napi_schedule()
7259 * and napi_complete() to close the race condition.
7260 */
7261 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7262 tw32(HOSTCC_MODE, tp->coalesce_mode |
7263 HOSTCC_MODE_ENABLE |
7264 tnapi->coal_now);
7265 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007266 mmiowb();
7267 break;
7268 }
7269 }
7270
Prashant Sreedharan506b0a32018-02-19 12:27:04 +05307271 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007272 return work_done;
7273
7274tx_recovery:
7275 /* work_done is guaranteed to be less than budget. */
7276 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007277 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007278 return work_done;
7279}
7280
Matt Carlsone64de4e2011-04-13 11:05:05 +00007281static void tg3_process_error(struct tg3 *tp)
7282{
7283 u32 val;
7284 bool real_error = false;
7285
Joe Perches63c3a662011-04-26 08:12:10 +00007286 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007287 return;
7288
7289 /* Check Flow Attention register */
7290 val = tr32(HOSTCC_FLOW_ATTN);
7291 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7292 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7293 real_error = true;
7294 }
7295
7296 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7297 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7298 real_error = true;
7299 }
7300
7301 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7302 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7303 real_error = true;
7304 }
7305
7306 if (!real_error)
7307 return;
7308
7309 tg3_dump_state(tp);
7310
Joe Perches63c3a662011-04-26 08:12:10 +00007311 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007312 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007313}
7314
David S. Miller6f535762007-10-11 18:08:29 -07007315static int tg3_poll(struct napi_struct *napi, int budget)
7316{
Matt Carlson8ef04422009-08-28 14:01:37 +00007317 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7318 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007319 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007320 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007321
7322 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007323 if (sblk->status & SD_STATUS_ERROR)
7324 tg3_process_error(tp);
7325
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007326 tg3_poll_link(tp);
7327
Matt Carlson17375d22009-08-28 14:02:18 +00007328 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007329
Joe Perches63c3a662011-04-26 08:12:10 +00007330 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007331 goto tx_recovery;
7332
7333 if (unlikely(work_done >= budget))
7334 break;
7335
Joe Perches63c3a662011-04-26 08:12:10 +00007336 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007337 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007338 * to tell the hw how much work has been processed,
7339 * so we must read it before checking for more work.
7340 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007341 tnapi->last_tag = sblk->status_tag;
7342 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007343 rmb();
7344 } else
7345 sblk->status &= ~SD_STATUS_UPDATED;
7346
Matt Carlson17375d22009-08-28 14:02:18 +00007347 if (likely(!tg3_has_work(tnapi))) {
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007348 napi_complete_done(napi, work_done);
Matt Carlson17375d22009-08-28 14:02:18 +00007349 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007350 break;
7351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 }
7353
Prashant Sreedharan506b0a32018-02-19 12:27:04 +05307354 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007355 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007356
7357tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007358 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007359 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007360 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007361 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362}
7363
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007364static void tg3_napi_disable(struct tg3 *tp)
7365{
7366 int i;
7367
7368 for (i = tp->irq_cnt - 1; i >= 0; i--)
7369 napi_disable(&tp->napi[i].napi);
7370}
7371
7372static void tg3_napi_enable(struct tg3 *tp)
7373{
7374 int i;
7375
7376 for (i = 0; i < tp->irq_cnt; i++)
7377 napi_enable(&tp->napi[i].napi);
7378}
7379
7380static void tg3_napi_init(struct tg3 *tp)
7381{
7382 int i;
7383
7384 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7385 for (i = 1; i < tp->irq_cnt; i++)
7386 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7387}
7388
7389static void tg3_napi_fini(struct tg3 *tp)
7390{
7391 int i;
7392
7393 for (i = 0; i < tp->irq_cnt; i++)
7394 netif_napi_del(&tp->napi[i].napi);
7395}
7396
7397static inline void tg3_netif_stop(struct tg3 *tp)
7398{
Florian Westphal860e9532016-05-03 16:33:13 +02007399 netif_trans_update(tp->dev); /* prevent tx timeout */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007400 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007401 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007402 netif_tx_disable(tp->dev);
7403}
7404
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007405/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007406static inline void tg3_netif_start(struct tg3 *tp)
7407{
Matt Carlsonbe947302012-12-03 19:36:57 +00007408 tg3_ptp_resume(tp);
7409
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007410 /* NOTE: unconditional netif_tx_wake_all_queues is only
7411 * appropriate so long as all callers are assured to
7412 * have free tx slots (such as after tg3_init_hw)
7413 */
7414 netif_tx_wake_all_queues(tp->dev);
7415
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007416 if (tp->link_up)
7417 netif_carrier_on(tp->dev);
7418
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007419 tg3_napi_enable(tp);
7420 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7421 tg3_enable_ints(tp);
7422}
7423
David S. Millerf47c11e2005-06-24 20:18:35 -07007424static void tg3_irq_quiesce(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007425 __releases(tp->lock)
7426 __acquires(tp->lock)
David S. Millerf47c11e2005-06-24 20:18:35 -07007427{
Matt Carlson4f125f42009-09-01 12:55:02 +00007428 int i;
7429
David S. Millerf47c11e2005-06-24 20:18:35 -07007430 BUG_ON(tp->irq_sync);
7431
7432 tp->irq_sync = 1;
7433 smp_mb();
7434
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007435 spin_unlock_bh(&tp->lock);
7436
Matt Carlson4f125f42009-09-01 12:55:02 +00007437 for (i = 0; i < tp->irq_cnt; i++)
7438 synchronize_irq(tp->napi[i].irq_vec);
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007439
7440 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007441}
7442
David S. Millerf47c11e2005-06-24 20:18:35 -07007443/* Fully shutdown all tg3 driver activity elsewhere in the system.
7444 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7445 * with as well. Most of the time, this is not necessary except when
7446 * shutting down the device.
7447 */
7448static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7449{
Michael Chan46966542007-07-11 19:47:19 -07007450 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007451 if (irq_sync)
7452 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007453}
7454
7455static inline void tg3_full_unlock(struct tg3 *tp)
7456{
David S. Millerf47c11e2005-06-24 20:18:35 -07007457 spin_unlock_bh(&tp->lock);
7458}
7459
Michael Chanfcfa0a32006-03-20 22:28:41 -08007460/* One-shot MSI handler - Chip automatically disables interrupt
7461 * after sending MSI so driver doesn't have to do it.
7462 */
David Howells7d12e782006-10-05 14:55:46 +01007463static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007464{
Matt Carlson09943a12009-08-28 14:01:57 +00007465 struct tg3_napi *tnapi = dev_id;
7466 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007467
Matt Carlson898a56f2009-08-28 14:02:40 +00007468 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007469 if (tnapi->rx_rcb)
7470 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007471
7472 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007473 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007474
7475 return IRQ_HANDLED;
7476}
7477
Michael Chan88b06bc22005-04-21 17:13:25 -07007478/* MSI ISR - No need to check for interrupt sharing and no need to
7479 * flush status block and interrupt mailbox. PCI ordering rules
7480 * guarantee that MSI will arrive after the status block.
7481 */
David Howells7d12e782006-10-05 14:55:46 +01007482static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007483{
Matt Carlson09943a12009-08-28 14:01:57 +00007484 struct tg3_napi *tnapi = dev_id;
7485 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007486
Matt Carlson898a56f2009-08-28 14:02:40 +00007487 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007488 if (tnapi->rx_rcb)
7489 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007490 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007491 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007492 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007493 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007494 * NIC to stop sending us irqs, engaging "in-intr-handler"
7495 * event coalescing.
7496 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007497 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007498 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007499 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007500
Michael Chan88b06bc22005-04-21 17:13:25 -07007501 return IRQ_RETVAL(1);
7502}
7503
David Howells7d12e782006-10-05 14:55:46 +01007504static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505{
Matt Carlson09943a12009-08-28 14:01:57 +00007506 struct tg3_napi *tnapi = dev_id;
7507 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007508 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 unsigned int handled = 1;
7510
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 /* In INTx mode, it is possible for the interrupt to arrive at
7512 * the CPU before the status block posted prior to the interrupt.
7513 * Reading the PCI State register will confirm whether the
7514 * interrupt is ours and will flush the status block.
7515 */
Michael Chand18edcb2007-03-24 20:57:11 -07007516 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007517 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007518 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7519 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007520 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007521 }
Michael Chand18edcb2007-03-24 20:57:11 -07007522 }
7523
7524 /*
7525 * Writing any value to intr-mbox-0 clears PCI INTA# and
7526 * chip-internal interrupt pending events.
7527 * Writing non-zero to intr-mbox-0 additional tells the
7528 * NIC to stop sending us irqs, engaging "in-intr-handler"
7529 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007530 *
7531 * Flush the mailbox to de-assert the IRQ immediately to prevent
7532 * spurious interrupts. The flush impacts performance but
7533 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007534 */
Michael Chanc04cb342007-05-07 00:26:15 -07007535 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007536 if (tg3_irq_sync(tp))
7537 goto out;
7538 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007539 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007540 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007541 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007542 } else {
7543 /* No work, shared interrupt perhaps? re-enable
7544 * interrupts, and flush that PCI write
7545 */
7546 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7547 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007548 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007549out:
David S. Millerfac9b832005-05-18 22:46:34 -07007550 return IRQ_RETVAL(handled);
7551}
7552
David Howells7d12e782006-10-05 14:55:46 +01007553static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007554{
Matt Carlson09943a12009-08-28 14:01:57 +00007555 struct tg3_napi *tnapi = dev_id;
7556 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007557 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007558 unsigned int handled = 1;
7559
David S. Millerfac9b832005-05-18 22:46:34 -07007560 /* In INTx mode, it is possible for the interrupt to arrive at
7561 * the CPU before the status block posted prior to the interrupt.
7562 * Reading the PCI State register will confirm whether the
7563 * interrupt is ours and will flush the status block.
7564 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007565 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007566 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007567 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7568 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 }
Michael Chand18edcb2007-03-24 20:57:11 -07007571 }
7572
7573 /*
7574 * writing any value to intr-mbox-0 clears PCI INTA# and
7575 * chip-internal interrupt pending events.
7576 * writing non-zero to intr-mbox-0 additional tells the
7577 * NIC to stop sending us irqs, engaging "in-intr-handler"
7578 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007579 *
7580 * Flush the mailbox to de-assert the IRQ immediately to prevent
7581 * spurious interrupts. The flush impacts performance but
7582 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007583 */
Michael Chanc04cb342007-05-07 00:26:15 -07007584 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007585
7586 /*
7587 * In a shared interrupt configuration, sometimes other devices'
7588 * interrupts will scream. We record the current status tag here
7589 * so that the above check can report that the screaming interrupts
7590 * are unhandled. Eventually they will be silenced.
7591 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007592 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007593
Michael Chand18edcb2007-03-24 20:57:11 -07007594 if (tg3_irq_sync(tp))
7595 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007596
Matt Carlson72334482009-08-28 14:03:01 +00007597 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007598
Matt Carlson09943a12009-08-28 14:01:57 +00007599 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007600
David S. Millerf47c11e2005-06-24 20:18:35 -07007601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602 return IRQ_RETVAL(handled);
7603}
7604
Michael Chan79381092005-04-21 17:13:59 -07007605/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007606static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007607{
Matt Carlson09943a12009-08-28 14:01:57 +00007608 struct tg3_napi *tnapi = dev_id;
7609 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007610 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007611
Michael Chanf9804dd2005-09-27 12:13:10 -07007612 if ((sblk->status & SD_STATUS_UPDATED) ||
7613 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007614 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007615 return IRQ_RETVAL(1);
7616 }
7617 return IRQ_RETVAL(0);
7618}
7619
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620#ifdef CONFIG_NET_POLL_CONTROLLER
7621static void tg3_poll_controller(struct net_device *dev)
7622{
Matt Carlson4f125f42009-09-01 12:55:02 +00007623 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007624 struct tg3 *tp = netdev_priv(dev);
7625
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007626 if (tg3_irq_sync(tp))
7627 return;
7628
Matt Carlson4f125f42009-09-01 12:55:02 +00007629 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007630 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631}
7632#endif
7633
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634static void tg3_tx_timeout(struct net_device *dev)
7635{
7636 struct tg3 *tp = netdev_priv(dev);
7637
Michael Chanb0408752007-02-13 12:18:30 -08007638 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007639 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007640 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
Matt Carlsondb219972011-11-04 09:15:03 +00007643 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644}
7645
Michael Chanc58ec932005-09-17 00:46:27 -07007646/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7647static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7648{
7649 u32 base = (u32) mapping & 0xffffffff;
7650
Nithin Sujir37567912013-12-19 17:44:11 -08007651 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007652}
7653
Michael Chan0f0d1512013-05-13 11:04:16 +00007654/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7655 * of any 4GB boundaries: 4G, 8G, etc
7656 */
7657static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7658 u32 len, u32 mss)
7659{
7660 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7661 u32 base = (u32) mapping & 0xffffffff;
7662
7663 return ((base + len + (mss & 0x3fff)) < base);
7664 }
7665 return 0;
7666}
7667
Michael Chan72f2afb2006-03-06 19:28:35 -08007668/* Test for DMA addresses > 40-bit */
7669static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7670 int len)
7671{
7672#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007673 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007674 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007675 return 0;
7676#else
7677 return 0;
7678#endif
7679}
7680
Matt Carlsond1a3b732011-07-27 14:20:51 +00007681static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007682 dma_addr_t mapping, u32 len, u32 flags,
7683 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007684{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007685 txbd->addr_hi = ((u64) mapping >> 32);
7686 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7687 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7688 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690
Matt Carlson84b67b22011-07-27 14:20:52 +00007691static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007692 dma_addr_t map, u32 len, u32 flags,
7693 u32 mss, u32 vlan)
7694{
7695 struct tg3 *tp = tnapi->tp;
7696 bool hwbug = false;
7697
7698 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007699 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007700
7701 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007702 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007703
Michael Chan0f0d1512013-05-13 11:04:16 +00007704 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7705 hwbug = true;
7706
Matt Carlsond1a3b732011-07-27 14:20:51 +00007707 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007708 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007709
Matt Carlsona4cb4282011-12-14 11:09:58 +00007710 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007711 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007712 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007713 while (len > tp->dma_limit && *budget) {
7714 u32 frag_len = tp->dma_limit;
7715 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007716
Matt Carlsonb9e45482011-11-04 09:14:59 +00007717 /* Avoid the 8byte DMA problem */
7718 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007719 len += tp->dma_limit / 2;
7720 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007721 }
7722
Matt Carlsonb9e45482011-11-04 09:14:59 +00007723 tnapi->tx_buffers[*entry].fragmented = true;
7724
7725 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7726 frag_len, tmp_flag, mss, vlan);
7727 *budget -= 1;
7728 prvidx = *entry;
7729 *entry = NEXT_TX(*entry);
7730
Matt Carlsone31aa982011-07-27 14:20:53 +00007731 map += frag_len;
7732 }
7733
7734 if (len) {
7735 if (*budget) {
7736 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7737 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007738 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007739 *entry = NEXT_TX(*entry);
7740 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007741 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007742 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007743 }
7744 }
7745 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007746 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7747 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007748 *entry = NEXT_TX(*entry);
7749 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007750
7751 return hwbug;
7752}
7753
Matt Carlson0d681b22011-07-27 14:20:49 +00007754static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007755{
7756 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007757 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007758 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007759
Matt Carlson0d681b22011-07-27 14:20:49 +00007760 skb = txb->skb;
7761 txb->skb = NULL;
7762
Matt Carlson432aa7e2011-05-19 12:12:45 +00007763 pci_unmap_single(tnapi->tp->pdev,
7764 dma_unmap_addr(txb, mapping),
7765 skb_headlen(skb),
7766 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007767
7768 while (txb->fragmented) {
7769 txb->fragmented = false;
7770 entry = NEXT_TX(entry);
7771 txb = &tnapi->tx_buffers[entry];
7772 }
7773
Matt Carlsonba1142e2011-11-04 09:15:00 +00007774 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007775 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007776
7777 entry = NEXT_TX(entry);
7778 txb = &tnapi->tx_buffers[entry];
7779
7780 pci_unmap_page(tnapi->tp->pdev,
7781 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007782 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007783
7784 while (txb->fragmented) {
7785 txb->fragmented = false;
7786 entry = NEXT_TX(entry);
7787 txb = &tnapi->tx_buffers[entry];
7788 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007789 }
7790}
7791
Michael Chan72f2afb2006-03-06 19:28:35 -08007792/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007793static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007794 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007795 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007796 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007798 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007799 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007800 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007801 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802
Joe Perches41535772013-02-16 11:20:04 +00007803 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007804 new_skb = skb_copy(skb, GFP_ATOMIC);
7805 else {
7806 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7807
7808 new_skb = skb_copy_expand(skb,
7809 skb_headroom(skb) + more_headroom,
7810 skb_tailroom(skb), GFP_ATOMIC);
7811 }
7812
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007814 ret = -1;
7815 } else {
7816 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007817 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7818 PCI_DMA_TODEVICE);
7819 /* Make sure the mapping succeeded */
7820 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007821 dev_kfree_skb_any(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007822 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007823 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007824 u32 save_entry = *entry;
7825
Matt Carlson92cd3a12011-07-27 14:20:47 +00007826 base_flags |= TXD_FLAG_END;
7827
Matt Carlson84b67b22011-07-27 14:20:52 +00007828 tnapi->tx_buffers[*entry].skb = new_skb;
7829 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007830 mapping, new_addr);
7831
Matt Carlson84b67b22011-07-27 14:20:52 +00007832 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007833 new_skb->len, base_flags,
7834 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007835 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007836 dev_kfree_skb_any(new_skb);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007837 ret = -1;
7838 }
Michael Chanc58ec932005-09-17 00:46:27 -07007839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 }
7841
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007842 dev_consume_skb_any(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007843 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007844 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845}
7846
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307847static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
7848{
7849 /* Check if we will never have enough descriptors,
7850 * as gso_segs can be more than current ring size
7851 */
7852 return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
7853}
7854
Matt Carlson2ffcc982011-05-19 12:12:44 +00007855static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007856
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007857/* Use GSO to workaround all TSO packets that meet HW bug conditions
7858 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007859 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007860static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7861 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007862{
7863 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007864 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007865
7866 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007867 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7868 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007869
7870 /* netif_tx_stop_queue() must be done before checking
7871 * checking tx index in tg3_tx_avail() below, because in
7872 * tg3_tx(), we update tx index before checking for
7873 * netif_tx_queue_stopped().
7874 */
7875 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007876 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007877 return NETDEV_TX_BUSY;
7878
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007879 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007880 }
7881
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007882 segs = skb_gso_segment(skb, tp->dev->features &
7883 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007884 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007885 goto tg3_tso_bug_end;
7886
7887 do {
7888 nskb = segs;
7889 segs = segs->next;
7890 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007891 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007892 } while (segs);
7893
7894tg3_tso_bug_end:
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007895 dev_consume_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007896
7897 return NETDEV_TX_OK;
7898}
Michael Chan52c0fd82006-06-29 20:15:54 -07007899
Michael Chand71c0dc2014-05-11 20:22:53 -07007900/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007901static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007902{
7903 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007904 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007905 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007906 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007907 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007908 struct tg3_napi *tnapi;
7909 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007910 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007911 struct iphdr *iph = NULL;
7912 struct tcphdr *tcph = NULL;
7913 __sum16 tcp_csum = 0, ip_csum = 0;
7914 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007915
Matt Carlson24f4efd2009-11-13 13:03:35 +00007916 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7917 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007918 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007919 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920
Matt Carlson84b67b22011-07-27 14:20:52 +00007921 budget = tg3_tx_avail(tnapi);
7922
Michael Chan00b70502006-06-17 21:58:45 -07007923 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007924 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007925 * interrupt. Furthermore, IRQ processing runs lockless so we have
7926 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007928 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007929 if (!netif_tx_queue_stopped(txq)) {
7930 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007931
7932 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007933 netdev_err(dev,
7934 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 return NETDEV_TX_BUSY;
7937 }
7938
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007939 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007941
Matt Carlsonbe98da62010-07-11 09:31:46 +00007942 mss = skb_shinfo(skb)->gso_size;
7943 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007944 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
françois romieu105dcb52014-03-29 12:26:29 +01007946 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007947 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948
Matt Carlson34195c32010-07-11 09:31:42 +00007949 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007950 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951
Eric Dumazeta5a11952012-01-23 01:22:09 +00007952 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007953
Vlad Yasevich476c1882014-09-18 10:31:17 -04007954 /* HW/FW can not correctly segment packets that have been
7955 * vlan encapsulated.
7956 */
7957 if (skb->protocol == htons(ETH_P_8021Q) ||
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307958 skb->protocol == htons(ETH_P_8021AD)) {
7959 if (tg3_tso_bug_gso_check(tnapi, skb))
7960 return tg3_tso_bug(tp, tnapi, txq, skb);
7961 goto drop;
7962 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007963
Eric Dumazeta5a11952012-01-23 01:22:09 +00007964 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007965 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307966 tg3_flag(tp, TSO_BUG)) {
7967 if (tg3_tso_bug_gso_check(tnapi, skb))
7968 return tg3_tso_bug(tp, tnapi, txq, skb);
7969 goto drop;
7970 }
Michael Chand3f6f3a2014-05-11 20:22:54 -07007971 ip_csum = iph->check;
7972 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007973 iph->check = 0;
7974 iph->tot_len = htons(mss + hdr_len);
7975 }
7976
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7978 TXD_FLAG_CPU_POST_DMA);
7979
Michael Chand3f6f3a2014-05-11 20:22:54 -07007980 tcph = tcp_hdr(skb);
7981 tcp_csum = tcph->check;
7982
Joe Perches63c3a662011-04-26 08:12:10 +00007983 if (tg3_flag(tp, HW_TSO_1) ||
7984 tg3_flag(tp, HW_TSO_2) ||
7985 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007986 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007988 } else {
7989 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7990 0, IPPROTO_TCP, 0);
7991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992
Joe Perches63c3a662011-04-26 08:12:10 +00007993 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007994 mss |= (hdr_len & 0xc) << 12;
7995 if (hdr_len & 0x10)
7996 base_flags |= 0x00000010;
7997 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007998 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007999 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00008000 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00008001 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008002 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 int tsflags;
8004
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008005 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006 mss |= (tsflags << 11);
8007 }
8008 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008009 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 int tsflags;
8011
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008012 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 base_flags |= tsflags << 12;
8014 }
8015 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04008016 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
8017 /* HW/FW can not correctly checksum packets that have been
8018 * vlan encapsulated.
8019 */
8020 if (skb->protocol == htons(ETH_P_8021Q) ||
8021 skb->protocol == htons(ETH_P_8021AD)) {
8022 if (skb_checksum_help(skb))
8023 goto drop;
8024 } else {
8025 base_flags |= TXD_FLAG_TCPUDP_CSUM;
8026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008028
Matt Carlson93a700a2011-08-31 11:44:54 +00008029 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8030 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8031 base_flags |= TXD_FLAG_JMB_PKT;
8032
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008033 if (skb_vlan_tag_present(skb)) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008034 base_flags |= TXD_FLAG_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008035 vlan = skb_vlan_tag_get(skb);
Matt Carlson92cd3a12011-07-27 14:20:47 +00008036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008038 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8039 tg3_flag(tp, TX_TSTAMP_EN)) {
8040 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8041 base_flags |= TXD_FLAG_HWTSTAMP;
8042 }
8043
Alexander Duyckf4188d82009-12-02 16:48:38 +00008044 len = skb_headlen(skb);
8045
8046 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008047 if (pci_dma_mapping_error(tp->pdev, mapping))
8048 goto drop;
8049
David S. Miller90079ce2008-09-11 04:52:51 -07008050
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008051 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008052 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008053
8054 would_hit_hwbug = 0;
8055
Joe Perches63c3a662011-04-26 08:12:10 +00008056 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008057 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058
Matt Carlson84b67b22011-07-27 14:20:52 +00008059 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008060 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008061 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008062 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008063 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008064 u32 tmp_mss = mss;
8065
8066 if (!tg3_flag(tp, HW_TSO_1) &&
8067 !tg3_flag(tp, HW_TSO_2) &&
8068 !tg3_flag(tp, HW_TSO_3))
8069 tmp_mss = 0;
8070
Matt Carlsonc5665a52012-02-13 10:20:12 +00008071 /* Now loop through additional data
8072 * fragments, and queue them.
8073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074 last = skb_shinfo(skb)->nr_frags - 1;
8075 for (i = 0; i <= last; i++) {
8076 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8077
Eric Dumazet9e903e02011-10-18 21:00:24 +00008078 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008079 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008080 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008082 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008083 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008084 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008085 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008086 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087
Matt Carlsonb9e45482011-11-04 09:14:59 +00008088 if (!budget ||
8089 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008090 len, base_flags |
8091 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008092 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008093 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008094 break;
8095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008096 }
8097 }
8098
8099 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008100 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05308102 if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07008103 /* If it's a TSO packet, do GSO instead of
8104 * allocating and copying to a large linear SKB
8105 */
8106 if (ip_tot_len) {
8107 iph->check = ip_csum;
8108 iph->tot_len = ip_tot_len;
8109 }
8110 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008111 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008112 }
8113
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 /* If the workaround fails due to memory/mapping
8115 * failure, silently drop this packet.
8116 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008117 entry = tnapi->tx_prod;
8118 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008119 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008120 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008121 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 }
8123
Richard Cochrand515b452011-06-19 03:31:41 +00008124 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008125 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008126
Michael Chan6541b802012-03-04 14:48:14 +00008127 /* Sync BD data before updating mailbox */
8128 wmb();
8129
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008130 tnapi->tx_prod = entry;
8131 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008132 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008133
8134 /* netif_tx_stop_queue() must be done before checking
8135 * checking tx index in tg3_tx_avail() below, because in
8136 * tg3_tx(), we update tx index before checking for
8137 * netif_tx_queue_stopped().
8138 */
8139 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008140 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008141 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008144 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8145 /* Packets are ready, update Tx producer idx on card. */
8146 tw32_tx_mbox(tnapi->prodmbox, entry);
8147 mmiowb();
8148 }
8149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008151
8152dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008153 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008154 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008155drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008156 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008157drop_nofree:
8158 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008159 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160}
8161
Matt Carlson6e01b202011-08-19 13:58:20 +00008162static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8163{
8164 if (enable) {
8165 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8166 MAC_MODE_PORT_MODE_MASK);
8167
8168 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8169
8170 if (!tg3_flag(tp, 5705_PLUS))
8171 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8172
8173 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8174 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8175 else
8176 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8177 } else {
8178 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8179
8180 if (tg3_flag(tp, 5705_PLUS) ||
8181 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008182 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008183 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8184 }
8185
8186 tw32(MAC_MODE, tp->mac_mode);
8187 udelay(40);
8188}
8189
Matt Carlson941ec902011-08-19 13:58:23 +00008190static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008191{
Matt Carlson941ec902011-08-19 13:58:23 +00008192 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008193
8194 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008195 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008196
Matt Carlson941ec902011-08-19 13:58:23 +00008197 if (extlpbk && tg3_phy_set_extloopbk(tp))
8198 return -EIO;
8199
8200 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008201 switch (speed) {
8202 case SPEED_10:
8203 break;
8204 case SPEED_100:
8205 bmcr |= BMCR_SPEED100;
8206 break;
8207 case SPEED_1000:
8208 default:
8209 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8210 speed = SPEED_100;
8211 bmcr |= BMCR_SPEED100;
8212 } else {
8213 speed = SPEED_1000;
8214 bmcr |= BMCR_SPEED1000;
8215 }
8216 }
8217
Matt Carlson941ec902011-08-19 13:58:23 +00008218 if (extlpbk) {
8219 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8220 tg3_readphy(tp, MII_CTRL1000, &val);
8221 val |= CTL1000_AS_MASTER |
8222 CTL1000_ENABLE_MASTER;
8223 tg3_writephy(tp, MII_CTRL1000, val);
8224 } else {
8225 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8226 MII_TG3_FET_PTEST_TRIM_2;
8227 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8228 }
8229 } else
8230 bmcr |= BMCR_LOOPBACK;
8231
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008232 tg3_writephy(tp, MII_BMCR, bmcr);
8233
8234 /* The write needs to be flushed for the FETs */
8235 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8236 tg3_readphy(tp, MII_BMCR, &bmcr);
8237
8238 udelay(40);
8239
8240 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008241 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008242 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008243 MII_TG3_FET_PTEST_FRC_TX_LINK |
8244 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8245
8246 /* The write needs to be flushed for the AC131 */
8247 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8248 }
8249
8250 /* Reset to prevent losing 1st rx packet intermittently */
8251 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8252 tg3_flag(tp, 5780_CLASS)) {
8253 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8254 udelay(10);
8255 tw32_f(MAC_RX_MODE, tp->rx_mode);
8256 }
8257
8258 mac_mode = tp->mac_mode &
8259 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8260 if (speed == SPEED_1000)
8261 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8262 else
8263 mac_mode |= MAC_MODE_PORT_MODE_MII;
8264
Joe Perches41535772013-02-16 11:20:04 +00008265 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008266 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8267
8268 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8269 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8270 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8271 mac_mode |= MAC_MODE_LINK_POLARITY;
8272
8273 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8274 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8275 }
8276
8277 tw32(MAC_MODE, mac_mode);
8278 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008279
8280 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008281}
8282
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008283static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008284{
8285 struct tg3 *tp = netdev_priv(dev);
8286
8287 if (features & NETIF_F_LOOPBACK) {
8288 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8289 return;
8290
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008291 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008292 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008293 netif_carrier_on(tp->dev);
8294 spin_unlock_bh(&tp->lock);
8295 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8296 } else {
8297 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8298 return;
8299
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008300 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008301 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008302 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008303 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008304 spin_unlock_bh(&tp->lock);
8305 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8306 }
8307}
8308
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008309static netdev_features_t tg3_fix_features(struct net_device *dev,
8310 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008311{
8312 struct tg3 *tp = netdev_priv(dev);
8313
Joe Perches63c3a662011-04-26 08:12:10 +00008314 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008315 features &= ~NETIF_F_ALL_TSO;
8316
8317 return features;
8318}
8319
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008320static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008321{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008322 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008323
8324 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8325 tg3_set_loopback(dev, features);
8326
8327 return 0;
8328}
8329
Matt Carlson21f581a2009-08-28 14:00:25 +00008330static void tg3_rx_prodring_free(struct tg3 *tp,
8331 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333 int i;
8334
Matt Carlson8fea32b2010-09-15 08:59:58 +00008335 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008336 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008337 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008338 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008339 tp->rx_pkt_map_sz);
8340
Joe Perches63c3a662011-04-26 08:12:10 +00008341 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008342 for (i = tpr->rx_jmb_cons_idx;
8343 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008344 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008345 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008346 TG3_RX_JMB_MAP_SZ);
8347 }
8348 }
8349
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008350 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352
Matt Carlson2c49a442010-09-30 10:34:35 +00008353 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008354 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008355 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
Joe Perches63c3a662011-04-26 08:12:10 +00008357 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008358 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008359 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008360 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361 }
8362}
8363
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008364/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 *
8366 * The chip has been shut down and the driver detached from
8367 * the networking, so no interrupts or new tx packets will
8368 * end up in the driver. tp->{tx,}lock are held and thus
8369 * we may not sleep.
8370 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008371static int tg3_rx_prodring_alloc(struct tg3 *tp,
8372 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373{
Matt Carlson287be122009-08-28 13:58:46 +00008374 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008376 tpr->rx_std_cons_idx = 0;
8377 tpr->rx_std_prod_idx = 0;
8378 tpr->rx_jmb_cons_idx = 0;
8379 tpr->rx_jmb_prod_idx = 0;
8380
Matt Carlson8fea32b2010-09-15 08:59:58 +00008381 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008382 memset(&tpr->rx_std_buffers[0], 0,
8383 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008384 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008385 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008386 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008387 goto done;
8388 }
8389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008391 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
Matt Carlson287be122009-08-28 13:58:46 +00008393 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008394 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008395 tp->dev->mtu > ETH_DATA_LEN)
8396 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8397 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008398
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 /* Initialize invariants of the rings, we only set this
8400 * stuff once. This works because the card does not
8401 * write into the rx buffer posting rings.
8402 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008403 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 struct tg3_rx_buffer_desc *rxd;
8405
Matt Carlson21f581a2009-08-28 14:00:25 +00008406 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008407 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8409 rxd->opaque = (RXD_OPAQUE_RING_STD |
8410 (i << RXD_OPAQUE_INDEX_SHIFT));
8411 }
8412
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008413 /* Now allocate fresh SKBs for each rx ring. */
8414 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008415 unsigned int frag_size;
8416
8417 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8418 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008419 netdev_warn(tp->dev,
8420 "Using a smaller RX standard ring. Only "
8421 "%d out of %d buffers were allocated "
8422 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008423 if (i == 0)
8424 goto initfail;
8425 tp->rx_pending = i;
8426 break;
8427 }
8428 }
8429
Joe Perches63c3a662011-04-26 08:12:10 +00008430 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008431 goto done;
8432
Matt Carlson2c49a442010-09-30 10:34:35 +00008433 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008434
Joe Perches63c3a662011-04-26 08:12:10 +00008435 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008436 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437
Matt Carlson2c49a442010-09-30 10:34:35 +00008438 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008439 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
Matt Carlson0d86df82010-02-17 15:17:00 +00008441 rxd = &tpr->rx_jmb[i].std;
8442 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8443 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8444 RXD_FLAG_JUMBO;
8445 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8446 (i << RXD_OPAQUE_INDEX_SHIFT));
8447 }
8448
8449 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008450 unsigned int frag_size;
8451
8452 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8453 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008454 netdev_warn(tp->dev,
8455 "Using a smaller RX jumbo ring. Only %d "
8456 "out of %d buffers were allocated "
8457 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008458 if (i == 0)
8459 goto initfail;
8460 tp->rx_jumbo_pending = i;
8461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 }
8463 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008464
8465done:
Michael Chan32d8c572006-07-25 16:38:29 -07008466 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008467
8468initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008469 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008470 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471}
8472
Matt Carlson21f581a2009-08-28 14:00:25 +00008473static void tg3_rx_prodring_fini(struct tg3 *tp,
8474 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475{
Matt Carlson21f581a2009-08-28 14:00:25 +00008476 kfree(tpr->rx_std_buffers);
8477 tpr->rx_std_buffers = NULL;
8478 kfree(tpr->rx_jmb_buffers);
8479 tpr->rx_jmb_buffers = NULL;
8480 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008481 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8482 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008483 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008485 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008486 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8487 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008488 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008490}
8491
Matt Carlson21f581a2009-08-28 14:00:25 +00008492static int tg3_rx_prodring_init(struct tg3 *tp,
8493 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008494{
Matt Carlson2c49a442010-09-30 10:34:35 +00008495 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8496 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008497 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008498 return -ENOMEM;
8499
Matt Carlson4bae65c2010-11-24 08:31:52 +00008500 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8501 TG3_RX_STD_RING_BYTES(tp),
8502 &tpr->rx_std_mapping,
8503 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008504 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008505 goto err_out;
8506
Joe Perches63c3a662011-04-26 08:12:10 +00008507 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008508 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008509 GFP_KERNEL);
8510 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008511 goto err_out;
8512
Matt Carlson4bae65c2010-11-24 08:31:52 +00008513 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8514 TG3_RX_JMB_RING_BYTES(tp),
8515 &tpr->rx_jmb_mapping,
8516 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008517 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008518 goto err_out;
8519 }
8520
8521 return 0;
8522
8523err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008524 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008525 return -ENOMEM;
8526}
8527
8528/* Free up pending packets in all rx/tx rings.
8529 *
8530 * The chip has been shut down and the driver detached from
8531 * the networking, so no interrupts or new tx packets will
8532 * end up in the driver. tp->{tx,}lock is not held and we are not
8533 * in an interrupt context and thus may sleep.
8534 */
8535static void tg3_free_rings(struct tg3 *tp)
8536{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008537 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008538
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008539 for (j = 0; j < tp->irq_cnt; j++) {
8540 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008541
Matt Carlson8fea32b2010-09-15 08:59:58 +00008542 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008543
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008544 if (!tnapi->tx_buffers)
8545 continue;
8546
Matt Carlson0d681b22011-07-27 14:20:49 +00008547 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8548 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008549
Matt Carlson0d681b22011-07-27 14:20:49 +00008550 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008551 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008552
Matt Carlsonba1142e2011-11-04 09:15:00 +00008553 tg3_tx_skb_unmap(tnapi, i,
8554 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008555
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07008556 dev_consume_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008557 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008558 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008559 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008560}
8561
8562/* Initialize tx/rx rings for packet processing.
8563 *
8564 * The chip has been shut down and the driver detached from
8565 * the networking, so no interrupts or new tx packets will
8566 * end up in the driver. tp->{tx,}lock are held and thus
8567 * we may not sleep.
8568 */
8569static int tg3_init_rings(struct tg3 *tp)
8570{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008571 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008572
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008573 /* Free up all the SKBs. */
8574 tg3_free_rings(tp);
8575
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008576 for (i = 0; i < tp->irq_cnt; i++) {
8577 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008578
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008579 tnapi->last_tag = 0;
8580 tnapi->last_irq_tag = 0;
8581 tnapi->hw_status->status = 0;
8582 tnapi->hw_status->status_tag = 0;
8583 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8584
8585 tnapi->tx_prod = 0;
8586 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008587 if (tnapi->tx_ring)
8588 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008589
8590 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008591 if (tnapi->rx_rcb)
8592 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008593
Thadeu Lima de Souza Cascardoa620a6b2014-11-25 14:21:11 -02008594 if (tnapi->prodring.rx_std &&
8595 tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008596 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008597 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008598 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008599 }
Matt Carlson72334482009-08-28 14:03:01 +00008600
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008601 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008602}
8603
Michael Chan49a359e2012-09-28 07:12:37 +00008604static void tg3_mem_tx_release(struct tg3 *tp)
8605{
8606 int i;
8607
8608 for (i = 0; i < tp->irq_max; i++) {
8609 struct tg3_napi *tnapi = &tp->napi[i];
8610
8611 if (tnapi->tx_ring) {
8612 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8613 tnapi->tx_ring, tnapi->tx_desc_mapping);
8614 tnapi->tx_ring = NULL;
8615 }
8616
8617 kfree(tnapi->tx_buffers);
8618 tnapi->tx_buffers = NULL;
8619 }
8620}
8621
8622static int tg3_mem_tx_acquire(struct tg3 *tp)
8623{
8624 int i;
8625 struct tg3_napi *tnapi = &tp->napi[0];
8626
8627 /* If multivector TSS is enabled, vector 0 does not handle
8628 * tx interrupts. Don't allocate any resources for it.
8629 */
8630 if (tg3_flag(tp, ENABLE_TSS))
8631 tnapi++;
8632
8633 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8634 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8635 TG3_TX_RING_SIZE, GFP_KERNEL);
8636 if (!tnapi->tx_buffers)
8637 goto err_out;
8638
8639 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8640 TG3_TX_RING_BYTES,
8641 &tnapi->tx_desc_mapping,
8642 GFP_KERNEL);
8643 if (!tnapi->tx_ring)
8644 goto err_out;
8645 }
8646
8647 return 0;
8648
8649err_out:
8650 tg3_mem_tx_release(tp);
8651 return -ENOMEM;
8652}
8653
8654static void tg3_mem_rx_release(struct tg3 *tp)
8655{
8656 int i;
8657
8658 for (i = 0; i < tp->irq_max; i++) {
8659 struct tg3_napi *tnapi = &tp->napi[i];
8660
8661 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8662
8663 if (!tnapi->rx_rcb)
8664 continue;
8665
8666 dma_free_coherent(&tp->pdev->dev,
8667 TG3_RX_RCB_RING_BYTES(tp),
8668 tnapi->rx_rcb,
8669 tnapi->rx_rcb_mapping);
8670 tnapi->rx_rcb = NULL;
8671 }
8672}
8673
8674static int tg3_mem_rx_acquire(struct tg3 *tp)
8675{
8676 unsigned int i, limit;
8677
8678 limit = tp->rxq_cnt;
8679
8680 /* If RSS is enabled, we need a (dummy) producer ring
8681 * set on vector zero. This is the true hw prodring.
8682 */
8683 if (tg3_flag(tp, ENABLE_RSS))
8684 limit++;
8685
8686 for (i = 0; i < limit; i++) {
8687 struct tg3_napi *tnapi = &tp->napi[i];
8688
8689 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8690 goto err_out;
8691
8692 /* If multivector RSS is enabled, vector 0
8693 * does not handle rx or tx interrupts.
8694 * Don't allocate any resources for it.
8695 */
8696 if (!i && tg3_flag(tp, ENABLE_RSS))
8697 continue;
8698
Joe Perchesede23fa82013-08-26 22:45:23 -07008699 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8700 TG3_RX_RCB_RING_BYTES(tp),
8701 &tnapi->rx_rcb_mapping,
8702 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008703 if (!tnapi->rx_rcb)
8704 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008705 }
8706
8707 return 0;
8708
8709err_out:
8710 tg3_mem_rx_release(tp);
8711 return -ENOMEM;
8712}
8713
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008714/*
8715 * Must not be invoked with interrupt sources disabled and
8716 * the hardware shutdown down.
8717 */
8718static void tg3_free_consistent(struct tg3 *tp)
8719{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008720 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008721
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008722 for (i = 0; i < tp->irq_cnt; i++) {
8723 struct tg3_napi *tnapi = &tp->napi[i];
8724
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008725 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008726 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8727 tnapi->hw_status,
8728 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008729 tnapi->hw_status = NULL;
8730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008732
Michael Chan49a359e2012-09-28 07:12:37 +00008733 tg3_mem_rx_release(tp);
8734 tg3_mem_tx_release(tp);
8735
Michael Chand89a2ad2018-05-03 20:04:27 -04008736 /* tp->hw_stats can be referenced safely:
8737 * 1. under rtnl_lock
8738 * 2. or under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
8739 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008741 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8742 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 tp->hw_stats = NULL;
8744 }
8745}
8746
8747/*
8748 * Must not be invoked with interrupt sources disabled and
8749 * the hardware shutdown down. Can sleep.
8750 */
8751static int tg3_alloc_consistent(struct tg3 *tp)
8752{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008753 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008754
Joe Perchesede23fa82013-08-26 22:45:23 -07008755 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8756 sizeof(struct tg3_hw_stats),
8757 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 if (!tp->hw_stats)
8759 goto err_out;
8760
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008761 for (i = 0; i < tp->irq_cnt; i++) {
8762 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008763 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008764
Joe Perchesede23fa82013-08-26 22:45:23 -07008765 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8766 TG3_HW_STATUS_SIZE,
8767 &tnapi->status_mapping,
8768 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008769 if (!tnapi->hw_status)
8770 goto err_out;
8771
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008772 sblk = tnapi->hw_status;
8773
Michael Chan49a359e2012-09-28 07:12:37 +00008774 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008775 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008776
Michael Chan49a359e2012-09-28 07:12:37 +00008777 /*
8778 * When RSS is enabled, the status block format changes
8779 * slightly. The "rx_jumbo_consumer", "reserved",
8780 * and "rx_mini_consumer" members get mapped to the
8781 * other three rx return ring producer indexes.
8782 */
8783 switch (i) {
8784 case 1:
8785 prodptr = &sblk->idx[0].rx_producer;
8786 break;
8787 case 2:
8788 prodptr = &sblk->rx_jumbo_consumer;
8789 break;
8790 case 3:
8791 prodptr = &sblk->reserved;
8792 break;
8793 case 4:
8794 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008795 break;
8796 }
Michael Chan49a359e2012-09-28 07:12:37 +00008797 tnapi->rx_rcb_prod_idx = prodptr;
8798 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008799 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008800 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008801 }
8802
Michael Chan49a359e2012-09-28 07:12:37 +00008803 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8804 goto err_out;
8805
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806 return 0;
8807
8808err_out:
8809 tg3_free_consistent(tp);
8810 return -ENOMEM;
8811}
8812
8813#define MAX_WAIT_CNT 1000
8814
8815/* To stop a block, clear the enable bit and poll till it
8816 * clears. tp->lock is held.
8817 */
Joe Perches953c96e2013-04-09 10:18:14 +00008818static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819{
8820 unsigned int i;
8821 u32 val;
8822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 switch (ofs) {
8825 case RCVLSC_MODE:
8826 case DMAC_MODE:
8827 case MBFREE_MODE:
8828 case BUFMGR_MODE:
8829 case MEMARB_MODE:
8830 /* We can't enable/disable these bits of the
8831 * 5705/5750, just say success.
8832 */
8833 return 0;
8834
8835 default:
8836 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 }
8839
8840 val = tr32(ofs);
8841 val &= ~enable_bit;
8842 tw32_f(ofs, val);
8843
8844 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008845 if (pci_channel_offline(tp->pdev)) {
8846 dev_err(&tp->pdev->dev,
8847 "tg3_stop_block device offline, "
8848 "ofs=%lx enable_bit=%x\n",
8849 ofs, enable_bit);
8850 return -ENODEV;
8851 }
8852
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 udelay(100);
8854 val = tr32(ofs);
8855 if ((val & enable_bit) == 0)
8856 break;
8857 }
8858
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008859 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008860 dev_err(&tp->pdev->dev,
8861 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8862 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 return -ENODEV;
8864 }
8865
8866 return 0;
8867}
8868
8869/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008870static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871{
8872 int i, err;
8873
8874 tg3_disable_ints(tp);
8875
Gavin Shan6d446ec2013-06-25 15:24:32 +08008876 if (pci_channel_offline(tp->pdev)) {
8877 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8878 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8879 err = -ENODEV;
8880 goto err_no_dev;
8881 }
8882
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883 tp->rx_mode &= ~RX_MODE_ENABLE;
8884 tw32_f(MAC_RX_MODE, tp->rx_mode);
8885 udelay(10);
8886
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008887 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8888 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8889 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8890 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8891 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8892 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008894 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8895 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8896 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8897 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8898 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8899 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8900 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901
8902 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8903 tw32_f(MAC_MODE, tp->mac_mode);
8904 udelay(40);
8905
8906 tp->tx_mode &= ~TX_MODE_ENABLE;
8907 tw32_f(MAC_TX_MODE, tp->tx_mode);
8908
8909 for (i = 0; i < MAX_WAIT_CNT; i++) {
8910 udelay(100);
8911 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8912 break;
8913 }
8914 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008915 dev_err(&tp->pdev->dev,
8916 "%s timed out, TX_MODE_ENABLE will not clear "
8917 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008918 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 }
8920
Michael Chane6de8ad2005-05-05 14:42:41 -07008921 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008922 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8923 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924
8925 tw32(FTQ_RESET, 0xffffffff);
8926 tw32(FTQ_RESET, 0x00000000);
8927
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008928 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8929 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930
Gavin Shan6d446ec2013-06-25 15:24:32 +08008931err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008932 for (i = 0; i < tp->irq_cnt; i++) {
8933 struct tg3_napi *tnapi = &tp->napi[i];
8934 if (tnapi->hw_status)
8935 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 return err;
8939}
8940
Michael Chanee6a99b2007-07-18 21:49:10 -07008941/* Save PCI command register before chip reset */
8942static void tg3_save_pci_state(struct tg3 *tp)
8943{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008944 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008945}
8946
8947/* Restore PCI state after chip reset */
8948static void tg3_restore_pci_state(struct tg3 *tp)
8949{
8950 u32 val;
8951
8952 /* Re-enable indirect register accesses. */
8953 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8954 tp->misc_host_ctrl);
8955
8956 /* Set MAX PCI retry to zero. */
8957 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008958 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008959 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008960 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008961 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008962 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008963 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008964 PCISTATE_ALLOW_APE_SHMEM_WR |
8965 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008966 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8967
Matt Carlson8a6eac92007-10-21 16:17:55 -07008968 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008969
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008970 if (!tg3_flag(tp, PCI_EXPRESS)) {
8971 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8972 tp->pci_cacheline_sz);
8973 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8974 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008975 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008976
Michael Chanee6a99b2007-07-18 21:49:10 -07008977 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008978 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008979 u16 pcix_cmd;
8980
8981 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8982 &pcix_cmd);
8983 pcix_cmd &= ~PCI_X_CMD_ERO;
8984 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8985 pcix_cmd);
8986 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008987
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008989
8990 /* Chip reset on 5780 will reset MSI enable bit,
8991 * so need to restore it.
8992 */
Joe Perches63c3a662011-04-26 08:12:10 +00008993 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008994 u16 ctrl;
8995
8996 pci_read_config_word(tp->pdev,
8997 tp->msi_cap + PCI_MSI_FLAGS,
8998 &ctrl);
8999 pci_write_config_word(tp->pdev,
9000 tp->msi_cap + PCI_MSI_FLAGS,
9001 ctrl | PCI_MSI_FLAGS_ENABLE);
9002 val = tr32(MSGINT_MODE);
9003 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
9004 }
9005 }
9006}
9007
Nithin Sujirf82995b2014-01-03 10:09:13 -08009008static void tg3_override_clk(struct tg3 *tp)
9009{
9010 u32 val;
9011
9012 switch (tg3_asic_rev(tp)) {
9013 case ASIC_REV_5717:
9014 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9015 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
9016 TG3_CPMU_MAC_ORIDE_ENABLE);
9017 break;
9018
9019 case ASIC_REV_5719:
9020 case ASIC_REV_5720:
9021 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9022 break;
9023
9024 default:
9025 return;
9026 }
9027}
9028
9029static void tg3_restore_clk(struct tg3 *tp)
9030{
9031 u32 val;
9032
9033 switch (tg3_asic_rev(tp)) {
9034 case ASIC_REV_5717:
9035 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9036 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9037 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9038 break;
9039
9040 case ASIC_REV_5719:
9041 case ASIC_REV_5720:
9042 val = tr32(TG3_CPMU_CLCK_ORIDE);
9043 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9044 break;
9045
9046 default:
9047 return;
9048 }
9049}
9050
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051/* tp->lock is held. */
9052static int tg3_chip_reset(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009053 __releases(tp->lock)
9054 __acquires(tp->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055{
9056 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009057 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009058 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009060 if (!pci_device_is_present(tp->pdev))
9061 return -ENODEV;
9062
David S. Millerf49639e2006-06-09 11:58:36 -07009063 tg3_nvram_lock(tp);
9064
Matt Carlson77b483f2008-08-15 14:07:24 -07009065 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9066
David S. Millerf49639e2006-06-09 11:58:36 -07009067 /* No matching tg3_nvram_unlock() after this because
9068 * chip reset below will undo the nvram lock.
9069 */
9070 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009071
Michael Chanee6a99b2007-07-18 21:49:10 -07009072 /* GRC_MISC_CFG core clock reset will clear the memory
9073 * enable bit in PCI register 4 and the MSI enable bit
9074 * on some chips, so we save relevant registers here.
9075 */
9076 tg3_save_pci_state(tp);
9077
Joe Perches41535772013-02-16 11:20:04 +00009078 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009079 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009080 tw32(GRC_FASTBOOT_PC, 0);
9081
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 /*
9083 * We must avoid the readl() that normally takes place.
9084 * It locks machines, causes machine checks, and other
9085 * fun things. So, temporarily disable the 5701
9086 * hardware workaround, while we do the reset.
9087 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009088 write_op = tp->write32;
9089 if (write_op == tg3_write_flush_reg32)
9090 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091
Michael Chand18edcb2007-03-24 20:57:11 -07009092 /* Prevent the irq handler from reading or writing PCI registers
9093 * during chip reset when the memory enable bit in the PCI command
9094 * register may be cleared. The chip does not generate interrupt
9095 * at this time, but the irq handler may still be called due to irq
9096 * sharing or irqpoll.
9097 */
Joe Perches63c3a662011-04-26 08:12:10 +00009098 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009099 for (i = 0; i < tp->irq_cnt; i++) {
9100 struct tg3_napi *tnapi = &tp->napi[i];
9101 if (tnapi->hw_status) {
9102 tnapi->hw_status->status = 0;
9103 tnapi->hw_status->status_tag = 0;
9104 }
9105 tnapi->last_tag = 0;
9106 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009107 }
Michael Chand18edcb2007-03-24 20:57:11 -07009108 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009109
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009110 tg3_full_unlock(tp);
9111
Matt Carlson4f125f42009-09-01 12:55:02 +00009112 for (i = 0; i < tp->irq_cnt; i++)
9113 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009114
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009115 tg3_full_lock(tp, 0);
9116
Joe Perches41535772013-02-16 11:20:04 +00009117 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009118 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9119 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9120 }
9121
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122 /* do the reset */
9123 val = GRC_MISC_CFG_CORECLK_RESET;
9124
Joe Perches63c3a662011-04-26 08:12:10 +00009125 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009126 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009127 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009128 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009129 tr32(TG3_PCIE_PHY_TSTCTL) ==
9130 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9131 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9132
Joe Perches41535772013-02-16 11:20:04 +00009133 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134 tw32(GRC_MISC_CFG, (1 << 29));
9135 val |= (1 << 29);
9136 }
9137 }
9138
Joe Perches41535772013-02-16 11:20:04 +00009139 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009140 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9141 tw32(GRC_VCPU_EXT_CTRL,
9142 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9143 }
9144
Nithin Sujirf82995b2014-01-03 10:09:13 -08009145 /* Set the clock to the highest frequency to avoid timeouts. With link
9146 * aware mode, the clock speed could be slow and bootcode does not
9147 * complete within the expected time. Override the clock to allow the
9148 * bootcode to finish sooner and then restore it.
9149 */
9150 tg3_override_clk(tp);
9151
Matt Carlsonf37500d2010-08-02 11:25:59 +00009152 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009153 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009155
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156 tw32(GRC_MISC_CFG, val);
9157
Michael Chan1ee582d2005-08-09 20:16:46 -07009158 /* restore 5701 hardware bug workaround write method */
9159 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160
9161 /* Unfortunately, we have to delay before the PCI read back.
9162 * Some 575X chips even will not respond to a PCI cfg access
9163 * when the reset command is given to the chip.
9164 *
9165 * How do these hardware designers expect things to work
9166 * properly if the PCI write is posted for a long period
9167 * of time? It is always necessary to have some method by
9168 * which a register read back can occur to push the write
9169 * out which does the reset.
9170 *
9171 * For most tg3 variants the trick below was working.
9172 * Ho hum...
9173 */
9174 udelay(120);
9175
9176 /* Flush PCI posted writes. The normal MMIO registers
9177 * are inaccessible at this time so this is the only
9178 * way to make this reliably (actually, this is no longer
9179 * the case, see above). I tried to use indirect
9180 * register read/write but this upset some 5701 variants.
9181 */
9182 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9183
9184 udelay(120);
9185
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009186 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009187 u16 val16;
9188
Joe Perches41535772013-02-16 11:20:04 +00009189 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009190 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191 u32 cfg_val;
9192
9193 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009194 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195 udelay(100);
9196
9197 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9198 pci_write_config_dword(tp->pdev, 0xc4,
9199 cfg_val | (1 << 15));
9200 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009201
Matt Carlsone7126992009-08-25 10:08:16 +00009202 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009203 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009204 /*
9205 * Older PCIe devices only support the 128 byte
9206 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009207 */
Joe Perches63c3a662011-04-26 08:12:10 +00009208 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009209 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9210 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009211
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009212 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009213 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009214 PCI_EXP_DEVSTA_CED |
9215 PCI_EXP_DEVSTA_NFED |
9216 PCI_EXP_DEVSTA_FED |
9217 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 }
9219
Michael Chanee6a99b2007-07-18 21:49:10 -07009220 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221
Joe Perches63c3a662011-04-26 08:12:10 +00009222 tg3_flag_clear(tp, CHIP_RESETTING);
9223 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009224
Michael Chanee6a99b2007-07-18 21:49:10 -07009225 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009226 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009227 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009228 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229
Joe Perches41535772013-02-16 11:20:04 +00009230 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 tg3_stop_fw(tp);
9232 tw32(0x5000, 0x400);
9233 }
9234
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009235 if (tg3_flag(tp, IS_SSB_CORE)) {
9236 /*
9237 * BCM4785: In order to avoid repercussions from using
9238 * potentially defective internal ROM, stop the Rx RISC CPU,
9239 * which is not required.
9240 */
9241 tg3_stop_fw(tp);
9242 tg3_halt_cpu(tp, RX_CPU_BASE);
9243 }
9244
Nithin Sujirfb03a432013-05-21 12:57:32 +00009245 err = tg3_poll_fw(tp);
9246 if (err)
9247 return err;
9248
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 tw32(GRC_MODE, tp->grc_mode);
9250
Joe Perches41535772013-02-16 11:20:04 +00009251 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009252 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253
9254 tw32(0xc4, val | (1 << 15));
9255 }
9256
9257 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009258 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009259 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009260 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9262 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9263 }
9264
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009265 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009266 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009267 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009268 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009269 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009270 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009272 val = 0;
9273
9274 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 udelay(40);
9276
Matt Carlson77b483f2008-08-15 14:07:24 -07009277 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9278
Matt Carlson0a9140c2009-08-28 12:27:50 +00009279 tg3_mdio_start(tp);
9280
Joe Perches63c3a662011-04-26 08:12:10 +00009281 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009282 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9283 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009284 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009285 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286
9287 tw32(0x7c00, val | (1 << 25));
9288 }
9289
Nithin Sujirf82995b2014-01-03 10:09:13 -08009290 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009291
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009293 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009294 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9295 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9296
Joe Perches63c3a662011-04-26 08:12:10 +00009297 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9299 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9300 u32 nic_cfg;
9301
9302 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9303 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009304 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009305 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009306 if (tg3_flag(tp, 5750_PLUS))
9307 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009308
9309 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9310 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9311 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9312 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9313 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314 }
9315 }
9316
9317 return 0;
9318}
9319
Matt Carlson65ec6982012-02-28 23:33:37 +00009320static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9321static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009322static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009323
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009325static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326{
9327 int err;
9328
9329 tg3_stop_fw(tp);
9330
Michael Chan944d9802005-05-29 14:57:48 -07009331 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009333 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 err = tg3_chip_reset(tp);
9335
Joe Perches953c96e2013-04-09 10:18:14 +00009336 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009337
Michael Chan944d9802005-05-29 14:57:48 -07009338 tg3_write_sig_legacy(tp, kind);
9339 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340
Matt Carlson92feeab2011-12-08 14:40:14 +00009341 if (tp->hw_stats) {
9342 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009343 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009344 tg3_get_estats(tp, &tp->estats_prev);
9345
9346 /* And make sure the next sample is new data */
9347 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9348 }
9349
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351}
9352
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353static int tg3_set_mac_addr(struct net_device *dev, void *p)
9354{
9355 struct tg3 *tp = netdev_priv(dev);
9356 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009357 int err = 0;
9358 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359
Michael Chanf9804dd2005-09-27 12:13:10 -07009360 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009361 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009362
Linus Torvalds1da177e2005-04-16 15:20:36 -07009363 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9364
Michael Chane75f7c92006-03-20 21:33:26 -08009365 if (!netif_running(dev))
9366 return 0;
9367
Joe Perches63c3a662011-04-26 08:12:10 +00009368 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009369 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009370
Michael Chan986e0ae2007-05-05 12:10:20 -07009371 addr0_high = tr32(MAC_ADDR_0_HIGH);
9372 addr0_low = tr32(MAC_ADDR_0_LOW);
9373 addr1_high = tr32(MAC_ADDR_1_HIGH);
9374 addr1_low = tr32(MAC_ADDR_1_LOW);
9375
9376 /* Skip MAC addr 1 if ASF is using it. */
9377 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9378 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009379 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009380 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009381 spin_lock_bh(&tp->lock);
9382 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009383 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009384 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385
Michael Chanb9ec6c12006-07-25 16:37:27 -07009386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387}
9388
9389/* tp->lock is held. */
9390static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9391 dma_addr_t mapping, u32 maxlen_flags,
9392 u32 nic_addr)
9393{
9394 tg3_write_mem(tp,
9395 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9396 ((u64) mapping >> 32));
9397 tg3_write_mem(tp,
9398 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9399 ((u64) mapping & 0xffffffff));
9400 tg3_write_mem(tp,
9401 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9402 maxlen_flags);
9403
Joe Perches63c3a662011-04-26 08:12:10 +00009404 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 tg3_write_mem(tp,
9406 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9407 nic_addr);
9408}
9409
Michael Chana489b6d2012-09-28 07:12:39 +00009410
9411static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009412{
Michael Chana489b6d2012-09-28 07:12:39 +00009413 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009414
Joe Perches63c3a662011-04-26 08:12:10 +00009415 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009416 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9417 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9418 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009419 } else {
9420 tw32(HOSTCC_TXCOL_TICKS, 0);
9421 tw32(HOSTCC_TXMAX_FRAMES, 0);
9422 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009423
9424 for (; i < tp->txq_cnt; i++) {
9425 u32 reg;
9426
9427 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9428 tw32(reg, ec->tx_coalesce_usecs);
9429 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9430 tw32(reg, ec->tx_max_coalesced_frames);
9431 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9432 tw32(reg, ec->tx_max_coalesced_frames_irq);
9433 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009434 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009435
Michael Chana489b6d2012-09-28 07:12:39 +00009436 for (; i < tp->irq_max - 1; i++) {
9437 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9438 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9439 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9440 }
9441}
9442
9443static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9444{
9445 int i = 0;
9446 u32 limit = tp->rxq_cnt;
9447
Joe Perches63c3a662011-04-26 08:12:10 +00009448 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009449 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9450 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9451 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009452 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009453 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009454 tw32(HOSTCC_RXCOL_TICKS, 0);
9455 tw32(HOSTCC_RXMAX_FRAMES, 0);
9456 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009457 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009458
Michael Chana489b6d2012-09-28 07:12:39 +00009459 for (; i < limit; i++) {
9460 u32 reg;
9461
9462 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9463 tw32(reg, ec->rx_coalesce_usecs);
9464 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9465 tw32(reg, ec->rx_max_coalesced_frames);
9466 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9467 tw32(reg, ec->rx_max_coalesced_frames_irq);
9468 }
9469
9470 for (; i < tp->irq_max - 1; i++) {
9471 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9472 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9473 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9474 }
9475}
9476
9477static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9478{
9479 tg3_coal_tx_init(tp, ec);
9480 tg3_coal_rx_init(tp, ec);
9481
Joe Perches63c3a662011-04-26 08:12:10 +00009482 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009483 u32 val = ec->stats_block_coalesce_usecs;
9484
Matt Carlsonb6080e12009-09-01 13:12:00 +00009485 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9486 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9487
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009488 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009489 val = 0;
9490
9491 tw32(HOSTCC_STAT_COAL_TICKS, val);
9492 }
9493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494
9495/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009496static void tg3_tx_rcbs_disable(struct tg3 *tp)
9497{
9498 u32 txrcb, limit;
9499
9500 /* Disable all transmit rings but the first. */
9501 if (!tg3_flag(tp, 5705_PLUS))
9502 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9503 else if (tg3_flag(tp, 5717_PLUS))
9504 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9505 else if (tg3_flag(tp, 57765_CLASS) ||
9506 tg3_asic_rev(tp) == ASIC_REV_5762)
9507 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9508 else
9509 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9510
9511 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9512 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9513 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9514 BDINFO_FLAGS_DISABLED);
9515}
9516
9517/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009518static void tg3_tx_rcbs_init(struct tg3 *tp)
9519{
9520 int i = 0;
9521 u32 txrcb = NIC_SRAM_SEND_RCB;
9522
9523 if (tg3_flag(tp, ENABLE_TSS))
9524 i++;
9525
9526 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9527 struct tg3_napi *tnapi = &tp->napi[i];
9528
9529 if (!tnapi->tx_ring)
9530 continue;
9531
9532 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9533 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9534 NIC_SRAM_TX_BUFFER_DESC);
9535 }
9536}
9537
9538/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009539static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9540{
9541 u32 rxrcb, limit;
9542
9543 /* Disable all receive return rings but the first. */
9544 if (tg3_flag(tp, 5717_PLUS))
9545 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9546 else if (!tg3_flag(tp, 5705_PLUS))
9547 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9548 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9549 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9550 tg3_flag(tp, 57765_CLASS))
9551 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9552 else
9553 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9554
9555 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9556 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9557 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9558 BDINFO_FLAGS_DISABLED);
9559}
9560
9561/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009562static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9563{
9564 int i = 0;
9565 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9566
9567 if (tg3_flag(tp, ENABLE_RSS))
9568 i++;
9569
9570 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9571 struct tg3_napi *tnapi = &tp->napi[i];
9572
9573 if (!tnapi->rx_rcb)
9574 continue;
9575
9576 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9577 (tp->rx_ret_ring_mask + 1) <<
9578 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9579 }
9580}
9581
9582/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009583static void tg3_rings_reset(struct tg3 *tp)
9584{
9585 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009586 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009587 struct tg3_napi *tnapi = &tp->napi[0];
9588
Nithin Sujir328947f2013-05-23 11:11:24 +00009589 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009590
Nithin Sujir328947f2013-05-23 11:11:24 +00009591 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009592
9593 /* Disable interrupts */
9594 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009595 tp->napi[0].chk_msi_cnt = 0;
9596 tp->napi[0].last_rx_cons = 0;
9597 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009598
9599 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009600 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009601 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009602 tp->napi[i].tx_prod = 0;
9603 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009604 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009605 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009606 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9607 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009608 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009609 tp->napi[i].last_rx_cons = 0;
9610 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009611 }
Joe Perches63c3a662011-04-26 08:12:10 +00009612 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009613 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009614 } else {
9615 tp->napi[0].tx_prod = 0;
9616 tp->napi[0].tx_cons = 0;
9617 tw32_mailbox(tp->napi[0].prodmbox, 0);
9618 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9619 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009620
9621 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009622 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009623 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9624 for (i = 0; i < 16; i++)
9625 tw32_tx_mbox(mbox + i * 8, 0);
9626 }
9627
Matt Carlson2d31eca2009-09-01 12:53:31 +00009628 /* Clear status block in ram. */
9629 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9630
9631 /* Set status block DMA address */
9632 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9633 ((u64) tnapi->status_mapping >> 32));
9634 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9635 ((u64) tnapi->status_mapping & 0xffffffff));
9636
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009637 stblk = HOSTCC_STATBLCK_RING1;
9638
9639 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9640 u64 mapping = (u64)tnapi->status_mapping;
9641 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9642 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009643 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009644
9645 /* Clear status block in ram. */
9646 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009647 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009648
9649 tg3_tx_rcbs_init(tp);
9650 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009651}
9652
Matt Carlsoneb07a942011-04-20 07:57:36 +00009653static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9654{
9655 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9656
Joe Perches63c3a662011-04-26 08:12:10 +00009657 if (!tg3_flag(tp, 5750_PLUS) ||
9658 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009659 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9660 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009661 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009662 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009663 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9664 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009665 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9666 else
9667 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9668
9669 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9670 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9671
9672 val = min(nic_rep_thresh, host_rep_thresh);
9673 tw32(RCVBDI_STD_THRESH, val);
9674
Joe Perches63c3a662011-04-26 08:12:10 +00009675 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009676 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9677
Joe Perches63c3a662011-04-26 08:12:10 +00009678 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009679 return;
9680
Matt Carlson513aa6e2011-11-21 15:01:18 +00009681 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009682
9683 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9684
9685 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9686 tw32(RCVBDI_JUMBO_THRESH, val);
9687
Joe Perches63c3a662011-04-26 08:12:10 +00009688 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009689 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9690}
9691
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009692static inline u32 calc_crc(unsigned char *buf, int len)
9693{
9694 u32 reg;
9695 u32 tmp;
9696 int j, k;
9697
9698 reg = 0xffffffff;
9699
9700 for (j = 0; j < len; j++) {
9701 reg ^= buf[j];
9702
9703 for (k = 0; k < 8; k++) {
9704 tmp = reg & 0x01;
9705
9706 reg >>= 1;
9707
9708 if (tmp)
9709 reg ^= 0xedb88320;
9710 }
9711 }
9712
9713 return ~reg;
9714}
9715
9716static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9717{
9718 /* accept or reject all multicast frames */
9719 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9720 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9721 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9722 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9723}
9724
9725static void __tg3_set_rx_mode(struct net_device *dev)
9726{
9727 struct tg3 *tp = netdev_priv(dev);
9728 u32 rx_mode;
9729
9730 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9731 RX_MODE_KEEP_VLAN_TAG);
9732
9733#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9734 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9735 * flag clear.
9736 */
9737 if (!tg3_flag(tp, ENABLE_ASF))
9738 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9739#endif
9740
9741 if (dev->flags & IFF_PROMISC) {
9742 /* Promiscuous mode. */
9743 rx_mode |= RX_MODE_PROMISC;
9744 } else if (dev->flags & IFF_ALLMULTI) {
9745 /* Accept all multicast. */
9746 tg3_set_multi(tp, 1);
9747 } else if (netdev_mc_empty(dev)) {
9748 /* Reject all multicast. */
9749 tg3_set_multi(tp, 0);
9750 } else {
9751 /* Accept one or more multicast(s). */
9752 struct netdev_hw_addr *ha;
9753 u32 mc_filter[4] = { 0, };
9754 u32 regidx;
9755 u32 bit;
9756 u32 crc;
9757
9758 netdev_for_each_mc_addr(ha, dev) {
9759 crc = calc_crc(ha->addr, ETH_ALEN);
9760 bit = ~crc & 0x7f;
9761 regidx = (bit & 0x60) >> 5;
9762 bit &= 0x1f;
9763 mc_filter[regidx] |= (1 << bit);
9764 }
9765
9766 tw32(MAC_HASH_REG_0, mc_filter[0]);
9767 tw32(MAC_HASH_REG_1, mc_filter[1]);
9768 tw32(MAC_HASH_REG_2, mc_filter[2]);
9769 tw32(MAC_HASH_REG_3, mc_filter[3]);
9770 }
9771
Michael Chane565eec2014-01-03 10:09:12 -08009772 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9773 rx_mode |= RX_MODE_PROMISC;
9774 } else if (!(dev->flags & IFF_PROMISC)) {
9775 /* Add all entries into to the mac addr filter list */
9776 int i = 0;
9777 struct netdev_hw_addr *ha;
9778
9779 netdev_for_each_uc_addr(ha, dev) {
9780 __tg3_set_one_mac_addr(tp, ha->addr,
9781 i + TG3_UCAST_ADDR_IDX(tp));
9782 i++;
9783 }
9784 }
9785
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009786 if (rx_mode != tp->rx_mode) {
9787 tp->rx_mode = rx_mode;
9788 tw32_f(MAC_RX_MODE, rx_mode);
9789 udelay(10);
9790 }
9791}
9792
Michael Chan91024262012-09-28 07:12:38 +00009793static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009794{
9795 int i;
9796
9797 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009798 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009799}
9800
9801static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009802{
9803 int i;
9804
9805 if (!tg3_flag(tp, SUPPORT_MSIX))
9806 return;
9807
Michael Chan0b3ba052012-11-14 14:44:29 +00009808 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009809 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009810 return;
9811 }
9812
9813 /* Validate table against current IRQ count */
9814 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009815 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009816 break;
9817 }
9818
9819 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009820 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009821}
9822
Matt Carlson90415472011-12-16 13:33:23 +00009823static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009824{
9825 int i = 0;
9826 u32 reg = MAC_RSS_INDIR_TBL_0;
9827
9828 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9829 u32 val = tp->rss_ind_tbl[i];
9830 i++;
9831 for (; i % 8; i++) {
9832 val <<= 4;
9833 val |= tp->rss_ind_tbl[i];
9834 }
9835 tw32(reg, val);
9836 reg += 4;
9837 }
9838}
9839
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009840static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9841{
9842 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9843 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9844 else
9845 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9846}
9847
Matt Carlson2d31eca2009-09-01 12:53:31 +00009848/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009849static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850{
9851 u32 val, rdmac_mode;
9852 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009853 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
9855 tg3_disable_ints(tp);
9856
9857 tg3_stop_fw(tp);
9858
9859 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9860
Joe Perches63c3a662011-04-26 08:12:10 +00009861 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009862 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009864 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9865 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9866 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009867 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009868 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9869 }
9870
Nithin Sujir400dfba2013-05-18 06:26:53 +00009871 /* Enable MAC control of LPI */
9872 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9873 tg3_setup_eee(tp);
9874
Matt Carlson603f1172010-02-12 14:47:10 +00009875 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009876 tg3_phy_reset(tp);
9877
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 err = tg3_chip_reset(tp);
9879 if (err)
9880 return err;
9881
9882 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9883
Joe Perches41535772013-02-16 11:20:04 +00009884 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009885 val = tr32(TG3_CPMU_CTRL);
9886 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9887 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009888
9889 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9890 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9891 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9892 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9893
9894 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9895 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9896 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9897 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9898
9899 val = tr32(TG3_CPMU_HST_ACC);
9900 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9901 val |= CPMU_HST_ACC_MACCLK_6_25;
9902 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009903 }
9904
Joe Perches41535772013-02-16 11:20:04 +00009905 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009906 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9907 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9908 PCIE_PWR_MGMT_L1_THRESH_4MS;
9909 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009910
9911 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9912 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9913
9914 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009915
Matt Carlsonf40386c2009-11-02 14:24:02 +00009916 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9917 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009918 }
9919
Joe Perches63c3a662011-04-26 08:12:10 +00009920 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009921 u32 grc_mode = tr32(GRC_MODE);
9922
9923 /* Access the lower 1K of PL PCIE block registers. */
9924 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9925 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9926
9927 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9928 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9929 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9930
9931 tw32(GRC_MODE, grc_mode);
9932 }
9933
Matt Carlson55086ad2011-12-14 11:09:59 +00009934 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009935 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009936 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009937
Matt Carlson5093eed2010-11-24 08:31:45 +00009938 /* Access the lower 1K of PL PCIE block registers. */
9939 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9940 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009941
Matt Carlson5093eed2010-11-24 08:31:45 +00009942 val = tr32(TG3_PCIE_TLDLPL_PORT +
9943 TG3_PCIE_PL_LO_PHYCTL5);
9944 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9945 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009946
Matt Carlson5093eed2010-11-24 08:31:45 +00009947 tw32(GRC_MODE, grc_mode);
9948 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009949
Joe Perches41535772013-02-16 11:20:04 +00009950 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009951 u32 grc_mode;
9952
9953 /* Fix transmit hangs */
9954 val = tr32(TG3_CPMU_PADRNG_CTL);
9955 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9956 tw32(TG3_CPMU_PADRNG_CTL, val);
9957
9958 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009959
9960 /* Access the lower 1K of DL PCIE block registers. */
9961 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9962 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9963
9964 val = tr32(TG3_PCIE_TLDLPL_PORT +
9965 TG3_PCIE_DL_LO_FTSMAX);
9966 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9967 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9968 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9969
9970 tw32(GRC_MODE, grc_mode);
9971 }
9972
Matt Carlsona977dbe2010-04-12 06:58:26 +00009973 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9974 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9975 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9976 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009977 }
9978
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979 /* This works around an issue with Athlon chipsets on
9980 * B3 tigon3 silicon. This bit has no effect on any
9981 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009982 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 */
Joe Perches63c3a662011-04-26 08:12:10 +00009984 if (!tg3_flag(tp, CPMU_PRESENT)) {
9985 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009986 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9987 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009989
Joe Perches41535772013-02-16 11:20:04 +00009990 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009991 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 val = tr32(TG3PCI_PCISTATE);
9993 val |= PCISTATE_RETRY_SAME_DMA;
9994 tw32(TG3PCI_PCISTATE, val);
9995 }
9996
Joe Perches63c3a662011-04-26 08:12:10 +00009997 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009998 /* Allow reads and writes to the
9999 * APE register and memory space.
10000 */
10001 val = tr32(TG3PCI_PCISTATE);
10002 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000010003 PCISTATE_ALLOW_APE_SHMEM_WR |
10004 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070010005 tw32(TG3PCI_PCISTATE, val);
10006 }
10007
Joe Perches41535772013-02-16 11:20:04 +000010008 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 /* Enable some hw fixes. */
10010 val = tr32(TG3PCI_MSI_DATA);
10011 val |= (1 << 26) | (1 << 28) | (1 << 29);
10012 tw32(TG3PCI_MSI_DATA, val);
10013 }
10014
10015 /* Descriptor ring init may make accesses to the
10016 * NIC SRAM area to setup the TX descriptors, so we
10017 * can only do this after the hardware has been
10018 * successfully reset.
10019 */
Michael Chan32d8c572006-07-25 16:38:29 -070010020 err = tg3_init_rings(tp);
10021 if (err)
10022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023
Joe Perches63c3a662011-04-26 08:12:10 +000010024 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010025 val = tr32(TG3PCI_DMA_RW_CTRL) &
10026 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +000010027 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +000010028 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +000010029 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +000010030 tg3_asic_rev(tp) != ASIC_REV_5717 &&
10031 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +000010032 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010033 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +000010034 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
10035 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -070010036 /* This value is determined during the probe time DMA
10037 * engine test, tg3_test_dma.
10038 */
10039 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041
10042 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10043 GRC_MODE_4X_NIC_SEND_RINGS |
10044 GRC_MODE_NO_TX_PHDR_CSUM |
10045 GRC_MODE_NO_RX_PHDR_CSUM);
10046 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010047
10048 /* Pseudo-header checksum is done by hardware logic and not
10049 * the offload processers, so make the chip do the pseudo-
10050 * header checksums on receive. For transmit it is more
10051 * convenient to do the pseudo-header checksum in software
10052 * as Linux does that on transmit for us in all cases.
10053 */
10054 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010056 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10057 if (tp->rxptpctl)
10058 tw32(TG3_RX_PTP_CTL,
10059 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10060
10061 if (tg3_flag(tp, PTP_CAPABLE))
10062 val |= GRC_MODE_TIME_SYNC_ENABLE;
10063
10064 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065
Siva Reddy Kallam4419bb12017-12-22 16:05:28 +053010066 /* On one of the AMD platform, MRRS is restricted to 4000 because of
10067 * south bridge limitation. As a workaround, Driver is setting MRRS
10068 * to 2048 instead of default 4096.
10069 */
10070 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
10071 tp->pdev->subsystem_device == TG3PCI_SUBDEVICE_ID_DELL_5762) {
10072 val = tr32(TG3PCI_DEV_STATUS_CTRL) & ~MAX_READ_REQ_MASK;
10073 tw32(TG3PCI_DEV_STATUS_CTRL, val | MAX_READ_REQ_SIZE_2048);
10074 }
10075
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10077 val = tr32(GRC_MISC_CFG);
10078 val &= ~0xff;
10079 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10080 tw32(GRC_MISC_CFG, val);
10081
10082 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010083 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010085 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010087 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10089 else
10090 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10091 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10092 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010093 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 int fw_len;
10095
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010096 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10098 tw32(BUFMGR_MB_POOL_ADDR,
10099 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10100 tw32(BUFMGR_MB_POOL_SIZE,
10101 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103
Michael Chan0f893dc2005-07-25 12:30:38 -070010104 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10106 tp->bufmgr_config.mbuf_read_dma_low_water);
10107 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10108 tp->bufmgr_config.mbuf_mac_rx_low_water);
10109 tw32(BUFMGR_MB_HIGH_WATER,
10110 tp->bufmgr_config.mbuf_high_water);
10111 } else {
10112 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10113 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10114 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10115 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10116 tw32(BUFMGR_MB_HIGH_WATER,
10117 tp->bufmgr_config.mbuf_high_water_jumbo);
10118 }
10119 tw32(BUFMGR_DMA_LOW_WATER,
10120 tp->bufmgr_config.dma_low_water);
10121 tw32(BUFMGR_DMA_HIGH_WATER,
10122 tp->bufmgr_config.dma_high_water);
10123
Matt Carlsond309a462010-09-30 10:34:31 +000010124 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010125 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010126 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010127 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010128 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010129 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10130 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010131 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010132 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 for (i = 0; i < 2000; i++) {
10134 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10135 break;
10136 udelay(10);
10137 }
10138 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010139 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 return -ENODEV;
10141 }
10142
Joe Perches41535772013-02-16 11:20:04 +000010143 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010144 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010145
Matt Carlsoneb07a942011-04-20 07:57:36 +000010146 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147
10148 /* Initialize TG3_BDINFO's at:
10149 * RCVDBDI_STD_BD: standard eth size rx ring
10150 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10151 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10152 *
10153 * like so:
10154 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10155 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10156 * ring attribute flags
10157 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10158 *
10159 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10160 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10161 *
10162 * The size of each ring is fixed in the firmware, but the location is
10163 * configurable.
10164 */
10165 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010166 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010168 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010169 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010170 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10171 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010173 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010174 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10176 BDINFO_FLAGS_DISABLED);
10177
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010178 /* Program the jumbo buffer descriptor ring control
10179 * blocks on those devices that have them.
10180 */
Joe Perches41535772013-02-16 11:20:04 +000010181 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010182 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183
Joe Perches63c3a662011-04-26 08:12:10 +000010184 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010186 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010188 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010189 val = TG3_RX_JMB_RING_SIZE(tp) <<
10190 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010192 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010193 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010194 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010195 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010196 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10197 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 } else {
10199 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10200 BDINFO_FLAGS_DISABLED);
10201 }
10202
Joe Perches63c3a662011-04-26 08:12:10 +000010203 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010204 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010205 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10206 val |= (TG3_RX_STD_DMA_SZ << 2);
10207 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010208 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010209 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010210 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010211
10212 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213
Matt Carlson411da642009-11-13 13:03:46 +000010214 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010215 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010216
Joe Perches63c3a662011-04-26 08:12:10 +000010217 tpr->rx_jmb_prod_idx =
10218 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010219 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
Matt Carlson2d31eca2009-09-01 12:53:31 +000010221 tg3_rings_reset(tp);
10222
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010224 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225
10226 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010227 tw32(MAC_RX_MTU_SIZE,
10228 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229
10230 /* The slot time is changed by tg3_setup_phy if we
10231 * run at gigabit with half duplex.
10232 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010233 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10234 (6 << TX_LENGTHS_IPG_SHIFT) |
10235 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10236
Joe Perches41535772013-02-16 11:20:04 +000010237 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10238 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010239 val |= tr32(MAC_TX_LENGTHS) &
10240 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10241 TX_LENGTHS_CNT_DWN_VAL_MSK);
10242
10243 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010244
10245 /* Receive rules. */
10246 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10247 tw32(RCVLPC_CONFIG, 0x0181);
10248
10249 /* Calculate RDMAC_MODE setting early, we need it to determine
10250 * the RCVLPC_STATE_ENABLE mask.
10251 */
10252 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10253 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10254 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10255 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10256 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010257
Joe Perches41535772013-02-16 11:20:04 +000010258 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010259 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10260
Joe Perches41535772013-02-16 11:20:04 +000010261 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10262 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10263 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010264 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10265 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10266 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10267
Joe Perches41535772013-02-16 11:20:04 +000010268 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10269 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010270 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010271 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10273 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010274 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10276 }
10277 }
10278
Joe Perches63c3a662011-04-26 08:12:10 +000010279 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010280 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10281
Joe Perches41535772013-02-16 11:20:04 +000010282 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010283 tp->dma_limit = 0;
10284 if (tp->dev->mtu <= ETH_DATA_LEN) {
10285 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10286 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10287 }
10288 }
10289
Joe Perches63c3a662011-04-26 08:12:10 +000010290 if (tg3_flag(tp, HW_TSO_1) ||
10291 tg3_flag(tp, HW_TSO_2) ||
10292 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010293 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10294
Matt Carlson108a6c12011-05-19 12:12:47 +000010295 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010296 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10297 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010298 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299
Joe Perches41535772013-02-16 11:20:04 +000010300 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10301 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010302 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10303
Joe Perches41535772013-02-16 11:20:04 +000010304 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10305 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10306 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10307 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010308 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010309 u32 tgtreg;
10310
Joe Perches41535772013-02-16 11:20:04 +000010311 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010312 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10313 else
10314 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10315
10316 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010317 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10318 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010319 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10320 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10321 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10322 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10323 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10324 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010325 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010326 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010327 }
10328
Joe Perches41535772013-02-16 11:20:04 +000010329 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10330 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10331 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010332 u32 tgtreg;
10333
Joe Perches41535772013-02-16 11:20:04 +000010334 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010335 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10336 else
10337 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10338
10339 val = tr32(tgtreg);
10340 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010341 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10342 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10343 }
10344
Linus Torvalds1da177e2005-04-16 15:20:36 -070010345 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010346 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010347 val = tr32(RCVLPC_STATS_ENABLE);
10348 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10349 tw32(RCVLPC_STATS_ENABLE, val);
10350 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010351 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352 val = tr32(RCVLPC_STATS_ENABLE);
10353 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10354 tw32(RCVLPC_STATS_ENABLE, val);
10355 } else {
10356 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10357 }
10358 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10359 tw32(SNDDATAI_STATSENAB, 0xffffff);
10360 tw32(SNDDATAI_STATSCTRL,
10361 (SNDDATAI_SCTRL_ENABLE |
10362 SNDDATAI_SCTRL_FASTUPD));
10363
10364 /* Setup host coalescing engine. */
10365 tw32(HOSTCC_MODE, 0);
10366 for (i = 0; i < 2000; i++) {
10367 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10368 break;
10369 udelay(10);
10370 }
10371
Michael Chand244c892005-07-05 14:42:33 -070010372 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010373
Joe Perches63c3a662011-04-26 08:12:10 +000010374 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 /* Status/statistics block address. See tg3_timer,
10376 * the tg3_periodic_fetch_stats call there, and
10377 * tg3_get_stats to see how this works for 5705/5750 chips.
10378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10380 ((u64) tp->stats_mapping >> 32));
10381 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10382 ((u64) tp->stats_mapping & 0xffffffff));
10383 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010384
Linus Torvalds1da177e2005-04-16 15:20:36 -070010385 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010386
10387 /* Clear statistics and status block memory areas */
10388 for (i = NIC_SRAM_STATS_BLK;
10389 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10390 i += sizeof(u32)) {
10391 tg3_write_mem(tp, i, 0);
10392 udelay(40);
10393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 }
10395
10396 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10397
10398 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10399 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010400 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10402
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010403 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10404 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010405 /* reset to prevent losing 1st rx packet intermittently */
10406 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10407 udelay(10);
10408 }
10409
Matt Carlson3bda1252008-08-15 14:08:22 -070010410 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010411 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10412 MAC_MODE_FHDE_ENABLE;
10413 if (tg3_flag(tp, ENABLE_APE))
10414 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010415 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010416 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010417 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010418 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10420 udelay(40);
10421
Michael Chan314fba32005-04-21 17:07:04 -070010422 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010423 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010424 * register to preserve the GPIO settings for LOMs. The GPIOs,
10425 * whether used as inputs or outputs, are set by boot code after
10426 * reset.
10427 */
Joe Perches63c3a662011-04-26 08:12:10 +000010428 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010429 u32 gpio_mask;
10430
Michael Chan9d26e212006-12-07 00:21:14 -080010431 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10432 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10433 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010434
Joe Perches41535772013-02-16 11:20:04 +000010435 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010436 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10437 GRC_LCLCTRL_GPIO_OUTPUT3;
10438
Joe Perches41535772013-02-16 11:20:04 +000010439 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010440 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10441
Gary Zambranoaaf84462007-05-05 11:51:45 -070010442 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010443 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10444
10445 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010446 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010447 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10448 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10451 udelay(100);
10452
Matt Carlsonc3b50032012-01-17 15:27:23 +000010453 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010454 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010455 val |= MSGINT_MODE_ENABLE;
10456 if (tp->irq_cnt > 1)
10457 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010458 if (!tg3_flag(tp, 1SHOT_MSI))
10459 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010460 tw32(MSGINT_MODE, val);
10461 }
10462
Joe Perches63c3a662011-04-26 08:12:10 +000010463 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10465 udelay(40);
10466 }
10467
10468 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10469 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10470 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10471 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10472 WDMAC_MODE_LNGREAD_ENAB);
10473
Joe Perches41535772013-02-16 11:20:04 +000010474 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10475 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010476 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010477 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10478 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479 /* nothing */
10480 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010481 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 val |= WDMAC_MODE_RX_ACCEL;
10483 }
10484 }
10485
Michael Chand9ab5ad12006-03-20 22:27:35 -080010486 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010487 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010488 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010489
Joe Perches41535772013-02-16 11:20:04 +000010490 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010491 val |= WDMAC_MODE_BURST_ALL_DATA;
10492
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 tw32_f(WDMAC_MODE, val);
10494 udelay(40);
10495
Joe Perches63c3a662011-04-26 08:12:10 +000010496 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010497 u16 pcix_cmd;
10498
10499 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10500 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010501 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010502 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10503 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010504 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010505 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10506 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 }
Matt Carlson9974a352007-10-07 23:27:28 -070010508 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10509 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 }
10511
10512 tw32_f(RDMAC_MODE, rdmac_mode);
10513 udelay(40);
10514
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010515 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10516 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010517 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10518 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10519 break;
10520 }
10521 if (i < TG3_NUM_RDMA_CHANNELS) {
10522 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010523 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010524 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010525 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010526 }
10527 }
10528
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010530 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010531 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010532
Joe Perches41535772013-02-16 11:20:04 +000010533 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010534 tw32(SNDDATAC_MODE,
10535 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10536 else
10537 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10538
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10540 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010541 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010542 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010543 val |= RCVDBDI_MODE_LRG_RING_SZ;
10544 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010546 if (tg3_flag(tp, HW_TSO_1) ||
10547 tg3_flag(tp, HW_TSO_2) ||
10548 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010550 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010551 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010552 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10553 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010554 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10555
Joe Perches41535772013-02-16 11:20:04 +000010556 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 err = tg3_load_5701_a0_firmware_fix(tp);
10558 if (err)
10559 return err;
10560 }
10561
Nithin Sujirc4dab502013-03-06 17:02:34 +000010562 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10563 /* Ignore any errors for the firmware download. If download
10564 * fails, the device will operate with EEE disabled
10565 */
10566 tg3_load_57766_firmware(tp);
10567 }
10568
Joe Perches63c3a662011-04-26 08:12:10 +000010569 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 err = tg3_load_tso_firmware(tp);
10571 if (err)
10572 return err;
10573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574
10575 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010576
Joe Perches63c3a662011-04-26 08:12:10 +000010577 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010578 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010579 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010580
Joe Perches41535772013-02-16 11:20:04 +000010581 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10582 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010583 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10584 tp->tx_mode &= ~val;
10585 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10586 }
10587
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588 tw32_f(MAC_TX_MODE, tp->tx_mode);
10589 udelay(100);
10590
Joe Perches63c3a662011-04-26 08:12:10 +000010591 if (tg3_flag(tp, ENABLE_RSS)) {
Eric Dumazet39648352014-11-16 06:23:08 -080010592 u32 rss_key[10];
10593
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010594 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010595
Eric Dumazet39648352014-11-16 06:23:08 -080010596 netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
10597
10598 for (i = 0; i < 10 ; i++)
10599 tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010600 }
10601
Linus Torvalds1da177e2005-04-16 15:20:36 -070010602 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010603 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010604 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10605
Nithin Sujir378b72c2013-07-29 13:58:39 -070010606 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10607 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10608
Joe Perches63c3a662011-04-26 08:12:10 +000010609 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010610 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10611 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10612 RX_MODE_RSS_IPV6_HASH_EN |
10613 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10614 RX_MODE_RSS_IPV4_HASH_EN |
10615 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10616
Linus Torvalds1da177e2005-04-16 15:20:36 -070010617 tw32_f(MAC_RX_MODE, tp->rx_mode);
10618 udelay(10);
10619
Linus Torvalds1da177e2005-04-16 15:20:36 -070010620 tw32(MAC_LED_CTRL, tp->led_ctrl);
10621
10622 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010623 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10625 udelay(10);
10626 }
10627 tw32_f(MAC_RX_MODE, tp->rx_mode);
10628 udelay(10);
10629
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010630 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010631 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10632 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633 /* Set drive transmission level to 1.2V */
10634 /* only if the signal pre-emphasis bit is not set */
10635 val = tr32(MAC_SERDES_CFG);
10636 val &= 0xfffff000;
10637 val |= 0x880;
10638 tw32(MAC_SERDES_CFG, val);
10639 }
Joe Perches41535772013-02-16 11:20:04 +000010640 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010641 tw32(MAC_SERDES_CFG, 0x616000);
10642 }
10643
10644 /* Prevent chip from dropping frames when flow control
10645 * is enabled.
10646 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010647 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010648 val = 1;
10649 else
10650 val = 2;
10651 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010652
Joe Perches41535772013-02-16 11:20:04 +000010653 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010654 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010655 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010656 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010657 }
10658
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010659 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010660 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010661 u32 tmp;
10662
10663 tmp = tr32(SERDES_RX_CTRL);
10664 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10665 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10666 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10667 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10668 }
10669
Joe Perches63c3a662011-04-26 08:12:10 +000010670 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010671 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010672 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673
Joe Perches953c96e2013-04-09 10:18:14 +000010674 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010675 if (err)
10676 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010677
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010678 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10679 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010680 u32 tmp;
10681
10682 /* Clear CRC stats. */
10683 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10684 tg3_writephy(tp, MII_TG3_TEST1,
10685 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010686 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688 }
10689 }
10690
10691 __tg3_set_rx_mode(tp->dev);
10692
10693 /* Initialize receive rules. */
10694 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10695 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10696 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10697 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10698
Joe Perches63c3a662011-04-26 08:12:10 +000010699 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700 limit = 8;
10701 else
10702 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010703 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 limit -= 4;
10705 switch (limit) {
10706 case 16:
10707 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10708 case 15:
10709 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10710 case 14:
10711 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10712 case 13:
10713 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10714 case 12:
10715 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10716 case 11:
10717 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10718 case 10:
10719 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10720 case 9:
10721 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10722 case 8:
10723 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10724 case 7:
10725 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10726 case 6:
10727 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10728 case 5:
10729 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10730 case 4:
10731 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10732 case 3:
10733 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10734 case 2:
10735 case 1:
10736
10737 default:
10738 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010740
Joe Perches63c3a662011-04-26 08:12:10 +000010741 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010742 /* Write our heartbeat update interval to APE. */
10743 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053010744 APE_HOST_HEARTBEAT_INT_5SEC);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010745
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10747
Linus Torvalds1da177e2005-04-16 15:20:36 -070010748 return 0;
10749}
10750
10751/* Called at device open time to get the chip ready for
10752 * packet processing. Invoked with tp->lock held.
10753 */
Joe Perches953c96e2013-04-09 10:18:14 +000010754static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010756 /* Chip may have been just powered on. If so, the boot code may still
10757 * be running initialization. Wait for it to finish to avoid races in
10758 * accessing the hardware.
10759 */
10760 tg3_enable_register_access(tp);
10761 tg3_poll_fw(tp);
10762
Linus Torvalds1da177e2005-04-16 15:20:36 -070010763 tg3_switch_clocks(tp);
10764
10765 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10766
Matt Carlson2f751b62008-08-04 23:17:34 -070010767 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010768}
10769
Florian Fainelli038e8932017-03-06 12:56:02 -080010770#ifdef CONFIG_TIGON3_HWMON
Michael Chanaed93e02012-07-16 16:24:02 +000010771static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10772{
10773 int i;
10774
10775 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10776 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10777
10778 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10779 off += len;
10780
10781 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10782 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10783 memset(ocir, 0, TG3_OCIR_LEN);
10784 }
10785}
10786
10787/* sysfs attributes for hwmon */
10788static ssize_t tg3_show_temp(struct device *dev,
10789 struct device_attribute *devattr, char *buf)
10790{
Michael Chanaed93e02012-07-16 16:24:02 +000010791 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010792 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010793 u32 temperature;
10794
10795 spin_lock_bh(&tp->lock);
10796 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10797 sizeof(temperature));
10798 spin_unlock_bh(&tp->lock);
Jean Delvared3d11fe2015-09-01 18:07:41 +020010799 return sprintf(buf, "%u\n", temperature * 1000);
Michael Chanaed93e02012-07-16 16:24:02 +000010800}
10801
10802
Joe Perchesd3757ba2018-03-23 16:34:44 -070010803static SENSOR_DEVICE_ATTR(temp1_input, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010804 TG3_TEMP_SENSOR_OFFSET);
Joe Perchesd3757ba2018-03-23 16:34:44 -070010805static SENSOR_DEVICE_ATTR(temp1_crit, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010806 TG3_TEMP_CAUTION_OFFSET);
Joe Perchesd3757ba2018-03-23 16:34:44 -070010807static SENSOR_DEVICE_ATTR(temp1_max, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010808 TG3_TEMP_MAX_OFFSET);
10809
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010810static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010811 &sensor_dev_attr_temp1_input.dev_attr.attr,
10812 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10813 &sensor_dev_attr_temp1_max.dev_attr.attr,
10814 NULL
10815};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010816ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010817
Michael Chanaed93e02012-07-16 16:24:02 +000010818static void tg3_hwmon_close(struct tg3 *tp)
10819{
Michael Chanaed93e02012-07-16 16:24:02 +000010820 if (tp->hwmon_dev) {
10821 hwmon_device_unregister(tp->hwmon_dev);
10822 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010823 }
Michael Chanaed93e02012-07-16 16:24:02 +000010824}
10825
10826static void tg3_hwmon_open(struct tg3 *tp)
10827{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010828 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010829 u32 size = 0;
10830 struct pci_dev *pdev = tp->pdev;
10831 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10832
10833 tg3_sd_scan_scratchpad(tp, ocirs);
10834
10835 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10836 if (!ocirs[i].src_data_length)
10837 continue;
10838
10839 size += ocirs[i].src_hdr_length;
10840 size += ocirs[i].src_data_length;
10841 }
10842
10843 if (!size)
10844 return;
10845
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010846 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10847 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010848 if (IS_ERR(tp->hwmon_dev)) {
10849 tp->hwmon_dev = NULL;
10850 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010851 }
Michael Chanaed93e02012-07-16 16:24:02 +000010852}
Florian Fainelli038e8932017-03-06 12:56:02 -080010853#else
10854static inline void tg3_hwmon_close(struct tg3 *tp) { }
10855static inline void tg3_hwmon_open(struct tg3 *tp) { }
10856#endif /* CONFIG_TIGON3_HWMON */
Michael Chanaed93e02012-07-16 16:24:02 +000010857
10858
Linus Torvalds1da177e2005-04-16 15:20:36 -070010859#define TG3_STAT_ADD32(PSTAT, REG) \
10860do { u32 __val = tr32(REG); \
10861 (PSTAT)->low += __val; \
10862 if ((PSTAT)->low < __val) \
10863 (PSTAT)->high += 1; \
10864} while (0)
10865
10866static void tg3_periodic_fetch_stats(struct tg3 *tp)
10867{
10868 struct tg3_hw_stats *sp = tp->hw_stats;
10869
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010870 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010871 return;
10872
10873 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10874 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10875 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10876 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10877 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10878 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10879 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10880 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10881 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10882 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10883 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10884 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10885 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010886 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010887 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10888 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10889 u32 val;
10890
10891 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010892 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010893 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010894 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010896
10897 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10898 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10899 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10900 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10901 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10902 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10903 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10904 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10905 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10906 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10907 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10908 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10909 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10910 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010911
10912 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010913 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010914 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010915 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10916 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010917 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10918 } else {
10919 u32 val = tr32(HOSTCC_FLOW_ATTN);
10920 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10921 if (val) {
10922 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10923 sp->rx_discards.low += val;
10924 if (sp->rx_discards.low < val)
10925 sp->rx_discards.high += 1;
10926 }
10927 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10928 }
Michael Chan463d3052006-05-22 16:36:27 -070010929 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010930}
10931
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010932static void tg3_chk_missed_msi(struct tg3 *tp)
10933{
10934 u32 i;
10935
10936 for (i = 0; i < tp->irq_cnt; i++) {
10937 struct tg3_napi *tnapi = &tp->napi[i];
10938
10939 if (tg3_has_work(tnapi)) {
10940 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10941 tnapi->last_tx_cons == tnapi->tx_cons) {
10942 if (tnapi->chk_msi_cnt < 1) {
10943 tnapi->chk_msi_cnt++;
10944 return;
10945 }
Matt Carlson7f230732011-08-31 11:44:48 +000010946 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010947 }
10948 }
10949 tnapi->chk_msi_cnt = 0;
10950 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10951 tnapi->last_tx_cons = tnapi->tx_cons;
10952 }
10953}
10954
Kees Cooke99e88a2017-10-16 14:43:17 -070010955static void tg3_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010956{
Kees Cooke99e88a2017-10-16 14:43:17 -070010957 struct tg3 *tp = from_timer(tp, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958
David S. Millerf47c11e2005-06-24 20:18:35 -070010959 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010960
Prashant Sreedharan4fd190a2015-01-14 11:33:49 -080010961 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
10962 spin_unlock(&tp->lock);
10963 goto restart_timer;
10964 }
10965
Joe Perches41535772013-02-16 11:20:04 +000010966 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010967 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010968 tg3_chk_missed_msi(tp);
10969
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010970 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10971 /* BCM4785: Flush posted writes from GbE to host memory. */
10972 tr32(HOSTCC_MODE);
10973 }
10974
Joe Perches63c3a662011-04-26 08:12:10 +000010975 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010976 /* All of this garbage is because when using non-tagged
10977 * IRQ status the mailbox/status_block protocol the chip
10978 * uses with the cpu is race prone.
10979 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010980 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010981 tw32(GRC_LOCAL_CTRL,
10982 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10983 } else {
10984 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010985 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010987
David S. Millerfac9b832005-05-18 22:46:34 -070010988 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010989 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010990 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010991 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010993 }
10994
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995 /* This part only runs once per second. */
10996 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010997 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010998 tg3_periodic_fetch_stats(tp);
10999
Matt Carlsonb0c59432011-05-19 12:12:48 +000011000 if (tp->setlpicnt && !--tp->setlpicnt)
11001 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000011002
Joe Perches63c3a662011-04-26 08:12:10 +000011003 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011004 u32 mac_stat;
11005 int phy_event;
11006
11007 mac_stat = tr32(MAC_STATUS);
11008
11009 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011010 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011011 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
11012 phy_event = 1;
11013 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
11014 phy_event = 1;
11015
11016 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000011017 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000011018 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011019 u32 mac_stat = tr32(MAC_STATUS);
11020 int need_setup = 0;
11021
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011022 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011023 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
11024 need_setup = 1;
11025 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011026 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011027 (mac_stat & (MAC_STATUS_PCS_SYNCED |
11028 MAC_STATUS_SIGNAL_DET))) {
11029 need_setup = 1;
11030 }
11031 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070011032 if (!tp->serdes_counter) {
11033 tw32_f(MAC_MODE,
11034 (tp->mac_mode &
11035 ~MAC_MODE_PORT_MODE_MASK));
11036 udelay(40);
11037 tw32_f(MAC_MODE, tp->mac_mode);
11038 udelay(40);
11039 }
Joe Perches953c96e2013-04-09 10:18:14 +000011040 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011042 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011043 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070011044 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080011045 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
11046 u32 cpmu = tr32(TG3_CPMU_STATUS);
11047 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
11048 TG3_CPMU_STATUS_LINK_MASK);
11049
11050 if (link_up != tp->link_up)
11051 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011053
11054 tp->timer_counter = tp->timer_multiplier;
11055 }
11056
Michael Chan130b8e42006-09-27 16:00:40 -070011057 /* Heartbeat is only sent once every 2 seconds.
11058 *
11059 * The heartbeat is to tell the ASF firmware that the host
11060 * driver is still alive. In the event that the OS crashes,
11061 * ASF needs to reset the hardware to free up the FIFO space
11062 * that may be filled with rx packets destined for the host.
11063 * If the FIFO is full, ASF will no longer function properly.
11064 *
11065 * Unintended resets have been reported on real time kernels
11066 * where the timer doesn't run on time. Netpoll will also have
11067 * same problem.
11068 *
11069 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11070 * to check the ring condition when the heartbeat is expiring
11071 * before doing the reset. This will prevent most unintended
11072 * resets.
11073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011074 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011075 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011076 tg3_wait_for_event_ack(tp);
11077
Michael Chanbbadf502006-04-06 21:46:34 -070011078 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011079 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011080 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011081 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11082 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011083
11084 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011085 }
11086 tp->asf_counter = tp->asf_multiplier;
11087 }
11088
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053011089 /* Update the APE heartbeat every 5 seconds.*/
11090 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL);
11091
David S. Millerf47c11e2005-06-24 20:18:35 -070011092 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011093
Michael Chanf475f162006-03-27 23:20:14 -080011094restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011095 tp->timer.expires = jiffies + tp->timer_offset;
11096 add_timer(&tp->timer);
11097}
11098
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011099static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011100{
11101 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011102 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011103 !tg3_flag(tp, 57765_CLASS))
11104 tp->timer_offset = HZ;
11105 else
11106 tp->timer_offset = HZ / 10;
11107
11108 BUG_ON(tp->timer_offset > HZ);
11109
11110 tp->timer_multiplier = (HZ / tp->timer_offset);
11111 tp->asf_multiplier = (HZ / tp->timer_offset) *
11112 TG3_FW_UPDATE_FREQ_SEC;
11113
Kees Cooke99e88a2017-10-16 14:43:17 -070011114 timer_setup(&tp->timer, tg3_timer, 0);
Matt Carlson21f76382012-02-22 12:35:21 +000011115}
11116
11117static void tg3_timer_start(struct tg3 *tp)
11118{
11119 tp->asf_counter = tp->asf_multiplier;
11120 tp->timer_counter = tp->timer_multiplier;
11121
11122 tp->timer.expires = jiffies + tp->timer_offset;
11123 add_timer(&tp->timer);
11124}
11125
11126static void tg3_timer_stop(struct tg3 *tp)
11127{
11128 del_timer_sync(&tp->timer);
11129}
11130
11131/* Restart hardware after configuration changes, self-test, etc.
11132 * Invoked with tp->lock held.
11133 */
Joe Perches953c96e2013-04-09 10:18:14 +000011134static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011135 __releases(tp->lock)
11136 __acquires(tp->lock)
11137{
11138 int err;
11139
11140 err = tg3_init_hw(tp, reset_phy);
11141 if (err) {
11142 netdev_err(tp->dev,
11143 "Failed to re-initialize device, aborting\n");
11144 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11145 tg3_full_unlock(tp);
11146 tg3_timer_stop(tp);
11147 tp->irq_sync = 0;
11148 tg3_napi_enable(tp);
11149 dev_close(tp->dev);
11150 tg3_full_lock(tp, 0);
11151 }
11152 return err;
11153}
11154
11155static void tg3_reset_task(struct work_struct *work)
11156{
11157 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11158 int err;
11159
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011160 rtnl_lock();
Matt Carlson21f76382012-02-22 12:35:21 +000011161 tg3_full_lock(tp, 0);
11162
11163 if (!netif_running(tp->dev)) {
11164 tg3_flag_clear(tp, RESET_TASK_PENDING);
11165 tg3_full_unlock(tp);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011166 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011167 return;
11168 }
11169
11170 tg3_full_unlock(tp);
11171
11172 tg3_phy_stop(tp);
11173
11174 tg3_netif_stop(tp);
11175
11176 tg3_full_lock(tp, 1);
11177
11178 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11179 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11180 tp->write32_rx_mbox = tg3_write_flush_reg32;
11181 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11182 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11183 }
11184
11185 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011186 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011187 if (err)
11188 goto out;
11189
11190 tg3_netif_start(tp);
11191
11192out:
11193 tg3_full_unlock(tp);
11194
11195 if (!err)
11196 tg3_phy_start(tp);
11197
11198 tg3_flag_clear(tp, RESET_TASK_PENDING);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011199 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011200}
11201
Matt Carlson4f125f42009-09-01 12:55:02 +000011202static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011203{
David Howells7d12e782006-10-05 14:55:46 +010011204 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011205 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011206 char *name;
11207 struct tg3_napi *tnapi = &tp->napi[irq_num];
11208
11209 if (tp->irq_cnt == 1)
11210 name = tp->dev->name;
11211 else {
11212 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011213 if (tnapi->tx_buffers && tnapi->rx_rcb)
11214 snprintf(name, IFNAMSIZ,
11215 "%s-txrx-%d", tp->dev->name, irq_num);
11216 else if (tnapi->tx_buffers)
11217 snprintf(name, IFNAMSIZ,
11218 "%s-tx-%d", tp->dev->name, irq_num);
11219 else if (tnapi->rx_rcb)
11220 snprintf(name, IFNAMSIZ,
11221 "%s-rx-%d", tp->dev->name, irq_num);
11222 else
11223 snprintf(name, IFNAMSIZ,
11224 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011225 name[IFNAMSIZ-1] = 0;
11226 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011227
Joe Perches63c3a662011-04-26 08:12:10 +000011228 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011229 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011230 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011231 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011232 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011233 } else {
11234 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011235 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011236 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011237 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011238 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011239
11240 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011241}
11242
Michael Chan79381092005-04-21 17:13:59 -070011243static int tg3_test_interrupt(struct tg3 *tp)
11244{
Matt Carlson09943a12009-08-28 14:01:57 +000011245 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011246 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011247 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011248 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011249
Michael Chand4bc3922005-05-29 14:59:20 -070011250 if (!netif_running(dev))
11251 return -ENODEV;
11252
Michael Chan79381092005-04-21 17:13:59 -070011253 tg3_disable_ints(tp);
11254
Matt Carlson4f125f42009-09-01 12:55:02 +000011255 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011256
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011257 /*
11258 * Turn off MSI one shot mode. Otherwise this test has no
11259 * observable way to know whether the interrupt was delivered.
11260 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011261 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011262 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11263 tw32(MSGINT_MODE, val);
11264 }
11265
Matt Carlson4f125f42009-09-01 12:55:02 +000011266 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011267 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011268 if (err)
11269 return err;
11270
Matt Carlson898a56f2009-08-28 14:02:40 +000011271 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011272 tg3_enable_ints(tp);
11273
11274 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011275 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011276
11277 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011278 u32 int_mbox, misc_host_ctrl;
11279
Matt Carlson898a56f2009-08-28 14:02:40 +000011280 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011281 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11282
11283 if ((int_mbox != 0) ||
11284 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11285 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011286 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011287 }
11288
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011289 if (tg3_flag(tp, 57765_PLUS) &&
11290 tnapi->hw_status->status_tag != tnapi->last_tag)
11291 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11292
Michael Chan79381092005-04-21 17:13:59 -070011293 msleep(10);
11294 }
11295
11296 tg3_disable_ints(tp);
11297
Matt Carlson4f125f42009-09-01 12:55:02 +000011298 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011299
Matt Carlson4f125f42009-09-01 12:55:02 +000011300 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011301
11302 if (err)
11303 return err;
11304
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011305 if (intr_ok) {
11306 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011307 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011308 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11309 tw32(MSGINT_MODE, val);
11310 }
Michael Chan79381092005-04-21 17:13:59 -070011311 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011312 }
Michael Chan79381092005-04-21 17:13:59 -070011313
11314 return -EIO;
11315}
11316
11317/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11318 * successfully restored
11319 */
11320static int tg3_test_msi(struct tg3 *tp)
11321{
Michael Chan79381092005-04-21 17:13:59 -070011322 int err;
11323 u16 pci_cmd;
11324
Joe Perches63c3a662011-04-26 08:12:10 +000011325 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011326 return 0;
11327
11328 /* Turn off SERR reporting in case MSI terminates with Master
11329 * Abort.
11330 */
11331 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11332 pci_write_config_word(tp->pdev, PCI_COMMAND,
11333 pci_cmd & ~PCI_COMMAND_SERR);
11334
11335 err = tg3_test_interrupt(tp);
11336
11337 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11338
11339 if (!err)
11340 return 0;
11341
11342 /* other failures */
11343 if (err != -EIO)
11344 return err;
11345
11346 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011347 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11348 "to INTx mode. Please report this failure to the PCI "
11349 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011350
Matt Carlson4f125f42009-09-01 12:55:02 +000011351 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011352
Michael Chan79381092005-04-21 17:13:59 -070011353 pci_disable_msi(tp->pdev);
11354
Joe Perches63c3a662011-04-26 08:12:10 +000011355 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011356 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011357
Matt Carlson4f125f42009-09-01 12:55:02 +000011358 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011359 if (err)
11360 return err;
11361
11362 /* Need to reset the chip because the MSI cycle may have terminated
11363 * with Master Abort.
11364 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011365 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011366
Michael Chan944d9802005-05-29 14:57:48 -070011367 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011368 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011369
David S. Millerf47c11e2005-06-24 20:18:35 -070011370 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011371
11372 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011373 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011374
11375 return err;
11376}
11377
Matt Carlson9e9fd122009-01-19 16:57:45 -080011378static int tg3_request_firmware(struct tg3 *tp)
11379{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011380 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011381
11382 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011383 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11384 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011385 return -ENOENT;
11386 }
11387
Nithin Sujir77997ea2013-03-06 17:02:32 +000011388 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011389
11390 /* Firmware blob starts with version numbers, followed by
11391 * start address and _full_ length including BSS sections
11392 * (which must be longer than the actual data, of course
11393 */
11394
Nithin Sujir77997ea2013-03-06 17:02:32 +000011395 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11396 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011397 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11398 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011399 release_firmware(tp->fw);
11400 tp->fw = NULL;
11401 return -EINVAL;
11402 }
11403
11404 /* We no longer need firmware; we have it. */
11405 tp->fw_needed = NULL;
11406 return 0;
11407}
11408
Michael Chan91024262012-09-28 07:12:38 +000011409static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011410{
Michael Chan91024262012-09-28 07:12:38 +000011411 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011412
Michael Chan91024262012-09-28 07:12:38 +000011413 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011414 /* We want as many rx rings enabled as there are cpus.
11415 * In multiqueue MSI-X mode, the first MSI-X vector
11416 * only deals with link interrupts, etc, so we add
11417 * one to the number of vectors we are requesting.
11418 */
Michael Chan91024262012-09-28 07:12:38 +000011419 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011420 }
Matt Carlson679563f2009-09-01 12:55:46 +000011421
Michael Chan91024262012-09-28 07:12:38 +000011422 return irq_cnt;
11423}
11424
11425static bool tg3_enable_msix(struct tg3 *tp)
11426{
11427 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011428 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011429
Michael Chan09681692012-09-28 07:12:42 +000011430 tp->txq_cnt = tp->txq_req;
11431 tp->rxq_cnt = tp->rxq_req;
11432 if (!tp->rxq_cnt)
11433 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011434 if (tp->rxq_cnt > tp->rxq_max)
11435 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011436
11437 /* Disable multiple TX rings by default. Simple round-robin hardware
11438 * scheduling of the TX rings can cause starvation of rings with
11439 * small packets when other rings have TSO or jumbo packets.
11440 */
11441 if (!tp->txq_req)
11442 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011443
11444 tp->irq_cnt = tg3_irq_count(tp);
11445
Matt Carlson679563f2009-09-01 12:55:46 +000011446 for (i = 0; i < tp->irq_max; i++) {
11447 msix_ent[i].entry = i;
11448 msix_ent[i].vector = 0;
11449 }
11450
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011451 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011452 if (rc < 0) {
11453 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011454 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011455 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11456 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011457 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011458 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011459 if (tp->txq_cnt)
11460 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011461 }
11462
11463 for (i = 0; i < tp->irq_max; i++)
11464 tp->napi[i].irq_vec = msix_ent[i].vector;
11465
Michael Chan49a359e2012-09-28 07:12:37 +000011466 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011467 pci_disable_msix(tp->pdev);
11468 return false;
11469 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011470
Michael Chan91024262012-09-28 07:12:38 +000011471 if (tp->irq_cnt == 1)
11472 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011473
Michael Chan91024262012-09-28 07:12:38 +000011474 tg3_flag_set(tp, ENABLE_RSS);
11475
11476 if (tp->txq_cnt > 1)
11477 tg3_flag_set(tp, ENABLE_TSS);
11478
11479 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011480
Matt Carlson679563f2009-09-01 12:55:46 +000011481 return true;
11482}
11483
Matt Carlson07b01732009-08-28 14:01:15 +000011484static void tg3_ints_init(struct tg3 *tp)
11485{
Joe Perches63c3a662011-04-26 08:12:10 +000011486 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11487 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011488 /* All MSI supporting chips should support tagged
11489 * status. Assert that this is the case.
11490 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011491 netdev_warn(tp->dev,
11492 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011493 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011494 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011495
Joe Perches63c3a662011-04-26 08:12:10 +000011496 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11497 tg3_flag_set(tp, USING_MSIX);
11498 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11499 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011500
Joe Perches63c3a662011-04-26 08:12:10 +000011501 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011502 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011503 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011504 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011505 if (!tg3_flag(tp, 1SHOT_MSI))
11506 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011507 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11508 }
11509defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011510 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011511 tp->irq_cnt = 1;
11512 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011513 }
11514
11515 if (tp->irq_cnt == 1) {
11516 tp->txq_cnt = 1;
11517 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011518 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011519 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011520 }
Matt Carlson07b01732009-08-28 14:01:15 +000011521}
11522
11523static void tg3_ints_fini(struct tg3 *tp)
11524{
Joe Perches63c3a662011-04-26 08:12:10 +000011525 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011526 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011527 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011528 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011529 tg3_flag_clear(tp, USING_MSI);
11530 tg3_flag_clear(tp, USING_MSIX);
11531 tg3_flag_clear(tp, ENABLE_RSS);
11532 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011533}
11534
Matt Carlsonbe947302012-12-03 19:36:57 +000011535static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11536 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011537{
Michael Chand8f4cd32012-09-28 07:12:40 +000011538 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011539 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011540
Matt Carlson679563f2009-09-01 12:55:46 +000011541 /*
11542 * Setup interrupts first so we know how
11543 * many NAPI resources to allocate
11544 */
11545 tg3_ints_init(tp);
11546
Matt Carlson90415472011-12-16 13:33:23 +000011547 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011548
Linus Torvalds1da177e2005-04-16 15:20:36 -070011549 /* The placement of this call is tied
11550 * to the setup and use of Host TX descriptors.
11551 */
11552 err = tg3_alloc_consistent(tp);
11553 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011554 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011556 tg3_napi_init(tp);
11557
Matt Carlsonfed97812009-09-01 13:10:19 +000011558 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011559
Matt Carlson4f125f42009-09-01 12:55:02 +000011560 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson4f125f42009-09-01 12:55:02 +000011561 err = tg3_request_irq(tp, i);
11562 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011563 for (i--; i >= 0; i--) {
Colin Ian King23f48222017-09-14 17:01:25 +010011564 struct tg3_napi *tnapi = &tp->napi[i];
11565
Matt Carlson4f125f42009-09-01 12:55:02 +000011566 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011567 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011568 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011569 }
11570 }
Matt Carlson07b01732009-08-28 14:01:15 +000011571
David S. Millerf47c11e2005-06-24 20:18:35 -070011572 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011574 if (init)
11575 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11576
Michael Chand8f4cd32012-09-28 07:12:40 +000011577 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011578 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011579 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011580 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581 }
11582
David S. Millerf47c11e2005-06-24 20:18:35 -070011583 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584
Matt Carlson07b01732009-08-28 14:01:15 +000011585 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011586 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011587
Michael Chand8f4cd32012-09-28 07:12:40 +000011588 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011589 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011590
Michael Chan79381092005-04-21 17:13:59 -070011591 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011592 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011593 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011594 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011595 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011596
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011597 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011598 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011599
Joe Perches63c3a662011-04-26 08:12:10 +000011600 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011601 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011602
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011603 tw32(PCIE_TRANSACTION_CFG,
11604 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011605 }
Michael Chan79381092005-04-21 17:13:59 -070011606 }
11607
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011608 tg3_phy_start(tp);
11609
Michael Chanaed93e02012-07-16 16:24:02 +000011610 tg3_hwmon_open(tp);
11611
David S. Millerf47c11e2005-06-24 20:18:35 -070011612 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011613
Matt Carlson21f76382012-02-22 12:35:21 +000011614 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011615 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011616 tg3_enable_ints(tp);
11617
Ivan Vecera20d14a52015-01-08 16:13:07 +010011618 tg3_ptp_resume(tp);
Matt Carlsonbe947302012-12-03 19:36:57 +000011619
David S. Millerf47c11e2005-06-24 20:18:35 -070011620 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011621
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011622 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011623
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011624 /*
11625 * Reset loopback feature if it was turned on while the device was down
11626 * make sure that it's installed properly now.
11627 */
11628 if (dev->features & NETIF_F_LOOPBACK)
11629 tg3_set_loopback(dev, dev->features);
11630
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011632
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011633out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011634 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11635 struct tg3_napi *tnapi = &tp->napi[i];
11636 free_irq(tnapi->irq_vec, tnapi);
11637 }
Matt Carlson07b01732009-08-28 14:01:15 +000011638
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011639out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011640 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011641 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011642 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011643
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011644out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011645 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011646
Matt Carlson07b01732009-08-28 14:01:15 +000011647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011648}
11649
Michael Chan65138592012-09-28 07:12:41 +000011650static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011651{
Matt Carlson4f125f42009-09-01 12:55:02 +000011652 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011653
Matt Carlsondb219972011-11-04 09:15:03 +000011654 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011655 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656
Matt Carlson21f76382012-02-22 12:35:21 +000011657 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658
Michael Chanaed93e02012-07-16 16:24:02 +000011659 tg3_hwmon_close(tp);
11660
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011661 tg3_phy_stop(tp);
11662
David S. Millerf47c11e2005-06-24 20:18:35 -070011663 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664
11665 tg3_disable_ints(tp);
11666
Michael Chan944d9802005-05-29 14:57:48 -070011667 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011668 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011669 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011670
David S. Millerf47c11e2005-06-24 20:18:35 -070011671 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672
Matt Carlson4f125f42009-09-01 12:55:02 +000011673 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11674 struct tg3_napi *tnapi = &tp->napi[i];
11675 free_irq(tnapi->irq_vec, tnapi);
11676 }
Matt Carlson07b01732009-08-28 14:01:15 +000011677
11678 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011680 tg3_napi_fini(tp);
11681
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011683}
11684
Michael Chand8f4cd32012-09-28 07:12:40 +000011685static int tg3_open(struct net_device *dev)
11686{
11687 struct tg3 *tp = netdev_priv(dev);
11688 int err;
11689
Ivan Vecera0486a062014-09-01 14:21:57 +020011690 if (tp->pcierr_recovery) {
11691 netdev_err(dev, "Failed to open device. PCI error recovery "
11692 "in progress\n");
11693 return -EAGAIN;
11694 }
11695
Michael Chand8f4cd32012-09-28 07:12:40 +000011696 if (tp->fw_needed) {
11697 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011698 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11699 if (err) {
11700 netdev_warn(tp->dev, "EEE capability disabled\n");
11701 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11702 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11703 netdev_warn(tp->dev, "EEE capability restored\n");
11704 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11705 }
11706 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011707 if (err)
11708 return err;
11709 } else if (err) {
11710 netdev_warn(tp->dev, "TSO capability disabled\n");
11711 tg3_flag_clear(tp, TSO_CAPABLE);
11712 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11713 netdev_notice(tp->dev, "TSO capability restored\n");
11714 tg3_flag_set(tp, TSO_CAPABLE);
11715 }
11716 }
11717
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011718 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011719
11720 err = tg3_power_up(tp);
11721 if (err)
11722 return err;
11723
11724 tg3_full_lock(tp, 0);
11725
11726 tg3_disable_ints(tp);
11727 tg3_flag_clear(tp, INIT_COMPLETE);
11728
11729 tg3_full_unlock(tp);
11730
Nithin Sujir942d1af2013-04-09 08:48:07 +000011731 err = tg3_start(tp,
11732 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11733 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011734 if (err) {
11735 tg3_frob_aux_power(tp, false);
11736 pci_set_power_state(tp->pdev, PCI_D3hot);
11737 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011738
Linus Torvalds1da177e2005-04-16 15:20:36 -070011739 return err;
11740}
11741
11742static int tg3_close(struct net_device *dev)
11743{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011744 struct tg3 *tp = netdev_priv(dev);
11745
Ivan Vecera0486a062014-09-01 14:21:57 +020011746 if (tp->pcierr_recovery) {
11747 netdev_err(dev, "Failed to close device. PCI error recovery "
11748 "in progress\n");
11749 return -EAGAIN;
11750 }
11751
Michael Chan65138592012-09-28 07:12:41 +000011752 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011753
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011754 if (pci_device_is_present(tp->pdev)) {
11755 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011756
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011757 tg3_carrier_off(tp);
11758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011759 return 0;
11760}
11761
11762static inline u64 get_stat64(tg3_stat64_t *val)
11763{
11764 return ((u64)val->high << 32) | ((u64)val->low);
11765}
11766
11767static u64 tg3_calc_crc_errors(struct tg3 *tp)
11768{
11769 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11770
11771 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011772 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11773 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011774 u32 val;
11775
11776 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11777 tg3_writephy(tp, MII_TG3_TEST1,
11778 val | MII_TG3_TEST1_CRC_EN);
11779 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11780 } else
11781 val = 0;
11782
11783 tp->phy_crc_errors += val;
11784
11785 return tp->phy_crc_errors;
11786 }
11787
11788 return get_stat64(&hw_stats->rx_fcs_errors);
11789}
11790
11791#define ESTAT_ADD(member) \
11792 estats->member = old_estats->member + \
11793 get_stat64(&hw_stats->member)
11794
11795static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11796{
11797 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11798 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11799
11800 ESTAT_ADD(rx_octets);
11801 ESTAT_ADD(rx_fragments);
11802 ESTAT_ADD(rx_ucast_packets);
11803 ESTAT_ADD(rx_mcast_packets);
11804 ESTAT_ADD(rx_bcast_packets);
11805 ESTAT_ADD(rx_fcs_errors);
11806 ESTAT_ADD(rx_align_errors);
11807 ESTAT_ADD(rx_xon_pause_rcvd);
11808 ESTAT_ADD(rx_xoff_pause_rcvd);
11809 ESTAT_ADD(rx_mac_ctrl_rcvd);
11810 ESTAT_ADD(rx_xoff_entered);
11811 ESTAT_ADD(rx_frame_too_long_errors);
11812 ESTAT_ADD(rx_jabbers);
11813 ESTAT_ADD(rx_undersize_packets);
11814 ESTAT_ADD(rx_in_length_errors);
11815 ESTAT_ADD(rx_out_length_errors);
11816 ESTAT_ADD(rx_64_or_less_octet_packets);
11817 ESTAT_ADD(rx_65_to_127_octet_packets);
11818 ESTAT_ADD(rx_128_to_255_octet_packets);
11819 ESTAT_ADD(rx_256_to_511_octet_packets);
11820 ESTAT_ADD(rx_512_to_1023_octet_packets);
11821 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11822 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11823 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11824 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11825 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11826
11827 ESTAT_ADD(tx_octets);
11828 ESTAT_ADD(tx_collisions);
11829 ESTAT_ADD(tx_xon_sent);
11830 ESTAT_ADD(tx_xoff_sent);
11831 ESTAT_ADD(tx_flow_control);
11832 ESTAT_ADD(tx_mac_errors);
11833 ESTAT_ADD(tx_single_collisions);
11834 ESTAT_ADD(tx_mult_collisions);
11835 ESTAT_ADD(tx_deferred);
11836 ESTAT_ADD(tx_excessive_collisions);
11837 ESTAT_ADD(tx_late_collisions);
11838 ESTAT_ADD(tx_collide_2times);
11839 ESTAT_ADD(tx_collide_3times);
11840 ESTAT_ADD(tx_collide_4times);
11841 ESTAT_ADD(tx_collide_5times);
11842 ESTAT_ADD(tx_collide_6times);
11843 ESTAT_ADD(tx_collide_7times);
11844 ESTAT_ADD(tx_collide_8times);
11845 ESTAT_ADD(tx_collide_9times);
11846 ESTAT_ADD(tx_collide_10times);
11847 ESTAT_ADD(tx_collide_11times);
11848 ESTAT_ADD(tx_collide_12times);
11849 ESTAT_ADD(tx_collide_13times);
11850 ESTAT_ADD(tx_collide_14times);
11851 ESTAT_ADD(tx_collide_15times);
11852 ESTAT_ADD(tx_ucast_packets);
11853 ESTAT_ADD(tx_mcast_packets);
11854 ESTAT_ADD(tx_bcast_packets);
11855 ESTAT_ADD(tx_carrier_sense_errors);
11856 ESTAT_ADD(tx_discards);
11857 ESTAT_ADD(tx_errors);
11858
11859 ESTAT_ADD(dma_writeq_full);
11860 ESTAT_ADD(dma_write_prioq_full);
11861 ESTAT_ADD(rxbds_empty);
11862 ESTAT_ADD(rx_discards);
11863 ESTAT_ADD(rx_errors);
11864 ESTAT_ADD(rx_threshold_hit);
11865
11866 ESTAT_ADD(dma_readq_full);
11867 ESTAT_ADD(dma_read_prioq_full);
11868 ESTAT_ADD(tx_comp_queue_full);
11869
11870 ESTAT_ADD(ring_set_send_prod_index);
11871 ESTAT_ADD(ring_status_update);
11872 ESTAT_ADD(nic_irqs);
11873 ESTAT_ADD(nic_avoided_irqs);
11874 ESTAT_ADD(nic_tx_threshold_hit);
11875
Matt Carlson4452d092011-05-19 12:12:51 +000011876 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877}
11878
Matt Carlson65ec6982012-02-28 23:33:37 +000011879static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011880{
Eric Dumazet511d2222010-07-07 20:44:24 +000011881 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011882 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11883
Linus Torvalds1da177e2005-04-16 15:20:36 -070011884 stats->rx_packets = old_stats->rx_packets +
11885 get_stat64(&hw_stats->rx_ucast_packets) +
11886 get_stat64(&hw_stats->rx_mcast_packets) +
11887 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011888
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 stats->tx_packets = old_stats->tx_packets +
11890 get_stat64(&hw_stats->tx_ucast_packets) +
11891 get_stat64(&hw_stats->tx_mcast_packets) +
11892 get_stat64(&hw_stats->tx_bcast_packets);
11893
11894 stats->rx_bytes = old_stats->rx_bytes +
11895 get_stat64(&hw_stats->rx_octets);
11896 stats->tx_bytes = old_stats->tx_bytes +
11897 get_stat64(&hw_stats->tx_octets);
11898
11899 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011900 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011901 stats->tx_errors = old_stats->tx_errors +
11902 get_stat64(&hw_stats->tx_errors) +
11903 get_stat64(&hw_stats->tx_mac_errors) +
11904 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11905 get_stat64(&hw_stats->tx_discards);
11906
11907 stats->multicast = old_stats->multicast +
11908 get_stat64(&hw_stats->rx_mcast_packets);
11909 stats->collisions = old_stats->collisions +
11910 get_stat64(&hw_stats->tx_collisions);
11911
11912 stats->rx_length_errors = old_stats->rx_length_errors +
11913 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11914 get_stat64(&hw_stats->rx_undersize_packets);
11915
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 stats->rx_frame_errors = old_stats->rx_frame_errors +
11917 get_stat64(&hw_stats->rx_align_errors);
11918 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11919 get_stat64(&hw_stats->tx_discards);
11920 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11921 get_stat64(&hw_stats->tx_carrier_sense_errors);
11922
11923 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011924 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011925
John W. Linville4f63b872005-09-12 14:43:18 -070011926 stats->rx_missed_errors = old_stats->rx_missed_errors +
11927 get_stat64(&hw_stats->rx_discards);
11928
Eric Dumazetb0057c52010-10-10 19:55:52 +000011929 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011930 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011931}
11932
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933static int tg3_get_regs_len(struct net_device *dev)
11934{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011935 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936}
11937
11938static void tg3_get_regs(struct net_device *dev,
11939 struct ethtool_regs *regs, void *_p)
11940{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
11943 regs->version = 0;
11944
Matt Carlson97bd8e42011-04-13 11:05:04 +000011945 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946
Matt Carlson80096062010-08-02 11:26:06 +000011947 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011948 return;
11949
David S. Millerf47c11e2005-06-24 20:18:35 -070011950 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951
Matt Carlson97bd8e42011-04-13 11:05:04 +000011952 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953
David S. Millerf47c11e2005-06-24 20:18:35 -070011954 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955}
11956
11957static int tg3_get_eeprom_len(struct net_device *dev)
11958{
11959 struct tg3 *tp = netdev_priv(dev);
11960
11961 return tp->nvram_size;
11962}
11963
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11965{
11966 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011967 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011969 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011970 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971
Joe Perches63c3a662011-04-26 08:12:10 +000011972 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011973 return -EINVAL;
11974
Linus Torvalds1da177e2005-04-16 15:20:36 -070011975 offset = eeprom->offset;
11976 len = eeprom->len;
11977 eeprom->len = 0;
11978
11979 eeprom->magic = TG3_EEPROM_MAGIC;
11980
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011981 /* Override clock, link aware and link idle modes */
11982 if (tg3_flag(tp, CPMU_PRESENT)) {
11983 cpmu_val = tr32(TG3_CPMU_CTRL);
11984 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11985 CPMU_CTRL_LINK_IDLE_MODE)) {
11986 tw32(TG3_CPMU_CTRL, cpmu_val &
11987 ~(CPMU_CTRL_LINK_AWARE_MODE |
11988 CPMU_CTRL_LINK_IDLE_MODE));
11989 cpmu_restore = 1;
11990 }
11991 }
11992 tg3_override_clk(tp);
11993
Linus Torvalds1da177e2005-04-16 15:20:36 -070011994 if (offset & 3) {
11995 /* adjustments to start on required 4 byte boundary */
11996 b_offset = offset & 3;
11997 b_count = 4 - b_offset;
11998 if (b_count > len) {
11999 /* i.e. offset=1 len=2 */
12000 b_count = len;
12001 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000012002 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012004 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000012005 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012006 len -= b_count;
12007 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012008 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 }
12010
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012011 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012012 pd = &data[eeprom->len];
12013 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012014 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012015 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012016 if (i)
12017 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012019 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012021 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012022 if (need_resched()) {
12023 if (signal_pending(current)) {
12024 eeprom->len += i;
12025 ret = -EINTR;
12026 goto eeprom_done;
12027 }
12028 cond_resched();
12029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012030 }
12031 eeprom->len += i;
12032
12033 if (len & 3) {
12034 /* read last bytes not ending on 4 byte boundary */
12035 pd = &data[eeprom->len];
12036 b_count = len & 3;
12037 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012038 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012039 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012040 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080012041 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012042 eeprom->len += b_count;
12043 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012044 ret = 0;
12045
12046eeprom_done:
12047 /* Restore clock, link aware and link idle modes */
12048 tg3_restore_clk(tp);
12049 if (cpmu_restore)
12050 tw32(TG3_CPMU_CTRL, cpmu_val);
12051
12052 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053}
12054
Linus Torvalds1da177e2005-04-16 15:20:36 -070012055static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12056{
12057 struct tg3 *tp = netdev_priv(dev);
12058 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012059 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060 u8 *buf;
Arnd Bergmanne434e042016-01-29 12:39:15 +010012061 __be32 start = 0, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012062
Joe Perches63c3a662011-04-26 08:12:10 +000012063 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012064 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012065 return -EINVAL;
12066
12067 offset = eeprom->offset;
12068 len = eeprom->len;
12069
12070 if ((b_offset = (offset & 3))) {
12071 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012072 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012073 if (ret)
12074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012075 len += b_offset;
12076 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070012077 if (len < 4)
12078 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079 }
12080
12081 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070012082 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 /* adjustments to end on required 4 byte boundary */
12084 odd_len = 1;
12085 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012086 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087 if (ret)
12088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089 }
12090
12091 buf = data;
12092 if (b_offset || odd_len) {
12093 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012094 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095 return -ENOMEM;
12096 if (b_offset)
12097 memcpy(buf, &start, 4);
12098 if (odd_len)
12099 memcpy(buf+len-4, &end, 4);
12100 memcpy(buf + b_offset, data, eeprom->len);
12101 }
12102
12103 ret = tg3_nvram_write_block(tp, offset, len, buf);
12104
12105 if (buf != data)
12106 kfree(buf);
12107
12108 return ret;
12109}
12110
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012111static int tg3_get_link_ksettings(struct net_device *dev,
12112 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012114 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012115 u32 supported, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012116
Joe Perches63c3a662011-04-26 08:12:10 +000012117 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012118 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012119 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012120 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012121 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +030012122 phy_ethtool_ksettings_get(phydev, cmd);
12123
12124 return 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012125 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012126
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012127 supported = (SUPPORTED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012128
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012129 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012130 supported |= (SUPPORTED_1000baseT_Half |
12131 SUPPORTED_1000baseT_Full);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012133 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012134 supported |= (SUPPORTED_100baseT_Half |
12135 SUPPORTED_100baseT_Full |
12136 SUPPORTED_10baseT_Half |
12137 SUPPORTED_10baseT_Full |
12138 SUPPORTED_TP);
12139 cmd->base.port = PORT_TP;
Karsten Keilef348142006-05-12 12:49:08 -070012140 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012141 supported |= SUPPORTED_FIBRE;
12142 cmd->base.port = PORT_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012143 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012144 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
12145 supported);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012146
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012147 advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012148 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12149 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12150 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012151 advertising |= ADVERTISED_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012152 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012153 advertising |= ADVERTISED_Pause |
12154 ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012155 }
12156 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012157 advertising |= ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012158 }
12159 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012160 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
12161 advertising);
12162
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012163 if (netif_running(dev) && tp->link_up) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012164 cmd->base.speed = tp->link_config.active_speed;
12165 cmd->base.duplex = tp->link_config.active_duplex;
12166 ethtool_convert_legacy_u32_to_link_mode(
12167 cmd->link_modes.lp_advertising,
12168 tp->link_config.rmt_adv);
12169
Matt Carlsone348c5e2011-11-21 15:01:20 +000012170 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12171 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012172 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012173 else
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012174 cmd->base.eth_tp_mdix = ETH_TP_MDI;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012175 }
Matt Carlson64c22182010-10-14 10:37:44 +000012176 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012177 cmd->base.speed = SPEED_UNKNOWN;
12178 cmd->base.duplex = DUPLEX_UNKNOWN;
12179 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012180 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012181 cmd->base.phy_address = tp->phy_addr;
12182 cmd->base.autoneg = tp->link_config.autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012183 return 0;
12184}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012185
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012186static int tg3_set_link_ksettings(struct net_device *dev,
12187 const struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012188{
12189 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012190 u32 speed = cmd->base.speed;
12191 u32 advertising;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012192
Joe Perches63c3a662011-04-26 08:12:10 +000012193 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012194 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012195 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012196 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012197 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012198 return phy_ethtool_ksettings_set(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012199 }
12200
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012201 if (cmd->base.autoneg != AUTONEG_ENABLE &&
12202 cmd->base.autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012203 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012204
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012205 if (cmd->base.autoneg == AUTONEG_DISABLE &&
12206 cmd->base.duplex != DUPLEX_FULL &&
12207 cmd->base.duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012208 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012209
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012210 ethtool_convert_link_mode_to_legacy_u32(&advertising,
12211 cmd->link_modes.advertising);
12212
12213 if (cmd->base.autoneg == AUTONEG_ENABLE) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000012214 u32 mask = ADVERTISED_Autoneg |
12215 ADVERTISED_Pause |
12216 ADVERTISED_Asym_Pause;
12217
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012218 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012219 mask |= ADVERTISED_1000baseT_Half |
12220 ADVERTISED_1000baseT_Full;
12221
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012222 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012223 mask |= ADVERTISED_100baseT_Half |
12224 ADVERTISED_100baseT_Full |
12225 ADVERTISED_10baseT_Half |
12226 ADVERTISED_10baseT_Full |
12227 ADVERTISED_TP;
12228 else
12229 mask |= ADVERTISED_FIBRE;
12230
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012231 if (advertising & ~mask)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012232 return -EINVAL;
12233
12234 mask &= (ADVERTISED_1000baseT_Half |
12235 ADVERTISED_1000baseT_Full |
12236 ADVERTISED_100baseT_Half |
12237 ADVERTISED_100baseT_Full |
12238 ADVERTISED_10baseT_Half |
12239 ADVERTISED_10baseT_Full);
12240
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012241 advertising &= mask;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012242 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012243 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012244 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012245 return -EINVAL;
12246
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012247 if (cmd->base.duplex != DUPLEX_FULL)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012248 return -EINVAL;
12249 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012250 if (speed != SPEED_100 &&
12251 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012252 return -EINVAL;
12253 }
12254 }
12255
David S. Millerf47c11e2005-06-24 20:18:35 -070012256 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012257
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012258 tp->link_config.autoneg = cmd->base.autoneg;
12259 if (cmd->base.autoneg == AUTONEG_ENABLE) {
12260 tp->link_config.advertising = (advertising |
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012261 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012262 tp->link_config.speed = SPEED_UNKNOWN;
12263 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012264 } else {
12265 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012266 tp->link_config.speed = speed;
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012267 tp->link_config.duplex = cmd->base.duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012268 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012269
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012270 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12271
Nithin Sujirce20f162013-04-09 08:48:04 +000012272 tg3_warn_mgmt_link_flap(tp);
12273
Linus Torvalds1da177e2005-04-16 15:20:36 -070012274 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012275 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276
David S. Millerf47c11e2005-06-24 20:18:35 -070012277 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012278
Linus Torvalds1da177e2005-04-16 15:20:36 -070012279 return 0;
12280}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012281
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12283{
12284 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012285
Rick Jones68aad782011-11-07 13:29:27 +000012286 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12287 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12288 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12289 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012291
Linus Torvalds1da177e2005-04-16 15:20:36 -070012292static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12293{
12294 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012295
Joe Perches63c3a662011-04-26 08:12:10 +000012296 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012297 wol->supported = WAKE_MAGIC;
12298 else
12299 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012300 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012301 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012302 wol->wolopts = WAKE_MAGIC;
12303 memset(&wol->sopass, 0, sizeof(wol->sopass));
12304}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012305
Linus Torvalds1da177e2005-04-16 15:20:36 -070012306static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12307{
12308 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012309 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012310
Linus Torvalds1da177e2005-04-16 15:20:36 -070012311 if (wol->wolopts & ~WAKE_MAGIC)
12312 return -EINVAL;
12313 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012314 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012315 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012316
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012317 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12318
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012319 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012320 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012321 else
Joe Perches63c3a662011-04-26 08:12:10 +000012322 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012323
Linus Torvalds1da177e2005-04-16 15:20:36 -070012324 return 0;
12325}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012326
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327static u32 tg3_get_msglevel(struct net_device *dev)
12328{
12329 struct tg3 *tp = netdev_priv(dev);
12330 return tp->msg_enable;
12331}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012332
Linus Torvalds1da177e2005-04-16 15:20:36 -070012333static void tg3_set_msglevel(struct net_device *dev, u32 value)
12334{
12335 struct tg3 *tp = netdev_priv(dev);
12336 tp->msg_enable = value;
12337}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012338
Linus Torvalds1da177e2005-04-16 15:20:36 -070012339static int tg3_nway_reset(struct net_device *dev)
12340{
12341 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012343
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344 if (!netif_running(dev))
12345 return -EAGAIN;
12346
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012347 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012348 return -EINVAL;
12349
Nithin Sujirce20f162013-04-09 08:48:04 +000012350 tg3_warn_mgmt_link_flap(tp);
12351
Joe Perches63c3a662011-04-26 08:12:10 +000012352 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012353 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012354 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012355 r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012356 } else {
12357 u32 bmcr;
12358
12359 spin_lock_bh(&tp->lock);
12360 r = -EINVAL;
12361 tg3_readphy(tp, MII_BMCR, &bmcr);
12362 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12363 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012364 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012365 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12366 BMCR_ANENABLE);
12367 r = 0;
12368 }
12369 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012371
Linus Torvalds1da177e2005-04-16 15:20:36 -070012372 return r;
12373}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012374
Linus Torvalds1da177e2005-04-16 15:20:36 -070012375static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12376{
12377 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012378
Matt Carlson2c49a442010-09-30 10:34:35 +000012379 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012380 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012381 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012382 else
12383 ering->rx_jumbo_max_pending = 0;
12384
12385 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386
12387 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012388 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012389 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12390 else
12391 ering->rx_jumbo_pending = 0;
12392
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012393 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012394}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012395
Linus Torvalds1da177e2005-04-16 15:20:36 -070012396static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12397{
12398 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012399 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012400
Matt Carlson2c49a442010-09-30 10:34:35 +000012401 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12402 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012403 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12404 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012405 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012406 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012407 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012408
Michael Chanbbe832c2005-06-24 20:20:04 -070012409 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012410 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012412 irq_sync = 1;
12413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414
Michael Chanbbe832c2005-06-24 20:20:04 -070012415 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012416
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 tp->rx_pending = ering->rx_pending;
12418
Joe Perches63c3a662011-04-26 08:12:10 +000012419 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 tp->rx_pending > 63)
12421 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012422
12423 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12424 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012425
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012426 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012427 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012428
12429 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012430 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012431 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012432 if (!err)
12433 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434 }
12435
David S. Millerf47c11e2005-06-24 20:18:35 -070012436 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012437
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012438 if (irq_sync && !err)
12439 tg3_phy_start(tp);
12440
Michael Chanb9ec6c12006-07-25 16:37:27 -070012441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012443
Linus Torvalds1da177e2005-04-16 15:20:36 -070012444static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12445{
12446 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012447
Joe Perches63c3a662011-04-26 08:12:10 +000012448 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012449
Matt Carlson4a2db502011-12-08 14:40:17 +000012450 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012451 epause->rx_pause = 1;
12452 else
12453 epause->rx_pause = 0;
12454
Matt Carlson4a2db502011-12-08 14:40:17 +000012455 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012456 epause->tx_pause = 1;
12457 else
12458 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012459}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012460
Linus Torvalds1da177e2005-04-16 15:20:36 -070012461static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12462{
12463 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012464 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012465
Nithin Sujirce20f162013-04-09 08:48:04 +000012466 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12467 tg3_warn_mgmt_link_flap(tp);
12468
Joe Perches63c3a662011-04-26 08:12:10 +000012469 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012470 u32 newadv;
12471 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012472
Andrew Lunn7f854422016-01-06 20:11:18 +010012473 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012474
Matt Carlson27121682010-02-17 15:16:57 +000012475 if (!(phydev->supported & SUPPORTED_Pause) ||
12476 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012477 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012478 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012479
Matt Carlson27121682010-02-17 15:16:57 +000012480 tp->link_config.flowctrl = 0;
12481 if (epause->rx_pause) {
12482 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012483
Matt Carlson27121682010-02-17 15:16:57 +000012484 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012485 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012486 newadv = ADVERTISED_Pause;
12487 } else
12488 newadv = ADVERTISED_Pause |
12489 ADVERTISED_Asym_Pause;
12490 } else if (epause->tx_pause) {
12491 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12492 newadv = ADVERTISED_Asym_Pause;
12493 } else
12494 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012495
Matt Carlson27121682010-02-17 15:16:57 +000012496 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012497 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012498 else
Joe Perches63c3a662011-04-26 08:12:10 +000012499 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012500
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012501 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012502 u32 oldadv = phydev->advertising &
12503 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12504 if (oldadv != newadv) {
12505 phydev->advertising &=
12506 ~(ADVERTISED_Pause |
12507 ADVERTISED_Asym_Pause);
12508 phydev->advertising |= newadv;
12509 if (phydev->autoneg) {
12510 /*
12511 * Always renegotiate the link to
12512 * inform our link partner of our
12513 * flow control settings, even if the
12514 * flow control is forced. Let
12515 * tg3_adjust_link() do the final
12516 * flow control setup.
12517 */
12518 return phy_start_aneg(phydev);
12519 }
12520 }
12521
12522 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012523 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012524 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012525 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012526 ~(ADVERTISED_Pause |
12527 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012528 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012529 }
12530 } else {
12531 int irq_sync = 0;
12532
12533 if (netif_running(dev)) {
12534 tg3_netif_stop(tp);
12535 irq_sync = 1;
12536 }
12537
12538 tg3_full_lock(tp, irq_sync);
12539
12540 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012541 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012542 else
Joe Perches63c3a662011-04-26 08:12:10 +000012543 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012544 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012545 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012546 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012547 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012548 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012549 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012550 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012551 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012552
12553 if (netif_running(dev)) {
12554 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012555 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012556 if (!err)
12557 tg3_netif_start(tp);
12558 }
12559
12560 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012562
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012563 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12564
Michael Chanb9ec6c12006-07-25 16:37:27 -070012565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012567
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012568static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012570 switch (sset) {
12571 case ETH_SS_TEST:
12572 return TG3_NUM_TEST;
12573 case ETH_SS_STATS:
12574 return TG3_NUM_STATS;
12575 default:
12576 return -EOPNOTSUPP;
12577 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012578}
12579
Matt Carlson90415472011-12-16 13:33:23 +000012580static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12581 u32 *rules __always_unused)
12582{
12583 struct tg3 *tp = netdev_priv(dev);
12584
12585 if (!tg3_flag(tp, SUPPORT_MSIX))
12586 return -EOPNOTSUPP;
12587
12588 switch (info->cmd) {
12589 case ETHTOOL_GRXRINGS:
12590 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012591 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012592 else {
12593 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012594 if (info->data > TG3_RSS_MAX_NUM_QS)
12595 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012596 }
12597
Matt Carlson90415472011-12-16 13:33:23 +000012598 return 0;
12599
12600 default:
12601 return -EOPNOTSUPP;
12602 }
12603}
12604
12605static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12606{
12607 u32 size = 0;
12608 struct tg3 *tp = netdev_priv(dev);
12609
12610 if (tg3_flag(tp, SUPPORT_MSIX))
12611 size = TG3_RSS_INDIR_TBL_SIZE;
12612
12613 return size;
12614}
12615
Eyal Perry892311f2014-12-02 18:12:10 +020012616static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012617{
12618 struct tg3 *tp = netdev_priv(dev);
12619 int i;
12620
Eyal Perry892311f2014-12-02 18:12:10 +020012621 if (hfunc)
12622 *hfunc = ETH_RSS_HASH_TOP;
12623 if (!indir)
12624 return 0;
12625
Matt Carlson90415472011-12-16 13:33:23 +000012626 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12627 indir[i] = tp->rss_ind_tbl[i];
12628
12629 return 0;
12630}
12631
Eyal Perry892311f2014-12-02 18:12:10 +020012632static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
12633 const u8 hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012634{
12635 struct tg3 *tp = netdev_priv(dev);
12636 size_t i;
12637
Eyal Perry892311f2014-12-02 18:12:10 +020012638 /* We require at least one supported parameter to be changed and no
12639 * change in any of the unsupported parameters
12640 */
12641 if (key ||
12642 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
12643 return -EOPNOTSUPP;
12644
12645 if (!indir)
12646 return 0;
12647
Matt Carlson90415472011-12-16 13:33:23 +000012648 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12649 tp->rss_ind_tbl[i] = indir[i];
12650
12651 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12652 return 0;
12653
12654 /* It is legal to write the indirection
12655 * table while the device is running.
12656 */
12657 tg3_full_lock(tp, 0);
12658 tg3_rss_write_indir_tbl(tp);
12659 tg3_full_unlock(tp);
12660
12661 return 0;
12662}
12663
Michael Chan09681692012-09-28 07:12:42 +000012664static void tg3_get_channels(struct net_device *dev,
12665 struct ethtool_channels *channel)
12666{
12667 struct tg3 *tp = netdev_priv(dev);
12668 u32 deflt_qs = netif_get_num_default_rss_queues();
12669
12670 channel->max_rx = tp->rxq_max;
12671 channel->max_tx = tp->txq_max;
12672
12673 if (netif_running(dev)) {
12674 channel->rx_count = tp->rxq_cnt;
12675 channel->tx_count = tp->txq_cnt;
12676 } else {
12677 if (tp->rxq_req)
12678 channel->rx_count = tp->rxq_req;
12679 else
12680 channel->rx_count = min(deflt_qs, tp->rxq_max);
12681
12682 if (tp->txq_req)
12683 channel->tx_count = tp->txq_req;
12684 else
12685 channel->tx_count = min(deflt_qs, tp->txq_max);
12686 }
12687}
12688
12689static int tg3_set_channels(struct net_device *dev,
12690 struct ethtool_channels *channel)
12691{
12692 struct tg3 *tp = netdev_priv(dev);
12693
12694 if (!tg3_flag(tp, SUPPORT_MSIX))
12695 return -EOPNOTSUPP;
12696
12697 if (channel->rx_count > tp->rxq_max ||
12698 channel->tx_count > tp->txq_max)
12699 return -EINVAL;
12700
12701 tp->rxq_req = channel->rx_count;
12702 tp->txq_req = channel->tx_count;
12703
12704 if (!netif_running(dev))
12705 return 0;
12706
12707 tg3_stop(tp);
12708
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012709 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012710
Matt Carlsonbe947302012-12-03 19:36:57 +000012711 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012712
12713 return 0;
12714}
12715
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012716static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012717{
12718 switch (stringset) {
12719 case ETH_SS_STATS:
12720 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12721 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012722 case ETH_SS_TEST:
12723 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725 default:
12726 WARN_ON(1); /* we need a WARN() */
12727 break;
12728 }
12729}
12730
stephen hemminger81b87092011-04-04 08:43:50 +000012731static int tg3_set_phys_id(struct net_device *dev,
12732 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012733{
12734 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012735
12736 if (!netif_running(tp->dev))
12737 return -EAGAIN;
12738
stephen hemminger81b87092011-04-04 08:43:50 +000012739 switch (state) {
12740 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012741 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012742
stephen hemminger81b87092011-04-04 08:43:50 +000012743 case ETHTOOL_ID_ON:
12744 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12745 LED_CTRL_1000MBPS_ON |
12746 LED_CTRL_100MBPS_ON |
12747 LED_CTRL_10MBPS_ON |
12748 LED_CTRL_TRAFFIC_OVERRIDE |
12749 LED_CTRL_TRAFFIC_BLINK |
12750 LED_CTRL_TRAFFIC_LED);
12751 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012752
stephen hemminger81b87092011-04-04 08:43:50 +000012753 case ETHTOOL_ID_OFF:
12754 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12755 LED_CTRL_TRAFFIC_OVERRIDE);
12756 break;
Michael Chan4009a932005-09-05 17:52:54 -070012757
stephen hemminger81b87092011-04-04 08:43:50 +000012758 case ETHTOOL_ID_INACTIVE:
12759 tw32(MAC_LED_CTRL, tp->led_ctrl);
12760 break;
Michael Chan4009a932005-09-05 17:52:54 -070012761 }
stephen hemminger81b87092011-04-04 08:43:50 +000012762
Michael Chan4009a932005-09-05 17:52:54 -070012763 return 0;
12764}
12765
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012766static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012767 struct ethtool_stats *estats, u64 *tmp_stats)
12768{
12769 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012770
Matt Carlsonb546e462012-02-13 15:20:09 +000012771 if (tp->hw_stats)
12772 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12773 else
12774 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775}
12776
Matt Carlson535a4902011-07-20 10:20:56 +000012777static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012778{
12779 int i;
12780 __be32 *buf;
12781 u32 offset = 0, len = 0;
12782 u32 magic, val;
12783
Joe Perches63c3a662011-04-26 08:12:10 +000012784 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012785 return NULL;
12786
12787 if (magic == TG3_EEPROM_MAGIC) {
12788 for (offset = TG3_NVM_DIR_START;
12789 offset < TG3_NVM_DIR_END;
12790 offset += TG3_NVM_DIRENT_SIZE) {
12791 if (tg3_nvram_read(tp, offset, &val))
12792 return NULL;
12793
12794 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12795 TG3_NVM_DIRTYPE_EXTVPD)
12796 break;
12797 }
12798
12799 if (offset != TG3_NVM_DIR_END) {
12800 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12801 if (tg3_nvram_read(tp, offset + 4, &offset))
12802 return NULL;
12803
12804 offset = tg3_nvram_logical_addr(tp, offset);
12805 }
12806 }
12807
12808 if (!offset || !len) {
12809 offset = TG3_NVM_VPD_OFF;
12810 len = TG3_NVM_VPD_LEN;
12811 }
12812
12813 buf = kmalloc(len, GFP_KERNEL);
12814 if (buf == NULL)
12815 return NULL;
12816
12817 if (magic == TG3_EEPROM_MAGIC) {
12818 for (i = 0; i < len; i += 4) {
12819 /* The data is in little-endian format in NVRAM.
12820 * Use the big-endian read routines to preserve
12821 * the byte order as it exists in NVRAM.
12822 */
12823 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12824 goto error;
12825 }
12826 } else {
12827 u8 *ptr;
12828 ssize_t cnt;
12829 unsigned int pos = 0;
12830
12831 ptr = (u8 *)&buf[0];
12832 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12833 cnt = pci_read_vpd(tp->pdev, pos,
12834 len - pos, ptr);
12835 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12836 cnt = 0;
12837 else if (cnt < 0)
12838 goto error;
12839 }
12840 if (pos != len)
12841 goto error;
12842 }
12843
Matt Carlson535a4902011-07-20 10:20:56 +000012844 *vpdlen = len;
12845
Matt Carlsonc3e94502011-04-13 11:05:08 +000012846 return buf;
12847
12848error:
12849 kfree(buf);
12850 return NULL;
12851}
12852
Michael Chan566f86a2005-05-29 14:56:58 -070012853#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012854#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12855#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12856#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012857#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12858#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012859#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012860#define NVRAM_SELFBOOT_HW_SIZE 0x20
12861#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012862
12863static int tg3_test_nvram(struct tg3 *tp)
12864{
Matt Carlson535a4902011-07-20 10:20:56 +000012865 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012866 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012867 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012868
Joe Perches63c3a662011-04-26 08:12:10 +000012869 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012870 return 0;
12871
Matt Carlsone4f34112009-02-25 14:25:00 +000012872 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012873 return -EIO;
12874
Michael Chan1b277772006-03-20 22:27:48 -080012875 if (magic == TG3_EEPROM_MAGIC)
12876 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012877 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012878 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12879 TG3_EEPROM_SB_FORMAT_1) {
12880 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12881 case TG3_EEPROM_SB_REVISION_0:
12882 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12883 break;
12884 case TG3_EEPROM_SB_REVISION_2:
12885 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12886 break;
12887 case TG3_EEPROM_SB_REVISION_3:
12888 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12889 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012890 case TG3_EEPROM_SB_REVISION_4:
12891 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12892 break;
12893 case TG3_EEPROM_SB_REVISION_5:
12894 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12895 break;
12896 case TG3_EEPROM_SB_REVISION_6:
12897 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12898 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012899 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012900 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012901 }
12902 } else
Michael Chan1b277772006-03-20 22:27:48 -080012903 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012904 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12905 size = NVRAM_SELFBOOT_HW_SIZE;
12906 else
Michael Chan1b277772006-03-20 22:27:48 -080012907 return -EIO;
12908
12909 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012910 if (buf == NULL)
12911 return -ENOMEM;
12912
Michael Chan1b277772006-03-20 22:27:48 -080012913 err = -EIO;
12914 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012915 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12916 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012917 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012918 }
Michael Chan1b277772006-03-20 22:27:48 -080012919 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012920 goto out;
12921
Michael Chan1b277772006-03-20 22:27:48 -080012922 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012923 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012924 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012925 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012926 u8 *buf8 = (u8 *) buf, csum8 = 0;
12927
Al Virob9fc7dc2007-12-17 22:59:57 -080012928 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012929 TG3_EEPROM_SB_REVISION_2) {
12930 /* For rev 2, the csum doesn't include the MBA. */
12931 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12932 csum8 += buf8[i];
12933 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12934 csum8 += buf8[i];
12935 } else {
12936 for (i = 0; i < size; i++)
12937 csum8 += buf8[i];
12938 }
Michael Chan1b277772006-03-20 22:27:48 -080012939
Adrian Bunkad96b482006-04-05 22:21:04 -070012940 if (csum8 == 0) {
12941 err = 0;
12942 goto out;
12943 }
12944
12945 err = -EIO;
12946 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012947 }
Michael Chan566f86a2005-05-29 14:56:58 -070012948
Al Virob9fc7dc2007-12-17 22:59:57 -080012949 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012950 TG3_EEPROM_MAGIC_HW) {
12951 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012952 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012953 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012954
12955 /* Separate the parity bits and the data bytes. */
12956 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12957 if ((i == 0) || (i == 8)) {
12958 int l;
12959 u8 msk;
12960
12961 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12962 parity[k++] = buf8[i] & msk;
12963 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012964 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012965 int l;
12966 u8 msk;
12967
12968 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12969 parity[k++] = buf8[i] & msk;
12970 i++;
12971
12972 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12973 parity[k++] = buf8[i] & msk;
12974 i++;
12975 }
12976 data[j++] = buf8[i];
12977 }
12978
12979 err = -EIO;
12980 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12981 u8 hw8 = hweight8(data[i]);
12982
12983 if ((hw8 & 0x1) && parity[i])
12984 goto out;
12985 else if (!(hw8 & 0x1) && !parity[i])
12986 goto out;
12987 }
12988 err = 0;
12989 goto out;
12990 }
12991
Matt Carlson01c3a392011-03-09 16:58:20 +000012992 err = -EIO;
12993
Michael Chan566f86a2005-05-29 14:56:58 -070012994 /* Bootstrap checksum at offset 0x10 */
12995 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012996 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012997 goto out;
12998
12999 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
13000 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000013001 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000013002 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070013003
Matt Carlsonc3e94502011-04-13 11:05:08 +000013004 kfree(buf);
13005
Matt Carlson535a4902011-07-20 10:20:56 +000013006 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000013007 if (!buf)
13008 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000013009
Matt Carlson535a4902011-07-20 10:20:56 +000013010 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000013011 if (i > 0) {
13012 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
13013 if (j < 0)
13014 goto out;
13015
Matt Carlson535a4902011-07-20 10:20:56 +000013016 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000013017 goto out;
13018
13019 i += PCI_VPD_LRDT_TAG_SIZE;
13020 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
13021 PCI_VPD_RO_KEYWORD_CHKSUM);
13022 if (j > 0) {
13023 u8 csum8 = 0;
13024
13025 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13026
13027 for (i = 0; i <= j; i++)
13028 csum8 += ((u8 *)buf)[i];
13029
13030 if (csum8)
13031 goto out;
13032 }
13033 }
13034
Michael Chan566f86a2005-05-29 14:56:58 -070013035 err = 0;
13036
13037out:
13038 kfree(buf);
13039 return err;
13040}
13041
Michael Chanca430072005-05-29 14:57:23 -070013042#define TG3_SERDES_TIMEOUT_SEC 2
13043#define TG3_COPPER_TIMEOUT_SEC 6
13044
13045static int tg3_test_link(struct tg3 *tp)
13046{
13047 int i, max;
13048
13049 if (!netif_running(tp->dev))
13050 return -ENODEV;
13051
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013052 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070013053 max = TG3_SERDES_TIMEOUT_SEC;
13054 else
13055 max = TG3_COPPER_TIMEOUT_SEC;
13056
13057 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000013058 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070013059 return 0;
13060
13061 if (msleep_interruptible(1000))
13062 break;
13063 }
13064
13065 return -EIO;
13066}
13067
Michael Chana71116d2005-05-29 14:58:11 -070013068/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013069static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013070{
Michael Chanb16250e2006-09-27 16:10:14 -070013071 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013072 u32 offset, read_mask, write_mask, val, save_val, read_val;
13073 static struct {
13074 u16 offset;
13075 u16 flags;
13076#define TG3_FL_5705 0x1
13077#define TG3_FL_NOT_5705 0x2
13078#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013079#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013080 u32 read_mask;
13081 u32 write_mask;
13082 } reg_tbl[] = {
13083 /* MAC Control Registers */
13084 { MAC_MODE, TG3_FL_NOT_5705,
13085 0x00000000, 0x00ef6f8c },
13086 { MAC_MODE, TG3_FL_5705,
13087 0x00000000, 0x01ef6b8c },
13088 { MAC_STATUS, TG3_FL_NOT_5705,
13089 0x03800107, 0x00000000 },
13090 { MAC_STATUS, TG3_FL_5705,
13091 0x03800100, 0x00000000 },
13092 { MAC_ADDR_0_HIGH, 0x0000,
13093 0x00000000, 0x0000ffff },
13094 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013095 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013096 { MAC_RX_MTU_SIZE, 0x0000,
13097 0x00000000, 0x0000ffff },
13098 { MAC_TX_MODE, 0x0000,
13099 0x00000000, 0x00000070 },
13100 { MAC_TX_LENGTHS, 0x0000,
13101 0x00000000, 0x00003fff },
13102 { MAC_RX_MODE, TG3_FL_NOT_5705,
13103 0x00000000, 0x000007fc },
13104 { MAC_RX_MODE, TG3_FL_5705,
13105 0x00000000, 0x000007dc },
13106 { MAC_HASH_REG_0, 0x0000,
13107 0x00000000, 0xffffffff },
13108 { MAC_HASH_REG_1, 0x0000,
13109 0x00000000, 0xffffffff },
13110 { MAC_HASH_REG_2, 0x0000,
13111 0x00000000, 0xffffffff },
13112 { MAC_HASH_REG_3, 0x0000,
13113 0x00000000, 0xffffffff },
13114
13115 /* Receive Data and Receive BD Initiator Control Registers. */
13116 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13117 0x00000000, 0xffffffff },
13118 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13119 0x00000000, 0xffffffff },
13120 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13121 0x00000000, 0x00000003 },
13122 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13123 0x00000000, 0xffffffff },
13124 { RCVDBDI_STD_BD+0, 0x0000,
13125 0x00000000, 0xffffffff },
13126 { RCVDBDI_STD_BD+4, 0x0000,
13127 0x00000000, 0xffffffff },
13128 { RCVDBDI_STD_BD+8, 0x0000,
13129 0x00000000, 0xffff0002 },
13130 { RCVDBDI_STD_BD+0xc, 0x0000,
13131 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013132
Michael Chana71116d2005-05-29 14:58:11 -070013133 /* Receive BD Initiator Control Registers. */
13134 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13135 0x00000000, 0xffffffff },
13136 { RCVBDI_STD_THRESH, TG3_FL_5705,
13137 0x00000000, 0x000003ff },
13138 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13139 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013140
Michael Chana71116d2005-05-29 14:58:11 -070013141 /* Host Coalescing Control Registers. */
13142 { HOSTCC_MODE, TG3_FL_NOT_5705,
13143 0x00000000, 0x00000004 },
13144 { HOSTCC_MODE, TG3_FL_5705,
13145 0x00000000, 0x000000f6 },
13146 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13147 0x00000000, 0xffffffff },
13148 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13149 0x00000000, 0x000003ff },
13150 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13151 0x00000000, 0xffffffff },
13152 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13153 0x00000000, 0x000003ff },
13154 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13155 0x00000000, 0xffffffff },
13156 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13157 0x00000000, 0x000000ff },
13158 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13159 0x00000000, 0xffffffff },
13160 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13161 0x00000000, 0x000000ff },
13162 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13163 0x00000000, 0xffffffff },
13164 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13165 0x00000000, 0xffffffff },
13166 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13167 0x00000000, 0xffffffff },
13168 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13169 0x00000000, 0x000000ff },
13170 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13171 0x00000000, 0xffffffff },
13172 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13173 0x00000000, 0x000000ff },
13174 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13175 0x00000000, 0xffffffff },
13176 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13177 0x00000000, 0xffffffff },
13178 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13179 0x00000000, 0xffffffff },
13180 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13181 0x00000000, 0xffffffff },
13182 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13183 0x00000000, 0xffffffff },
13184 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13185 0xffffffff, 0x00000000 },
13186 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13187 0xffffffff, 0x00000000 },
13188
13189 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013190 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013191 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013192 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013193 0x00000000, 0x007fffff },
13194 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13195 0x00000000, 0x0000003f },
13196 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13197 0x00000000, 0x000001ff },
13198 { BUFMGR_MB_HIGH_WATER, 0x0000,
13199 0x00000000, 0x000001ff },
13200 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13201 0xffffffff, 0x00000000 },
13202 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13203 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013204
Michael Chana71116d2005-05-29 14:58:11 -070013205 /* Mailbox Registers */
13206 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13207 0x00000000, 0x000001ff },
13208 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13209 0x00000000, 0x000001ff },
13210 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13211 0x00000000, 0x000007ff },
13212 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13213 0x00000000, 0x000001ff },
13214
13215 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13216 };
13217
Michael Chanb16250e2006-09-27 16:10:14 -070013218 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013219 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013220 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013221 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013222 is_5750 = 1;
13223 }
Michael Chana71116d2005-05-29 14:58:11 -070013224
13225 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13226 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13227 continue;
13228
13229 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13230 continue;
13231
Joe Perches63c3a662011-04-26 08:12:10 +000013232 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013233 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13234 continue;
13235
Michael Chanb16250e2006-09-27 16:10:14 -070013236 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13237 continue;
13238
Michael Chana71116d2005-05-29 14:58:11 -070013239 offset = (u32) reg_tbl[i].offset;
13240 read_mask = reg_tbl[i].read_mask;
13241 write_mask = reg_tbl[i].write_mask;
13242
13243 /* Save the original register content */
13244 save_val = tr32(offset);
13245
13246 /* Determine the read-only value. */
13247 read_val = save_val & read_mask;
13248
13249 /* Write zero to the register, then make sure the read-only bits
13250 * are not changed and the read/write bits are all zeros.
13251 */
13252 tw32(offset, 0);
13253
13254 val = tr32(offset);
13255
13256 /* Test the read-only and read/write bits. */
13257 if (((val & read_mask) != read_val) || (val & write_mask))
13258 goto out;
13259
13260 /* Write ones to all the bits defined by RdMask and WrMask, then
13261 * make sure the read-only bits are not changed and the
13262 * read/write bits are all ones.
13263 */
13264 tw32(offset, read_mask | write_mask);
13265
13266 val = tr32(offset);
13267
13268 /* Test the read-only bits. */
13269 if ((val & read_mask) != read_val)
13270 goto out;
13271
13272 /* Test the read/write bits. */
13273 if ((val & write_mask) != write_mask)
13274 goto out;
13275
13276 tw32(offset, save_val);
13277 }
13278
13279 return 0;
13280
13281out:
Michael Chan9f88f292006-12-07 00:22:54 -080013282 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013283 netdev_err(tp->dev,
13284 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013285 tw32(offset, save_val);
13286 return -EIO;
13287}
13288
Michael Chan7942e1d2005-05-29 14:58:36 -070013289static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13290{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013291 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013292 int i;
13293 u32 j;
13294
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013295 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013296 for (j = 0; j < len; j += 4) {
13297 u32 val;
13298
13299 tg3_write_mem(tp, offset + j, test_pattern[i]);
13300 tg3_read_mem(tp, offset + j, &val);
13301 if (val != test_pattern[i])
13302 return -EIO;
13303 }
13304 }
13305 return 0;
13306}
13307
13308static int tg3_test_memory(struct tg3 *tp)
13309{
13310 static struct mem_entry {
13311 u32 offset;
13312 u32 len;
13313 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013314 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013315 { 0x00002000, 0x1c000},
13316 { 0xffffffff, 0x00000}
13317 }, mem_tbl_5705[] = {
13318 { 0x00000100, 0x0000c},
13319 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013320 { 0x00004000, 0x00800},
13321 { 0x00006000, 0x01000},
13322 { 0x00008000, 0x02000},
13323 { 0x00010000, 0x0e000},
13324 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013325 }, mem_tbl_5755[] = {
13326 { 0x00000200, 0x00008},
13327 { 0x00004000, 0x00800},
13328 { 0x00006000, 0x00800},
13329 { 0x00008000, 0x02000},
13330 { 0x00010000, 0x0c000},
13331 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013332 }, mem_tbl_5906[] = {
13333 { 0x00000200, 0x00008},
13334 { 0x00004000, 0x00400},
13335 { 0x00006000, 0x00400},
13336 { 0x00008000, 0x01000},
13337 { 0x00010000, 0x01000},
13338 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013339 }, mem_tbl_5717[] = {
13340 { 0x00000200, 0x00008},
13341 { 0x00010000, 0x0a000},
13342 { 0x00020000, 0x13c00},
13343 { 0xffffffff, 0x00000}
13344 }, mem_tbl_57765[] = {
13345 { 0x00000200, 0x00008},
13346 { 0x00004000, 0x00800},
13347 { 0x00006000, 0x09800},
13348 { 0x00010000, 0x0a000},
13349 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013350 };
13351 struct mem_entry *mem_tbl;
13352 int err = 0;
13353 int i;
13354
Joe Perches63c3a662011-04-26 08:12:10 +000013355 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013356 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013357 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013358 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013359 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013360 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013361 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013362 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013363 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013364 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013365 mem_tbl = mem_tbl_5705;
13366 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013367 mem_tbl = mem_tbl_570x;
13368
13369 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013370 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13371 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013372 break;
13373 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013374
Michael Chan7942e1d2005-05-29 14:58:36 -070013375 return err;
13376}
13377
Matt Carlsonbb158d62011-04-25 12:42:47 +000013378#define TG3_TSO_MSS 500
13379
13380#define TG3_TSO_IP_HDR_LEN 20
13381#define TG3_TSO_TCP_HDR_LEN 20
13382#define TG3_TSO_TCP_OPT_LEN 12
13383
13384static const u8 tg3_tso_header[] = {
133850x08, 0x00,
133860x45, 0x00, 0x00, 0x00,
133870x00, 0x00, 0x40, 0x00,
133880x40, 0x06, 0x00, 0x00,
133890x0a, 0x00, 0x00, 0x01,
133900x0a, 0x00, 0x00, 0x02,
133910x0d, 0x00, 0xe0, 0x00,
133920x00, 0x00, 0x01, 0x00,
133930x00, 0x00, 0x02, 0x00,
133940x80, 0x10, 0x10, 0x00,
133950x14, 0x09, 0x00, 0x00,
133960x01, 0x01, 0x08, 0x0a,
133970x11, 0x11, 0x11, 0x11,
133980x11, 0x11, 0x11, 0x11,
13399};
Michael Chan9f40dea2005-09-05 17:53:06 -070013400
Matt Carlson28a45952011-08-19 13:58:22 +000013401static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013402{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013403 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013404 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013405 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013406 struct sk_buff *skb;
13407 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013408 dma_addr_t map;
13409 int num_pkts, tx_len, rx_len, i, err;
13410 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013411 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013412 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013413
Matt Carlsonc8873402010-02-12 14:47:11 +000013414 tnapi = &tp->napi[0];
13415 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013416 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013417 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013418 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013419 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013420 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013421 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013422 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013423
Michael Chanc76949a2005-05-29 14:58:59 -070013424 err = -EIO;
13425
Matt Carlson4852a862011-04-13 11:05:07 +000013426 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013427 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013428 if (!skb)
13429 return -ENOMEM;
13430
Michael Chanc76949a2005-05-29 14:58:59 -070013431 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013432 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13433 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013434
Matt Carlson4852a862011-04-13 11:05:07 +000013435 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013436
Matt Carlson28a45952011-08-19 13:58:22 +000013437 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013438 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13439
13440 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13441 TG3_TSO_TCP_OPT_LEN;
13442
13443 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13444 sizeof(tg3_tso_header));
13445 mss = TG3_TSO_MSS;
13446
13447 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13448 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13449
13450 /* Set the total length field in the IP header */
13451 iph->tot_len = htons((u16)(mss + hdr_len));
13452
13453 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13454 TXD_FLAG_CPU_POST_DMA);
13455
Joe Perches63c3a662011-04-26 08:12:10 +000013456 if (tg3_flag(tp, HW_TSO_1) ||
13457 tg3_flag(tp, HW_TSO_2) ||
13458 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013459 struct tcphdr *th;
13460 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13461 th = (struct tcphdr *)&tx_data[val];
13462 th->check = 0;
13463 } else
13464 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13465
Joe Perches63c3a662011-04-26 08:12:10 +000013466 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013467 mss |= (hdr_len & 0xc) << 12;
13468 if (hdr_len & 0x10)
13469 base_flags |= 0x00000010;
13470 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013471 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013472 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013473 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013474 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013475 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13476 } else {
13477 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13478 }
13479
13480 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13481 } else {
13482 num_pkts = 1;
13483 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013484
13485 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13486 tx_len > VLAN_ETH_FRAME_LEN)
13487 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013488 }
13489
13490 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013491 tx_data[i] = (u8) (i & 0xff);
13492
Alexander Duyckf4188d82009-12-02 16:48:38 +000013493 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13494 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013495 dev_kfree_skb(skb);
13496 return -EIO;
13497 }
Michael Chanc76949a2005-05-29 14:58:59 -070013498
Matt Carlson0d681b22011-07-27 14:20:49 +000013499 val = tnapi->tx_prod;
13500 tnapi->tx_buffers[val].skb = skb;
13501 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13502
Michael Chanc76949a2005-05-29 14:58:59 -070013503 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013504 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013505
13506 udelay(10);
13507
Matt Carlson898a56f2009-08-28 14:02:40 +000013508 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013509
Matt Carlson84b67b22011-07-27 14:20:52 +000013510 budget = tg3_tx_avail(tnapi);
13511 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013512 base_flags | TXD_FLAG_END, mss, 0)) {
13513 tnapi->tx_buffers[val].skb = NULL;
13514 dev_kfree_skb(skb);
13515 return -EIO;
13516 }
Michael Chanc76949a2005-05-29 14:58:59 -070013517
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013518 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013519
Michael Chan6541b802012-03-04 14:48:14 +000013520 /* Sync BD data before updating mailbox */
13521 wmb();
13522
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013523 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13524 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013525
13526 udelay(10);
13527
Matt Carlson303fc922009-11-02 14:27:34 +000013528 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13529 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013530 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013531 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013532
13533 udelay(10);
13534
Matt Carlson898a56f2009-08-28 14:02:40 +000013535 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13536 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013537 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013538 (rx_idx == (rx_start_idx + num_pkts)))
13539 break;
13540 }
13541
Matt Carlsonba1142e2011-11-04 09:15:00 +000013542 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013543 dev_kfree_skb(skb);
13544
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013545 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013546 goto out;
13547
13548 if (rx_idx != rx_start_idx + num_pkts)
13549 goto out;
13550
Matt Carlsonbb158d62011-04-25 12:42:47 +000013551 val = data_off;
13552 while (rx_idx != rx_start_idx) {
13553 desc = &rnapi->rx_rcb[rx_start_idx++];
13554 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13555 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013556
Matt Carlsonbb158d62011-04-25 12:42:47 +000013557 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13558 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013559 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013560
Matt Carlsonbb158d62011-04-25 12:42:47 +000013561 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13562 - ETH_FCS_LEN;
13563
Matt Carlson28a45952011-08-19 13:58:22 +000013564 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013565 if (rx_len != tx_len)
13566 goto out;
13567
13568 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13569 if (opaque_key != RXD_OPAQUE_RING_STD)
13570 goto out;
13571 } else {
13572 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13573 goto out;
13574 }
13575 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13576 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013577 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013578 goto out;
13579 }
13580
13581 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013582 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013583 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13584 mapping);
13585 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013586 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013587 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13588 mapping);
13589 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013590 goto out;
13591
Matt Carlsonbb158d62011-04-25 12:42:47 +000013592 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13593 PCI_DMA_FROMDEVICE);
13594
Eric Dumazet9205fd92011-11-18 06:47:01 +000013595 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013596 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013597 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013598 goto out;
13599 }
Matt Carlson4852a862011-04-13 11:05:07 +000013600 }
13601
Michael Chanc76949a2005-05-29 14:58:59 -070013602 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013603
Eric Dumazet9205fd92011-11-18 06:47:01 +000013604 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013605out:
13606 return err;
13607}
13608
Matt Carlson00c266b2011-04-25 12:42:46 +000013609#define TG3_STD_LOOPBACK_FAILED 1
13610#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013611#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013612#define TG3_LOOPBACK_FAILED \
13613 (TG3_STD_LOOPBACK_FAILED | \
13614 TG3_JMB_LOOPBACK_FAILED | \
13615 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013616
Matt Carlson941ec902011-08-19 13:58:23 +000013617static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013618{
Matt Carlson28a45952011-08-19 13:58:22 +000013619 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013620 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013621 u32 jmb_pkt_sz = 9000;
13622
13623 if (tp->dma_limit)
13624 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013625
Matt Carlsonab789042011-01-25 15:58:54 +000013626 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13627 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13628
Matt Carlson28a45952011-08-19 13:58:22 +000013629 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013630 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13631 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013632 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013633 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013634 goto done;
13635 }
13636
Joe Perches953c96e2013-04-09 10:18:14 +000013637 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013638 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013639 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13640 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013641 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013642 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013643 goto done;
13644 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013645
Joe Perches63c3a662011-04-26 08:12:10 +000013646 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013647 int i;
13648
13649 /* Reroute all rx packets to the 1st queue */
13650 for (i = MAC_RSS_INDIR_TBL_0;
13651 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13652 tw32(i, 0x0);
13653 }
13654
Matt Carlson6e01b202011-08-19 13:58:20 +000013655 /* HW errata - mac loopback fails in some cases on 5780.
13656 * Normal traffic and PHY loopback are not affected by
13657 * errata. Also, the MAC loopback test is deprecated for
13658 * all newer ASIC revisions.
13659 */
Joe Perches41535772013-02-16 11:20:04 +000013660 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013661 !tg3_flag(tp, CPMU_PRESENT)) {
13662 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013663
Matt Carlson28a45952011-08-19 13:58:22 +000013664 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013665 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013666
13667 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013668 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013669 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013670
13671 tg3_mac_loopback(tp, false);
13672 }
Matt Carlson4852a862011-04-13 11:05:07 +000013673
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013674 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013675 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013676 int i;
13677
Matt Carlson941ec902011-08-19 13:58:23 +000013678 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013679
13680 /* Wait for link */
13681 for (i = 0; i < 100; i++) {
13682 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13683 break;
13684 mdelay(1);
13685 }
13686
Matt Carlson28a45952011-08-19 13:58:22 +000013687 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013688 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013689 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013690 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013691 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013692 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013693 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013694 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013695
Matt Carlson941ec902011-08-19 13:58:23 +000013696 if (do_extlpbk) {
13697 tg3_phy_lpbk_set(tp, 0, true);
13698
13699 /* All link indications report up, but the hardware
13700 * isn't really ready for about 20 msec. Double it
13701 * to be sure.
13702 */
13703 mdelay(40);
13704
13705 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013706 data[TG3_EXT_LOOPB_TEST] |=
13707 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013708 if (tg3_flag(tp, TSO_CAPABLE) &&
13709 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013710 data[TG3_EXT_LOOPB_TEST] |=
13711 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013712 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013713 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013714 data[TG3_EXT_LOOPB_TEST] |=
13715 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013716 }
13717
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013718 /* Re-enable gphy autopowerdown. */
13719 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13720 tg3_phy_toggle_apd(tp, true);
13721 }
Matt Carlson6833c042008-11-21 17:18:59 -080013722
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013723 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13724 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013725
Matt Carlsonab789042011-01-25 15:58:54 +000013726done:
13727 tp->phy_flags |= eee_cap;
13728
Michael Chan9f40dea2005-09-05 17:53:06 -070013729 return err;
13730}
13731
Michael Chan4cafd3f2005-05-29 14:56:34 -070013732static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13733 u64 *data)
13734{
Michael Chan566f86a2005-05-29 14:56:58 -070013735 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013736 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013737
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013738 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13739 if (tg3_power_up(tp)) {
13740 etest->flags |= ETH_TEST_FL_FAILED;
13741 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13742 return;
13743 }
13744 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013745 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013746
Michael Chan566f86a2005-05-29 14:56:58 -070013747 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13748
13749 if (tg3_test_nvram(tp) != 0) {
13750 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013751 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013752 }
Matt Carlson941ec902011-08-19 13:58:23 +000013753 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013754 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013755 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013756 }
Michael Chana71116d2005-05-29 14:58:11 -070013757 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013758 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013759
Michael Chanbbe832c2005-06-24 20:20:04 -070013760 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013761 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013762 tg3_netif_stop(tp);
13763 irq_sync = 1;
13764 }
13765
13766 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013767 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013768 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013769 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013770 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013771 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013772 if (!err)
13773 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013774
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013775 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013776 tg3_phy_reset(tp);
13777
Michael Chana71116d2005-05-29 14:58:11 -070013778 if (tg3_test_registers(tp) != 0) {
13779 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013780 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013781 }
Matt Carlson28a45952011-08-19 13:58:22 +000013782
Michael Chan7942e1d2005-05-29 14:58:36 -070013783 if (tg3_test_memory(tp) != 0) {
13784 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013785 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013786 }
Matt Carlson28a45952011-08-19 13:58:22 +000013787
Matt Carlson941ec902011-08-19 13:58:23 +000013788 if (doextlpbk)
13789 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13790
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013791 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013792 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013793
David S. Millerf47c11e2005-06-24 20:18:35 -070013794 tg3_full_unlock(tp);
13795
Michael Chand4bc3922005-05-29 14:59:20 -070013796 if (tg3_test_interrupt(tp) != 0) {
13797 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013798 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013799 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013800
13801 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013802
Michael Chana71116d2005-05-29 14:58:11 -070013803 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13804 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013805 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013806 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013807 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013808 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013809 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013810
13811 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013812
13813 if (irq_sync && !err2)
13814 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013815 }
Matt Carlson80096062010-08-02 11:26:06 +000013816 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013817 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013818
Michael Chan4cafd3f2005-05-29 14:56:34 -070013819}
13820
Ben Hutchings72608992013-11-18 22:59:43 +000013821static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013822{
13823 struct tg3 *tp = netdev_priv(dev);
13824 struct hwtstamp_config stmpconf;
13825
13826 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013827 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013828
13829 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13830 return -EFAULT;
13831
13832 if (stmpconf.flags)
13833 return -EINVAL;
13834
Ben Hutchings58b187c2013-11-14 00:40:56 +000013835 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13836 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013837 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013838
13839 switch (stmpconf.rx_filter) {
13840 case HWTSTAMP_FILTER_NONE:
13841 tp->rxptpctl = 0;
13842 break;
13843 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13844 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13845 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13846 break;
13847 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13848 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13849 TG3_RX_PTP_CTL_SYNC_EVNT;
13850 break;
13851 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13852 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13853 TG3_RX_PTP_CTL_DELAY_REQ;
13854 break;
13855 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13856 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13857 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13858 break;
13859 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13860 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13861 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13862 break;
13863 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13864 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13865 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13866 break;
13867 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13868 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13869 TG3_RX_PTP_CTL_SYNC_EVNT;
13870 break;
13871 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13872 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13873 TG3_RX_PTP_CTL_SYNC_EVNT;
13874 break;
13875 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13876 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13877 TG3_RX_PTP_CTL_SYNC_EVNT;
13878 break;
13879 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13880 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13881 TG3_RX_PTP_CTL_DELAY_REQ;
13882 break;
13883 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13884 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13885 TG3_RX_PTP_CTL_DELAY_REQ;
13886 break;
13887 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13888 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13889 TG3_RX_PTP_CTL_DELAY_REQ;
13890 break;
13891 default:
13892 return -ERANGE;
13893 }
13894
13895 if (netif_running(dev) && tp->rxptpctl)
13896 tw32(TG3_RX_PTP_CTL,
13897 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13898
Ben Hutchings58b187c2013-11-14 00:40:56 +000013899 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13900 tg3_flag_set(tp, TX_TSTAMP_EN);
13901 else
13902 tg3_flag_clear(tp, TX_TSTAMP_EN);
13903
Matt Carlson0a633ac2012-12-03 19:36:59 +000013904 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13905 -EFAULT : 0;
13906}
13907
Ben Hutchings72608992013-11-18 22:59:43 +000013908static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13909{
13910 struct tg3 *tp = netdev_priv(dev);
13911 struct hwtstamp_config stmpconf;
13912
13913 if (!tg3_flag(tp, PTP_CAPABLE))
13914 return -EOPNOTSUPP;
13915
13916 stmpconf.flags = 0;
13917 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13918 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13919
13920 switch (tp->rxptpctl) {
13921 case 0:
13922 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13923 break;
13924 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13925 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13926 break;
13927 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13928 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13929 break;
13930 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13931 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13932 break;
13933 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13934 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13935 break;
13936 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13937 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13938 break;
13939 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13940 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13941 break;
13942 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13943 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13944 break;
13945 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13946 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13947 break;
13948 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13949 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13950 break;
13951 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13952 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13953 break;
13954 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13955 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13956 break;
13957 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13958 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13959 break;
13960 default:
13961 WARN_ON_ONCE(1);
13962 return -ERANGE;
13963 }
13964
13965 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13966 -EFAULT : 0;
13967}
13968
Linus Torvalds1da177e2005-04-16 15:20:36 -070013969static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13970{
13971 struct mii_ioctl_data *data = if_mii(ifr);
13972 struct tg3 *tp = netdev_priv(dev);
13973 int err;
13974
Joe Perches63c3a662011-04-26 08:12:10 +000013975 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013976 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013977 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013978 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010013979 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Richard Cochran28b04112010-07-17 08:48:55 +000013980 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013981 }
13982
Matt Carlson33f401a2010-04-05 10:19:27 +000013983 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013985 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013986
13987 /* fallthru */
13988 case SIOCGMIIREG: {
13989 u32 mii_regval;
13990
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013991 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992 break; /* We have no PHY */
13993
Matt Carlson34eea5a2011-04-20 07:57:38 +000013994 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013995 return -EAGAIN;
13996
David S. Millerf47c11e2005-06-24 20:18:35 -070013997 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013998 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13999 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070014000 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014001
14002 data->val_out = mii_regval;
14003
14004 return err;
14005 }
14006
14007 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014008 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014009 break; /* We have no PHY */
14010
Matt Carlson34eea5a2011-04-20 07:57:38 +000014011 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080014012 return -EAGAIN;
14013
David S. Millerf47c11e2005-06-24 20:18:35 -070014014 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000014015 err = __tg3_writephy(tp, data->phy_id & 0x1f,
14016 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070014017 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014018
14019 return err;
14020
Matt Carlson0a633ac2012-12-03 19:36:59 +000014021 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000014022 return tg3_hwtstamp_set(dev, ifr);
14023
14024 case SIOCGHWTSTAMP:
14025 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000014026
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 default:
14028 /* do nothing */
14029 break;
14030 }
14031 return -EOPNOTSUPP;
14032}
14033
David S. Miller15f98502005-05-18 22:49:26 -070014034static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14035{
14036 struct tg3 *tp = netdev_priv(dev);
14037
14038 memcpy(ec, &tp->coal, sizeof(*ec));
14039 return 0;
14040}
14041
Michael Chand244c892005-07-05 14:42:33 -070014042static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14043{
14044 struct tg3 *tp = netdev_priv(dev);
14045 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
14046 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
14047
Joe Perches63c3a662011-04-26 08:12:10 +000014048 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014049 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
14050 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
14051 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
14052 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
14053 }
14054
14055 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
Satish Baddipadige087d7a8c2016-08-03 09:43:59 +053014056 (!ec->rx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014057 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
Ivan Veceraaabdd092016-09-01 11:28:59 +020014058 (!ec->tx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014059 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
14060 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
14061 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
14062 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
14063 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
14064 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
14065 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14066 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14067 return -EINVAL;
14068
Michael Chand244c892005-07-05 14:42:33 -070014069 /* Only copy relevant parameters, ignore all others. */
14070 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14071 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14072 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14073 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14074 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14075 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14076 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14077 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14078 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14079
14080 if (netif_running(dev)) {
14081 tg3_full_lock(tp, 0);
14082 __tg3_set_coalesce(tp, &tp->coal);
14083 tg3_full_unlock(tp);
14084 }
14085 return 0;
14086}
14087
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014088static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14089{
14090 struct tg3 *tp = netdev_priv(dev);
14091
14092 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14093 netdev_warn(tp->dev, "Board does not support EEE!\n");
14094 return -EOPNOTSUPP;
14095 }
14096
14097 if (edata->advertised != tp->eee.advertised) {
14098 netdev_warn(tp->dev,
14099 "Direct manipulation of EEE advertisement is not supported\n");
14100 return -EINVAL;
14101 }
14102
14103 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14104 netdev_warn(tp->dev,
14105 "Maximal Tx Lpi timer supported is %#x(u)\n",
14106 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14107 return -EINVAL;
14108 }
14109
14110 tp->eee = *edata;
14111
14112 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14113 tg3_warn_mgmt_link_flap(tp);
14114
14115 if (netif_running(tp->dev)) {
14116 tg3_full_lock(tp, 0);
14117 tg3_setup_eee(tp);
14118 tg3_phy_reset(tp);
14119 tg3_full_unlock(tp);
14120 }
14121
14122 return 0;
14123}
14124
14125static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14126{
14127 struct tg3 *tp = netdev_priv(dev);
14128
14129 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14130 netdev_warn(tp->dev,
14131 "Board does not support EEE!\n");
14132 return -EOPNOTSUPP;
14133 }
14134
14135 *edata = tp->eee;
14136 return 0;
14137}
14138
Jeff Garzik7282d492006-09-13 14:30:00 -040014139static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140 .get_drvinfo = tg3_get_drvinfo,
14141 .get_regs_len = tg3_get_regs_len,
14142 .get_regs = tg3_get_regs,
14143 .get_wol = tg3_get_wol,
14144 .set_wol = tg3_set_wol,
14145 .get_msglevel = tg3_get_msglevel,
14146 .set_msglevel = tg3_set_msglevel,
14147 .nway_reset = tg3_nway_reset,
14148 .get_link = ethtool_op_get_link,
14149 .get_eeprom_len = tg3_get_eeprom_len,
14150 .get_eeprom = tg3_get_eeprom,
14151 .set_eeprom = tg3_set_eeprom,
14152 .get_ringparam = tg3_get_ringparam,
14153 .set_ringparam = tg3_set_ringparam,
14154 .get_pauseparam = tg3_get_pauseparam,
14155 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014156 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014157 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014158 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014159 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014160 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014161 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014162 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014163 .get_rxnfc = tg3_get_rxnfc,
14164 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014165 .get_rxfh = tg3_get_rxfh,
14166 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014167 .get_channels = tg3_get_channels,
14168 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014169 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014170 .get_eee = tg3_get_eee,
14171 .set_eee = tg3_set_eee,
Philippe Reynesb6f5be22016-09-25 23:31:24 +020014172 .get_link_ksettings = tg3_get_link_ksettings,
14173 .set_link_ksettings = tg3_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174};
14175
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014176static void tg3_get_stats64(struct net_device *dev,
14177 struct rtnl_link_stats64 *stats)
David S. Millerb4017c52012-03-01 17:57:40 -050014178{
14179 struct tg3 *tp = netdev_priv(dev);
14180
David S. Millerb4017c52012-03-01 17:57:40 -050014181 spin_lock_bh(&tp->lock);
Michael Chand89a2ad2018-05-03 20:04:27 -040014182 if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014183 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014184 spin_unlock_bh(&tp->lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014185 return;
Michael Chan0f566b22012-07-29 19:15:44 +000014186 }
14187
David S. Millerb4017c52012-03-01 17:57:40 -050014188 tg3_get_nstats(tp, stats);
14189 spin_unlock_bh(&tp->lock);
David S. Millerb4017c52012-03-01 17:57:40 -050014190}
14191
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014192static void tg3_set_rx_mode(struct net_device *dev)
14193{
14194 struct tg3 *tp = netdev_priv(dev);
14195
14196 if (!netif_running(dev))
14197 return;
14198
14199 tg3_full_lock(tp, 0);
14200 __tg3_set_rx_mode(dev);
14201 tg3_full_unlock(tp);
14202}
14203
Matt Carlsonfaf16272012-02-13 10:20:07 +000014204static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14205 int new_mtu)
14206{
14207 dev->mtu = new_mtu;
14208
14209 if (new_mtu > ETH_DATA_LEN) {
14210 if (tg3_flag(tp, 5780_CLASS)) {
14211 netdev_update_features(dev);
14212 tg3_flag_clear(tp, TSO_CAPABLE);
14213 } else {
14214 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14215 }
14216 } else {
14217 if (tg3_flag(tp, 5780_CLASS)) {
14218 tg3_flag_set(tp, TSO_CAPABLE);
14219 netdev_update_features(dev);
14220 }
14221 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14222 }
14223}
14224
14225static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14226{
14227 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014228 int err;
14229 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014230
Matt Carlsonfaf16272012-02-13 10:20:07 +000014231 if (!netif_running(dev)) {
14232 /* We'll just catch it later when the
14233 * device is up'd.
14234 */
14235 tg3_set_mtu(dev, tp, new_mtu);
14236 return 0;
14237 }
14238
14239 tg3_phy_stop(tp);
14240
14241 tg3_netif_stop(tp);
14242
Nithin Sujirc6993df2014-02-06 14:13:05 -080014243 tg3_set_mtu(dev, tp, new_mtu);
14244
Matt Carlsonfaf16272012-02-13 10:20:07 +000014245 tg3_full_lock(tp, 1);
14246
14247 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14248
Michael Chan2fae5e32012-03-04 14:48:15 +000014249 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14250 * breaks all requests to 256 bytes.
14251 */
Brian King748a2402017-12-15 15:21:50 -060014252 if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
14253 tg3_asic_rev(tp) == ASIC_REV_5717 ||
Siva Reddy Kallame60ee412017-12-22 16:05:29 +053014254 tg3_asic_rev(tp) == ASIC_REV_5719 ||
14255 tg3_asic_rev(tp) == ASIC_REV_5720)
Joe Perches953c96e2013-04-09 10:18:14 +000014256 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014257
14258 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014259
14260 if (!err)
14261 tg3_netif_start(tp);
14262
14263 tg3_full_unlock(tp);
14264
14265 if (!err)
14266 tg3_phy_start(tp);
14267
14268 return err;
14269}
14270
14271static const struct net_device_ops tg3_netdev_ops = {
14272 .ndo_open = tg3_open,
14273 .ndo_stop = tg3_close,
14274 .ndo_start_xmit = tg3_start_xmit,
14275 .ndo_get_stats64 = tg3_get_stats64,
14276 .ndo_validate_addr = eth_validate_addr,
14277 .ndo_set_rx_mode = tg3_set_rx_mode,
14278 .ndo_set_mac_address = tg3_set_mac_addr,
14279 .ndo_do_ioctl = tg3_ioctl,
14280 .ndo_tx_timeout = tg3_tx_timeout,
14281 .ndo_change_mtu = tg3_change_mtu,
14282 .ndo_fix_features = tg3_fix_features,
14283 .ndo_set_features = tg3_set_features,
14284#ifdef CONFIG_NET_POLL_CONTROLLER
14285 .ndo_poll_controller = tg3_poll_controller,
14286#endif
14287};
14288
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014289static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290{
Michael Chan1b277772006-03-20 22:27:48 -080014291 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292
14293 tp->nvram_size = EEPROM_CHIP_SIZE;
14294
Matt Carlsone4f34112009-02-25 14:25:00 +000014295 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014296 return;
14297
Michael Chanb16250e2006-09-27 16:10:14 -070014298 if ((magic != TG3_EEPROM_MAGIC) &&
14299 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14300 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301 return;
14302
14303 /*
14304 * Size the chip by reading offsets at increasing powers of two.
14305 * When we encounter our validation signature, we know the addressing
14306 * has wrapped around, and thus have our chip size.
14307 */
Michael Chan1b277772006-03-20 22:27:48 -080014308 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309
14310 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014311 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312 return;
14313
Michael Chan18201802006-03-20 22:29:15 -080014314 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315 break;
14316
14317 cursize <<= 1;
14318 }
14319
14320 tp->nvram_size = cursize;
14321}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014322
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014323static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324{
14325 u32 val;
14326
Joe Perches63c3a662011-04-26 08:12:10 +000014327 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014328 return;
14329
14330 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014331 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014332 tg3_get_eeprom_size(tp);
14333 return;
14334 }
14335
Matt Carlson6d348f22009-02-25 14:25:52 +000014336 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014338 /* This is confusing. We want to operate on the
14339 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14340 * call will read from NVRAM and byteswap the data
14341 * according to the byteswapping settings for all
14342 * other register accesses. This ensures the data we
14343 * want will always reside in the lower 16-bits.
14344 * However, the data in NVRAM is in LE format, which
14345 * means the data from the NVRAM read will always be
14346 * opposite the endianness of the CPU. The 16-bit
14347 * byteswap then brings the data to CPU endianness.
14348 */
14349 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350 return;
14351 }
14352 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014353 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354}
14355
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014356static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357{
14358 u32 nvcfg1;
14359
14360 nvcfg1 = tr32(NVRAM_CFG1);
14361 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014362 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014363 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14365 tw32(NVRAM_CFG1, nvcfg1);
14366 }
14367
Joe Perches41535772013-02-16 11:20:04 +000014368 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014369 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014371 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14372 tp->nvram_jedecnum = JEDEC_ATMEL;
14373 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014374 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014375 break;
14376 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14377 tp->nvram_jedecnum = JEDEC_ATMEL;
14378 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14379 break;
14380 case FLASH_VENDOR_ATMEL_EEPROM:
14381 tp->nvram_jedecnum = JEDEC_ATMEL;
14382 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014383 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014384 break;
14385 case FLASH_VENDOR_ST:
14386 tp->nvram_jedecnum = JEDEC_ST;
14387 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014388 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014389 break;
14390 case FLASH_VENDOR_SAIFUN:
14391 tp->nvram_jedecnum = JEDEC_SAIFUN;
14392 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14393 break;
14394 case FLASH_VENDOR_SST_SMALL:
14395 case FLASH_VENDOR_SST_LARGE:
14396 tp->nvram_jedecnum = JEDEC_SST;
14397 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014399 }
Matt Carlson8590a602009-08-28 12:29:16 +000014400 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014401 tp->nvram_jedecnum = JEDEC_ATMEL;
14402 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014403 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404 }
14405}
14406
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014407static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014408{
14409 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14410 case FLASH_5752PAGE_SIZE_256:
14411 tp->nvram_pagesize = 256;
14412 break;
14413 case FLASH_5752PAGE_SIZE_512:
14414 tp->nvram_pagesize = 512;
14415 break;
14416 case FLASH_5752PAGE_SIZE_1K:
14417 tp->nvram_pagesize = 1024;
14418 break;
14419 case FLASH_5752PAGE_SIZE_2K:
14420 tp->nvram_pagesize = 2048;
14421 break;
14422 case FLASH_5752PAGE_SIZE_4K:
14423 tp->nvram_pagesize = 4096;
14424 break;
14425 case FLASH_5752PAGE_SIZE_264:
14426 tp->nvram_pagesize = 264;
14427 break;
14428 case FLASH_5752PAGE_SIZE_528:
14429 tp->nvram_pagesize = 528;
14430 break;
14431 }
14432}
14433
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014434static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014435{
14436 u32 nvcfg1;
14437
14438 nvcfg1 = tr32(NVRAM_CFG1);
14439
Michael Chane6af3012005-04-21 17:12:05 -070014440 /* NVRAM protection for TPM */
14441 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014442 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014443
Michael Chan361b4ac2005-04-21 17:11:21 -070014444 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014445 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14446 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14447 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014448 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014449 break;
14450 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
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 break;
14455 case FLASH_5752VENDOR_ST_M45PE10:
14456 case FLASH_5752VENDOR_ST_M45PE20:
14457 case FLASH_5752VENDOR_ST_M45PE40:
14458 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014459 tg3_flag_set(tp, NVRAM_BUFFERED);
14460 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014461 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014462 }
14463
Joe Perches63c3a662011-04-26 08:12:10 +000014464 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014465 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014466 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014467 /* For eeprom, set pagesize to maximum eeprom size */
14468 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14469
14470 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14471 tw32(NVRAM_CFG1, nvcfg1);
14472 }
14473}
14474
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014475static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014476{
Matt Carlson989a9d22007-05-05 11:51:05 -070014477 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014478
14479 nvcfg1 = tr32(NVRAM_CFG1);
14480
14481 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014482 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014483 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014484 protect = 1;
14485 }
Michael Chand3c7b882006-03-23 01:28:25 -080014486
Matt Carlson989a9d22007-05-05 11:51:05 -070014487 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14488 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014489 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14490 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14491 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14492 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14493 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014494 tg3_flag_set(tp, NVRAM_BUFFERED);
14495 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014496 tp->nvram_pagesize = 264;
14497 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14498 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14499 tp->nvram_size = (protect ? 0x3e200 :
14500 TG3_NVRAM_SIZE_512KB);
14501 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14502 tp->nvram_size = (protect ? 0x1f200 :
14503 TG3_NVRAM_SIZE_256KB);
14504 else
14505 tp->nvram_size = (protect ? 0x1f200 :
14506 TG3_NVRAM_SIZE_128KB);
14507 break;
14508 case FLASH_5752VENDOR_ST_M45PE10:
14509 case FLASH_5752VENDOR_ST_M45PE20:
14510 case FLASH_5752VENDOR_ST_M45PE40:
14511 tp->nvram_jedecnum = JEDEC_ST;
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 = 256;
14515 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14516 tp->nvram_size = (protect ?
14517 TG3_NVRAM_SIZE_64KB :
14518 TG3_NVRAM_SIZE_128KB);
14519 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14520 tp->nvram_size = (protect ?
14521 TG3_NVRAM_SIZE_64KB :
14522 TG3_NVRAM_SIZE_256KB);
14523 else
14524 tp->nvram_size = (protect ?
14525 TG3_NVRAM_SIZE_128KB :
14526 TG3_NVRAM_SIZE_512KB);
14527 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014528 }
14529}
14530
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014531static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014532{
14533 u32 nvcfg1;
14534
14535 nvcfg1 = tr32(NVRAM_CFG1);
14536
14537 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014538 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14539 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14540 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14541 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14542 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014543 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014544 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014545
Matt Carlson8590a602009-08-28 12:29:16 +000014546 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14547 tw32(NVRAM_CFG1, nvcfg1);
14548 break;
14549 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14550 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14551 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14552 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14553 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014554 tg3_flag_set(tp, NVRAM_BUFFERED);
14555 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014556 tp->nvram_pagesize = 264;
14557 break;
14558 case FLASH_5752VENDOR_ST_M45PE10:
14559 case FLASH_5752VENDOR_ST_M45PE20:
14560 case FLASH_5752VENDOR_ST_M45PE40:
14561 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014562 tg3_flag_set(tp, NVRAM_BUFFERED);
14563 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014564 tp->nvram_pagesize = 256;
14565 break;
Michael Chan1b277772006-03-20 22:27:48 -080014566 }
14567}
14568
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014569static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014570{
14571 u32 nvcfg1, protect = 0;
14572
14573 nvcfg1 = tr32(NVRAM_CFG1);
14574
14575 /* NVRAM protection for TPM */
14576 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014577 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014578 protect = 1;
14579 }
14580
14581 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14582 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014583 case FLASH_5761VENDOR_ATMEL_ADB021D:
14584 case FLASH_5761VENDOR_ATMEL_ADB041D:
14585 case FLASH_5761VENDOR_ATMEL_ADB081D:
14586 case FLASH_5761VENDOR_ATMEL_ADB161D:
14587 case FLASH_5761VENDOR_ATMEL_MDB021D:
14588 case FLASH_5761VENDOR_ATMEL_MDB041D:
14589 case FLASH_5761VENDOR_ATMEL_MDB081D:
14590 case FLASH_5761VENDOR_ATMEL_MDB161D:
14591 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014592 tg3_flag_set(tp, NVRAM_BUFFERED);
14593 tg3_flag_set(tp, FLASH);
14594 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014595 tp->nvram_pagesize = 256;
14596 break;
14597 case FLASH_5761VENDOR_ST_A_M45PE20:
14598 case FLASH_5761VENDOR_ST_A_M45PE40:
14599 case FLASH_5761VENDOR_ST_A_M45PE80:
14600 case FLASH_5761VENDOR_ST_A_M45PE16:
14601 case FLASH_5761VENDOR_ST_M_M45PE20:
14602 case FLASH_5761VENDOR_ST_M_M45PE40:
14603 case FLASH_5761VENDOR_ST_M_M45PE80:
14604 case FLASH_5761VENDOR_ST_M_M45PE16:
14605 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014606 tg3_flag_set(tp, NVRAM_BUFFERED);
14607 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014608 tp->nvram_pagesize = 256;
14609 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014610 }
14611
14612 if (protect) {
14613 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14614 } else {
14615 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014616 case FLASH_5761VENDOR_ATMEL_ADB161D:
14617 case FLASH_5761VENDOR_ATMEL_MDB161D:
14618 case FLASH_5761VENDOR_ST_A_M45PE16:
14619 case FLASH_5761VENDOR_ST_M_M45PE16:
14620 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14621 break;
14622 case FLASH_5761VENDOR_ATMEL_ADB081D:
14623 case FLASH_5761VENDOR_ATMEL_MDB081D:
14624 case FLASH_5761VENDOR_ST_A_M45PE80:
14625 case FLASH_5761VENDOR_ST_M_M45PE80:
14626 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14627 break;
14628 case FLASH_5761VENDOR_ATMEL_ADB041D:
14629 case FLASH_5761VENDOR_ATMEL_MDB041D:
14630 case FLASH_5761VENDOR_ST_A_M45PE40:
14631 case FLASH_5761VENDOR_ST_M_M45PE40:
14632 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14633 break;
14634 case FLASH_5761VENDOR_ATMEL_ADB021D:
14635 case FLASH_5761VENDOR_ATMEL_MDB021D:
14636 case FLASH_5761VENDOR_ST_A_M45PE20:
14637 case FLASH_5761VENDOR_ST_M_M45PE20:
14638 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14639 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014640 }
14641 }
14642}
14643
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014644static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014645{
14646 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014647 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014648 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14649}
14650
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014651static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014652{
14653 u32 nvcfg1;
14654
14655 nvcfg1 = tr32(NVRAM_CFG1);
14656
14657 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14658 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14659 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14660 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014661 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014662 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14663
14664 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14665 tw32(NVRAM_CFG1, nvcfg1);
14666 return;
14667 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14668 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14669 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14670 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14671 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14672 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14673 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14674 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014675 tg3_flag_set(tp, NVRAM_BUFFERED);
14676 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014677
14678 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14679 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14680 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14681 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14682 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14683 break;
14684 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14685 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14686 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14687 break;
14688 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14689 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14690 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14691 break;
14692 }
14693 break;
14694 case FLASH_5752VENDOR_ST_M45PE10:
14695 case FLASH_5752VENDOR_ST_M45PE20:
14696 case FLASH_5752VENDOR_ST_M45PE40:
14697 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014698 tg3_flag_set(tp, NVRAM_BUFFERED);
14699 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014700
14701 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14702 case FLASH_5752VENDOR_ST_M45PE10:
14703 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14704 break;
14705 case FLASH_5752VENDOR_ST_M45PE20:
14706 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14707 break;
14708 case FLASH_5752VENDOR_ST_M45PE40:
14709 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14710 break;
14711 }
14712 break;
14713 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014714 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014715 return;
14716 }
14717
Matt Carlsona1b950d2009-09-01 13:20:17 +000014718 tg3_nvram_get_pagesize(tp, nvcfg1);
14719 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014720 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014721}
14722
14723
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014724static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014725{
14726 u32 nvcfg1;
14727
14728 nvcfg1 = tr32(NVRAM_CFG1);
14729
14730 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14731 case FLASH_5717VENDOR_ATMEL_EEPROM:
14732 case FLASH_5717VENDOR_MICRO_EEPROM:
14733 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014734 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014735 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14736
14737 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14738 tw32(NVRAM_CFG1, nvcfg1);
14739 return;
14740 case FLASH_5717VENDOR_ATMEL_MDB011D:
14741 case FLASH_5717VENDOR_ATMEL_ADB011B:
14742 case FLASH_5717VENDOR_ATMEL_ADB011D:
14743 case FLASH_5717VENDOR_ATMEL_MDB021D:
14744 case FLASH_5717VENDOR_ATMEL_ADB021B:
14745 case FLASH_5717VENDOR_ATMEL_ADB021D:
14746 case FLASH_5717VENDOR_ATMEL_45USPT:
14747 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014748 tg3_flag_set(tp, NVRAM_BUFFERED);
14749 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014750
14751 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14752 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014753 /* Detect size with tg3_nvram_get_size() */
14754 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014755 case FLASH_5717VENDOR_ATMEL_ADB021B:
14756 case FLASH_5717VENDOR_ATMEL_ADB021D:
14757 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14758 break;
14759 default:
14760 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14761 break;
14762 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014763 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014764 case FLASH_5717VENDOR_ST_M_M25PE10:
14765 case FLASH_5717VENDOR_ST_A_M25PE10:
14766 case FLASH_5717VENDOR_ST_M_M45PE10:
14767 case FLASH_5717VENDOR_ST_A_M45PE10:
14768 case FLASH_5717VENDOR_ST_M_M25PE20:
14769 case FLASH_5717VENDOR_ST_A_M25PE20:
14770 case FLASH_5717VENDOR_ST_M_M45PE20:
14771 case FLASH_5717VENDOR_ST_A_M45PE20:
14772 case FLASH_5717VENDOR_ST_25USPT:
14773 case FLASH_5717VENDOR_ST_45USPT:
14774 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014775 tg3_flag_set(tp, NVRAM_BUFFERED);
14776 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014777
14778 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14779 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014780 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014781 /* Detect size with tg3_nvram_get_size() */
14782 break;
14783 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014784 case FLASH_5717VENDOR_ST_A_M45PE20:
14785 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14786 break;
14787 default:
14788 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14789 break;
14790 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014791 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014792 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014793 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014794 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014795 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014796
14797 tg3_nvram_get_pagesize(tp, nvcfg1);
14798 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014799 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014800}
14801
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014802static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014803{
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014804 u32 nvcfg1, nvmpinstrp, nv_status;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014805
14806 nvcfg1 = tr32(NVRAM_CFG1);
14807 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14808
Joe Perches41535772013-02-16 11:20:04 +000014809 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014810 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14811 tg3_flag_set(tp, NO_NVRAM);
14812 return;
14813 }
14814
14815 switch (nvmpinstrp) {
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014816 case FLASH_5762_MX25L_100:
14817 case FLASH_5762_MX25L_200:
14818 case FLASH_5762_MX25L_400:
14819 case FLASH_5762_MX25L_800:
14820 case FLASH_5762_MX25L_160_320:
14821 tp->nvram_pagesize = 4096;
14822 tp->nvram_jedecnum = JEDEC_MACRONIX;
14823 tg3_flag_set(tp, NVRAM_BUFFERED);
14824 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
14825 tg3_flag_set(tp, FLASH);
14826 nv_status = tr32(NVRAM_AUTOSENSE_STATUS);
14827 tp->nvram_size =
14828 (1 << (nv_status >> AUTOSENSE_DEVID &
14829 AUTOSENSE_DEVID_MASK)
14830 << AUTOSENSE_SIZE_IN_MB);
14831 return;
14832
Michael Chanc86a8562013-01-06 12:51:08 +000014833 case FLASH_5762_EEPROM_HD:
14834 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014835 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014836 case FLASH_5762_EEPROM_LD:
14837 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014838 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014839 case FLASH_5720VENDOR_M_ST_M45PE20:
14840 /* This pinstrap supports multiple sizes, so force it
14841 * to read the actual size from location 0xf0.
14842 */
14843 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14844 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014845 }
14846 }
14847
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014848 switch (nvmpinstrp) {
14849 case FLASH_5720_EEPROM_HD:
14850 case FLASH_5720_EEPROM_LD:
14851 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014852 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014853
14854 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14855 tw32(NVRAM_CFG1, nvcfg1);
14856 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14857 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14858 else
14859 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14860 return;
14861 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14862 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14863 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14864 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14865 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14866 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14867 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14868 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14869 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14870 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14871 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14872 case FLASH_5720VENDOR_ATMEL_45USPT:
14873 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014874 tg3_flag_set(tp, NVRAM_BUFFERED);
14875 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014876
14877 switch (nvmpinstrp) {
14878 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14879 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14880 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14881 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14882 break;
14883 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14884 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14885 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14886 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14887 break;
14888 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14889 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14890 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14891 break;
14892 default:
Joe Perches41535772013-02-16 11:20:04 +000014893 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014894 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014895 break;
14896 }
14897 break;
14898 case FLASH_5720VENDOR_M_ST_M25PE10:
14899 case FLASH_5720VENDOR_M_ST_M45PE10:
14900 case FLASH_5720VENDOR_A_ST_M25PE10:
14901 case FLASH_5720VENDOR_A_ST_M45PE10:
14902 case FLASH_5720VENDOR_M_ST_M25PE20:
14903 case FLASH_5720VENDOR_M_ST_M45PE20:
14904 case FLASH_5720VENDOR_A_ST_M25PE20:
14905 case FLASH_5720VENDOR_A_ST_M45PE20:
14906 case FLASH_5720VENDOR_M_ST_M25PE40:
14907 case FLASH_5720VENDOR_M_ST_M45PE40:
14908 case FLASH_5720VENDOR_A_ST_M25PE40:
14909 case FLASH_5720VENDOR_A_ST_M45PE40:
14910 case FLASH_5720VENDOR_M_ST_M25PE80:
14911 case FLASH_5720VENDOR_M_ST_M45PE80:
14912 case FLASH_5720VENDOR_A_ST_M25PE80:
14913 case FLASH_5720VENDOR_A_ST_M45PE80:
14914 case FLASH_5720VENDOR_ST_25USPT:
14915 case FLASH_5720VENDOR_ST_45USPT:
14916 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014917 tg3_flag_set(tp, NVRAM_BUFFERED);
14918 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014919
14920 switch (nvmpinstrp) {
14921 case FLASH_5720VENDOR_M_ST_M25PE20:
14922 case FLASH_5720VENDOR_M_ST_M45PE20:
14923 case FLASH_5720VENDOR_A_ST_M25PE20:
14924 case FLASH_5720VENDOR_A_ST_M45PE20:
14925 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14926 break;
14927 case FLASH_5720VENDOR_M_ST_M25PE40:
14928 case FLASH_5720VENDOR_M_ST_M45PE40:
14929 case FLASH_5720VENDOR_A_ST_M25PE40:
14930 case FLASH_5720VENDOR_A_ST_M45PE40:
14931 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14932 break;
14933 case FLASH_5720VENDOR_M_ST_M25PE80:
14934 case FLASH_5720VENDOR_M_ST_M45PE80:
14935 case FLASH_5720VENDOR_A_ST_M25PE80:
14936 case FLASH_5720VENDOR_A_ST_M45PE80:
14937 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14938 break;
14939 default:
Joe Perches41535772013-02-16 11:20:04 +000014940 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014941 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014942 break;
14943 }
14944 break;
14945 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014946 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014947 return;
14948 }
14949
14950 tg3_nvram_get_pagesize(tp, nvcfg1);
14951 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014952 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014953
Joe Perches41535772013-02-16 11:20:04 +000014954 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014955 u32 val;
14956
14957 if (tg3_nvram_read(tp, 0, &val))
14958 return;
14959
14960 if (val != TG3_EEPROM_MAGIC &&
14961 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14962 tg3_flag_set(tp, NO_NVRAM);
14963 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014964}
14965
Linus Torvalds1da177e2005-04-16 15:20:36 -070014966/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014967static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014968{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014969 if (tg3_flag(tp, IS_SSB_CORE)) {
14970 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14971 tg3_flag_clear(tp, NVRAM);
14972 tg3_flag_clear(tp, NVRAM_BUFFERED);
14973 tg3_flag_set(tp, NO_NVRAM);
14974 return;
14975 }
14976
Linus Torvalds1da177e2005-04-16 15:20:36 -070014977 tw32_f(GRC_EEPROM_ADDR,
14978 (EEPROM_ADDR_FSM_RESET |
14979 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14980 EEPROM_ADDR_CLKPERD_SHIFT)));
14981
Michael Chan9d57f012006-12-07 00:23:25 -080014982 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983
14984 /* Enable seeprom accesses. */
14985 tw32_f(GRC_LOCAL_CTRL,
14986 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14987 udelay(100);
14988
Joe Perches41535772013-02-16 11:20:04 +000014989 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14990 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014991 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014992
Michael Chanec41c7d2006-01-17 02:40:55 -080014993 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014994 netdev_warn(tp->dev,
14995 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014996 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014997 return;
14998 }
Michael Chane6af3012005-04-21 17:12:05 -070014999 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000
Matt Carlson989a9d22007-05-05 11:51:05 -070015001 tp->nvram_size = 0;
15002
Joe Perches41535772013-02-16 11:20:04 +000015003 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070015004 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015005 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080015006 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015007 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
15008 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15009 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080015010 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015011 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070015012 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015013 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015014 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015015 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015016 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080015017 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015018 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15019 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015020 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015021 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15022 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000015023 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070015024 else
15025 tg3_get_nvram_info(tp);
15026
Matt Carlson989a9d22007-05-05 11:51:05 -070015027 if (tp->nvram_size == 0)
15028 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015029
Michael Chane6af3012005-04-21 17:12:05 -070015030 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080015031 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015032
15033 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015034 tg3_flag_clear(tp, NVRAM);
15035 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015036
15037 tg3_get_eeprom_size(tp);
15038 }
15039}
15040
Linus Torvalds1da177e2005-04-16 15:20:36 -070015041struct subsys_tbl_ent {
15042 u16 subsys_vendor, subsys_devid;
15043 u32 phy_id;
15044};
15045
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015046static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015048 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015049 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015050 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015051 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015052 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015053 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015054 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15055 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
15056 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015057 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015058 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015059 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015060 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15061 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
15062 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015063 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015064 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015065 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015066 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015067 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015068 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015069 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070
15071 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015072 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015073 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015074 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015075 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015076 { TG3PCI_SUBVENDOR_ID_3COM,
15077 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15078 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015079 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015080 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015081 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015082
15083 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015084 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015085 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015086 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015087 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015088 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015089 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015090 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015091 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015092
15093 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015094 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015095 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015096 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015097 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015098 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15099 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15100 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015101 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015102 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015103 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104
15105 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015106 { TG3PCI_SUBVENDOR_ID_IBM,
15107 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015108};
15109
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015110static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111{
15112 int i;
15113
15114 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15115 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15116 tp->pdev->subsystem_vendor) &&
15117 (subsys_id_to_phy_id[i].subsys_devid ==
15118 tp->pdev->subsystem_device))
15119 return &subsys_id_to_phy_id[i];
15120 }
15121 return NULL;
15122}
15123
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015124static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015125{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015126 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015127
Matt Carlson79eb6902010-02-17 15:17:03 +000015128 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015129 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15130
Gary Zambranoa85feb82007-05-05 11:52:19 -070015131 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015132 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15133 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015134
Joe Perches41535772013-02-16 11:20:04 +000015135 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015136 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015137 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15138 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015139 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015140 val = tr32(VCPU_CFGSHDW);
15141 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015142 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015143 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015144 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015145 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015146 device_set_wakeup_enable(&tp->pdev->dev, true);
15147 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015148 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015149 }
15150
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15152 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15153 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015154 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15155 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015156 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015157
15158 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15159 tp->nic_sram_data_cfg = nic_cfg;
15160
15161 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15162 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015163 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15164 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15165 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015166 (ver > 0) && (ver < 0x100))
15167 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15168
Joe Perches41535772013-02-16 11:20:04 +000015169 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015170 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15171
Nithin Sujir7c786062013-12-06 09:53:17 -080015172 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15173 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15174 tg3_asic_rev(tp) == ASIC_REV_5720)
15175 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15176
Linus Torvalds1da177e2005-04-16 15:20:36 -070015177 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15178 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15179 eeprom_phy_serdes = 1;
15180
15181 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15182 if (nic_phy_id != 0) {
15183 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15184 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15185
15186 eeprom_phy_id = (id1 >> 16) << 10;
15187 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15188 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15189 } else
15190 eeprom_phy_id = 0;
15191
Michael Chan7d0c41e2005-04-21 17:06:20 -070015192 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015193 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015194 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015195 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015196 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015197 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015198 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015199
Joe Perches63c3a662011-04-26 08:12:10 +000015200 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15202 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015203 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15205
15206 switch (led_cfg) {
15207 default:
15208 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15209 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15210 break;
15211
15212 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15213 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15214 break;
15215
15216 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15217 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015218
15219 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15220 * read on some older 5700/5701 bootcode.
15221 */
Joe Perches41535772013-02-16 11:20:04 +000015222 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15223 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015224 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15225
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226 break;
15227
15228 case SHASTA_EXT_LED_SHARED:
15229 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015230 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15231 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015232 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15233 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015234
15235 if (tg3_flag(tp, 5717_PLUS) ||
15236 tg3_asic_rev(tp) == ASIC_REV_5762)
15237 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15238 LED_CTRL_BLINK_RATE_MASK;
15239
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240 break;
15241
15242 case SHASTA_EXT_LED_MAC:
15243 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15244 break;
15245
15246 case SHASTA_EXT_LED_COMBO:
15247 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015248 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015249 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15250 LED_CTRL_MODE_PHY_2);
15251 break;
15252
Stephen Hemminger855e1112008-04-16 16:37:28 -070015253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015254
Joe Perches41535772013-02-16 11:20:04 +000015255 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15256 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015257 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15258 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15259
Joe Perches41535772013-02-16 11:20:04 +000015260 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015261 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015262
Michael Chan9d26e212006-12-07 00:21:14 -080015263 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015264 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015265 if ((tp->pdev->subsystem_vendor ==
15266 PCI_VENDOR_ID_ARIMA) &&
15267 (tp->pdev->subsystem_device == 0x205a ||
15268 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015269 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015270 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015271 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15272 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015274
15275 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015276 tg3_flag_set(tp, ENABLE_ASF);
15277 if (tg3_flag(tp, 5750_PLUS))
15278 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015279 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015280
15281 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015282 tg3_flag(tp, 5750_PLUS))
15283 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015284
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015285 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015286 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015287 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
Joe Perches63c3a662011-04-26 08:12:10 +000015289 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015290 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015291 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015292 device_set_wakeup_enable(&tp->pdev->dev, true);
15293 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015294
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015296 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297
15298 /* serdes signal pre-emphasis in register 0x590 set by */
15299 /* bootcode if bit 18 is set */
15300 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015301 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015302
Joe Perches63c3a662011-04-26 08:12:10 +000015303 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015304 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15305 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015306 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015307 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015308
Nithin Sujir942d1af2013-04-09 08:48:07 +000015309 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015310 u32 cfg3;
15311
15312 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015313 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15314 !tg3_flag(tp, 57765_PLUS) &&
15315 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015316 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015317 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15318 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15319 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15320 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015321 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015322
Matt Carlson14417062010-02-17 15:16:59 +000015323 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015324 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015325 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015326 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015327 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015328 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015329
15330 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15331 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015333done:
Joe Perches63c3a662011-04-26 08:12:10 +000015334 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015335 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015336 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015337 else
15338 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015339}
15340
Michael Chanc86a8562013-01-06 12:51:08 +000015341static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15342{
15343 int i, err;
15344 u32 val2, off = offset * 8;
15345
15346 err = tg3_nvram_lock(tp);
15347 if (err)
15348 return err;
15349
15350 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15351 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15352 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15353 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15354 udelay(10);
15355
15356 for (i = 0; i < 100; i++) {
15357 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15358 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15359 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15360 break;
15361 }
15362 udelay(10);
15363 }
15364
15365 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15366
15367 tg3_nvram_unlock(tp);
15368 if (val2 & APE_OTP_STATUS_CMD_DONE)
15369 return 0;
15370
15371 return -EBUSY;
15372}
15373
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015374static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015375{
15376 int i;
15377 u32 val;
15378
15379 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15380 tw32(OTP_CTRL, cmd);
15381
15382 /* Wait for up to 1 ms for command to execute. */
15383 for (i = 0; i < 100; i++) {
15384 val = tr32(OTP_STATUS);
15385 if (val & OTP_STATUS_CMD_DONE)
15386 break;
15387 udelay(10);
15388 }
15389
15390 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15391}
15392
15393/* Read the gphy configuration from the OTP region of the chip. The gphy
15394 * configuration is a 32-bit value that straddles the alignment boundary.
15395 * We do two 32-bit reads and then shift and merge the results.
15396 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015397static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015398{
15399 u32 bhalf_otp, thalf_otp;
15400
15401 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15402
15403 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15404 return 0;
15405
15406 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15407
15408 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15409 return 0;
15410
15411 thalf_otp = tr32(OTP_READ_DATA);
15412
15413 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15414
15415 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15416 return 0;
15417
15418 bhalf_otp = tr32(OTP_READ_DATA);
15419
15420 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15421}
15422
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015423static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015424{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015425 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015426
Nithin Sujir7c786062013-12-06 09:53:17 -080015427 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15428 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15429 adv |= ADVERTISED_1000baseT_Half;
15430 adv |= ADVERTISED_1000baseT_Full;
15431 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015432
15433 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15434 adv |= ADVERTISED_100baseT_Half |
15435 ADVERTISED_100baseT_Full |
15436 ADVERTISED_10baseT_Half |
15437 ADVERTISED_10baseT_Full |
15438 ADVERTISED_TP;
15439 else
15440 adv |= ADVERTISED_FIBRE;
15441
15442 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015443 tp->link_config.speed = SPEED_UNKNOWN;
15444 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015445 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015446 tp->link_config.active_speed = SPEED_UNKNOWN;
15447 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015448
15449 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015450}
15451
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015452static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015453{
15454 u32 hw_phy_id_1, hw_phy_id_2;
15455 u32 hw_phy_id, hw_phy_id_masked;
15456 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457
Matt Carlsone256f8a2011-03-09 16:58:24 +000015458 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015459 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015460 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15461
Michael Chan8151ad52012-07-29 19:15:41 +000015462 if (tg3_flag(tp, ENABLE_APE)) {
15463 switch (tp->pci_fn) {
15464 case 0:
15465 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15466 break;
15467 case 1:
15468 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15469 break;
15470 case 2:
15471 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15472 break;
15473 case 3:
15474 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15475 break;
15476 }
15477 }
15478
Nithin Sujir942d1af2013-04-09 08:48:07 +000015479 if (!tg3_flag(tp, ENABLE_ASF) &&
15480 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15481 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15482 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15483 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15484
Joe Perches63c3a662011-04-26 08:12:10 +000015485 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015486 return tg3_phy_init(tp);
15487
Linus Torvalds1da177e2005-04-16 15:20:36 -070015488 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015489 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015490 */
15491 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015492 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015493 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015494 } else {
15495 /* Now read the physical PHY_ID from the chip and verify
15496 * that it is sane. If it doesn't look good, we fall back
15497 * to either the hard-coded table based PHY_ID and failing
15498 * that the value found in the eeprom area.
15499 */
15500 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15501 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15502
15503 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15504 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15505 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15506
Matt Carlson79eb6902010-02-17 15:17:03 +000015507 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 }
15509
Matt Carlson79eb6902010-02-17 15:17:03 +000015510 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015511 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015512 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015513 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015514 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015515 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015516 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015517 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015518 /* Do nothing, phy ID already set up in
15519 * tg3_get_eeprom_hw_cfg().
15520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015521 } else {
15522 struct subsys_tbl_ent *p;
15523
15524 /* No eeprom signature? Try the hardcoded
15525 * subsys device table.
15526 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015527 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015528 if (p) {
15529 tp->phy_id = p->phy_id;
15530 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15531 /* For now we saw the IDs 0xbc050cd0,
15532 * 0xbc050f80 and 0xbc050c30 on devices
15533 * connected to an BCM4785 and there are
15534 * probably more. Just assume that the phy is
15535 * supported when it is connected to a SSB core
15536 * for now.
15537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540
Linus Torvalds1da177e2005-04-16 15:20:36 -070015541 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015542 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015543 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015544 }
15545 }
15546
Matt Carlsona6b68da2010-12-06 08:28:52 +000015547 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015548 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15549 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015550 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015551 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15552 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15553 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15554 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015555 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015556 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15557
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015558 tp->eee.supported = SUPPORTED_100baseT_Full |
15559 SUPPORTED_1000baseT_Full;
15560 tp->eee.advertised = ADVERTISED_100baseT_Full |
15561 ADVERTISED_1000baseT_Full;
15562 tp->eee.eee_enabled = 1;
15563 tp->eee.tx_lpi_enabled = 1;
15564 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15565 }
15566
Matt Carlsone256f8a2011-03-09 16:58:24 +000015567 tg3_phy_init_link_config(tp);
15568
Nithin Sujir942d1af2013-04-09 08:48:07 +000015569 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15570 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015571 !tg3_flag(tp, ENABLE_APE) &&
15572 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015573 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015574
15575 tg3_readphy(tp, MII_BMSR, &bmsr);
15576 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15577 (bmsr & BMSR_LSTATUS))
15578 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015579
Linus Torvalds1da177e2005-04-16 15:20:36 -070015580 err = tg3_phy_reset(tp);
15581 if (err)
15582 return err;
15583
Matt Carlson42b64a42011-05-19 12:12:49 +000015584 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015586 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015587 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15588 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015589
15590 tg3_writephy(tp, MII_BMCR,
15591 BMCR_ANENABLE | BMCR_ANRESTART);
15592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015593 }
15594
15595skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015596 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015597 err = tg3_init_5401phy_dsp(tp);
15598 if (err)
15599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015600
Linus Torvalds1da177e2005-04-16 15:20:36 -070015601 err = tg3_init_5401phy_dsp(tp);
15602 }
15603
Linus Torvalds1da177e2005-04-16 15:20:36 -070015604 return err;
15605}
15606
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015607static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015608{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015609 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015610 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015611 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015612 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015613
Matt Carlson535a4902011-07-20 10:20:56 +000015614 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015615 if (!vpd_data)
15616 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015617
Matt Carlson535a4902011-07-20 10:20:56 +000015618 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015619 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015620 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015621
15622 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15623 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15624 i += PCI_VPD_LRDT_TAG_SIZE;
15625
Matt Carlson535a4902011-07-20 10:20:56 +000015626 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015627 goto out_not_found;
15628
Matt Carlson184b8902010-04-05 10:19:25 +000015629 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15630 PCI_VPD_RO_KEYWORD_MFR_ID);
15631 if (j > 0) {
15632 len = pci_vpd_info_field_size(&vpd_data[j]);
15633
15634 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15635 if (j + len > block_end || len != 4 ||
15636 memcmp(&vpd_data[j], "1028", 4))
15637 goto partno;
15638
15639 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15640 PCI_VPD_RO_KEYWORD_VENDOR0);
15641 if (j < 0)
15642 goto partno;
15643
15644 len = pci_vpd_info_field_size(&vpd_data[j]);
15645
15646 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15647 if (j + len > block_end)
15648 goto partno;
15649
Kees Cook715230a2013-03-27 06:40:50 +000015650 if (len >= sizeof(tp->fw_ver))
15651 len = sizeof(tp->fw_ver) - 1;
15652 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15653 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15654 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015655 }
15656
15657partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015658 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15659 PCI_VPD_RO_KEYWORD_PARTNO);
15660 if (i < 0)
15661 goto out_not_found;
15662
15663 len = pci_vpd_info_field_size(&vpd_data[i]);
15664
15665 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15666 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015667 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015668 goto out_not_found;
15669
15670 memcpy(tp->board_part_number, &vpd_data[i], len);
15671
Linus Torvalds1da177e2005-04-16 15:20:36 -070015672out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015673 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015674 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015675 return;
15676
15677out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015678 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015679 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15680 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015681 strcpy(tp->board_part_number, "BCM5717");
15682 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15683 strcpy(tp->board_part_number, "BCM5718");
15684 else
15685 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015686 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015687 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15688 strcpy(tp->board_part_number, "BCM57780");
15689 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15690 strcpy(tp->board_part_number, "BCM57760");
15691 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15692 strcpy(tp->board_part_number, "BCM57790");
15693 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15694 strcpy(tp->board_part_number, "BCM57788");
15695 else
15696 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015697 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015698 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15699 strcpy(tp->board_part_number, "BCM57761");
15700 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15701 strcpy(tp->board_part_number, "BCM57765");
15702 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15703 strcpy(tp->board_part_number, "BCM57781");
15704 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15705 strcpy(tp->board_part_number, "BCM57785");
15706 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15707 strcpy(tp->board_part_number, "BCM57791");
15708 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15709 strcpy(tp->board_part_number, "BCM57795");
15710 else
15711 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015712 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015713 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15714 strcpy(tp->board_part_number, "BCM57762");
15715 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15716 strcpy(tp->board_part_number, "BCM57766");
15717 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15718 strcpy(tp->board_part_number, "BCM57782");
15719 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15720 strcpy(tp->board_part_number, "BCM57786");
15721 else
15722 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015723 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015724 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015725 } else {
15726nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015727 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015729}
15730
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015731static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015732{
15733 u32 val;
15734
Matt Carlsone4f34112009-02-25 14:25:00 +000015735 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015736 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015737 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015738 val != 0)
15739 return 0;
15740
15741 return 1;
15742}
15743
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015744static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015745{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015746 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015747 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015748 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015749
15750 if (tg3_nvram_read(tp, 0xc, &offset) ||
15751 tg3_nvram_read(tp, 0x4, &start))
15752 return;
15753
15754 offset = tg3_nvram_logical_addr(tp, offset);
15755
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015756 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015757 return;
15758
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015759 if ((val & 0xfc000000) == 0x0c000000) {
15760 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015761 return;
15762
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015763 if (val == 0)
15764 newver = true;
15765 }
15766
Matt Carlson75f99362010-04-05 10:19:24 +000015767 dst_off = strlen(tp->fw_ver);
15768
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015769 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015770 if (TG3_VER_SIZE - dst_off < 16 ||
15771 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015772 return;
15773
15774 offset = offset + ver_offset - start;
15775 for (i = 0; i < 16; i += 4) {
15776 __be32 v;
15777 if (tg3_nvram_read_be32(tp, offset + i, &v))
15778 return;
15779
Matt Carlson75f99362010-04-05 10:19:24 +000015780 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015781 }
15782 } else {
15783 u32 major, minor;
15784
15785 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15786 return;
15787
15788 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15789 TG3_NVM_BCVER_MAJSFT;
15790 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015791 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15792 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015793 }
15794}
15795
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015796static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015797{
15798 u32 val, major, minor;
15799
15800 /* Use native endian representation */
15801 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15802 return;
15803
15804 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15805 TG3_NVM_HWSB_CFG1_MAJSFT;
15806 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15807 TG3_NVM_HWSB_CFG1_MINSFT;
15808
15809 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15810}
15811
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015812static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015813{
15814 u32 offset, major, minor, build;
15815
Matt Carlson75f99362010-04-05 10:19:24 +000015816 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015817
15818 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15819 return;
15820
15821 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15822 case TG3_EEPROM_SB_REVISION_0:
15823 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15824 break;
15825 case TG3_EEPROM_SB_REVISION_2:
15826 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15827 break;
15828 case TG3_EEPROM_SB_REVISION_3:
15829 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15830 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015831 case TG3_EEPROM_SB_REVISION_4:
15832 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15833 break;
15834 case TG3_EEPROM_SB_REVISION_5:
15835 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15836 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015837 case TG3_EEPROM_SB_REVISION_6:
15838 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15839 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015840 default:
15841 return;
15842 }
15843
Matt Carlsone4f34112009-02-25 14:25:00 +000015844 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015845 return;
15846
15847 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15848 TG3_EEPROM_SB_EDH_BLD_SHFT;
15849 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15850 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15851 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15852
15853 if (minor > 99 || build > 26)
15854 return;
15855
Matt Carlson75f99362010-04-05 10:19:24 +000015856 offset = strlen(tp->fw_ver);
15857 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15858 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015859
15860 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015861 offset = strlen(tp->fw_ver);
15862 if (offset < TG3_VER_SIZE - 1)
15863 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015864 }
15865}
15866
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015867static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015868{
15869 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015870 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015871
15872 for (offset = TG3_NVM_DIR_START;
15873 offset < TG3_NVM_DIR_END;
15874 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015875 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015876 return;
15877
15878 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15879 break;
15880 }
15881
15882 if (offset == TG3_NVM_DIR_END)
15883 return;
15884
Joe Perches63c3a662011-04-26 08:12:10 +000015885 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015886 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015887 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015888 return;
15889
Matt Carlsone4f34112009-02-25 14:25:00 +000015890 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015891 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015892 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015893 return;
15894
15895 offset += val - start;
15896
Matt Carlsonacd9c112009-02-25 14:26:33 +000015897 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015898
Matt Carlsonacd9c112009-02-25 14:26:33 +000015899 tp->fw_ver[vlen++] = ',';
15900 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015901
15902 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015903 __be32 v;
15904 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015905 return;
15906
Al Virob9fc7dc2007-12-17 22:59:57 -080015907 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015908
Matt Carlsonacd9c112009-02-25 14:26:33 +000015909 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15910 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015911 break;
15912 }
15913
Matt Carlsonacd9c112009-02-25 14:26:33 +000015914 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15915 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015916 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015917}
15918
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015919static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015920{
Matt Carlson7fd76442009-02-25 14:27:20 +000015921 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015922
15923 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15924 if (apedata != APE_SEG_SIG_MAGIC)
15925 return;
15926
15927 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15928 if (!(apedata & APE_FW_STATUS_READY))
15929 return;
15930
Michael Chan165f4d12012-07-16 16:23:59 +000015931 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15932 tg3_flag_set(tp, APE_HAS_NCSI);
15933}
15934
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015935static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015936{
15937 int vlen;
15938 u32 apedata;
15939 char *fwtype;
15940
Matt Carlson7fd76442009-02-25 14:27:20 +000015941 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15942
Michael Chan165f4d12012-07-16 16:23:59 +000015943 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015944 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015945 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15946 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015947 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015948 fwtype = "DASH";
15949
Matt Carlson7fd76442009-02-25 14:27:20 +000015950 vlen = strlen(tp->fw_ver);
15951
Matt Carlsonecc79642010-08-02 11:26:01 +000015952 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15953 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015954 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15955 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15956 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15957 (apedata & APE_FW_VERSION_BLDMSK));
15958}
15959
Michael Chanc86a8562013-01-06 12:51:08 +000015960static void tg3_read_otp_ver(struct tg3 *tp)
15961{
15962 u32 val, val2;
15963
Joe Perches41535772013-02-16 11:20:04 +000015964 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015965 return;
15966
15967 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15968 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15969 TG3_OTP_MAGIC0_VALID(val)) {
15970 u64 val64 = (u64) val << 32 | val2;
15971 u32 ver = 0;
15972 int i, vlen;
15973
15974 for (i = 0; i < 7; i++) {
15975 if ((val64 & 0xff) == 0)
15976 break;
15977 ver = val64 & 0xff;
15978 val64 >>= 8;
15979 }
15980 vlen = strlen(tp->fw_ver);
15981 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15982 }
15983}
15984
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015985static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015986{
15987 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015988 bool vpd_vers = false;
15989
15990 if (tp->fw_ver[0] != 0)
15991 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015992
Joe Perches63c3a662011-04-26 08:12:10 +000015993 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015994 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015995 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015996 return;
15997 }
15998
Matt Carlsonacd9c112009-02-25 14:26:33 +000015999 if (tg3_nvram_read(tp, 0, &val))
16000 return;
16001
16002 if (val == TG3_EEPROM_MAGIC)
16003 tg3_read_bc_ver(tp);
16004 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
16005 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000016006 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
16007 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000016008
Michael Chan165f4d12012-07-16 16:23:59 +000016009 if (tg3_flag(tp, ENABLE_ASF)) {
16010 if (tg3_flag(tp, ENABLE_APE)) {
16011 tg3_probe_ncsi(tp);
16012 if (!vpd_vers)
16013 tg3_read_dash_ver(tp);
16014 } else if (!vpd_vers) {
16015 tg3_read_mgmtfw_ver(tp);
16016 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000016017 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070016018
16019 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080016020}
16021
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016022static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
16023{
Joe Perches63c3a662011-04-26 08:12:10 +000016024 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016025 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000016026 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016027 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016028 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000016029 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016030}
16031
Benoit Taine9baa3c32014-08-08 15:56:03 +020016032static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080016033 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
16034 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
16035 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
16036 { },
16037};
16038
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016039static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000016040{
16041 struct pci_dev *peer;
16042 unsigned int func, devnr = tp->pdev->devfn & ~7;
16043
16044 for (func = 0; func < 8; func++) {
16045 peer = pci_get_slot(tp->pdev->bus, devnr | func);
16046 if (peer && peer != tp->pdev)
16047 break;
16048 pci_dev_put(peer);
16049 }
16050 /* 5704 can be configured in single-port mode, set peer to
16051 * tp->pdev in that case.
16052 */
16053 if (!peer) {
16054 peer = tp->pdev;
16055 return peer;
16056 }
16057
16058 /*
16059 * We don't need to keep the refcount elevated; there's no way
16060 * to remove one half of this device without removing the other
16061 */
16062 pci_dev_put(peer);
16063
16064 return peer;
16065}
16066
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016067static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000016068{
16069 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000016070 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016071 u32 reg;
16072
16073 /* All devices that use the alternate
16074 * ASIC REV location have a CPMU.
16075 */
16076 tg3_flag_set(tp, CPMU_PRESENT);
16077
16078 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016079 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016080 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16081 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016082 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016083 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16084 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016085 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16086 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016087 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16088 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016089 reg = TG3PCI_GEN2_PRODID_ASICREV;
16090 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16091 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16092 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16093 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16094 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16095 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16096 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16097 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16098 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16100 reg = TG3PCI_GEN15_PRODID_ASICREV;
16101 else
16102 reg = TG3PCI_PRODID_ASICREV;
16103
16104 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16105 }
16106
16107 /* Wrong chip ID in 5752 A0. This code can be removed later
16108 * as A0 is not in production.
16109 */
Joe Perches41535772013-02-16 11:20:04 +000016110 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016111 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16112
Joe Perches41535772013-02-16 11:20:04 +000016113 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016114 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16115
Joe Perches41535772013-02-16 11:20:04 +000016116 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16117 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16118 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016119 tg3_flag_set(tp, 5717_PLUS);
16120
Joe Perches41535772013-02-16 11:20:04 +000016121 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16122 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016123 tg3_flag_set(tp, 57765_CLASS);
16124
Michael Chanc65a17f2013-01-06 12:51:07 +000016125 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016126 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016127 tg3_flag_set(tp, 57765_PLUS);
16128
16129 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016130 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16131 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16132 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16133 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16134 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16135 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016136 tg3_flag(tp, 57765_PLUS))
16137 tg3_flag_set(tp, 5755_PLUS);
16138
Joe Perches41535772013-02-16 11:20:04 +000016139 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16140 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016141 tg3_flag_set(tp, 5780_CLASS);
16142
Joe Perches41535772013-02-16 11:20:04 +000016143 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16144 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16145 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016146 tg3_flag(tp, 5755_PLUS) ||
16147 tg3_flag(tp, 5780_CLASS))
16148 tg3_flag_set(tp, 5750_PLUS);
16149
Joe Perches41535772013-02-16 11:20:04 +000016150 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016151 tg3_flag(tp, 5750_PLUS))
16152 tg3_flag_set(tp, 5705_PLUS);
16153}
16154
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016155static bool tg3_10_100_only_device(struct tg3 *tp,
16156 const struct pci_device_id *ent)
16157{
16158 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16159
Joe Perches41535772013-02-16 11:20:04 +000016160 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16161 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016162 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16163 return true;
16164
16165 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016166 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016167 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16168 return true;
16169 } else {
16170 return true;
16171 }
16172 }
16173
16174 return false;
16175}
16176
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016177static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016178{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016179 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016180 u32 pci_state_reg, grc_misc_cfg;
16181 u32 val;
16182 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016184
Linus Torvalds1da177e2005-04-16 15:20:36 -070016185 /* Force memory write invalidate off. If we leave it on,
16186 * then on 5700_BX chips we have to enable a workaround.
16187 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16188 * to match the cacheline size. The Broadcom driver have this
16189 * workaround but turns MWI off all the times so never uses
16190 * it. This seems to suggest that the workaround is insufficient.
16191 */
16192 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16193 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16194 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16195
Matt Carlson16821282011-07-13 09:27:28 +000016196 /* Important! -- Make sure register accesses are byteswapped
16197 * correctly. Also, for those chips that require it, make
16198 * sure that indirect register accesses are enabled before
16199 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016200 */
16201 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16202 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016203 tp->misc_host_ctrl |= (misc_ctrl_reg &
16204 MISC_HOST_CTRL_CHIPREV);
16205 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16206 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016207
Matt Carlson42b123b2012-02-13 15:20:13 +000016208 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016209
Michael Chan68929142005-08-09 20:17:14 -070016210 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16211 * we need to disable memory and use config. cycles
16212 * only to access all registers. The 5702/03 chips
16213 * can mistakenly decode the special cycles from the
16214 * ICH chipsets as memory write cycles, causing corruption
16215 * of register and memory space. Only certain ICH bridges
16216 * will drive special cycles with non-zero data during the
16217 * address phase which can fall within the 5703's address
16218 * range. This is not an ICH bug as the PCI spec allows
16219 * non-zero address during special cycles. However, only
16220 * these ICH bridges are known to drive non-zero addresses
16221 * during special cycles.
16222 *
16223 * Since special cycles do not cross PCI bridges, we only
16224 * enable this workaround if the 5703 is on the secondary
16225 * bus of these ICH bridges.
16226 */
Joe Perches41535772013-02-16 11:20:04 +000016227 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16228 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016229 static struct tg3_dev_id {
16230 u32 vendor;
16231 u32 device;
16232 u32 rev;
16233 } ich_chipsets[] = {
16234 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16235 PCI_ANY_ID },
16236 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16237 PCI_ANY_ID },
16238 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16239 0xa },
16240 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16241 PCI_ANY_ID },
16242 { },
16243 };
16244 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16245 struct pci_dev *bridge = NULL;
16246
16247 while (pci_id->vendor != 0) {
16248 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16249 bridge);
16250 if (!bridge) {
16251 pci_id++;
16252 continue;
16253 }
16254 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016255 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016256 continue;
16257 }
16258 if (bridge->subordinate &&
16259 (bridge->subordinate->number ==
16260 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016261 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016262 pci_dev_put(bridge);
16263 break;
16264 }
16265 }
16266 }
16267
Joe Perches41535772013-02-16 11:20:04 +000016268 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016269 static struct tg3_dev_id {
16270 u32 vendor;
16271 u32 device;
16272 } bridge_chipsets[] = {
16273 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16274 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16275 { },
16276 };
16277 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16278 struct pci_dev *bridge = NULL;
16279
16280 while (pci_id->vendor != 0) {
16281 bridge = pci_get_device(pci_id->vendor,
16282 pci_id->device,
16283 bridge);
16284 if (!bridge) {
16285 pci_id++;
16286 continue;
16287 }
16288 if (bridge->subordinate &&
16289 (bridge->subordinate->number <=
16290 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016291 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016292 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016293 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016294 pci_dev_put(bridge);
16295 break;
16296 }
16297 }
16298 }
16299
Michael Chan4a29cc22006-03-19 13:21:12 -080016300 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16301 * DMA addresses > 40-bit. This bridge may have other additional
16302 * 57xx devices behind it in some 4-port NIC designs for example.
16303 * Any tg3 device found behind the bridge will also need the 40-bit
16304 * DMA workaround.
16305 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016306 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016307 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016308 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016309 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016310 struct pci_dev *bridge = NULL;
16311
16312 do {
16313 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16314 PCI_DEVICE_ID_SERVERWORKS_EPB,
16315 bridge);
16316 if (bridge && bridge->subordinate &&
16317 (bridge->subordinate->number <=
16318 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016319 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016320 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016321 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016322 pci_dev_put(bridge);
16323 break;
16324 }
16325 } while (bridge);
16326 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016327
Joe Perches41535772013-02-16 11:20:04 +000016328 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16329 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016330 tp->pdev_peer = tg3_find_peer(tp);
16331
Matt Carlson507399f2009-11-13 13:03:37 +000016332 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016333 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016334 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016335 else if (tg3_flag(tp, 57765_PLUS))
16336 tg3_flag_set(tp, HW_TSO_3);
16337 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016338 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016339 tg3_flag_set(tp, HW_TSO_2);
16340 else if (tg3_flag(tp, 5750_PLUS)) {
16341 tg3_flag_set(tp, HW_TSO_1);
16342 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016343 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16344 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016345 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016346 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16347 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16348 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016349 tg3_flag_set(tp, FW_TSO);
16350 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016351 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016352 tp->fw_needed = FIRMWARE_TG3TSO5;
16353 else
16354 tp->fw_needed = FIRMWARE_TG3TSO;
16355 }
16356
Matt Carlsondabc5c62011-05-19 12:12:52 +000016357 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016358 if (tg3_flag(tp, HW_TSO_1) ||
16359 tg3_flag(tp, HW_TSO_2) ||
16360 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016361 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016362 /* For firmware TSO, assume ASF is disabled.
16363 * We'll disable TSO later if we discover ASF
16364 * is enabled in tg3_get_eeprom_hw_cfg().
16365 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016366 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016367 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016368 tg3_flag_clear(tp, TSO_CAPABLE);
16369 tg3_flag_clear(tp, TSO_BUG);
16370 tp->fw_needed = NULL;
16371 }
16372
Joe Perches41535772013-02-16 11:20:04 +000016373 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016374 tp->fw_needed = FIRMWARE_TG3;
16375
Nithin Sujirc4dab502013-03-06 17:02:34 +000016376 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16377 tp->fw_needed = FIRMWARE_TG357766;
16378
Matt Carlson507399f2009-11-13 13:03:37 +000016379 tp->irq_max = 1;
16380
Joe Perches63c3a662011-04-26 08:12:10 +000016381 if (tg3_flag(tp, 5750_PLUS)) {
16382 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016383 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16384 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16385 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16386 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016387 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016388 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016389
Joe Perches63c3a662011-04-26 08:12:10 +000016390 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016391 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016392 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016394
Joe Perches63c3a662011-04-26 08:12:10 +000016395 if (tg3_flag(tp, 57765_PLUS)) {
16396 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016397 tp->irq_max = TG3_IRQ_MAX_VECS;
16398 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016399 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016400
Michael Chan91024262012-09-28 07:12:38 +000016401 tp->txq_max = 1;
16402 tp->rxq_max = 1;
16403 if (tp->irq_max > 1) {
16404 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16405 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16406
Joe Perches41535772013-02-16 11:20:04 +000016407 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16408 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016409 tp->txq_max = tp->irq_max - 1;
16410 }
16411
Matt Carlsonb7abee62012-06-07 12:56:54 +000016412 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016413 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016414 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016415
Joe Perches41535772013-02-16 11:20:04 +000016416 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016417 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016418
Joe Perches41535772013-02-16 11:20:04 +000016419 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16420 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16421 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16422 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016423 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016424
Joe Perches63c3a662011-04-26 08:12:10 +000016425 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016426 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016427 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016428
Joe Perches63c3a662011-04-26 08:12:10 +000016429 if (!tg3_flag(tp, 5705_PLUS) ||
16430 tg3_flag(tp, 5780_CLASS) ||
16431 tg3_flag(tp, USE_JUMBO_BDFLAG))
16432 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016433
Matt Carlson52f44902008-11-21 17:17:04 -080016434 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16435 &pci_state_reg);
16436
Jon Mason708ebb3a2011-06-27 12:56:50 +000016437 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016438 u16 lnkctl;
16439
Joe Perches63c3a662011-04-26 08:12:10 +000016440 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016441
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016442 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016443 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016444 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016445 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016446 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016447 }
Joe Perches41535772013-02-16 11:20:04 +000016448 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16449 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16450 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16451 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016452 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016453 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016454 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016455 }
Joe Perches41535772013-02-16 11:20:04 +000016456 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016457 /* BCM5785 devices are effectively PCIe devices, and should
16458 * follow PCIe codepaths, but do not have a PCIe capabilities
16459 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016460 */
Joe Perches63c3a662011-04-26 08:12:10 +000016461 tg3_flag_set(tp, PCI_EXPRESS);
16462 } else if (!tg3_flag(tp, 5705_PLUS) ||
16463 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016464 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16465 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016466 dev_err(&tp->pdev->dev,
16467 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016468 return -EIO;
16469 }
16470
16471 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016472 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016474
Michael Chan399de502005-10-03 14:02:39 -070016475 /* If we have an AMD 762 or VIA K8T800 chipset, write
16476 * reordering to the mailbox registers done by the host
16477 * controller can cause major troubles. We read back from
16478 * every mailbox register write to force the writes to be
16479 * posted to the chip in order.
16480 */
Matt Carlson41434702011-03-09 16:58:22 +000016481 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016482 !tg3_flag(tp, PCI_EXPRESS))
16483 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016484
Matt Carlson69fc4052008-12-21 20:19:57 -080016485 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16486 &tp->pci_cacheline_sz);
16487 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16488 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016489 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016490 tp->pci_lat_timer < 64) {
16491 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016492 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16493 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016494 }
16495
Matt Carlson16821282011-07-13 09:27:28 +000016496 /* Important! -- It is critical that the PCI-X hw workaround
16497 * situation is decided before the first MMIO register access.
16498 */
Joe Perches41535772013-02-16 11:20:04 +000016499 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016500 /* 5700 BX chips need to have their TX producer index
16501 * mailboxes written twice to workaround a bug.
16502 */
Joe Perches63c3a662011-04-26 08:12:10 +000016503 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016504
Matt Carlson52f44902008-11-21 17:17:04 -080016505 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016506 *
16507 * The workaround is to use indirect register accesses
16508 * for all chip writes not to mailbox registers.
16509 */
Joe Perches63c3a662011-04-26 08:12:10 +000016510 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016511 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016512
Joe Perches63c3a662011-04-26 08:12:10 +000016513 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016514
16515 /* The chip can have it's power management PCI config
16516 * space registers clobbered due to this bug.
16517 * So explicitly force the chip into D0 here.
16518 */
Matt Carlson9974a352007-10-07 23:27:28 -070016519 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016520 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016521 &pm_reg);
16522 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16523 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016524 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016525 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016526 pm_reg);
16527
16528 /* Also, force SERR#/PERR# in PCI command. */
16529 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16530 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16531 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16532 }
16533 }
16534
Linus Torvalds1da177e2005-04-16 15:20:36 -070016535 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016536 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016537 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016538 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016539
16540 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016541 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016542 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16543 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16544 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16545 }
16546
Michael Chan1ee582d2005-08-09 20:16:46 -070016547 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016548 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016549 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016550 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016551 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016552 tp->write32_tx_mbox = tg3_write32;
16553 tp->write32_rx_mbox = tg3_write32;
16554
16555 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016556 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016557 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016558 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016559 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016560 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016561 /*
16562 * Back to back register writes can cause problems on these
16563 * chips, the workaround is to read back all reg writes
16564 * except those to mailbox regs.
16565 *
16566 * See tg3_write_indirect_reg32().
16567 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016568 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016569 }
16570
Joe Perches63c3a662011-04-26 08:12:10 +000016571 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016572 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016573 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016574 tp->write32_rx_mbox = tg3_write_flush_reg32;
16575 }
Michael Chan20094932005-08-09 20:16:32 -070016576
Joe Perches63c3a662011-04-26 08:12:10 +000016577 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016578 tp->read32 = tg3_read_indirect_reg32;
16579 tp->write32 = tg3_write_indirect_reg32;
16580 tp->read32_mbox = tg3_read_indirect_mbox;
16581 tp->write32_mbox = tg3_write_indirect_mbox;
16582 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16583 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16584
16585 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016586 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016587
16588 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16589 pci_cmd &= ~PCI_COMMAND_MEMORY;
16590 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16591 }
Joe Perches41535772013-02-16 11:20:04 +000016592 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016593 tp->read32_mbox = tg3_read32_mbox_5906;
16594 tp->write32_mbox = tg3_write32_mbox_5906;
16595 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16596 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16597 }
Michael Chan68929142005-08-09 20:17:14 -070016598
Michael Chanbbadf502006-04-06 21:46:34 -070016599 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016600 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016601 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16602 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016603 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016604
Matt Carlson16821282011-07-13 09:27:28 +000016605 /* The memory arbiter has to be enabled in order for SRAM accesses
16606 * to succeed. Normally on powerup the tg3 chip firmware will make
16607 * sure it is enabled, but other entities such as system netboot
16608 * code might disable it.
16609 */
16610 val = tr32(MEMARB_MODE);
16611 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16612
Matt Carlson9dc5e342011-11-04 09:15:02 +000016613 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016614 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016615 tg3_flag(tp, 5780_CLASS)) {
16616 if (tg3_flag(tp, PCIX_MODE)) {
16617 pci_read_config_dword(tp->pdev,
16618 tp->pcix_cap + PCI_X_STATUS,
16619 &val);
16620 tp->pci_fn = val & 0x7;
16621 }
Joe Perches41535772013-02-16 11:20:04 +000016622 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16623 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16624 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016625 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016626 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16627 val = tr32(TG3_CPMU_STATUS);
16628
Joe Perches41535772013-02-16 11:20:04 +000016629 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016630 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16631 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016632 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16633 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016634 }
16635
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016636 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16637 tp->write32_tx_mbox = tg3_write_flush_reg32;
16638 tp->write32_rx_mbox = tg3_write_flush_reg32;
16639 }
16640
Michael Chan7d0c41e2005-04-21 17:06:20 -070016641 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016642 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016643 * determined before calling tg3_set_power_state() so that
16644 * we know whether or not to switch out of Vaux power.
16645 * When the flag is set, it means that GPIO1 is used for eeprom
16646 * write protect and also implies that it is a LOM where GPIOs
16647 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016648 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016649 tg3_get_eeprom_hw_cfg(tp);
16650
Matt Carlson1caf13e2013-03-06 17:02:29 +000016651 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016652 tg3_flag_clear(tp, TSO_CAPABLE);
16653 tg3_flag_clear(tp, TSO_BUG);
16654 tp->fw_needed = NULL;
16655 }
16656
Joe Perches63c3a662011-04-26 08:12:10 +000016657 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016658 /* Allow reads and writes to the
16659 * APE register and memory space.
16660 */
16661 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016662 PCISTATE_ALLOW_APE_SHMEM_WR |
16663 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016664 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16665 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016666
16667 tg3_ape_lock_init(tp);
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053016668 tp->ape_hb_interval =
16669 msecs_to_jiffies(APE_HOST_HEARTBEAT_INT_5SEC);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016670 }
16671
Matt Carlson16821282011-07-13 09:27:28 +000016672 /* Set up tp->grc_local_ctrl before calling
16673 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16674 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016675 * It is also used as eeprom write protect on LOMs.
16676 */
16677 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016678 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016679 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016680 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16681 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016682 /* Unused GPIO3 must be driven as output on 5752 because there
16683 * are no pull-up resistors on unused GPIO pins.
16684 */
Joe Perches41535772013-02-16 11:20:04 +000016685 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016686 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016687
Joe Perches41535772013-02-16 11:20:04 +000016688 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16689 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016690 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016691 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16692
Matt Carlson8d519ab2009-04-20 06:58:01 +000016693 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16694 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016695 /* Turn off the debug UART. */
16696 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016697 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016698 /* Keep VMain power. */
16699 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16700 GRC_LCLCTRL_GPIO_OUTPUT0;
16701 }
16702
Joe Perches41535772013-02-16 11:20:04 +000016703 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016704 tp->grc_local_ctrl |=
16705 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16706
Matt Carlson16821282011-07-13 09:27:28 +000016707 /* Switch out of Vaux if it is a NIC */
16708 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016709
Linus Torvalds1da177e2005-04-16 15:20:36 -070016710 /* Derive initial jumbo mode from MTU assigned in
16711 * ether_setup() via the alloc_etherdev() call
16712 */
Joe Perches63c3a662011-04-26 08:12:10 +000016713 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16714 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016715
16716 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016717 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16718 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16719 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16720 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016721 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016722 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016723 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016724 }
16725
Joe Perches41535772013-02-16 11:20:04 +000016726 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016727 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016728
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016730 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16731 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16732 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16733 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016734 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16735 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16736 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016737
Joe Perches41535772013-02-16 11:20:04 +000016738 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16739 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016740 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016741 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016742 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016743
Joe Perches63c3a662011-04-26 08:12:10 +000016744 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016745 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016746 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16747 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016748 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016749 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16750 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16751 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16752 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016753 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16754 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016755 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016756 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016757 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016758 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016759 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016761
Joe Perches41535772013-02-16 11:20:04 +000016762 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16763 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016764 tp->phy_otp = tg3_read_otp_phycfg(tp);
16765 if (tp->phy_otp == 0)
16766 tp->phy_otp = TG3_OTP_DEFAULT;
16767 }
16768
Joe Perches63c3a662011-04-26 08:12:10 +000016769 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016770 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16771 else
16772 tp->mi_mode = MAC_MI_MODE_BASE;
16773
Linus Torvalds1da177e2005-04-16 15:20:36 -070016774 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016775 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16776 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016777 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16778
Matt Carlson4d958472011-04-20 07:57:35 +000016779 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016780 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016781 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016782 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16783 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016784 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16785 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16786 }
16787
Joe Perches41535772013-02-16 11:20:04 +000016788 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16789 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016790 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016791
Matt Carlson158d7ab2008-05-29 01:37:54 -070016792 err = tg3_mdio_init(tp);
16793 if (err)
16794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016795
16796 /* Initialize data/descriptor byte/word swapping. */
16797 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016798 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16799 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016800 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16801 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16802 GRC_MODE_B2HRX_ENABLE |
16803 GRC_MODE_HTX2B_ENABLE |
16804 GRC_MODE_HOST_STACKUP);
16805 else
16806 val &= GRC_MODE_HOST_STACKUP;
16807
Linus Torvalds1da177e2005-04-16 15:20:36 -070016808 tw32(GRC_MODE, val | tp->grc_mode);
16809
16810 tg3_switch_clocks(tp);
16811
16812 /* Clear this out for sanity. */
16813 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16814
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016815 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16816 tw32(TG3PCI_REG_BASE_ADDR, 0);
16817
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16819 &pci_state_reg);
16820 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016821 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016822 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16823 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16824 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16825 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016826 void __iomem *sram_base;
16827
16828 /* Write some dummy words into the SRAM status block
16829 * area, see if it reads back correctly. If the return
16830 * value is bad, force enable the PCIX workaround.
16831 */
16832 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16833
16834 writel(0x00000000, sram_base);
16835 writel(0x00000000, sram_base + 4);
16836 writel(0xffffffff, sram_base + 4);
16837 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016838 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016839 }
16840 }
16841
16842 udelay(50);
16843 tg3_nvram_init(tp);
16844
Nithin Sujirc4dab502013-03-06 17:02:34 +000016845 /* If the device has an NVRAM, no need to load patch firmware */
16846 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16847 !tg3_flag(tp, NO_NVRAM))
16848 tp->fw_needed = NULL;
16849
Linus Torvalds1da177e2005-04-16 15:20:36 -070016850 grc_misc_cfg = tr32(GRC_MISC_CFG);
16851 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16852
Joe Perches41535772013-02-16 11:20:04 +000016853 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016854 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16855 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016856 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016857
Joe Perches63c3a662011-04-26 08:12:10 +000016858 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016859 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016860 tg3_flag_set(tp, TAGGED_STATUS);
16861 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016862 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16863 HOSTCC_MODE_CLRTICK_TXBD);
16864
16865 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16866 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16867 tp->misc_host_ctrl);
16868 }
16869
Matt Carlson3bda1252008-08-15 14:08:22 -070016870 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016871 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016872 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016873 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016874 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016875
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016876 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016877 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016878
16879 err = tg3_phy_probe(tp);
16880 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016881 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016882 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016883 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016884 }
16885
Matt Carlson184b8902010-04-05 10:19:25 +000016886 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016887 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016888
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016889 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16890 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016891 } else {
Joe Perches41535772013-02-16 11:20:04 +000016892 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016893 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016894 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016895 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016896 }
16897
16898 /* 5700 {AX,BX} chips have a broken status block link
16899 * change bit implementation, so we must use the
16900 * status register in those cases.
16901 */
Joe Perches41535772013-02-16 11:20:04 +000016902 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016903 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016904 else
Joe Perches63c3a662011-04-26 08:12:10 +000016905 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016906
16907 /* The led_ctrl is set during tg3_phy_probe, here we might
16908 * have to force the link status polling mechanism based
16909 * upon subsystem IDs.
16910 */
16911 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016912 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016913 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16914 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016915 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016916 }
16917
16918 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016919 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016920 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016921 else
Joe Perches63c3a662011-04-26 08:12:10 +000016922 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016923
Nithin Sujir1743b832014-01-03 10:09:14 -080016924 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16925 tg3_flag_set(tp, POLL_CPMU_LINK);
16926
Eric Dumazet9205fd92011-11-18 06:47:01 +000016927 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016928 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016929 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016930 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016931 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016932#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016933 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016934#endif
16935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016936
Matt Carlson2c49a442010-09-30 10:34:35 +000016937 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16938 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016939 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16940
Matt Carlson2c49a442010-09-30 10:34:35 +000016941 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016942
16943 /* Increment the rx prod index on the rx std ring by at most
16944 * 8 for these chips to workaround hw errata.
16945 */
Joe Perches41535772013-02-16 11:20:04 +000016946 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16947 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16948 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016949 tp->rx_std_max_post = 8;
16950
Joe Perches63c3a662011-04-26 08:12:10 +000016951 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016952 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16953 PCIE_PWR_MGMT_L1_THRESH_MSK;
16954
Linus Torvalds1da177e2005-04-16 15:20:36 -070016955 return err;
16956}
16957
David S. Miller49b6e95f2007-03-29 01:38:42 -070016958#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016959static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016960{
16961 struct net_device *dev = tp->dev;
16962 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016963 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016964 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016965 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016966
David S. Miller49b6e95f2007-03-29 01:38:42 -070016967 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016968 if (addr && len == ETH_ALEN) {
16969 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016971 }
16972 return -ENODEV;
16973}
16974
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016975static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016976{
16977 struct net_device *dev = tp->dev;
16978
Joe Perchesd458cdf2013-10-01 19:04:40 -070016979 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016980 return 0;
16981}
16982#endif
16983
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016984static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016985{
16986 struct net_device *dev = tp->dev;
16987 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016988 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016989 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016990
David S. Miller49b6e95f2007-03-29 01:38:42 -070016991#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016992 if (!tg3_get_macaddr_sparc(tp))
16993 return 0;
16994#endif
16995
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016996 if (tg3_flag(tp, IS_SSB_CORE)) {
16997 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16998 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16999 return 0;
17000 }
17001
Linus Torvalds1da177e2005-04-16 15:20:36 -070017002 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000017003 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000017004 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017005 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
17006 mac_offset = 0xcc;
17007 if (tg3_nvram_lock(tp))
17008 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
17009 else
17010 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000017011 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000017012 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000017013 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000017014 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000017015 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000017016 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070017017 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017018
17019 /* First try to get it from MAC address mailbox. */
17020 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
17021 if ((hi >> 16) == 0x484b) {
17022 dev->dev_addr[0] = (hi >> 8) & 0xff;
17023 dev->dev_addr[1] = (hi >> 0) & 0xff;
17024
17025 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
17026 dev->dev_addr[2] = (lo >> 24) & 0xff;
17027 dev->dev_addr[3] = (lo >> 16) & 0xff;
17028 dev->dev_addr[4] = (lo >> 8) & 0xff;
17029 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017030
Michael Chan008652b2006-03-27 23:14:53 -080017031 /* Some old bootcode may report a 0 MAC address in SRAM */
17032 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
17033 }
17034 if (!addr_ok) {
17035 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000017036 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000017037 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000017038 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070017039 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
17040 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080017041 }
17042 /* Finally just fetch it out of the MAC control regs. */
17043 else {
17044 hi = tr32(MAC_ADDR_0_HIGH);
17045 lo = tr32(MAC_ADDR_0_LOW);
17046
17047 dev->dev_addr[5] = lo & 0xff;
17048 dev->dev_addr[4] = (lo >> 8) & 0xff;
17049 dev->dev_addr[3] = (lo >> 16) & 0xff;
17050 dev->dev_addr[2] = (lo >> 24) & 0xff;
17051 dev->dev_addr[1] = hi & 0xff;
17052 dev->dev_addr[0] = (hi >> 8) & 0xff;
17053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017054 }
17055
17056 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070017057#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070017058 if (!tg3_get_default_macaddr_sparc(tp))
17059 return 0;
17060#endif
17061 return -EINVAL;
17062 }
17063 return 0;
17064}
17065
David S. Miller59e6b432005-05-18 22:50:10 -070017066#define BOUNDARY_SINGLE_CACHELINE 1
17067#define BOUNDARY_MULTI_CACHELINE 2
17068
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017069static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070017070{
17071 int cacheline_size;
17072 u8 byte;
17073 int goal;
17074
17075 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17076 if (byte == 0)
17077 cacheline_size = 1024;
17078 else
17079 cacheline_size = (int) byte * 4;
17080
17081 /* On 5703 and later chips, the boundary bits have no
17082 * effect.
17083 */
Joe Perches41535772013-02-16 11:20:04 +000017084 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17085 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017086 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017087 goto out;
17088
17089#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17090 goal = BOUNDARY_MULTI_CACHELINE;
17091#else
17092#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17093 goal = BOUNDARY_SINGLE_CACHELINE;
17094#else
17095 goal = 0;
17096#endif
17097#endif
17098
Joe Perches63c3a662011-04-26 08:12:10 +000017099 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017100 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17101 goto out;
17102 }
17103
David S. Miller59e6b432005-05-18 22:50:10 -070017104 if (!goal)
17105 goto out;
17106
17107 /* PCI controllers on most RISC systems tend to disconnect
17108 * when a device tries to burst across a cache-line boundary.
17109 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17110 *
17111 * Unfortunately, for PCI-E there are only limited
17112 * write-side controls for this, and thus for reads
17113 * we will still get the disconnects. We'll also waste
17114 * these PCI cycles for both read and write for chips
17115 * other than 5700 and 5701 which do not implement the
17116 * boundary bits.
17117 */
Joe Perches63c3a662011-04-26 08:12:10 +000017118 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017119 switch (cacheline_size) {
17120 case 16:
17121 case 32:
17122 case 64:
17123 case 128:
17124 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17125 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17126 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17127 } else {
17128 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17129 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17130 }
17131 break;
17132
17133 case 256:
17134 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17135 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17136 break;
17137
17138 default:
17139 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17140 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17141 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017142 }
Joe Perches63c3a662011-04-26 08:12:10 +000017143 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017144 switch (cacheline_size) {
17145 case 16:
17146 case 32:
17147 case 64:
17148 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17149 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17150 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17151 break;
17152 }
17153 /* fallthrough */
17154 case 128:
17155 default:
17156 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17157 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17158 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017159 }
David S. Miller59e6b432005-05-18 22:50:10 -070017160 } else {
17161 switch (cacheline_size) {
17162 case 16:
17163 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17164 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17165 DMA_RWCTRL_WRITE_BNDRY_16);
17166 break;
17167 }
17168 /* fallthrough */
17169 case 32:
17170 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17171 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17172 DMA_RWCTRL_WRITE_BNDRY_32);
17173 break;
17174 }
17175 /* fallthrough */
17176 case 64:
17177 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17178 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17179 DMA_RWCTRL_WRITE_BNDRY_64);
17180 break;
17181 }
17182 /* fallthrough */
17183 case 128:
17184 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17185 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17186 DMA_RWCTRL_WRITE_BNDRY_128);
17187 break;
17188 }
17189 /* fallthrough */
17190 case 256:
17191 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17192 DMA_RWCTRL_WRITE_BNDRY_256);
17193 break;
17194 case 512:
17195 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17196 DMA_RWCTRL_WRITE_BNDRY_512);
17197 break;
17198 case 1024:
17199 default:
17200 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17201 DMA_RWCTRL_WRITE_BNDRY_1024);
17202 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017203 }
David S. Miller59e6b432005-05-18 22:50:10 -070017204 }
17205
17206out:
17207 return val;
17208}
17209
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017210static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017211 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017212{
17213 struct tg3_internal_buffer_desc test_desc;
17214 u32 sram_dma_descs;
17215 int i, ret;
17216
17217 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17218
17219 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17220 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17221 tw32(RDMAC_STATUS, 0);
17222 tw32(WDMAC_STATUS, 0);
17223
17224 tw32(BUFMGR_MODE, 0);
17225 tw32(FTQ_RESET, 0);
17226
17227 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17228 test_desc.addr_lo = buf_dma & 0xffffffff;
17229 test_desc.nic_mbuf = 0x00002100;
17230 test_desc.len = size;
17231
17232 /*
17233 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17234 * the *second* time the tg3 driver was getting loaded after an
17235 * initial scan.
17236 *
17237 * Broadcom tells me:
17238 * ...the DMA engine is connected to the GRC block and a DMA
17239 * reset may affect the GRC block in some unpredictable way...
17240 * The behavior of resets to individual blocks has not been tested.
17241 *
17242 * Broadcom noted the GRC reset will also reset all sub-components.
17243 */
17244 if (to_device) {
17245 test_desc.cqid_sqid = (13 << 8) | 2;
17246
17247 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17248 udelay(40);
17249 } else {
17250 test_desc.cqid_sqid = (16 << 8) | 7;
17251
17252 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17253 udelay(40);
17254 }
17255 test_desc.flags = 0x00000005;
17256
17257 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17258 u32 val;
17259
17260 val = *(((u32 *)&test_desc) + i);
17261 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17262 sram_dma_descs + (i * sizeof(u32)));
17263 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17264 }
17265 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17266
Matt Carlson859a588792010-04-05 10:19:28 +000017267 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017268 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017269 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017270 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017271
17272 ret = -ENODEV;
17273 for (i = 0; i < 40; i++) {
17274 u32 val;
17275
17276 if (to_device)
17277 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17278 else
17279 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17280 if ((val & 0xffff) == sram_dma_descs) {
17281 ret = 0;
17282 break;
17283 }
17284
17285 udelay(100);
17286 }
17287
17288 return ret;
17289}
17290
David S. Millerded73402005-05-23 13:59:47 -070017291#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017292
Benoit Taine9baa3c32014-08-08 15:56:03 +020017293static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017294 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17295 { },
17296};
17297
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017298static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017299{
17300 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017301 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017302 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017303
Matt Carlson4bae65c2010-11-24 08:31:52 +000017304 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17305 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017306 if (!buf) {
17307 ret = -ENOMEM;
17308 goto out_nofree;
17309 }
17310
17311 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17312 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17313
David S. Miller59e6b432005-05-18 22:50:10 -070017314 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017315
Joe Perches63c3a662011-04-26 08:12:10 +000017316 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017317 goto out;
17318
Joe Perches63c3a662011-04-26 08:12:10 +000017319 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017320 /* DMA read watermark not used on PCIE */
17321 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017322 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017323 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17324 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017325 tp->dma_rwctrl |= 0x003f0000;
17326 else
17327 tp->dma_rwctrl |= 0x003f000f;
17328 } else {
Joe Perches41535772013-02-16 11:20:04 +000017329 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17330 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017331 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017332 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017333
Michael Chan4a29cc22006-03-19 13:21:12 -080017334 /* If the 5704 is behind the EPB bridge, we can
17335 * do the less restrictive ONE_DMA workaround for
17336 * better performance.
17337 */
Joe Perches63c3a662011-04-26 08:12:10 +000017338 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017339 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017340 tp->dma_rwctrl |= 0x8000;
17341 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017342 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17343
Joe Perches41535772013-02-16 11:20:04 +000017344 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017345 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017346 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017347 tp->dma_rwctrl |=
17348 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17349 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17350 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017351 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017352 /* 5780 always in PCIX mode */
17353 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017354 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017355 /* 5714 always in PCIX mode */
17356 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017357 } else {
17358 tp->dma_rwctrl |= 0x001b000f;
17359 }
17360 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017361 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17362 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017363
Joe Perches41535772013-02-16 11:20:04 +000017364 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17365 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017366 tp->dma_rwctrl &= 0xfffffff0;
17367
Joe Perches41535772013-02-16 11:20:04 +000017368 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17369 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017370 /* Remove this if it causes problems for some boards. */
17371 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17372
17373 /* On 5700/5701 chips, we need to set this bit.
17374 * Otherwise the chip will issue cacheline transactions
17375 * to streamable DMA memory with not all the byte
17376 * enables turned on. This is an error on several
17377 * RISC PCI controllers, in particular sparc64.
17378 *
17379 * On 5703/5704 chips, this bit has been reassigned
17380 * a different meaning. In particular, it is used
17381 * on those chips to enable a PCI-X workaround.
17382 */
17383 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17384 }
17385
17386 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17387
Linus Torvalds1da177e2005-04-16 15:20:36 -070017388
Joe Perches41535772013-02-16 11:20:04 +000017389 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17390 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017391 goto out;
17392
David S. Miller59e6b432005-05-18 22:50:10 -070017393 /* It is best to perform DMA test with maximum write burst size
17394 * to expose the 5700/5701 write DMA bug.
17395 */
17396 saved_dma_rwctrl = tp->dma_rwctrl;
17397 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17398 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17399
Linus Torvalds1da177e2005-04-16 15:20:36 -070017400 while (1) {
17401 u32 *p = buf, i;
17402
17403 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17404 p[i] = i;
17405
17406 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017407 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017408 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017409 dev_err(&tp->pdev->dev,
17410 "%s: Buffer write failed. err = %d\n",
17411 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017412 break;
17413 }
17414
Linus Torvalds1da177e2005-04-16 15:20:36 -070017415 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017416 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017418 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17419 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017420 break;
17421 }
17422
17423 /* Verify it. */
17424 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17425 if (p[i] == i)
17426 continue;
17427
David S. Miller59e6b432005-05-18 22:50:10 -070017428 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17429 DMA_RWCTRL_WRITE_BNDRY_16) {
17430 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017431 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17432 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17433 break;
17434 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017435 dev_err(&tp->pdev->dev,
17436 "%s: Buffer corrupted on read back! "
17437 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017438 ret = -ENODEV;
17439 goto out;
17440 }
17441 }
17442
17443 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17444 /* Success. */
17445 ret = 0;
17446 break;
17447 }
17448 }
David S. Miller59e6b432005-05-18 22:50:10 -070017449 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17450 DMA_RWCTRL_WRITE_BNDRY_16) {
17451 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017452 * now look for chipsets that are known to expose the
17453 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017454 */
Matt Carlson41434702011-03-09 16:58:22 +000017455 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017456 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17457 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017458 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017459 /* Safe to use the calculated DMA boundary. */
17460 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017461 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017462
David S. Miller59e6b432005-05-18 22:50:10 -070017463 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017465
17466out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017467 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017468out_nofree:
17469 return ret;
17470}
17471
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017472static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017473{
Joe Perches63c3a662011-04-26 08:12:10 +000017474 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017475 tp->bufmgr_config.mbuf_read_dma_low_water =
17476 DEFAULT_MB_RDMA_LOW_WATER_5705;
17477 tp->bufmgr_config.mbuf_mac_rx_low_water =
17478 DEFAULT_MB_MACRX_LOW_WATER_57765;
17479 tp->bufmgr_config.mbuf_high_water =
17480 DEFAULT_MB_HIGH_WATER_57765;
17481
17482 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17483 DEFAULT_MB_RDMA_LOW_WATER_5705;
17484 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17485 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17486 tp->bufmgr_config.mbuf_high_water_jumbo =
17487 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017488 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017489 tp->bufmgr_config.mbuf_read_dma_low_water =
17490 DEFAULT_MB_RDMA_LOW_WATER_5705;
17491 tp->bufmgr_config.mbuf_mac_rx_low_water =
17492 DEFAULT_MB_MACRX_LOW_WATER_5705;
17493 tp->bufmgr_config.mbuf_high_water =
17494 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017495 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017496 tp->bufmgr_config.mbuf_mac_rx_low_water =
17497 DEFAULT_MB_MACRX_LOW_WATER_5906;
17498 tp->bufmgr_config.mbuf_high_water =
17499 DEFAULT_MB_HIGH_WATER_5906;
17500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017501
Michael Chanfdfec1722005-07-25 12:31:48 -070017502 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17503 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17504 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17505 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17506 tp->bufmgr_config.mbuf_high_water_jumbo =
17507 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17508 } else {
17509 tp->bufmgr_config.mbuf_read_dma_low_water =
17510 DEFAULT_MB_RDMA_LOW_WATER;
17511 tp->bufmgr_config.mbuf_mac_rx_low_water =
17512 DEFAULT_MB_MACRX_LOW_WATER;
17513 tp->bufmgr_config.mbuf_high_water =
17514 DEFAULT_MB_HIGH_WATER;
17515
17516 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17517 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17518 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17519 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17520 tp->bufmgr_config.mbuf_high_water_jumbo =
17521 DEFAULT_MB_HIGH_WATER_JUMBO;
17522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017523
17524 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17525 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17526}
17527
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017528static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017529{
Matt Carlson79eb6902010-02-17 15:17:03 +000017530 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17531 case TG3_PHY_ID_BCM5400: return "5400";
17532 case TG3_PHY_ID_BCM5401: return "5401";
17533 case TG3_PHY_ID_BCM5411: return "5411";
17534 case TG3_PHY_ID_BCM5701: return "5701";
17535 case TG3_PHY_ID_BCM5703: return "5703";
17536 case TG3_PHY_ID_BCM5704: return "5704";
17537 case TG3_PHY_ID_BCM5705: return "5705";
17538 case TG3_PHY_ID_BCM5750: return "5750";
17539 case TG3_PHY_ID_BCM5752: return "5752";
17540 case TG3_PHY_ID_BCM5714: return "5714";
17541 case TG3_PHY_ID_BCM5780: return "5780";
17542 case TG3_PHY_ID_BCM5755: return "5755";
17543 case TG3_PHY_ID_BCM5787: return "5787";
17544 case TG3_PHY_ID_BCM5784: return "5784";
17545 case TG3_PHY_ID_BCM5756: return "5722/5756";
17546 case TG3_PHY_ID_BCM5906: return "5906";
17547 case TG3_PHY_ID_BCM5761: return "5761";
17548 case TG3_PHY_ID_BCM5718C: return "5718C";
17549 case TG3_PHY_ID_BCM5718S: return "5718S";
17550 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017551 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017552 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017553 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017554 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017555 case 0: return "serdes";
17556 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017558}
17559
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017560static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017561{
Joe Perches63c3a662011-04-26 08:12:10 +000017562 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017563 strcpy(str, "PCI Express");
17564 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017565 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017566 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17567
17568 strcpy(str, "PCIX:");
17569
17570 if ((clock_ctrl == 7) ||
17571 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17572 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17573 strcat(str, "133MHz");
17574 else if (clock_ctrl == 0)
17575 strcat(str, "33MHz");
17576 else if (clock_ctrl == 2)
17577 strcat(str, "50MHz");
17578 else if (clock_ctrl == 4)
17579 strcat(str, "66MHz");
17580 else if (clock_ctrl == 6)
17581 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017582 } else {
17583 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017584 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017585 strcat(str, "66MHz");
17586 else
17587 strcat(str, "33MHz");
17588 }
Joe Perches63c3a662011-04-26 08:12:10 +000017589 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017590 strcat(str, ":32-bit");
17591 else
17592 strcat(str, ":64-bit");
17593 return str;
17594}
17595
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017596static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017597{
17598 struct ethtool_coalesce *ec = &tp->coal;
17599
17600 memset(ec, 0, sizeof(*ec));
17601 ec->cmd = ETHTOOL_GCOALESCE;
17602 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17603 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17604 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17605 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17606 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17607 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17608 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17609 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17610 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17611
17612 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17613 HOSTCC_MODE_CLRTICK_TXBD)) {
17614 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17615 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17616 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17617 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17618 }
Michael Chand244c892005-07-05 14:42:33 -070017619
Joe Perches63c3a662011-04-26 08:12:10 +000017620 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017621 ec->rx_coalesce_usecs_irq = 0;
17622 ec->tx_coalesce_usecs_irq = 0;
17623 ec->stats_block_coalesce_usecs = 0;
17624 }
David S. Miller15f98502005-05-18 22:49:26 -070017625}
17626
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017627static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017628 const struct pci_device_id *ent)
17629{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017630 struct net_device *dev;
17631 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017632 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017633 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017634 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017635 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017636 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017637
Joe Perches05dbe002010-02-17 19:44:19 +000017638 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017639
17640 err = pci_enable_device(pdev);
17641 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017642 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017643 return err;
17644 }
17645
Linus Torvalds1da177e2005-04-16 15:20:36 -070017646 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17647 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017648 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017649 goto err_out_disable_pdev;
17650 }
17651
17652 pci_set_master(pdev);
17653
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017654 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017655 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017656 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017657 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017658 }
17659
Linus Torvalds1da177e2005-04-16 15:20:36 -070017660 SET_NETDEV_DEV(dev, &pdev->dev);
17661
Linus Torvalds1da177e2005-04-16 15:20:36 -070017662 tp = netdev_priv(dev);
17663 tp->pdev = pdev;
17664 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017665 tp->rx_mode = TG3_DEF_RX_MODE;
17666 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017667 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017668 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017669
Linus Torvalds1da177e2005-04-16 15:20:36 -070017670 if (tg3_debug > 0)
17671 tp->msg_enable = tg3_debug;
17672 else
17673 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17674
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017675 if (pdev_is_ssb_gige_core(pdev)) {
17676 tg3_flag_set(tp, IS_SSB_CORE);
17677 if (ssb_gige_must_flush_posted_writes(pdev))
17678 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17679 if (ssb_gige_one_dma_at_once(pdev))
17680 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017681 if (ssb_gige_have_roboswitch(pdev)) {
17682 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017683 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017684 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017685 if (ssb_gige_is_rgmii(pdev))
17686 tg3_flag_set(tp, RGMII_MODE);
17687 }
17688
Linus Torvalds1da177e2005-04-16 15:20:36 -070017689 /* The word/byte swap controls here control register access byte
17690 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17691 * setting below.
17692 */
17693 tp->misc_host_ctrl =
17694 MISC_HOST_CTRL_MASK_PCI_INT |
17695 MISC_HOST_CTRL_WORD_SWAP |
17696 MISC_HOST_CTRL_INDIR_ACCESS |
17697 MISC_HOST_CTRL_PCISTATE_RW;
17698
17699 /* The NONFRM (non-frame) byte/word swap controls take effect
17700 * on descriptor entries, anything which isn't packet data.
17701 *
17702 * The StrongARM chips on the board (one for tx, one for rx)
17703 * are running in big-endian mode.
17704 */
17705 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17706 GRC_MODE_WSWAP_NONFRM_DATA);
17707#ifdef __BIG_ENDIAN
17708 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17709#endif
17710 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017711 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017712 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017713
Matt Carlsond5fe4882008-11-21 17:20:32 -080017714 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017715 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017716 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017717 err = -ENOMEM;
17718 goto err_out_free_dev;
17719 }
17720
Matt Carlsonc9cab242011-07-13 09:27:27 +000017721 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17722 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17723 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17724 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17725 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017726 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017727 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17728 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017729 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017730 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17731 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017732 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17733 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017734 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17735 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017736 tg3_flag_set(tp, ENABLE_APE);
17737 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17738 if (!tp->aperegs) {
17739 dev_err(&pdev->dev,
17740 "Cannot map APE registers, aborting\n");
17741 err = -ENOMEM;
17742 goto err_out_iounmap;
17743 }
17744 }
17745
Linus Torvalds1da177e2005-04-16 15:20:36 -070017746 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17747 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017748
Linus Torvalds1da177e2005-04-16 15:20:36 -070017749 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017750 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017751 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017752 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017753
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017754 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017755 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017756 dev_err(&pdev->dev,
17757 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017758 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017759 }
17760
Michael Chan4a29cc22006-03-19 13:21:12 -080017761 /* The EPB bridge inside 5714, 5715, and 5780 and any
17762 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017763 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17764 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17765 * do DMA address check in tg3_start_xmit().
17766 */
Joe Perches63c3a662011-04-26 08:12:10 +000017767 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017768 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017769 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017770 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017771#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017772 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017773#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017774 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017775 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017776
17777 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017778 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017779 err = pci_set_dma_mask(pdev, dma_mask);
17780 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017781 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017782 err = pci_set_consistent_dma_mask(pdev,
17783 persist_dma_mask);
17784 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017785 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17786 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017787 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017788 }
17789 }
17790 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017791 if (err || dma_mask == DMA_BIT_MASK(32)) {
17792 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017793 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017794 dev_err(&pdev->dev,
17795 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017796 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017797 }
17798 }
17799
Michael Chanfdfec1722005-07-25 12:31:48 -070017800 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017801
Matt Carlson0da06062011-05-19 12:12:53 +000017802 /* 5700 B0 chips do not support checksumming correctly due
17803 * to hardware bugs.
17804 */
Joe Perches41535772013-02-16 11:20:04 +000017805 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017806 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17807
17808 if (tg3_flag(tp, 5755_PLUS))
17809 features |= NETIF_F_IPV6_CSUM;
17810 }
17811
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017812 /* TSO is on by default on chips that support hardware TSO.
17813 * Firmware TSO on older chips gives lower performance, so it
17814 * is off by default, but can be enabled using ethtool.
17815 */
Joe Perches63c3a662011-04-26 08:12:10 +000017816 if ((tg3_flag(tp, HW_TSO_1) ||
17817 tg3_flag(tp, HW_TSO_2) ||
17818 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017819 (features & NETIF_F_IP_CSUM))
17820 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017821 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017822 if (features & NETIF_F_IPV6_CSUM)
17823 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017824 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017825 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17826 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17827 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17828 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17829 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017830 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017832
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017833 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17834 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017835 dev->vlan_features |= features;
17836
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017837 /*
17838 * Add loopback capability only for a subset of devices that support
17839 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17840 * loopback for the remaining devices.
17841 */
Joe Perches41535772013-02-16 11:20:04 +000017842 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017843 !tg3_flag(tp, CPMU_PRESENT))
17844 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017845 features |= NETIF_F_LOOPBACK;
17846
Matt Carlson0da06062011-05-19 12:12:53 +000017847 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017848 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017849
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -040017850 /* MTU range: 60 - 9000 or 1500, depending on hardware */
17851 dev->min_mtu = TG3_MIN_MTU;
17852 dev->max_mtu = TG3_MAX_MTU(tp);
17853
Joe Perches41535772013-02-16 11:20:04 +000017854 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017855 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017856 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017857 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017858 tp->rx_pending = 63;
17859 }
17860
Linus Torvalds1da177e2005-04-16 15:20:36 -070017861 err = tg3_get_device_address(tp);
17862 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017863 dev_err(&pdev->dev,
17864 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017865 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017866 }
17867
Matt Carlson78f90dc2009-11-13 13:03:42 +000017868 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17869 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17870 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017871 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017872 struct tg3_napi *tnapi = &tp->napi[i];
17873
17874 tnapi->tp = tp;
17875 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17876
17877 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017878 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017879 intmbx += 0x8;
17880 else
17881 intmbx += 0x4;
17882
17883 tnapi->consmbox = rcvmbx;
17884 tnapi->prodmbox = sndmbx;
17885
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017886 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017887 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017888 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017889 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017890
Joe Perches63c3a662011-04-26 08:12:10 +000017891 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017892 break;
17893
17894 /*
17895 * If we support MSIX, we'll be using RSS. If we're using
17896 * RSS, the first vector only handles link interrupts and the
17897 * remaining vectors handle rx and tx interrupts. Reuse the
17898 * mailbox values for the next iteration. The values we setup
17899 * above are still useful for the single vectored mode.
17900 */
17901 if (!i)
17902 continue;
17903
17904 rcvmbx += 0x8;
17905
17906 if (sndmbx & 0x4)
17907 sndmbx -= 0x4;
17908 else
17909 sndmbx += 0xc;
17910 }
17911
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017912 /*
17913 * Reset chip in case UNDI or EFI driver did not shutdown
17914 * DMA self test will enable WDMAC and we'll see (spurious)
17915 * pending DMA on the PCI bus at that point.
17916 */
17917 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17918 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017919 tg3_full_lock(tp, 0);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017920 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17921 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017922 tg3_full_unlock(tp);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017923 }
17924
17925 err = tg3_test_dma(tp);
17926 if (err) {
17927 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
17928 goto err_out_apeunmap;
17929 }
17930
Matt Carlsonc88864d2007-11-12 21:07:01 -080017931 tg3_init_coal(tp);
17932
Michael Chanc49a1562006-12-17 17:07:29 -080017933 pci_set_drvdata(pdev, dev);
17934
Joe Perches41535772013-02-16 11:20:04 +000017935 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17936 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17937 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017938 tg3_flag_set(tp, PTP_CAPABLE);
17939
Matt Carlson21f76382012-02-22 12:35:21 +000017940 tg3_timer_init(tp);
17941
Michael Chan402e1392013-02-14 12:13:41 +000017942 tg3_carrier_off(tp);
17943
Linus Torvalds1da177e2005-04-16 15:20:36 -070017944 err = register_netdev(dev);
17945 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017946 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017947 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017948 }
17949
Ivan Vecera20d14a52015-01-08 16:13:07 +010017950 if (tg3_flag(tp, PTP_CAPABLE)) {
17951 tg3_ptp_init(tp);
17952 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
17953 &tp->pdev->dev);
17954 if (IS_ERR(tp->ptp_clock))
17955 tp->ptp_clock = NULL;
17956 }
17957
Joe Perches05dbe002010-02-17 19:44:19 +000017958 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17959 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017960 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017961 tg3_bus_string(tp, str),
17962 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017963
Andrew Lunn22209432016-01-06 20:11:13 +010017964 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017965 char *ethtype;
17966
17967 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17968 ethtype = "10/100Base-TX";
17969 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17970 ethtype = "1000Base-SX";
17971 else
17972 ethtype = "10/100/1000Base-T";
17973
Matt Carlson5129c3a2010-04-05 10:19:23 +000017974 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017975 "(WireSpeed[%d], EEE[%d])\n",
17976 tg3_phy_string(tp), ethtype,
17977 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17978 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017979 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017980
Joe Perches05dbe002010-02-17 19:44:19 +000017981 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017982 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017983 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017984 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017985 tg3_flag(tp, ENABLE_ASF) != 0,
17986 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017987 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17988 tp->dma_rwctrl,
17989 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17990 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017991
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017992 pci_save_state(pdev);
17993
Linus Torvalds1da177e2005-04-16 15:20:36 -070017994 return 0;
17995
Matt Carlson0d3031d2007-10-10 18:02:43 -070017996err_out_apeunmap:
17997 if (tp->aperegs) {
17998 iounmap(tp->aperegs);
17999 tp->aperegs = NULL;
18000 }
18001
Linus Torvalds1da177e2005-04-16 15:20:36 -070018002err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070018003 if (tp->regs) {
18004 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070018005 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070018006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018007
18008err_out_free_dev:
18009 free_netdev(dev);
18010
18011err_out_free_res:
18012 pci_release_regions(pdev);
18013
18014err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080018015 if (pci_is_enabled(pdev))
18016 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018017 return err;
18018}
18019
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018020static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018021{
18022 struct net_device *dev = pci_get_drvdata(pdev);
18023
18024 if (dev) {
18025 struct tg3 *tp = netdev_priv(dev);
18026
Ivan Vecera20d14a52015-01-08 16:13:07 +010018027 tg3_ptp_fini(tp);
18028
Jesper Juhle3c55302012-04-09 22:50:15 +020018029 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080018030
Matt Carlsondb219972011-11-04 09:15:03 +000018031 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018032
David S. Miller1805b2f2011-10-24 18:18:09 -040018033 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018034 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018035 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018036 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070018037
Linus Torvalds1da177e2005-04-16 15:20:36 -070018038 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070018039 if (tp->aperegs) {
18040 iounmap(tp->aperegs);
18041 tp->aperegs = NULL;
18042 }
Michael Chan68929142005-08-09 20:17:14 -070018043 if (tp->regs) {
18044 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070018045 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070018046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018047 free_netdev(dev);
18048 pci_release_regions(pdev);
18049 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018050 }
18051}
18052
Eric Dumazetaa6027c2011-01-01 05:22:46 +000018053#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018054static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018055{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018056 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018057 struct net_device *dev = pci_get_drvdata(pdev);
18058 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018059 int err = 0;
18060
18061 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018062
18063 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018064 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018065
Matt Carlsondb219972011-11-04 09:15:03 +000018066 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018067 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018068 tg3_netif_stop(tp);
18069
Matt Carlson21f76382012-02-22 12:35:21 +000018070 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018071
David S. Millerf47c11e2005-06-24 20:18:35 -070018072 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018073 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070018074 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018075
18076 netif_device_detach(dev);
18077
David S. Millerf47c11e2005-06-24 20:18:35 -070018078 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070018079 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000018080 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070018081 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018082
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018083 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018084 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018085 int err2;
18086
David S. Millerf47c11e2005-06-24 20:18:35 -070018087 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018088
Joe Perches63c3a662011-04-26 08:12:10 +000018089 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018090 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018091 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018093
Matt Carlson21f76382012-02-22 12:35:21 +000018094 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018095
18096 netif_device_attach(dev);
18097 tg3_netif_start(tp);
18098
Michael Chanb9ec6c12006-07-25 16:37:27 -070018099out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018100 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018101
18102 if (!err2)
18103 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018104 }
18105
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018106unlock:
18107 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018108 return err;
18109}
18110
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018111static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018112{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018113 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018114 struct net_device *dev = pci_get_drvdata(pdev);
18115 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018116 int err = 0;
18117
18118 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018119
18120 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018121 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018122
Linus Torvalds1da177e2005-04-16 15:20:36 -070018123 netif_device_attach(dev);
18124
David S. Millerf47c11e2005-06-24 20:18:35 -070018125 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018126
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018127 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18128
Joe Perches63c3a662011-04-26 08:12:10 +000018129 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018130 err = tg3_restart_hw(tp,
18131 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018132 if (err)
18133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018134
Matt Carlson21f76382012-02-22 12:35:21 +000018135 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018136
Linus Torvalds1da177e2005-04-16 15:20:36 -070018137 tg3_netif_start(tp);
18138
Michael Chanb9ec6c12006-07-25 16:37:27 -070018139out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018140 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018141
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018142 if (!err)
18143 tg3_phy_start(tp);
18144
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018145unlock:
18146 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018148}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018149#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018150
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018151static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18152
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018153static void tg3_shutdown(struct pci_dev *pdev)
18154{
18155 struct net_device *dev = pci_get_drvdata(pdev);
18156 struct tg3 *tp = netdev_priv(dev);
18157
18158 rtnl_lock();
18159 netif_device_detach(dev);
18160
18161 if (netif_running(dev))
18162 dev_close(dev);
18163
18164 if (system_state == SYSTEM_POWER_OFF)
18165 tg3_power_down(tp);
18166
18167 rtnl_unlock();
18168}
18169
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018170/**
18171 * tg3_io_error_detected - called when PCI error is detected
18172 * @pdev: Pointer to PCI device
18173 * @state: The current pci connection state
18174 *
18175 * This function is called after a PCI bus error affecting
18176 * this device has been detected.
18177 */
18178static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18179 pci_channel_state_t state)
18180{
18181 struct net_device *netdev = pci_get_drvdata(pdev);
18182 struct tg3 *tp = netdev_priv(netdev);
18183 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18184
18185 netdev_info(netdev, "PCI I/O error detected\n");
18186
18187 rtnl_lock();
18188
Gavin Shand8af4df2013-07-24 17:25:08 +080018189 /* We probably don't have netdev yet */
18190 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018191 goto done;
18192
Milton Miller1b0ff892016-09-29 13:24:08 -030018193 /* We needn't recover from permanent error */
18194 if (state == pci_channel_io_frozen)
18195 tp->pcierr_recovery = true;
18196
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018197 tg3_phy_stop(tp);
18198
18199 tg3_netif_stop(tp);
18200
Matt Carlson21f76382012-02-22 12:35:21 +000018201 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018202
18203 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018204 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018205
18206 netif_device_detach(netdev);
18207
18208 /* Clean up software state, even if MMIO is blocked */
18209 tg3_full_lock(tp, 0);
18210 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18211 tg3_full_unlock(tp);
18212
18213done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018214 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018215 if (netdev) {
18216 tg3_napi_enable(tp);
18217 dev_close(netdev);
18218 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018219 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018220 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018221 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018222 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018223
18224 rtnl_unlock();
18225
18226 return err;
18227}
18228
18229/**
18230 * tg3_io_slot_reset - called after the pci bus has been reset.
18231 * @pdev: Pointer to PCI device
18232 *
18233 * Restart the card from scratch, as if from a cold-boot.
18234 * At this point, the card has exprienced a hard reset,
18235 * followed by fixups by BIOS, and has its config space
18236 * set up identically to what it was at cold boot.
18237 */
18238static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18239{
18240 struct net_device *netdev = pci_get_drvdata(pdev);
18241 struct tg3 *tp = netdev_priv(netdev);
18242 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18243 int err;
18244
18245 rtnl_lock();
18246
18247 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018248 dev_err(&pdev->dev,
18249 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018250 goto done;
18251 }
18252
18253 pci_set_master(pdev);
18254 pci_restore_state(pdev);
18255 pci_save_state(pdev);
18256
Daniel Borkmann68293092013-08-13 11:45:13 -070018257 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018258 rc = PCI_ERS_RESULT_RECOVERED;
18259 goto done;
18260 }
18261
18262 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018263 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018264 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018265
18266 rc = PCI_ERS_RESULT_RECOVERED;
18267
18268done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018269 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018270 tg3_napi_enable(tp);
18271 dev_close(netdev);
18272 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018273 rtnl_unlock();
18274
18275 return rc;
18276}
18277
18278/**
18279 * tg3_io_resume - called when traffic can start flowing again.
18280 * @pdev: Pointer to PCI device
18281 *
18282 * This callback is called when the error recovery driver tells
18283 * us that its OK to resume normal operation.
18284 */
18285static void tg3_io_resume(struct pci_dev *pdev)
18286{
18287 struct net_device *netdev = pci_get_drvdata(pdev);
18288 struct tg3 *tp = netdev_priv(netdev);
18289 int err;
18290
18291 rtnl_lock();
18292
Milton Miller1b0ff892016-09-29 13:24:08 -030018293 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018294 goto done;
18295
18296 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018297 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018298 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018299 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018300 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018301 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018302 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18303 goto done;
18304 }
18305
18306 netif_device_attach(netdev);
18307
Matt Carlson21f76382012-02-22 12:35:21 +000018308 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018309
18310 tg3_netif_start(tp);
18311
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018312 tg3_full_unlock(tp);
18313
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018314 tg3_phy_start(tp);
18315
18316done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018317 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018318 rtnl_unlock();
18319}
18320
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018321static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018322 .error_detected = tg3_io_error_detected,
18323 .slot_reset = tg3_io_slot_reset,
18324 .resume = tg3_io_resume
18325};
18326
Linus Torvalds1da177e2005-04-16 15:20:36 -070018327static struct pci_driver tg3_driver = {
18328 .name = DRV_MODULE_NAME,
18329 .id_table = tg3_pci_tbl,
18330 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018331 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018332 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018333 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018334 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018335};
18336
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018337module_pci_driver(tg3_driver);