blob: a77ee2f8fb8d223cc121a36a2ffb703cc32a02d9 [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
823 udelay(10);
824 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
925 /* Wait for up to 1 millisecond for APE to service previous event. */
926 err = tg3_ape_event_lock(tp, 1000);
927 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:
949 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
950 APE_HOST_SEG_SIG_MAGIC);
951 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
952 APE_HOST_SEG_LEN_MAGIC);
953 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
954 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
955 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
956 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
957 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
958 APE_HOST_BEHAV_NO_PHYLOCK);
959 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
960 TG3_APE_HOST_DRVR_STATE_START);
961
962 event = APE_EVENT_STATUS_STATE_START;
963 break;
964 case RESET_KIND_SHUTDOWN:
965 /* With the interface we are currently using,
966 * APE does not track driver state. Wiping
967 * out the HOST SEGMENT SIGNATURE forces
968 * the APE to assume OS absent status.
969 */
970 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
971
972 if (device_may_wakeup(&tp->pdev->dev) &&
973 tg3_flag(tp, WOL_ENABLE)) {
974 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
975 TG3_APE_HOST_WOL_SPEED_AUTO);
976 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
977 } else
978 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
979
980 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
981
982 event = APE_EVENT_STATUS_STATE_UNLOAD;
983 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000984 default:
985 return;
986 }
987
988 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
989
990 tg3_ape_send_event(tp, event);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993static void tg3_disable_ints(struct tg3 *tp)
994{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000995 int i;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tw32(TG3PCI_MISC_HOST_CTRL,
998 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000999 for (i = 0; i < tp->irq_max; i++)
1000 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003static void tg3_enable_ints(struct tg3 *tp)
1004{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001005 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006
Michael Chanbbe832c2005-06-24 20:20:04 -07001007 tp->irq_sync = 0;
1008 wmb();
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 tw32(TG3PCI_MISC_HOST_CTRL,
1011 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001012
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001013 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 for (i = 0; i < tp->irq_cnt; i++) {
1015 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001016
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001017 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001018 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001019 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1020
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001021 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001022 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001023
1024 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001025 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001026 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1027 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1028 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001029 tw32(HOSTCC_MODE, tp->coal_now);
1030
1031 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Matt Carlson17375d22009-08-28 14:02:18 +00001034static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001035{
Matt Carlson17375d22009-08-28 14:02:18 +00001036 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001037 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001038 unsigned int work_exists = 0;
1039
1040 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001041 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001042 if (sblk->status & SD_STATUS_LINK_CHG)
1043 work_exists = 1;
1044 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001045
1046 /* check for TX work to do */
1047 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1048 work_exists = 1;
1049
1050 /* check for RX work to do */
1051 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001052 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001053 work_exists = 1;
1054
1055 return work_exists;
1056}
1057
Matt Carlson17375d22009-08-28 14:02:18 +00001058/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001059 * similar to tg3_enable_ints, but it accurately determines whether there
1060 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001061 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Matt Carlson17375d22009-08-28 14:02:18 +00001063static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Matt Carlson17375d22009-08-28 14:02:18 +00001065 struct tg3 *tp = tnapi->tp;
1066
Matt Carlson898a56f2009-08-28 14:02:40 +00001067 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mmiowb();
1069
David S. Millerfac9b832005-05-18 22:46:34 -07001070 /* When doing tagged status, this work check is unnecessary.
1071 * The last_tag we write above tells the chip which piece of
1072 * work we've completed.
1073 */
Joe Perches63c3a662011-04-26 08:12:10 +00001074 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001075 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001076 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079static void tg3_switch_clocks(struct tg3 *tp)
1080{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001081 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 u32 orig_clock_ctrl;
1083
Joe Perches63c3a662011-04-26 08:12:10 +00001084 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001085 return;
1086
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001087 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 orig_clock_ctrl = clock_ctrl;
1090 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1091 CLOCK_CTRL_CLKRUN_OENABLE |
1092 0x1f);
1093 tp->pci_clock_ctrl = clock_ctrl;
1094
Joe Perches63c3a662011-04-26 08:12:10 +00001095 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001097 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1098 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001101 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1102 clock_ctrl |
1103 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1104 40);
1105 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1106 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1107 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001109 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112#define PHY_BUSY_LOOPS 5000
1113
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001114static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1115 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
1117 u32 frame_val;
1118 unsigned int loops;
1119 int ret;
1120
1121 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1122 tw32_f(MAC_MI_MODE,
1123 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1124 udelay(80);
1125 }
1126
Michael Chan8151ad52012-07-29 19:15:41 +00001127 tg3_ape_lock(tp, tp->phy_ape_lock);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 *val = 0x0;
1130
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001131 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 MI_COM_PHY_ADDR_MASK);
1133 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1134 MI_COM_REG_ADDR_MASK);
1135 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 tw32_f(MAC_MI_COM, frame_val);
1138
1139 loops = PHY_BUSY_LOOPS;
1140 while (loops != 0) {
1141 udelay(10);
1142 frame_val = tr32(MAC_MI_COM);
1143
1144 if ((frame_val & MI_COM_BUSY) == 0) {
1145 udelay(5);
1146 frame_val = tr32(MAC_MI_COM);
1147 break;
1148 }
1149 loops -= 1;
1150 }
1151
1152 ret = -EBUSY;
1153 if (loops != 0) {
1154 *val = frame_val & MI_COM_DATA_MASK;
1155 ret = 0;
1156 }
1157
1158 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1159 tw32_f(MAC_MI_MODE, tp->mi_mode);
1160 udelay(80);
1161 }
1162
Michael Chan8151ad52012-07-29 19:15:41 +00001163 tg3_ape_unlock(tp, tp->phy_ape_lock);
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return ret;
1166}
1167
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001168static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1169{
1170 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1171}
1172
1173static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1174 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 u32 frame_val;
1177 unsigned int loops;
1178 int ret;
1179
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001180 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001181 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001182 return 0;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1185 tw32_f(MAC_MI_MODE,
1186 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1187 udelay(80);
1188 }
1189
Michael Chan8151ad52012-07-29 19:15:41 +00001190 tg3_ape_lock(tp, tp->phy_ape_lock);
1191
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001192 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 MI_COM_PHY_ADDR_MASK);
1194 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1195 MI_COM_REG_ADDR_MASK);
1196 frame_val |= (val & MI_COM_DATA_MASK);
1197 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tw32_f(MAC_MI_COM, frame_val);
1200
1201 loops = PHY_BUSY_LOOPS;
1202 while (loops != 0) {
1203 udelay(10);
1204 frame_val = tr32(MAC_MI_COM);
1205 if ((frame_val & MI_COM_BUSY) == 0) {
1206 udelay(5);
1207 frame_val = tr32(MAC_MI_COM);
1208 break;
1209 }
1210 loops -= 1;
1211 }
1212
1213 ret = -EBUSY;
1214 if (loops != 0)
1215 ret = 0;
1216
1217 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1218 tw32_f(MAC_MI_MODE, tp->mi_mode);
1219 udelay(80);
1220 }
1221
Michael Chan8151ad52012-07-29 19:15:41 +00001222 tg3_ape_unlock(tp, tp->phy_ape_lock);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return ret;
1225}
1226
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001227static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1228{
1229 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1230}
1231
Matt Carlsonb0988c12011-04-20 07:57:39 +00001232static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1233{
1234 int err;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1237 if (err)
1238 goto done;
1239
1240 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1241 if (err)
1242 goto done;
1243
1244 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1245 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1246 if (err)
1247 goto done;
1248
1249 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1250
1251done:
1252 return err;
1253}
1254
1255static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1256{
1257 int err;
1258
1259 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1260 if (err)
1261 goto done;
1262
1263 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1264 if (err)
1265 goto done;
1266
1267 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1268 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1269 if (err)
1270 goto done;
1271
1272 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1273
1274done:
1275 return err;
1276}
1277
1278static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1279{
1280 int err;
1281
1282 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1283 if (!err)
1284 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1285
1286 return err;
1287}
1288
1289static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1290{
1291 int err;
1292
1293 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1294 if (!err)
1295 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1296
1297 return err;
1298}
1299
Matt Carlson15ee95c2011-04-20 07:57:40 +00001300static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1301{
1302 int err;
1303
1304 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1305 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1306 MII_TG3_AUXCTL_SHDWSEL_MISC);
1307 if (!err)
1308 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1309
1310 return err;
1311}
1312
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001313static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1314{
1315 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1316 set |= MII_TG3_AUXCTL_MISC_WREN;
1317
1318 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1319}
1320
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001321static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1322{
1323 u32 val;
1324 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001325
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001326 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1327
1328 if (err)
1329 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001330
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001331 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001332 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1333 else
1334 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1335
1336 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1337 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1338
1339 return err;
1340}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001341
Nithin Sujir3ab71072013-09-20 16:46:55 -07001342static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1343{
1344 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1345 reg | val | MII_TG3_MISC_SHDW_WREN);
1346}
1347
Matt Carlson95e28692008-05-25 23:44:14 -07001348static int tg3_bmcr_reset(struct tg3 *tp)
1349{
1350 u32 phy_control;
1351 int limit, err;
1352
1353 /* OK, reset it, and poll the BMCR_RESET bit until it
1354 * clears or we time out.
1355 */
1356 phy_control = BMCR_RESET;
1357 err = tg3_writephy(tp, MII_BMCR, phy_control);
1358 if (err != 0)
1359 return -EBUSY;
1360
1361 limit = 5000;
1362 while (limit--) {
1363 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1364 if (err != 0)
1365 return -EBUSY;
1366
1367 if ((phy_control & BMCR_RESET) == 0) {
1368 udelay(40);
1369 break;
1370 }
1371 udelay(10);
1372 }
Roel Kluind4675b52009-02-12 16:33:27 -08001373 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001374 return -EBUSY;
1375
1376 return 0;
1377}
1378
Matt Carlson158d7ab2008-05-29 01:37:54 -07001379static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1380{
Francois Romieu3d165432009-01-19 16:56:50 -08001381 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001382 u32 val;
1383
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001384 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385
Hauke Mehrtensead24022013-09-28 23:15:26 +02001386 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001387 val = -EIO;
1388
1389 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001390
1391 return val;
1392}
1393
1394static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1395{
Francois Romieu3d165432009-01-19 16:56:50 -08001396 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001397 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001398
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001399 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001400
Hauke Mehrtensead24022013-09-28 23:15:26 +02001401 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001402 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001404 spin_unlock_bh(&tp->lock);
1405
1406 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001407}
1408
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001409static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001410{
1411 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001413
Andrew Lunn7f854422016-01-06 20:11:18 +01001414 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001415 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001416 case PHY_ID_BCM50610:
1417 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 val = MAC_PHYCFG2_50610_LED_MODES;
1419 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001420 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001421 val = MAC_PHYCFG2_AC131_LED_MODES;
1422 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001423 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001424 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1425 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001426 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001427 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1428 break;
1429 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001430 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001431 }
1432
1433 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1434 tw32(MAC_PHYCFG2, val);
1435
1436 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001437 val &= ~(MAC_PHYCFG1_RGMII_INT |
1438 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1439 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001440 tw32(MAC_PHYCFG1, val);
1441
1442 return;
1443 }
1444
Joe Perches63c3a662011-04-26 08:12:10 +00001445 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001446 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1447 MAC_PHYCFG2_FMODE_MASK_MASK |
1448 MAC_PHYCFG2_GMODE_MASK_MASK |
1449 MAC_PHYCFG2_ACT_MASK_MASK |
1450 MAC_PHYCFG2_QUAL_MASK_MASK |
1451 MAC_PHYCFG2_INBAND_ENABLE;
1452
1453 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001454
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001455 val = tr32(MAC_PHYCFG1);
1456 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1457 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001458 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1459 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001460 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001462 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1463 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001464 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1465 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1466 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001467
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 val = tr32(MAC_EXT_RGMII_MODE);
1469 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1470 MAC_RGMII_MODE_RX_QUALITY |
1471 MAC_RGMII_MODE_RX_ACTIVITY |
1472 MAC_RGMII_MODE_RX_ENG_DET |
1473 MAC_RGMII_MODE_TX_ENABLE |
1474 MAC_RGMII_MODE_TX_LOWPWR |
1475 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001476 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1477 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001478 val |= MAC_RGMII_MODE_RX_INT_B |
1479 MAC_RGMII_MODE_RX_QUALITY |
1480 MAC_RGMII_MODE_RX_ACTIVITY |
1481 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001483 val |= MAC_RGMII_MODE_TX_ENABLE |
1484 MAC_RGMII_MODE_TX_LOWPWR |
1485 MAC_RGMII_MODE_TX_RESET;
1486 }
1487 tw32(MAC_EXT_RGMII_MODE, val);
1488}
1489
Matt Carlson158d7ab2008-05-29 01:37:54 -07001490static void tg3_mdio_start(struct tg3 *tp)
1491{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001492 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1493 tw32_f(MAC_MI_MODE, tp->mi_mode);
1494 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001495
Joe Perches63c3a662011-04-26 08:12:10 +00001496 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001497 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001498 tg3_mdio_config_5785(tp);
1499}
1500
1501static int tg3_mdio_init(struct tg3 *tp)
1502{
1503 int i;
1504 u32 reg;
1505 struct phy_device *phydev;
1506
Joe Perches63c3a662011-04-26 08:12:10 +00001507 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001508 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001509
Matt Carlson69f11c92011-07-13 09:27:30 +00001510 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001511
Joe Perches41535772013-02-16 11:20:04 +00001512 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001513 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1514 else
1515 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1516 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001517 if (is_serdes)
1518 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001519 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1520 int addr;
1521
1522 addr = ssb_gige_get_phyaddr(tp->pdev);
1523 if (addr < 0)
1524 return addr;
1525 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001526 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001527 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001528
Matt Carlson158d7ab2008-05-29 01:37:54 -07001529 tg3_mdio_start(tp);
1530
Joe Perches63c3a662011-04-26 08:12:10 +00001531 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001532 return 0;
1533
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001534 tp->mdio_bus = mdiobus_alloc();
1535 if (tp->mdio_bus == NULL)
1536 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001537
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001538 tp->mdio_bus->name = "tg3 mdio bus";
1539 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001540 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001541 tp->mdio_bus->priv = tp;
1542 tp->mdio_bus->parent = &tp->pdev->dev;
1543 tp->mdio_bus->read = &tg3_mdio_read;
1544 tp->mdio_bus->write = &tg3_mdio_write;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001545 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001546
1547 /* The bus registration will look for all the PHYs on the mdio bus.
1548 * Unfortunately, it does not ensure the PHY is powered up before
1549 * accessing the PHY ID registers. A chip reset is the
1550 * quickest way to bring the device back to an operational state..
1551 */
1552 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1553 tg3_bmcr_reset(tp);
1554
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001555 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001556 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001557 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001558 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001559 return i;
1560 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001561
Andrew Lunn7f854422016-01-06 20:11:18 +01001562 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsona9daf362008-05-25 23:49:44 -07001563
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001564 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001565 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001566 mdiobus_unregister(tp->mdio_bus);
1567 mdiobus_free(tp->mdio_bus);
1568 return -ENODEV;
1569 }
1570
1571 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001572 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001573 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001574 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001575 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001576 case PHY_ID_BCM50610:
1577 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001578 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001579 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001580 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001581 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001582 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001583 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001584 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001585 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001586 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001587 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001588 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001589 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001590 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001591 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001592 case PHY_ID_RTL8201E:
1593 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001594 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001595 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001596 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001597 break;
1598 }
1599
Joe Perches63c3a662011-04-26 08:12:10 +00001600 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001601
Joe Perches41535772013-02-16 11:20:04 +00001602 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001603 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001604
1605 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001606}
1607
1608static void tg3_mdio_fini(struct tg3 *tp)
1609{
Joe Perches63c3a662011-04-26 08:12:10 +00001610 if (tg3_flag(tp, MDIOBUS_INITED)) {
1611 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001612 mdiobus_unregister(tp->mdio_bus);
1613 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001614 }
1615}
1616
Matt Carlson95e28692008-05-25 23:44:14 -07001617/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001618static inline void tg3_generate_fw_event(struct tg3 *tp)
1619{
1620 u32 val;
1621
1622 val = tr32(GRC_RX_CPU_EVENT);
1623 val |= GRC_RX_CPU_DRIVER_EVENT;
1624 tw32_f(GRC_RX_CPU_EVENT, val);
1625
1626 tp->last_event_jiffies = jiffies;
1627}
1628
1629#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1630
1631/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001632static void tg3_wait_for_event_ack(struct tg3 *tp)
1633{
1634 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001635 unsigned int delay_cnt;
1636 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001637
Matt Carlson4ba526c2008-08-15 14:10:04 -07001638 /* If enough time has passed, no wait is necessary. */
1639 time_remain = (long)(tp->last_event_jiffies + 1 +
1640 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1641 (long)jiffies;
1642 if (time_remain < 0)
1643 return;
1644
1645 /* Check if we can shorten the wait time. */
1646 delay_cnt = jiffies_to_usecs(time_remain);
1647 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1648 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1649 delay_cnt = (delay_cnt >> 3) + 1;
1650
1651 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001652 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1653 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001654 if (pci_channel_offline(tp->pdev))
1655 break;
1656
Matt Carlson4ba526c2008-08-15 14:10:04 -07001657 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001658 }
1659}
1660
1661/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001662static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001663{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001664 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001665
1666 val = 0;
1667 if (!tg3_readphy(tp, MII_BMCR, &reg))
1668 val = reg << 16;
1669 if (!tg3_readphy(tp, MII_BMSR, &reg))
1670 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001671 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001672
1673 val = 0;
1674 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1675 val = reg << 16;
1676 if (!tg3_readphy(tp, MII_LPA, &reg))
1677 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001678 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001679
1680 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001681 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001682 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1683 val = reg << 16;
1684 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1685 val |= (reg & 0xffff);
1686 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001687 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001688
1689 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1690 val = reg << 16;
1691 else
1692 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001693 *data++ = val;
1694}
1695
1696/* tp->lock is held. */
1697static void tg3_ump_link_report(struct tg3 *tp)
1698{
1699 u32 data[4];
1700
1701 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1702 return;
1703
1704 tg3_phy_gather_ump_data(tp, data);
1705
1706 tg3_wait_for_event_ack(tp);
1707
1708 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1709 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1710 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1711 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1712 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1713 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001714
Matt Carlson4ba526c2008-08-15 14:10:04 -07001715 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001716}
1717
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001718/* tp->lock is held. */
1719static void tg3_stop_fw(struct tg3 *tp)
1720{
1721 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1722 /* Wait for RX cpu to ACK the previous event. */
1723 tg3_wait_for_event_ack(tp);
1724
1725 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1726
1727 tg3_generate_fw_event(tp);
1728
1729 /* Wait for RX cpu to ACK this event. */
1730 tg3_wait_for_event_ack(tp);
1731 }
1732}
1733
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001734/* tp->lock is held. */
1735static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1736{
1737 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1738 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1739
1740 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1741 switch (kind) {
1742 case RESET_KIND_INIT:
1743 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1744 DRV_STATE_START);
1745 break;
1746
1747 case RESET_KIND_SHUTDOWN:
1748 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1749 DRV_STATE_UNLOAD);
1750 break;
1751
1752 case RESET_KIND_SUSPEND:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_SUSPEND);
1755 break;
1756
1757 default:
1758 break;
1759 }
1760 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001761}
1762
1763/* tp->lock is held. */
1764static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1765{
1766 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1767 switch (kind) {
1768 case RESET_KIND_INIT:
1769 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1770 DRV_STATE_START_DONE);
1771 break;
1772
1773 case RESET_KIND_SHUTDOWN:
1774 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1775 DRV_STATE_UNLOAD_DONE);
1776 break;
1777
1778 default:
1779 break;
1780 }
1781 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001782}
1783
1784/* tp->lock is held. */
1785static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1786{
1787 if (tg3_flag(tp, ENABLE_ASF)) {
1788 switch (kind) {
1789 case RESET_KIND_INIT:
1790 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1791 DRV_STATE_START);
1792 break;
1793
1794 case RESET_KIND_SHUTDOWN:
1795 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1796 DRV_STATE_UNLOAD);
1797 break;
1798
1799 case RESET_KIND_SUSPEND:
1800 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1801 DRV_STATE_SUSPEND);
1802 break;
1803
1804 default:
1805 break;
1806 }
1807 }
1808}
1809
1810static int tg3_poll_fw(struct tg3 *tp)
1811{
1812 int i;
1813 u32 val;
1814
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001815 if (tg3_flag(tp, NO_FWARE_REPORTED))
1816 return 0;
1817
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001818 if (tg3_flag(tp, IS_SSB_CORE)) {
1819 /* We don't use firmware. */
1820 return 0;
1821 }
1822
Joe Perches41535772013-02-16 11:20:04 +00001823 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001824 /* Wait up to 20ms for init done. */
1825 for (i = 0; i < 200; i++) {
1826 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1827 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001828 if (pci_channel_offline(tp->pdev))
1829 return -ENODEV;
1830
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001831 udelay(100);
1832 }
1833 return -ENODEV;
1834 }
1835
1836 /* Wait for firmware initialization to complete. */
1837 for (i = 0; i < 100000; i++) {
1838 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1839 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1840 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001841 if (pci_channel_offline(tp->pdev)) {
1842 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1843 tg3_flag_set(tp, NO_FWARE_REPORTED);
1844 netdev_info(tp->dev, "No firmware running\n");
1845 }
1846
1847 break;
1848 }
1849
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001850 udelay(10);
1851 }
1852
1853 /* Chip might not be fitted with firmware. Some Sun onboard
1854 * parts are configured like that. So don't signal the timeout
1855 * of the above loop as an error, but do report the lack of
1856 * running firmware once.
1857 */
1858 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1859 tg3_flag_set(tp, NO_FWARE_REPORTED);
1860
1861 netdev_info(tp->dev, "No firmware running\n");
1862 }
1863
Joe Perches41535772013-02-16 11:20:04 +00001864 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001865 /* The 57765 A0 needs a little more
1866 * time to do some important work.
1867 */
1868 mdelay(10);
1869 }
1870
1871 return 0;
1872}
1873
Matt Carlson95e28692008-05-25 23:44:14 -07001874static void tg3_link_report(struct tg3 *tp)
1875{
1876 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001877 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001878 tg3_ump_link_report(tp);
1879 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001880 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1881 (tp->link_config.active_speed == SPEED_1000 ?
1882 1000 :
1883 (tp->link_config.active_speed == SPEED_100 ?
1884 100 : 10)),
1885 (tp->link_config.active_duplex == DUPLEX_FULL ?
1886 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001887
Joe Perches05dbe002010-02-17 19:44:19 +00001888 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1889 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1890 "on" : "off",
1891 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1892 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001893
1894 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1895 netdev_info(tp->dev, "EEE is %s\n",
1896 tp->setlpicnt ? "enabled" : "disabled");
1897
Matt Carlson95e28692008-05-25 23:44:14 -07001898 tg3_ump_link_report(tp);
1899 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001900
1901 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001902}
1903
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001904static u32 tg3_decode_flowctrl_1000T(u32 adv)
1905{
1906 u32 flowctrl = 0;
1907
1908 if (adv & ADVERTISE_PAUSE_CAP) {
1909 flowctrl |= FLOW_CTRL_RX;
1910 if (!(adv & ADVERTISE_PAUSE_ASYM))
1911 flowctrl |= FLOW_CTRL_TX;
1912 } else if (adv & ADVERTISE_PAUSE_ASYM)
1913 flowctrl |= FLOW_CTRL_TX;
1914
1915 return flowctrl;
1916}
1917
Matt Carlson95e28692008-05-25 23:44:14 -07001918static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1919{
1920 u16 miireg;
1921
Steve Glendinninge18ce342008-12-16 02:00:00 -08001922 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001923 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001924 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001925 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001926 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001927 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1928 else
1929 miireg = 0;
1930
1931 return miireg;
1932}
1933
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001934static u32 tg3_decode_flowctrl_1000X(u32 adv)
1935{
1936 u32 flowctrl = 0;
1937
1938 if (adv & ADVERTISE_1000XPAUSE) {
1939 flowctrl |= FLOW_CTRL_RX;
1940 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1941 flowctrl |= FLOW_CTRL_TX;
1942 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1943 flowctrl |= FLOW_CTRL_TX;
1944
1945 return flowctrl;
1946}
1947
Matt Carlson95e28692008-05-25 23:44:14 -07001948static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1949{
1950 u8 cap = 0;
1951
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001952 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1953 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1954 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1955 if (lcladv & ADVERTISE_1000XPAUSE)
1956 cap = FLOW_CTRL_RX;
1957 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001958 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001959 }
1960
1961 return cap;
1962}
1963
Matt Carlsonf51f3562008-05-25 23:45:08 -07001964static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001965{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001966 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001967 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001968 u32 old_rx_mode = tp->rx_mode;
1969 u32 old_tx_mode = tp->tx_mode;
1970
Joe Perches63c3a662011-04-26 08:12:10 +00001971 if (tg3_flag(tp, USE_PHYLIB))
Andrew Lunn7f854422016-01-06 20:11:18 +01001972 autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001973 else
1974 autoneg = tp->link_config.autoneg;
1975
Joe Perches63c3a662011-04-26 08:12:10 +00001976 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001977 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001978 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001979 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001980 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001981 } else
1982 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001983
Matt Carlsonf51f3562008-05-25 23:45:08 -07001984 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001985
Steve Glendinninge18ce342008-12-16 02:00:00 -08001986 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001987 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1988 else
1989 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1990
Matt Carlsonf51f3562008-05-25 23:45:08 -07001991 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001992 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001993
Steve Glendinninge18ce342008-12-16 02:00:00 -08001994 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001995 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1996 else
1997 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1998
Matt Carlsonf51f3562008-05-25 23:45:08 -07001999 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002000 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002001}
2002
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002003static void tg3_adjust_link(struct net_device *dev)
2004{
2005 u8 oldflowctrl, linkmesg = 0;
2006 u32 mac_mode, lcl_adv, rmt_adv;
2007 struct tg3 *tp = netdev_priv(dev);
Andrew Lunn7f854422016-01-06 20:11:18 +01002008 struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002009
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002010 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002011
2012 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2013 MAC_MODE_HALF_DUPLEX);
2014
2015 oldflowctrl = tp->link_config.active_flowctrl;
2016
2017 if (phydev->link) {
2018 lcl_adv = 0;
2019 rmt_adv = 0;
2020
2021 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2022 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002023 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002024 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002025 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002026 else
2027 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002028
2029 if (phydev->duplex == DUPLEX_HALF)
2030 mac_mode |= MAC_MODE_HALF_DUPLEX;
2031 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002032 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033 tp->link_config.flowctrl);
2034
2035 if (phydev->pause)
2036 rmt_adv = LPA_PAUSE_CAP;
2037 if (phydev->asym_pause)
2038 rmt_adv |= LPA_PAUSE_ASYM;
2039 }
2040
2041 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2042 } else
2043 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2044
2045 if (mac_mode != tp->mac_mode) {
2046 tp->mac_mode = mac_mode;
2047 tw32_f(MAC_MODE, tp->mac_mode);
2048 udelay(40);
2049 }
2050
Joe Perches41535772013-02-16 11:20:04 +00002051 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002052 if (phydev->speed == SPEED_10)
2053 tw32(MAC_MI_STAT,
2054 MAC_MI_STAT_10MBPS_MODE |
2055 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2056 else
2057 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2058 }
2059
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002060 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2061 tw32(MAC_TX_LENGTHS,
2062 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2063 (6 << TX_LENGTHS_IPG_SHIFT) |
2064 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2065 else
2066 tw32(MAC_TX_LENGTHS,
2067 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2068 (6 << TX_LENGTHS_IPG_SHIFT) |
2069 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2070
Matt Carlson34655ad2012-02-22 12:35:18 +00002071 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002072 phydev->speed != tp->link_config.active_speed ||
2073 phydev->duplex != tp->link_config.active_duplex ||
2074 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002075 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002076
Matt Carlson34655ad2012-02-22 12:35:18 +00002077 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002078 tp->link_config.active_speed = phydev->speed;
2079 tp->link_config.active_duplex = phydev->duplex;
2080
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002081 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082
2083 if (linkmesg)
2084 tg3_link_report(tp);
2085}
2086
2087static int tg3_phy_init(struct tg3 *tp)
2088{
2089 struct phy_device *phydev;
2090
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002091 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002092 return 0;
2093
2094 /* Bring the PHY back to a known state. */
2095 tg3_bmcr_reset(tp);
2096
Andrew Lunn7f854422016-01-06 20:11:18 +01002097 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002098
2099 /* Attach the MAC to the PHY. */
Andrew Lunn84eff6d2016-01-06 20:11:10 +01002100 phydev = phy_connect(tp->dev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +00002101 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002103 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002104 return PTR_ERR(phydev);
2105 }
2106
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002107 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002108 switch (phydev->interface) {
2109 case PHY_INTERFACE_MODE_GMII:
2110 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002111 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002112 phydev->supported &= (PHY_GBIT_FEATURES |
2113 SUPPORTED_Pause |
2114 SUPPORTED_Asym_Pause);
2115 break;
2116 }
2117 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002118 case PHY_INTERFACE_MODE_MII:
2119 phydev->supported &= (PHY_BASIC_FEATURES |
2120 SUPPORTED_Pause |
2121 SUPPORTED_Asym_Pause);
2122 break;
2123 default:
Andrew Lunn7f854422016-01-06 20:11:18 +01002124 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002125 return -EINVAL;
2126 }
2127
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002128 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002129
2130 phydev->advertising = phydev->supported;
2131
Andrew Lunn22209432016-01-06 20:11:13 +01002132 phy_attached_info(phydev);
2133
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002134 return 0;
2135}
2136
2137static void tg3_phy_start(struct tg3 *tp)
2138{
2139 struct phy_device *phydev;
2140
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002141 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002142 return;
2143
Andrew Lunn7f854422016-01-06 20:11:18 +01002144 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002145
Matt Carlson80096062010-08-02 11:26:06 +00002146 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2147 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002148 phydev->speed = tp->link_config.speed;
2149 phydev->duplex = tp->link_config.duplex;
2150 phydev->autoneg = tp->link_config.autoneg;
2151 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002152 }
2153
2154 phy_start(phydev);
2155
2156 phy_start_aneg(phydev);
2157}
2158
2159static void tg3_phy_stop(struct tg3 *tp)
2160{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002161 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002162 return;
2163
Andrew Lunn7f854422016-01-06 20:11:18 +01002164 phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002165}
2166
2167static void tg3_phy_fini(struct tg3 *tp)
2168{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002169 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Andrew Lunn7f854422016-01-06 20:11:18 +01002170 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002171 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002172 }
2173}
2174
Matt Carlson941ec902011-08-19 13:58:23 +00002175static int tg3_phy_set_extloopbk(struct tg3 *tp)
2176{
2177 int err;
2178 u32 val;
2179
2180 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2181 return 0;
2182
2183 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2184 /* Cannot do read-modify-write on 5401 */
2185 err = tg3_phy_auxctl_write(tp,
2186 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2187 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2188 0x4c20);
2189 goto done;
2190 }
2191
2192 err = tg3_phy_auxctl_read(tp,
2193 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2194 if (err)
2195 return err;
2196
2197 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2198 err = tg3_phy_auxctl_write(tp,
2199 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2200
2201done:
2202 return err;
2203}
2204
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002205static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2206{
2207 u32 phytest;
2208
2209 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2210 u32 phy;
2211
2212 tg3_writephy(tp, MII_TG3_FET_TEST,
2213 phytest | MII_TG3_FET_SHADOW_EN);
2214 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2215 if (enable)
2216 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2217 else
2218 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2219 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2220 }
2221 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2222 }
2223}
2224
Matt Carlson6833c042008-11-21 17:18:59 -08002225static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2226{
2227 u32 reg;
2228
Joe Perches63c3a662011-04-26 08:12:10 +00002229 if (!tg3_flag(tp, 5705_PLUS) ||
2230 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002231 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002232 return;
2233
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002234 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002235 tg3_phy_fet_toggle_apd(tp, enable);
2236 return;
2237 }
2238
Nithin Sujir3ab71072013-09-20 16:46:55 -07002239 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002240 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2241 MII_TG3_MISC_SHDW_SCR5_SDTL |
2242 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002243 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002244 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2245
Nithin Sujir3ab71072013-09-20 16:46:55 -07002246 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002247
2248
Nithin Sujir3ab71072013-09-20 16:46:55 -07002249 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002250 if (enable)
2251 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2252
Nithin Sujir3ab71072013-09-20 16:46:55 -07002253 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002254}
2255
Joe Perches953c96e2013-04-09 10:18:14 +00002256static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002257{
2258 u32 phy;
2259
Joe Perches63c3a662011-04-26 08:12:10 +00002260 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002261 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002262 return;
2263
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002264 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002265 u32 ephy;
2266
Matt Carlson535ef6e2009-08-25 10:09:36 +00002267 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2268 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2269
2270 tg3_writephy(tp, MII_TG3_FET_TEST,
2271 ephy | MII_TG3_FET_SHADOW_EN);
2272 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002273 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002274 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002275 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002276 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2277 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002278 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002279 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002280 }
2281 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002282 int ret;
2283
2284 ret = tg3_phy_auxctl_read(tp,
2285 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2286 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002287 if (enable)
2288 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2289 else
2290 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002291 tg3_phy_auxctl_write(tp,
2292 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002293 }
2294 }
2295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static void tg3_phy_set_wirespeed(struct tg3 *tp)
2298{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002299 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 u32 val;
2301
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002302 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return;
2304
Matt Carlson15ee95c2011-04-20 07:57:40 +00002305 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2306 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002307 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2308 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002311static void tg3_phy_apply_otp(struct tg3 *tp)
2312{
2313 u32 otp, phy;
2314
2315 if (!tp->phy_otp)
2316 return;
2317
2318 otp = tp->phy_otp;
2319
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002320 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002321 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002322
2323 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2324 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2325 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2326
2327 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2328 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2329 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2330
2331 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2332 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2333 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2334
2335 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2336 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2337
2338 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2339 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2340
2341 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2342 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2343 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2344
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002345 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002346}
2347
Nithin Sujir400dfba2013-05-18 06:26:53 +00002348static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2349{
2350 u32 val;
2351 struct ethtool_eee *dest = &tp->eee;
2352
2353 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2354 return;
2355
2356 if (eee)
2357 dest = eee;
2358
2359 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2360 return;
2361
2362 /* Pull eee_active */
2363 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2364 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2365 dest->eee_active = 1;
2366 } else
2367 dest->eee_active = 0;
2368
2369 /* Pull lp advertised settings */
2370 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2371 return;
2372 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2373
2374 /* Pull advertised and eee_enabled settings */
2375 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2376 return;
2377 dest->eee_enabled = !!val;
2378 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2379
2380 /* Pull tx_lpi_enabled */
2381 val = tr32(TG3_CPMU_EEE_MODE);
2382 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2383
2384 /* Pull lpi timer value */
2385 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2386}
2387
Joe Perches953c96e2013-04-09 10:18:14 +00002388static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002389{
2390 u32 val;
2391
2392 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2393 return;
2394
2395 tp->setlpicnt = 0;
2396
2397 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002398 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002399 tp->link_config.active_duplex == DUPLEX_FULL &&
2400 (tp->link_config.active_speed == SPEED_100 ||
2401 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002402 u32 eeectl;
2403
2404 if (tp->link_config.active_speed == SPEED_1000)
2405 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2406 else
2407 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2408
2409 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2410
Nithin Sujir400dfba2013-05-18 06:26:53 +00002411 tg3_eee_pull_config(tp, NULL);
2412 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002413 tp->setlpicnt = 2;
2414 }
2415
2416 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002417 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002418 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002419 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002420 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002421 }
2422
Matt Carlson52b02d02010-10-14 10:37:41 +00002423 val = tr32(TG3_CPMU_EEE_MODE);
2424 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2425 }
2426}
2427
Matt Carlsonb0c59432011-05-19 12:12:48 +00002428static void tg3_phy_eee_enable(struct tg3 *tp)
2429{
2430 u32 val;
2431
2432 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002433 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2434 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002435 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002436 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002437 val = MII_TG3_DSP_TAP26_ALNOKO |
2438 MII_TG3_DSP_TAP26_RMRXSTO;
2439 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002440 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002441 }
2442
2443 val = tr32(TG3_CPMU_EEE_MODE);
2444 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447static int tg3_wait_macro_done(struct tg3 *tp)
2448{
2449 int limit = 100;
2450
2451 while (limit--) {
2452 u32 tmp32;
2453
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002454 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if ((tmp32 & 0x1000) == 0)
2456 break;
2457 }
2458 }
Roel Kluind4675b52009-02-12 16:33:27 -08002459 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return -EBUSY;
2461
2462 return 0;
2463}
2464
2465static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2466{
2467 static const u32 test_pat[4][6] = {
2468 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2469 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2470 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2471 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2472 };
2473 int chan;
2474
2475 for (chan = 0; chan < 4; chan++) {
2476 int i;
2477
2478 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2479 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002480 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 for (i = 0; i < 6; i++)
2483 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2484 test_pat[chan][i]);
2485
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002486 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (tg3_wait_macro_done(tp)) {
2488 *resetp = 1;
2489 return -EBUSY;
2490 }
2491
2492 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2493 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002494 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (tg3_wait_macro_done(tp)) {
2496 *resetp = 1;
2497 return -EBUSY;
2498 }
2499
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002500 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (tg3_wait_macro_done(tp)) {
2502 *resetp = 1;
2503 return -EBUSY;
2504 }
2505
2506 for (i = 0; i < 6; i += 2) {
2507 u32 low, high;
2508
2509 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2510 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2511 tg3_wait_macro_done(tp)) {
2512 *resetp = 1;
2513 return -EBUSY;
2514 }
2515 low &= 0x7fff;
2516 high &= 0x000f;
2517 if (low != test_pat[chan][i] ||
2518 high != test_pat[chan][i+1]) {
2519 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2520 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2521 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2522
2523 return -EBUSY;
2524 }
2525 }
2526 }
2527
2528 return 0;
2529}
2530
2531static int tg3_phy_reset_chanpat(struct tg3 *tp)
2532{
2533 int chan;
2534
2535 for (chan = 0; chan < 4; chan++) {
2536 int i;
2537
2538 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2539 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002540 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 for (i = 0; i < 6; i++)
2542 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002543 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (tg3_wait_macro_done(tp))
2545 return -EBUSY;
2546 }
2547
2548 return 0;
2549}
2550
2551static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2552{
2553 u32 reg32, phy9_orig;
2554 int retries, do_phy_reset, err;
2555
2556 retries = 10;
2557 do_phy_reset = 1;
2558 do {
2559 if (do_phy_reset) {
2560 err = tg3_bmcr_reset(tp);
2561 if (err)
2562 return err;
2563 do_phy_reset = 0;
2564 }
2565
2566 /* Disable transmitter and interrupt. */
2567 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2568 continue;
2569
2570 reg32 |= 0x3000;
2571 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2572
2573 /* Set full-duplex, 1000 mbps. */
2574 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002575 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002578 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 continue;
2580
Matt Carlson221c5632011-06-13 13:39:01 +00002581 tg3_writephy(tp, MII_CTRL1000,
2582 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002584 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002585 if (err)
2586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002589 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2592 if (!err)
2593 break;
2594 } while (--retries);
2595
2596 err = tg3_phy_reset_chanpat(tp);
2597 if (err)
2598 return err;
2599
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002600 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002603 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002605 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Matt Carlson221c5632011-06-13 13:39:01 +00002607 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002609 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2610 if (err)
2611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002613 reg32 &= ~0x3000;
2614 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2615
2616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002619static void tg3_carrier_off(struct tg3 *tp)
2620{
2621 netif_carrier_off(tp->dev);
2622 tp->link_up = false;
2623}
2624
Nithin Sujirce20f162013-04-09 08:48:04 +00002625static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2626{
2627 if (tg3_flag(tp, ENABLE_ASF))
2628 netdev_warn(tp->dev,
2629 "Management side-band traffic will be interrupted during phy settings change\n");
2630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632/* This will reset the tigon3 PHY if there is no valid
2633 * link unless the FORCE argument is non-zero.
2634 */
2635static int tg3_phy_reset(struct tg3 *tp)
2636{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002637 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int err;
2639
Joe Perches41535772013-02-16 11:20:04 +00002640 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002641 val = tr32(GRC_MISC_CFG);
2642 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2643 udelay(40);
2644 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002645 err = tg3_readphy(tp, MII_BMSR, &val);
2646 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (err != 0)
2648 return -EBUSY;
2649
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002650 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002651 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002652 tg3_link_report(tp);
2653 }
2654
Joe Perches41535772013-02-16 11:20:04 +00002655 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2656 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2657 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 err = tg3_phy_reset_5703_4_5(tp);
2659 if (err)
2660 return err;
2661 goto out;
2662 }
2663
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002664 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002665 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2666 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002667 cpmuctrl = tr32(TG3_CPMU_CTRL);
2668 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2669 tw32(TG3_CPMU_CTRL,
2670 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2671 }
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 err = tg3_bmcr_reset(tp);
2674 if (err)
2675 return err;
2676
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002677 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002678 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2679 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002680
2681 tw32(TG3_CPMU_CTRL, cpmuctrl);
2682 }
2683
Joe Perches41535772013-02-16 11:20:04 +00002684 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2685 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002686 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2687 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2688 CPMU_LSPD_1000MB_MACCLK_12_5) {
2689 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2690 udelay(40);
2691 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2692 }
2693 }
2694
Joe Perches63c3a662011-04-26 08:12:10 +00002695 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002696 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002697 return 0;
2698
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002699 tg3_phy_apply_otp(tp);
2700
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002701 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002702 tg3_phy_toggle_apd(tp, true);
2703 else
2704 tg3_phy_toggle_apd(tp, false);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002707 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002708 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002709 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2710 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002711 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002713
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002714 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002715 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2716 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002718
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002719 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002720 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002721 tg3_phydsp_write(tp, 0x000a, 0x310b);
2722 tg3_phydsp_write(tp, 0x201f, 0x9506);
2723 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002724 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002725 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002726 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002727 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002728 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2729 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2730 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2731 tg3_writephy(tp, MII_TG3_TEST1,
2732 MII_TG3_TEST1_TRIM_EN | 0x4);
2733 } else
2734 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2735
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002736 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002737 }
Michael Chanc424cb22006-04-29 18:56:34 -07002738 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /* Set Extended packet length bit (bit 14) on all chips that */
2741 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002742 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002744 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002745 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002747 err = tg3_phy_auxctl_read(tp,
2748 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2749 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002750 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2751 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753
2754 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2755 * jumbo frames transmission.
2756 */
Joe Perches63c3a662011-04-26 08:12:10 +00002757 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002758 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002759 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002760 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762
Joe Perches41535772013-02-16 11:20:04 +00002763 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002764 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002765 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002766 }
2767
Joe Perches41535772013-02-16 11:20:04 +00002768 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002769 tg3_phydsp_write(tp, 0xffb, 0x4000);
2770
Joe Perches953c96e2013-04-09 10:18:14 +00002771 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 tg3_phy_set_wirespeed(tp);
2773 return 0;
2774}
2775
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002776#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2777#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2778#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2779 TG3_GPIO_MSG_NEED_VAUX)
2780#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2781 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2782 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2783 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2784 (TG3_GPIO_MSG_DRVR_PRES << 12))
2785
2786#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2787 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2788 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2789 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2790 (TG3_GPIO_MSG_NEED_VAUX << 12))
2791
2792static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2793{
2794 u32 status, shift;
2795
Joe Perches41535772013-02-16 11:20:04 +00002796 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2797 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002798 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2799 else
2800 status = tr32(TG3_CPMU_DRV_STATUS);
2801
2802 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2803 status &= ~(TG3_GPIO_MSG_MASK << shift);
2804 status |= (newstat << shift);
2805
Joe Perches41535772013-02-16 11:20:04 +00002806 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2807 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002808 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2809 else
2810 tw32(TG3_CPMU_DRV_STATUS, status);
2811
2812 return status >> TG3_APE_GPIO_MSG_SHIFT;
2813}
2814
Matt Carlson520b2752011-06-13 13:39:02 +00002815static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2816{
2817 if (!tg3_flag(tp, IS_NIC))
2818 return 0;
2819
Joe Perches41535772013-02-16 11:20:04 +00002820 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2821 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2822 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002823 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2824 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002825
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002826 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2827
2828 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2829 TG3_GRC_LCLCTL_PWRSW_DELAY);
2830
2831 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2832 } else {
2833 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2834 TG3_GRC_LCLCTL_PWRSW_DELAY);
2835 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002836
Matt Carlson520b2752011-06-13 13:39:02 +00002837 return 0;
2838}
2839
2840static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2841{
2842 u32 grc_local_ctrl;
2843
2844 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002845 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2846 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002847 return;
2848
2849 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2850
2851 tw32_wait_f(GRC_LOCAL_CTRL,
2852 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2853 TG3_GRC_LCLCTL_PWRSW_DELAY);
2854
2855 tw32_wait_f(GRC_LOCAL_CTRL,
2856 grc_local_ctrl,
2857 TG3_GRC_LCLCTL_PWRSW_DELAY);
2858
2859 tw32_wait_f(GRC_LOCAL_CTRL,
2860 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2861 TG3_GRC_LCLCTL_PWRSW_DELAY);
2862}
2863
2864static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2865{
2866 if (!tg3_flag(tp, IS_NIC))
2867 return;
2868
Joe Perches41535772013-02-16 11:20:04 +00002869 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2870 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002871 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2872 (GRC_LCLCTRL_GPIO_OE0 |
2873 GRC_LCLCTRL_GPIO_OE1 |
2874 GRC_LCLCTRL_GPIO_OE2 |
2875 GRC_LCLCTRL_GPIO_OUTPUT0 |
2876 GRC_LCLCTRL_GPIO_OUTPUT1),
2877 TG3_GRC_LCLCTL_PWRSW_DELAY);
2878 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2879 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2880 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2881 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2882 GRC_LCLCTRL_GPIO_OE1 |
2883 GRC_LCLCTRL_GPIO_OE2 |
2884 GRC_LCLCTRL_GPIO_OUTPUT0 |
2885 GRC_LCLCTRL_GPIO_OUTPUT1 |
2886 tp->grc_local_ctrl;
2887 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2888 TG3_GRC_LCLCTL_PWRSW_DELAY);
2889
2890 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2891 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2892 TG3_GRC_LCLCTL_PWRSW_DELAY);
2893
2894 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2895 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2896 TG3_GRC_LCLCTL_PWRSW_DELAY);
2897 } else {
2898 u32 no_gpio2;
2899 u32 grc_local_ctrl = 0;
2900
2901 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002902 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002903 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2904 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2905 grc_local_ctrl,
2906 TG3_GRC_LCLCTL_PWRSW_DELAY);
2907 }
2908
2909 /* On 5753 and variants, GPIO2 cannot be used. */
2910 no_gpio2 = tp->nic_sram_data_cfg &
2911 NIC_SRAM_DATA_CFG_NO_GPIO2;
2912
2913 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2914 GRC_LCLCTRL_GPIO_OE1 |
2915 GRC_LCLCTRL_GPIO_OE2 |
2916 GRC_LCLCTRL_GPIO_OUTPUT1 |
2917 GRC_LCLCTRL_GPIO_OUTPUT2;
2918 if (no_gpio2) {
2919 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2920 GRC_LCLCTRL_GPIO_OUTPUT2);
2921 }
2922 tw32_wait_f(GRC_LOCAL_CTRL,
2923 tp->grc_local_ctrl | grc_local_ctrl,
2924 TG3_GRC_LCLCTL_PWRSW_DELAY);
2925
2926 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2927
2928 tw32_wait_f(GRC_LOCAL_CTRL,
2929 tp->grc_local_ctrl | grc_local_ctrl,
2930 TG3_GRC_LCLCTL_PWRSW_DELAY);
2931
2932 if (!no_gpio2) {
2933 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2934 tw32_wait_f(GRC_LOCAL_CTRL,
2935 tp->grc_local_ctrl | grc_local_ctrl,
2936 TG3_GRC_LCLCTL_PWRSW_DELAY);
2937 }
2938 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002939}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002940
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002941static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002942{
2943 u32 msg = 0;
2944
2945 /* Serialize power state transitions */
2946 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2947 return;
2948
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002949 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002950 msg = TG3_GPIO_MSG_NEED_VAUX;
2951
2952 msg = tg3_set_function_status(tp, msg);
2953
2954 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2955 goto done;
2956
2957 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2958 tg3_pwrsrc_switch_to_vaux(tp);
2959 else
2960 tg3_pwrsrc_die_with_vmain(tp);
2961
2962done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002963 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002964}
2965
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002966static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Matt Carlson683644b2011-03-09 16:58:23 +00002968 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Matt Carlson334355a2010-01-20 16:58:10 +00002970 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002971 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return;
2973
Joe Perches41535772013-02-16 11:20:04 +00002974 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2975 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2976 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002977 tg3_frob_aux_power_5717(tp, include_wol ?
2978 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002979 return;
2980 }
2981
2982 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002983 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002985 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002986
Michael Chanbc1c7562006-03-20 17:48:03 -08002987 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002988 if (dev_peer) {
2989 struct tg3 *tp_peer = netdev_priv(dev_peer);
2990
Joe Perches63c3a662011-04-26 08:12:10 +00002991 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002992 return;
2993
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002994 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002995 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002996 need_vaux = true;
2997 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003000 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3001 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003002 need_vaux = true;
3003
Matt Carlson520b2752011-06-13 13:39:02 +00003004 if (need_vaux)
3005 tg3_pwrsrc_switch_to_vaux(tp);
3006 else
3007 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003010static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3011{
3012 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3013 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003014 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003015 if (speed != SPEED_10)
3016 return 1;
3017 } else if (speed == SPEED_10)
3018 return 1;
3019
3020 return 0;
3021}
3022
Nithin Sujir44f3b502013-05-13 11:04:15 +00003023static bool tg3_phy_power_bug(struct tg3 *tp)
3024{
3025 switch (tg3_asic_rev(tp)) {
3026 case ASIC_REV_5700:
3027 case ASIC_REV_5704:
3028 return true;
3029 case ASIC_REV_5780:
3030 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3031 return true;
3032 return false;
3033 case ASIC_REV_5717:
3034 if (!tp->pci_fn)
3035 return true;
3036 return false;
3037 case ASIC_REV_5719:
3038 case ASIC_REV_5720:
3039 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3040 !tp->pci_fn)
3041 return true;
3042 return false;
3043 }
3044
3045 return false;
3046}
3047
Nithin Sujir989038e2013-08-30 17:01:36 -07003048static bool tg3_phy_led_bug(struct tg3 *tp)
3049{
3050 switch (tg3_asic_rev(tp)) {
3051 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003052 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003053 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3054 !tp->pci_fn)
3055 return true;
3056 return false;
3057 }
3058
3059 return false;
3060}
3061
Matt Carlson0a459aa2008-11-03 16:54:15 -08003062static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003063{
Matt Carlsonce057f02007-11-12 21:08:03 -08003064 u32 val;
3065
Nithin Sujir942d1af2013-04-09 08:48:07 +00003066 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3067 return;
3068
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003069 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003070 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003071 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3072 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3073
3074 sg_dig_ctrl |=
3075 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3076 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3077 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3078 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003079 return;
Michael Chan51297242007-02-13 12:17:57 -08003080 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003081
Joe Perches41535772013-02-16 11:20:04 +00003082 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003083 tg3_bmcr_reset(tp);
3084 val = tr32(GRC_MISC_CFG);
3085 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3086 udelay(40);
3087 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003088 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003089 u32 phytest;
3090 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3091 u32 phy;
3092
3093 tg3_writephy(tp, MII_ADVERTISE, 0);
3094 tg3_writephy(tp, MII_BMCR,
3095 BMCR_ANENABLE | BMCR_ANRESTART);
3096
3097 tg3_writephy(tp, MII_TG3_FET_TEST,
3098 phytest | MII_TG3_FET_SHADOW_EN);
3099 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3100 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3101 tg3_writephy(tp,
3102 MII_TG3_FET_SHDW_AUXMODE4,
3103 phy);
3104 }
3105 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3106 }
3107 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003108 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003109 if (!tg3_phy_led_bug(tp))
3110 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3111 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003112
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003113 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3114 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3115 MII_TG3_AUXCTL_PCTL_VREG_11V;
3116 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003117 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003118
Michael Chan15c3b692006-03-22 01:06:52 -08003119 /* The PHY should not be powered down on some chips because
3120 * of bugs.
3121 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003122 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003123 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003124
Joe Perches41535772013-02-16 11:20:04 +00003125 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3126 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003127 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3128 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3129 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3130 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3131 }
3132
Michael Chan15c3b692006-03-22 01:06:52 -08003133 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3134}
3135
Matt Carlson3f007892008-11-03 16:51:36 -08003136/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003137static int tg3_nvram_lock(struct tg3 *tp)
3138{
Joe Perches63c3a662011-04-26 08:12:10 +00003139 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003140 int i;
3141
3142 if (tp->nvram_lock_cnt == 0) {
3143 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3144 for (i = 0; i < 8000; i++) {
3145 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3146 break;
3147 udelay(20);
3148 }
3149 if (i == 8000) {
3150 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3151 return -ENODEV;
3152 }
3153 }
3154 tp->nvram_lock_cnt++;
3155 }
3156 return 0;
3157}
3158
3159/* tp->lock is held. */
3160static void tg3_nvram_unlock(struct tg3 *tp)
3161{
Joe Perches63c3a662011-04-26 08:12:10 +00003162 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003163 if (tp->nvram_lock_cnt > 0)
3164 tp->nvram_lock_cnt--;
3165 if (tp->nvram_lock_cnt == 0)
3166 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3167 }
3168}
3169
3170/* tp->lock is held. */
3171static void tg3_enable_nvram_access(struct tg3 *tp)
3172{
Joe Perches63c3a662011-04-26 08:12:10 +00003173 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003174 u32 nvaccess = tr32(NVRAM_ACCESS);
3175
3176 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3177 }
3178}
3179
3180/* tp->lock is held. */
3181static void tg3_disable_nvram_access(struct tg3 *tp)
3182{
Joe Perches63c3a662011-04-26 08:12:10 +00003183 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003184 u32 nvaccess = tr32(NVRAM_ACCESS);
3185
3186 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3187 }
3188}
3189
3190static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3191 u32 offset, u32 *val)
3192{
3193 u32 tmp;
3194 int i;
3195
3196 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3197 return -EINVAL;
3198
3199 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3200 EEPROM_ADDR_DEVID_MASK |
3201 EEPROM_ADDR_READ);
3202 tw32(GRC_EEPROM_ADDR,
3203 tmp |
3204 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3205 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3206 EEPROM_ADDR_ADDR_MASK) |
3207 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3208
3209 for (i = 0; i < 1000; i++) {
3210 tmp = tr32(GRC_EEPROM_ADDR);
3211
3212 if (tmp & EEPROM_ADDR_COMPLETE)
3213 break;
3214 msleep(1);
3215 }
3216 if (!(tmp & EEPROM_ADDR_COMPLETE))
3217 return -EBUSY;
3218
Matt Carlson62cedd12009-04-20 14:52:29 -07003219 tmp = tr32(GRC_EEPROM_DATA);
3220
3221 /*
3222 * The data will always be opposite the native endian
3223 * format. Perform a blind byteswap to compensate.
3224 */
3225 *val = swab32(tmp);
3226
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003227 return 0;
3228}
3229
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +05303230#define NVRAM_CMD_TIMEOUT 10000
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003231
3232static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3233{
3234 int i;
3235
3236 tw32(NVRAM_CMD, nvram_cmd);
3237 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003238 usleep_range(10, 40);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003239 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3240 udelay(10);
3241 break;
3242 }
3243 }
3244
3245 if (i == NVRAM_CMD_TIMEOUT)
3246 return -EBUSY;
3247
3248 return 0;
3249}
3250
3251static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3252{
Joe Perches63c3a662011-04-26 08:12:10 +00003253 if (tg3_flag(tp, NVRAM) &&
3254 tg3_flag(tp, NVRAM_BUFFERED) &&
3255 tg3_flag(tp, FLASH) &&
3256 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003257 (tp->nvram_jedecnum == JEDEC_ATMEL))
3258
3259 addr = ((addr / tp->nvram_pagesize) <<
3260 ATMEL_AT45DB0X1B_PAGE_POS) +
3261 (addr % tp->nvram_pagesize);
3262
3263 return addr;
3264}
3265
3266static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3267{
Joe Perches63c3a662011-04-26 08:12:10 +00003268 if (tg3_flag(tp, NVRAM) &&
3269 tg3_flag(tp, NVRAM_BUFFERED) &&
3270 tg3_flag(tp, FLASH) &&
3271 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003272 (tp->nvram_jedecnum == JEDEC_ATMEL))
3273
3274 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3275 tp->nvram_pagesize) +
3276 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3277
3278 return addr;
3279}
3280
Matt Carlsone4f34112009-02-25 14:25:00 +00003281/* NOTE: Data read in from NVRAM is byteswapped according to
3282 * the byteswapping settings for all other register accesses.
3283 * tg3 devices are BE devices, so on a BE machine, the data
3284 * returned will be exactly as it is seen in NVRAM. On a LE
3285 * machine, the 32-bit value will be byteswapped.
3286 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003287static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3288{
3289 int ret;
3290
Joe Perches63c3a662011-04-26 08:12:10 +00003291 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003292 return tg3_nvram_read_using_eeprom(tp, offset, val);
3293
3294 offset = tg3_nvram_phys_addr(tp, offset);
3295
3296 if (offset > NVRAM_ADDR_MSK)
3297 return -EINVAL;
3298
3299 ret = tg3_nvram_lock(tp);
3300 if (ret)
3301 return ret;
3302
3303 tg3_enable_nvram_access(tp);
3304
3305 tw32(NVRAM_ADDR, offset);
3306 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3307 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3308
3309 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003310 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003311
3312 tg3_disable_nvram_access(tp);
3313
3314 tg3_nvram_unlock(tp);
3315
3316 return ret;
3317}
3318
Matt Carlsona9dc5292009-02-25 14:25:30 +00003319/* Ensures NVRAM data is in bytestream format. */
3320static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003321{
3322 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003323 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003324 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003325 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003326 return res;
3327}
3328
Matt Carlsondbe9b922012-02-13 10:20:09 +00003329static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3330 u32 offset, u32 len, u8 *buf)
3331{
3332 int i, j, rc = 0;
3333 u32 val;
3334
3335 for (i = 0; i < len; i += 4) {
3336 u32 addr;
3337 __be32 data;
3338
3339 addr = offset + i;
3340
3341 memcpy(&data, buf + i, 4);
3342
3343 /*
3344 * The SEEPROM interface expects the data to always be opposite
3345 * the native endian format. We accomplish this by reversing
3346 * all the operations that would have been performed on the
3347 * data from a call to tg3_nvram_read_be32().
3348 */
3349 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3350
3351 val = tr32(GRC_EEPROM_ADDR);
3352 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3353
3354 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3355 EEPROM_ADDR_READ);
3356 tw32(GRC_EEPROM_ADDR, val |
3357 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3358 (addr & EEPROM_ADDR_ADDR_MASK) |
3359 EEPROM_ADDR_START |
3360 EEPROM_ADDR_WRITE);
3361
3362 for (j = 0; j < 1000; j++) {
3363 val = tr32(GRC_EEPROM_ADDR);
3364
3365 if (val & EEPROM_ADDR_COMPLETE)
3366 break;
3367 msleep(1);
3368 }
3369 if (!(val & EEPROM_ADDR_COMPLETE)) {
3370 rc = -EBUSY;
3371 break;
3372 }
3373 }
3374
3375 return rc;
3376}
3377
3378/* offset and length are dword aligned */
3379static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3380 u8 *buf)
3381{
3382 int ret = 0;
3383 u32 pagesize = tp->nvram_pagesize;
3384 u32 pagemask = pagesize - 1;
3385 u32 nvram_cmd;
3386 u8 *tmp;
3387
3388 tmp = kmalloc(pagesize, GFP_KERNEL);
3389 if (tmp == NULL)
3390 return -ENOMEM;
3391
3392 while (len) {
3393 int j;
3394 u32 phy_addr, page_off, size;
3395
3396 phy_addr = offset & ~pagemask;
3397
3398 for (j = 0; j < pagesize; j += 4) {
3399 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3400 (__be32 *) (tmp + j));
3401 if (ret)
3402 break;
3403 }
3404 if (ret)
3405 break;
3406
3407 page_off = offset & pagemask;
3408 size = pagesize;
3409 if (len < size)
3410 size = len;
3411
3412 len -= size;
3413
3414 memcpy(tmp + page_off, buf, size);
3415
3416 offset = offset + (pagesize - page_off);
3417
3418 tg3_enable_nvram_access(tp);
3419
3420 /*
3421 * Before we can erase the flash page, we need
3422 * to issue a special "write enable" command.
3423 */
3424 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3425
3426 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3427 break;
3428
3429 /* Erase the target page */
3430 tw32(NVRAM_ADDR, phy_addr);
3431
3432 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3433 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3434
3435 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3436 break;
3437
3438 /* Issue another write enable to start the write. */
3439 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3440
3441 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3442 break;
3443
3444 for (j = 0; j < pagesize; j += 4) {
3445 __be32 data;
3446
3447 data = *((__be32 *) (tmp + j));
3448
3449 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3450
3451 tw32(NVRAM_ADDR, phy_addr + j);
3452
3453 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3454 NVRAM_CMD_WR;
3455
3456 if (j == 0)
3457 nvram_cmd |= NVRAM_CMD_FIRST;
3458 else if (j == (pagesize - 4))
3459 nvram_cmd |= NVRAM_CMD_LAST;
3460
3461 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3462 if (ret)
3463 break;
3464 }
3465 if (ret)
3466 break;
3467 }
3468
3469 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3470 tg3_nvram_exec_cmd(tp, nvram_cmd);
3471
3472 kfree(tmp);
3473
3474 return ret;
3475}
3476
3477/* offset and length are dword aligned */
3478static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3479 u8 *buf)
3480{
3481 int i, ret = 0;
3482
3483 for (i = 0; i < len; i += 4, offset += 4) {
3484 u32 page_off, phy_addr, nvram_cmd;
3485 __be32 data;
3486
3487 memcpy(&data, buf + i, 4);
3488 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3489
3490 page_off = offset % tp->nvram_pagesize;
3491
3492 phy_addr = tg3_nvram_phys_addr(tp, offset);
3493
Matt Carlsondbe9b922012-02-13 10:20:09 +00003494 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3495
3496 if (page_off == 0 || i == 0)
3497 nvram_cmd |= NVRAM_CMD_FIRST;
3498 if (page_off == (tp->nvram_pagesize - 4))
3499 nvram_cmd |= NVRAM_CMD_LAST;
3500
3501 if (i == (len - 4))
3502 nvram_cmd |= NVRAM_CMD_LAST;
3503
Matt Carlson42278222012-02-13 15:20:11 +00003504 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3505 !tg3_flag(tp, FLASH) ||
3506 !tg3_flag(tp, 57765_PLUS))
3507 tw32(NVRAM_ADDR, phy_addr);
3508
Joe Perches41535772013-02-16 11:20:04 +00003509 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003510 !tg3_flag(tp, 5755_PLUS) &&
3511 (tp->nvram_jedecnum == JEDEC_ST) &&
3512 (nvram_cmd & NVRAM_CMD_FIRST)) {
3513 u32 cmd;
3514
3515 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3516 ret = tg3_nvram_exec_cmd(tp, cmd);
3517 if (ret)
3518 break;
3519 }
3520 if (!tg3_flag(tp, FLASH)) {
3521 /* We always do complete word writes to eeprom. */
3522 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3523 }
3524
3525 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3526 if (ret)
3527 break;
3528 }
3529 return ret;
3530}
3531
3532/* offset and length are dword aligned */
3533static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3534{
3535 int ret;
3536
3537 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3538 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3539 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3540 udelay(40);
3541 }
3542
3543 if (!tg3_flag(tp, NVRAM)) {
3544 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3545 } else {
3546 u32 grc_mode;
3547
3548 ret = tg3_nvram_lock(tp);
3549 if (ret)
3550 return ret;
3551
3552 tg3_enable_nvram_access(tp);
3553 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3554 tw32(NVRAM_WRITE1, 0x406);
3555
3556 grc_mode = tr32(GRC_MODE);
3557 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3558
3559 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3560 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3561 buf);
3562 } else {
3563 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3564 buf);
3565 }
3566
3567 grc_mode = tr32(GRC_MODE);
3568 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3569
3570 tg3_disable_nvram_access(tp);
3571 tg3_nvram_unlock(tp);
3572 }
3573
3574 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3575 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3576 udelay(40);
3577 }
3578
3579 return ret;
3580}
3581
Matt Carlson997b4f12011-08-31 11:44:53 +00003582#define RX_CPU_SCRATCH_BASE 0x30000
3583#define RX_CPU_SCRATCH_SIZE 0x04000
3584#define TX_CPU_SCRATCH_BASE 0x34000
3585#define TX_CPU_SCRATCH_SIZE 0x04000
3586
3587/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003588static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003589{
3590 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003591 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003592
Nithin Sujir837c45b2013-03-06 17:02:30 +00003593 for (i = 0; i < iters; i++) {
3594 tw32(cpu_base + CPU_STATE, 0xffffffff);
3595 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3596 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3597 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003598 if (pci_channel_offline(tp->pdev))
3599 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003600 }
3601
3602 return (i == iters) ? -EBUSY : 0;
3603}
3604
3605/* tp->lock is held. */
3606static int tg3_rxcpu_pause(struct tg3 *tp)
3607{
3608 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3609
3610 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3611 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3612 udelay(10);
3613
3614 return rc;
3615}
3616
3617/* tp->lock is held. */
3618static int tg3_txcpu_pause(struct tg3 *tp)
3619{
3620 return tg3_pause_cpu(tp, TX_CPU_BASE);
3621}
3622
3623/* tp->lock is held. */
3624static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3625{
3626 tw32(cpu_base + CPU_STATE, 0xffffffff);
3627 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3628}
3629
3630/* tp->lock is held. */
3631static void tg3_rxcpu_resume(struct tg3 *tp)
3632{
3633 tg3_resume_cpu(tp, RX_CPU_BASE);
3634}
3635
3636/* tp->lock is held. */
3637static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3638{
3639 int rc;
3640
3641 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003642
Joe Perches41535772013-02-16 11:20:04 +00003643 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003644 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3645
3646 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3647 return 0;
3648 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003649 if (cpu_base == RX_CPU_BASE) {
3650 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003651 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003652 /*
3653 * There is only an Rx CPU for the 5750 derivative in the
3654 * BCM4785.
3655 */
3656 if (tg3_flag(tp, IS_SSB_CORE))
3657 return 0;
3658
Nithin Sujir837c45b2013-03-06 17:02:30 +00003659 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003660 }
3661
Nithin Sujir837c45b2013-03-06 17:02:30 +00003662 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003663 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003664 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003665 return -ENODEV;
3666 }
3667
3668 /* Clear firmware's nvram arbitration. */
3669 if (tg3_flag(tp, NVRAM))
3670 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3671 return 0;
3672}
3673
Nithin Sujir31f11a92013-03-06 17:02:33 +00003674static int tg3_fw_data_len(struct tg3 *tp,
3675 const struct tg3_firmware_hdr *fw_hdr)
3676{
3677 int fw_len;
3678
3679 /* Non fragmented firmware have one firmware header followed by a
3680 * contiguous chunk of data to be written. The length field in that
3681 * header is not the length of data to be written but the complete
3682 * length of the bss. The data length is determined based on
3683 * tp->fw->size minus headers.
3684 *
3685 * Fragmented firmware have a main header followed by multiple
3686 * fragments. Each fragment is identical to non fragmented firmware
3687 * with a firmware header followed by a contiguous chunk of data. In
3688 * the main header, the length field is unused and set to 0xffffffff.
3689 * In each fragment header the length is the entire size of that
3690 * fragment i.e. fragment data + header length. Data length is
3691 * therefore length field in the header minus TG3_FW_HDR_LEN.
3692 */
3693 if (tp->fw_len == 0xffffffff)
3694 fw_len = be32_to_cpu(fw_hdr->len);
3695 else
3696 fw_len = tp->fw->size;
3697
3698 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3699}
3700
Matt Carlson997b4f12011-08-31 11:44:53 +00003701/* tp->lock is held. */
3702static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3703 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003704 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003705{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003706 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003707 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003708 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003709
3710 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3711 netdev_err(tp->dev,
3712 "%s: Trying to load TX cpu firmware which is 5705\n",
3713 __func__);
3714 return -EINVAL;
3715 }
3716
Nithin Sujirc4dab502013-03-06 17:02:34 +00003717 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003718 write_op = tg3_write_mem;
3719 else
3720 write_op = tg3_write_indirect_reg32;
3721
Nithin Sujirc4dab502013-03-06 17:02:34 +00003722 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3723 /* It is possible that bootcode is still loading at this point.
3724 * Get the nvram lock first before halting the cpu.
3725 */
3726 int lock_err = tg3_nvram_lock(tp);
3727 err = tg3_halt_cpu(tp, cpu_base);
3728 if (!lock_err)
3729 tg3_nvram_unlock(tp);
3730 if (err)
3731 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003732
Nithin Sujirc4dab502013-03-06 17:02:34 +00003733 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3734 write_op(tp, cpu_scratch_base + i, 0);
3735 tw32(cpu_base + CPU_STATE, 0xffffffff);
3736 tw32(cpu_base + CPU_MODE,
3737 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3738 } else {
3739 /* Subtract additional main header for fragmented firmware and
3740 * advance to the first fragment
3741 */
3742 total_len -= TG3_FW_HDR_LEN;
3743 fw_hdr++;
3744 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003745
Nithin Sujir31f11a92013-03-06 17:02:33 +00003746 do {
3747 u32 *fw_data = (u32 *)(fw_hdr + 1);
3748 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3749 write_op(tp, cpu_scratch_base +
3750 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3751 (i * sizeof(u32)),
3752 be32_to_cpu(fw_data[i]));
3753
3754 total_len -= be32_to_cpu(fw_hdr->len);
3755
3756 /* Advance to next fragment */
3757 fw_hdr = (struct tg3_firmware_hdr *)
3758 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3759 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003760
3761 err = 0;
3762
3763out:
3764 return err;
3765}
3766
3767/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003768static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3769{
3770 int i;
3771 const int iters = 5;
3772
3773 tw32(cpu_base + CPU_STATE, 0xffffffff);
3774 tw32_f(cpu_base + CPU_PC, pc);
3775
3776 for (i = 0; i < iters; i++) {
3777 if (tr32(cpu_base + CPU_PC) == pc)
3778 break;
3779 tw32(cpu_base + CPU_STATE, 0xffffffff);
3780 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3781 tw32_f(cpu_base + CPU_PC, pc);
3782 udelay(1000);
3783 }
3784
3785 return (i == iters) ? -EBUSY : 0;
3786}
3787
3788/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003789static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3790{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003791 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003792 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003793
Nithin Sujir77997ea2013-03-06 17:02:32 +00003794 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003795
3796 /* Firmware blob starts with version numbers, followed by
3797 start address and length. We are setting complete length.
3798 length = end_address_of_bss - start_address_of_text.
3799 Remainder is the blob to be loaded contiguously
3800 from start address. */
3801
Matt Carlson997b4f12011-08-31 11:44:53 +00003802 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3803 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003804 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003805 if (err)
3806 return err;
3807
3808 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3809 TX_CPU_SCRATCH_BASE, TX_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 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003815 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3816 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003817 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003818 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3819 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003820 tr32(RX_CPU_BASE + CPU_PC),
3821 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003822 return -ENODEV;
3823 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003824
3825 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003826
3827 return 0;
3828}
3829
Nithin Sujirc4dab502013-03-06 17:02:34 +00003830static int tg3_validate_rxcpu_state(struct tg3 *tp)
3831{
3832 const int iters = 1000;
3833 int i;
3834 u32 val;
3835
3836 /* Wait for boot code to complete initialization and enter service
3837 * loop. It is then safe to download service patches
3838 */
3839 for (i = 0; i < iters; i++) {
3840 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3841 break;
3842
3843 udelay(10);
3844 }
3845
3846 if (i == iters) {
3847 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3848 return -EBUSY;
3849 }
3850
3851 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3852 if (val & 0xff) {
3853 netdev_warn(tp->dev,
3854 "Other patches exist. Not downloading EEE patch\n");
3855 return -EEXIST;
3856 }
3857
3858 return 0;
3859}
3860
3861/* tp->lock is held. */
3862static void tg3_load_57766_firmware(struct tg3 *tp)
3863{
3864 struct tg3_firmware_hdr *fw_hdr;
3865
3866 if (!tg3_flag(tp, NO_NVRAM))
3867 return;
3868
3869 if (tg3_validate_rxcpu_state(tp))
3870 return;
3871
3872 if (!tp->fw)
3873 return;
3874
3875 /* This firmware blob has a different format than older firmware
3876 * releases as given below. The main difference is we have fragmented
3877 * data to be written to non-contiguous locations.
3878 *
3879 * In the beginning we have a firmware header identical to other
3880 * firmware which consists of version, base addr and length. The length
3881 * here is unused and set to 0xffffffff.
3882 *
3883 * This is followed by a series of firmware fragments which are
3884 * individually identical to previous firmware. i.e. they have the
3885 * firmware header and followed by data for that fragment. The version
3886 * field of the individual fragment header is unused.
3887 */
3888
3889 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3890 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3891 return;
3892
3893 if (tg3_rxcpu_pause(tp))
3894 return;
3895
3896 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3897 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3898
3899 tg3_rxcpu_resume(tp);
3900}
3901
Matt Carlson997b4f12011-08-31 11:44:53 +00003902/* tp->lock is held. */
3903static int tg3_load_tso_firmware(struct tg3 *tp)
3904{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003905 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003906 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003907 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003908
Matt Carlson1caf13e2013-03-06 17:02:29 +00003909 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003910 return 0;
3911
Nithin Sujir77997ea2013-03-06 17:02:32 +00003912 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003913
3914 /* Firmware blob starts with version numbers, followed by
3915 start address and length. We are setting complete length.
3916 length = end_address_of_bss - start_address_of_text.
3917 Remainder is the blob to be loaded contiguously
3918 from start address. */
3919
Matt Carlson997b4f12011-08-31 11:44:53 +00003920 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003921
Joe Perches41535772013-02-16 11:20:04 +00003922 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003923 cpu_base = RX_CPU_BASE;
3924 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3925 } else {
3926 cpu_base = TX_CPU_BASE;
3927 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3928 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3929 }
3930
3931 err = tg3_load_firmware_cpu(tp, cpu_base,
3932 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003933 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003934 if (err)
3935 return err;
3936
3937 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003938 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3939 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003940 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003941 netdev_err(tp->dev,
3942 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003943 __func__, tr32(cpu_base + CPU_PC),
3944 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003945 return -ENODEV;
3946 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003947
3948 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003949 return 0;
3950}
3951
Michael Chanf022ae62014-01-03 10:09:11 -08003952/* tp->lock is held. */
3953static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3954{
3955 u32 addr_high, addr_low;
3956
3957 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3958 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3959 (mac_addr[4] << 8) | mac_addr[5]);
3960
3961 if (index < 4) {
3962 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3963 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3964 } else {
3965 index -= 4;
3966 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3967 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3968 }
3969}
Matt Carlson997b4f12011-08-31 11:44:53 +00003970
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003971/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003972static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003973{
Michael Chanf022ae62014-01-03 10:09:11 -08003974 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003975 int i;
3976
Matt Carlson3f007892008-11-03 16:51:36 -08003977 for (i = 0; i < 4; i++) {
3978 if (i == 1 && skip_mac_1)
3979 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003980 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003981 }
3982
Joe Perches41535772013-02-16 11:20:04 +00003983 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3984 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003985 for (i = 4; i < 16; i++)
3986 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003987 }
3988
3989 addr_high = (tp->dev->dev_addr[0] +
3990 tp->dev->dev_addr[1] +
3991 tp->dev->dev_addr[2] +
3992 tp->dev->dev_addr[3] +
3993 tp->dev->dev_addr[4] +
3994 tp->dev->dev_addr[5]) &
3995 TX_BACKOFF_SEED_MASK;
3996 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3997}
3998
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003999static void tg3_enable_register_access(struct tg3 *tp)
4000{
4001 /*
4002 * Make sure register accesses (indirect or otherwise) will function
4003 * correctly.
4004 */
4005 pci_write_config_dword(tp->pdev,
4006 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4007}
4008
4009static int tg3_power_up(struct tg3 *tp)
4010{
Matt Carlsonbed98292011-07-13 09:27:29 +00004011 int err;
4012
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004013 tg3_enable_register_access(tp);
4014
Matt Carlsonbed98292011-07-13 09:27:29 +00004015 err = pci_set_power_state(tp->pdev, PCI_D0);
4016 if (!err) {
4017 /* Switch out of Vaux if it is a NIC */
4018 tg3_pwrsrc_switch_to_vmain(tp);
4019 } else {
4020 netdev_err(tp->dev, "Transition to D0 failed\n");
4021 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004022
Matt Carlsonbed98292011-07-13 09:27:29 +00004023 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004024}
4025
Joe Perches953c96e2013-04-09 10:18:14 +00004026static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004027
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004028static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
4030 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004031 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004033 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004034
4035 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004036 if (tg3_flag(tp, CLKREQ_BUG))
4037 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4038 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4041 tw32(TG3PCI_MISC_HOST_CTRL,
4042 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4043
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004044 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004045 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004046
Joe Perches63c3a662011-04-26 08:12:10 +00004047 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004048 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004049 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004050 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004051 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004052 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004053
Andrew Lunn7f854422016-01-06 20:11:18 +01004054 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004055
Matt Carlson80096062010-08-02 11:26:06 +00004056 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004057
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004058 tp->link_config.speed = phydev->speed;
4059 tp->link_config.duplex = phydev->duplex;
4060 tp->link_config.autoneg = phydev->autoneg;
4061 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004062
4063 advertising = ADVERTISED_TP |
4064 ADVERTISED_Pause |
4065 ADVERTISED_Autoneg |
4066 ADVERTISED_10baseT_Half;
4067
Joe Perches63c3a662011-04-26 08:12:10 +00004068 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4069 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004070 advertising |=
4071 ADVERTISED_100baseT_Half |
4072 ADVERTISED_100baseT_Full |
4073 ADVERTISED_10baseT_Full;
4074 else
4075 advertising |= ADVERTISED_10baseT_Full;
4076 }
4077
4078 phydev->advertising = advertising;
4079
4080 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004081
4082 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004083 if (phyid != PHY_ID_BCMAC131) {
4084 phyid &= PHY_BCM_OUI_MASK;
4085 if (phyid == PHY_BCM_OUI_1 ||
4086 phyid == PHY_BCM_OUI_2 ||
4087 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004088 do_low_power = true;
4089 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004090 }
Matt Carlsondd477002008-05-25 23:45:58 -07004091 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004092 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004093
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004094 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004095 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Matt Carlson2855b9f2012-02-13 15:20:14 +00004097 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004098 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
4100
Joe Perches41535772013-02-16 11:20:04 +00004101 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004102 u32 val;
4103
4104 val = tr32(GRC_VCPU_EXT_CTRL);
4105 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004106 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004107 int i;
4108 u32 val;
4109
4110 for (i = 0; i < 200; i++) {
4111 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4112 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4113 break;
4114 msleep(1);
4115 }
4116 }
Joe Perches63c3a662011-04-26 08:12:10 +00004117 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004118 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4119 WOL_DRV_STATE_SHUTDOWN |
4120 WOL_DRV_WOL |
4121 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004122
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004123 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 u32 mac_mode;
4125
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004126 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004127 if (do_low_power &&
4128 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4129 tg3_phy_auxctl_write(tp,
4130 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4131 MII_TG3_AUXCTL_PCTL_WOL_EN |
4132 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4133 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004134 udelay(40);
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004137 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004138 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004139 else if (tp->phy_flags &
4140 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4141 if (tp->link_config.active_speed == SPEED_1000)
4142 mac_mode = MAC_MODE_PORT_MODE_GMII;
4143 else
4144 mac_mode = MAC_MODE_PORT_MODE_MII;
4145 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004146 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004148 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004149 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004150 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004151 SPEED_100 : SPEED_10;
4152 if (tg3_5700_link_polarity(tp, speed))
4153 mac_mode |= MAC_MODE_LINK_POLARITY;
4154 else
4155 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 } else {
4158 mac_mode = MAC_MODE_PORT_MODE_TBI;
4159 }
4160
Joe Perches63c3a662011-04-26 08:12:10 +00004161 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 tw32(MAC_LED_CTRL, tp->led_ctrl);
4163
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004164 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004165 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4166 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004167 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Joe Perches63c3a662011-04-26 08:12:10 +00004169 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004170 mac_mode |= MAC_MODE_APE_TX_EN |
4171 MAC_MODE_APE_RX_EN |
4172 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 tw32_f(MAC_MODE, mac_mode);
4175 udelay(100);
4176
4177 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4178 udelay(10);
4179 }
4180
Joe Perches63c3a662011-04-26 08:12:10 +00004181 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004182 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4183 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 u32 base_val;
4185
4186 base_val = tp->pci_clock_ctrl;
4187 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4188 CLOCK_CTRL_TXCLK_DISABLE);
4189
Michael Chanb401e9e2005-12-19 16:27:04 -08004190 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4191 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004192 } else if (tg3_flag(tp, 5780_CLASS) ||
4193 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004194 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004195 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004196 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 u32 newbits1, newbits2;
4198
Joe Perches41535772013-02-16 11:20:04 +00004199 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4200 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4202 CLOCK_CTRL_TXCLK_DISABLE |
4203 CLOCK_CTRL_ALTCLK);
4204 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004205 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 newbits1 = CLOCK_CTRL_625_CORE;
4207 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4208 } else {
4209 newbits1 = CLOCK_CTRL_ALTCLK;
4210 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4211 }
4212
Michael Chanb401e9e2005-12-19 16:27:04 -08004213 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4214 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Michael Chanb401e9e2005-12-19 16:27:04 -08004216 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4217 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Joe Perches63c3a662011-04-26 08:12:10 +00004219 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 u32 newbits3;
4221
Joe Perches41535772013-02-16 11:20:04 +00004222 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4223 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4225 CLOCK_CTRL_TXCLK_DISABLE |
4226 CLOCK_CTRL_44MHZ_CORE);
4227 } else {
4228 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4229 }
4230
Michael Chanb401e9e2005-12-19 16:27:04 -08004231 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4232 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234 }
4235
Joe Perches63c3a662011-04-26 08:12:10 +00004236 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004237 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004238
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004239 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004242 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004243 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4244 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 u32 val = tr32(0x7d00);
4246
4247 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4248 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004249 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004250 int err;
4251
4252 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004254 if (!err)
4255 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 }
4258
Michael Chanbbadf502006-04-06 21:46:34 -07004259 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4260
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004261 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return 0;
4264}
4265
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004266static void tg3_power_down(struct tg3 *tp)
4267{
Joe Perches63c3a662011-04-26 08:12:10 +00004268 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004269 pci_set_power_state(tp->pdev, PCI_D3hot);
4270}
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4273{
4274 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4275 case MII_TG3_AUX_STAT_10HALF:
4276 *speed = SPEED_10;
4277 *duplex = DUPLEX_HALF;
4278 break;
4279
4280 case MII_TG3_AUX_STAT_10FULL:
4281 *speed = SPEED_10;
4282 *duplex = DUPLEX_FULL;
4283 break;
4284
4285 case MII_TG3_AUX_STAT_100HALF:
4286 *speed = SPEED_100;
4287 *duplex = DUPLEX_HALF;
4288 break;
4289
4290 case MII_TG3_AUX_STAT_100FULL:
4291 *speed = SPEED_100;
4292 *duplex = DUPLEX_FULL;
4293 break;
4294
4295 case MII_TG3_AUX_STAT_1000HALF:
4296 *speed = SPEED_1000;
4297 *duplex = DUPLEX_HALF;
4298 break;
4299
4300 case MII_TG3_AUX_STAT_1000FULL:
4301 *speed = SPEED_1000;
4302 *duplex = DUPLEX_FULL;
4303 break;
4304
4305 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004306 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004307 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4308 SPEED_10;
4309 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4310 DUPLEX_HALF;
4311 break;
4312 }
Matt Carlsone7405222012-02-13 15:20:16 +00004313 *speed = SPEED_UNKNOWN;
4314 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
Matt Carlson42b64a42011-05-19 12:12:49 +00004319static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Matt Carlson42b64a42011-05-19 12:12:49 +00004321 int err = 0;
4322 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Matt Carlson42b64a42011-05-19 12:12:49 +00004324 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004325 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004326 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Matt Carlson42b64a42011-05-19 12:12:49 +00004328 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4329 if (err)
4330 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
Matt Carlson4f272092011-12-14 11:09:57 +00004332 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4333 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004334
Joe Perches41535772013-02-16 11:20:04 +00004335 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4336 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004337 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004338
Matt Carlson4f272092011-12-14 11:09:57 +00004339 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4340 if (err)
4341 goto done;
4342 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004343
Matt Carlson42b64a42011-05-19 12:12:49 +00004344 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4345 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Matt Carlson42b64a42011-05-19 12:12:49 +00004347 tw32(TG3_CPMU_EEE_MODE,
4348 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004349
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004350 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004351 if (!err) {
4352 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004353
Matt Carlsona6b68da2010-12-06 08:28:52 +00004354 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004355 /* Advertise 100-BaseTX EEE ability */
4356 if (advertise & ADVERTISED_100baseT_Full)
4357 val |= MDIO_AN_EEE_ADV_100TX;
4358 /* Advertise 1000-BaseT EEE ability */
4359 if (advertise & ADVERTISED_1000baseT_Full)
4360 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004361
4362 if (!tp->eee.eee_enabled) {
4363 val = 0;
4364 tp->eee.advertised = 0;
4365 } else {
4366 tp->eee.advertised = advertise &
4367 (ADVERTISED_100baseT_Full |
4368 ADVERTISED_1000baseT_Full);
4369 }
4370
Matt Carlson42b64a42011-05-19 12:12:49 +00004371 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004372 if (err)
4373 val = 0;
4374
Joe Perches41535772013-02-16 11:20:04 +00004375 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004376 case ASIC_REV_5717:
4377 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004378 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004379 case ASIC_REV_5719:
4380 /* If we advertised any eee advertisements above... */
4381 if (val)
4382 val = MII_TG3_DSP_TAP26_ALNOKO |
4383 MII_TG3_DSP_TAP26_RMRXSTO |
4384 MII_TG3_DSP_TAP26_OPCSINPT;
4385 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4386 /* Fall through */
4387 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004388 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004389 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4390 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4391 MII_TG3_DSP_CH34TP2_HIBW01);
4392 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004393
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004394 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004395 if (!err)
4396 err = err2;
4397 }
4398
4399done:
4400 return err;
4401}
4402
4403static void tg3_phy_copper_begin(struct tg3 *tp)
4404{
Matt Carlsond13ba512012-02-22 12:35:19 +00004405 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4406 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4407 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004408
Nithin Sujir942d1af2013-04-09 08:48:07 +00004409 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4410 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004411 adv = ADVERTISED_10baseT_Half |
4412 ADVERTISED_10baseT_Full;
4413 if (tg3_flag(tp, WOL_SPEED_100MB))
4414 adv |= ADVERTISED_100baseT_Half |
4415 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004416 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4417 if (!(tp->phy_flags &
4418 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4419 adv |= ADVERTISED_1000baseT_Half;
4420 adv |= ADVERTISED_1000baseT_Full;
4421 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004422
Matt Carlsond13ba512012-02-22 12:35:19 +00004423 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004424 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004425 adv = tp->link_config.advertising;
4426 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4427 adv &= ~(ADVERTISED_1000baseT_Half |
4428 ADVERTISED_1000baseT_Full);
4429
4430 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004431 }
4432
Matt Carlsond13ba512012-02-22 12:35:19 +00004433 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004434
Nithin Sujir942d1af2013-04-09 08:48:07 +00004435 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4436 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4437 /* Normally during power down we want to autonegotiate
4438 * the lowest possible speed for WOL. However, to avoid
4439 * link flap, we leave it untouched.
4440 */
4441 return;
4442 }
4443
Matt Carlsond13ba512012-02-22 12:35:19 +00004444 tg3_writephy(tp, MII_BMCR,
4445 BMCR_ANENABLE | BMCR_ANRESTART);
4446 } else {
4447 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 u32 bmcr, orig_bmcr;
4449
4450 tp->link_config.active_speed = tp->link_config.speed;
4451 tp->link_config.active_duplex = tp->link_config.duplex;
4452
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004453 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4454 /* With autoneg disabled, 5715 only links up when the
4455 * advertisement register has the configured speed
4456 * enabled.
4457 */
4458 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4459 }
4460
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 bmcr = 0;
4462 switch (tp->link_config.speed) {
4463 default:
4464 case SPEED_10:
4465 break;
4466
4467 case SPEED_100:
4468 bmcr |= BMCR_SPEED100;
4469 break;
4470
4471 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004472 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 if (tp->link_config.duplex == DUPLEX_FULL)
4477 bmcr |= BMCR_FULLDPLX;
4478
4479 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4480 (bmcr != orig_bmcr)) {
4481 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4482 for (i = 0; i < 1500; i++) {
4483 u32 tmp;
4484
4485 udelay(10);
4486 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4487 tg3_readphy(tp, MII_BMSR, &tmp))
4488 continue;
4489 if (!(tmp & BMSR_LSTATUS)) {
4490 udelay(40);
4491 break;
4492 }
4493 }
4494 tg3_writephy(tp, MII_BMCR, bmcr);
4495 udelay(40);
4496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
4498}
4499
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004500static int tg3_phy_pull_config(struct tg3 *tp)
4501{
4502 int err;
4503 u32 val;
4504
4505 err = tg3_readphy(tp, MII_BMCR, &val);
4506 if (err)
4507 goto done;
4508
4509 if (!(val & BMCR_ANENABLE)) {
4510 tp->link_config.autoneg = AUTONEG_DISABLE;
4511 tp->link_config.advertising = 0;
4512 tg3_flag_clear(tp, PAUSE_AUTONEG);
4513
4514 err = -EIO;
4515
4516 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4517 case 0:
4518 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4519 goto done;
4520
4521 tp->link_config.speed = SPEED_10;
4522 break;
4523 case BMCR_SPEED100:
4524 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4525 goto done;
4526
4527 tp->link_config.speed = SPEED_100;
4528 break;
4529 case BMCR_SPEED1000:
4530 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4531 tp->link_config.speed = SPEED_1000;
4532 break;
4533 }
4534 /* Fall through */
4535 default:
4536 goto done;
4537 }
4538
4539 if (val & BMCR_FULLDPLX)
4540 tp->link_config.duplex = DUPLEX_FULL;
4541 else
4542 tp->link_config.duplex = DUPLEX_HALF;
4543
4544 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4545
4546 err = 0;
4547 goto done;
4548 }
4549
4550 tp->link_config.autoneg = AUTONEG_ENABLE;
4551 tp->link_config.advertising = ADVERTISED_Autoneg;
4552 tg3_flag_set(tp, PAUSE_AUTONEG);
4553
4554 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4555 u32 adv;
4556
4557 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4558 if (err)
4559 goto done;
4560
4561 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4562 tp->link_config.advertising |= adv | ADVERTISED_TP;
4563
4564 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4565 } else {
4566 tp->link_config.advertising |= ADVERTISED_FIBRE;
4567 }
4568
4569 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4570 u32 adv;
4571
4572 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4573 err = tg3_readphy(tp, MII_CTRL1000, &val);
4574 if (err)
4575 goto done;
4576
4577 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4578 } else {
4579 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4580 if (err)
4581 goto done;
4582
4583 adv = tg3_decode_flowctrl_1000X(val);
4584 tp->link_config.flowctrl = adv;
4585
4586 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4587 adv = mii_adv_to_ethtool_adv_x(val);
4588 }
4589
4590 tp->link_config.advertising |= adv;
4591 }
4592
4593done:
4594 return err;
4595}
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597static int tg3_init_5401phy_dsp(struct tg3 *tp)
4598{
4599 int err;
4600
4601 /* Turn off tap power management. */
4602 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004603 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004605 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4606 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4607 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4608 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4609 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 udelay(40);
4612
4613 return err;
4614}
4615
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004616static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4617{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004618 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004619
4620 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4621 return true;
4622
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004623 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004624
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004625 if (tp->eee.eee_enabled) {
4626 if (tp->eee.advertised != eee.advertised ||
4627 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4628 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4629 return false;
4630 } else {
4631 /* EEE is disabled but we're advertising */
4632 if (eee.advertised)
4633 return false;
4634 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004635
4636 return true;
4637}
4638
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004639static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004641 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004642
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004643 advertising = tp->link_config.advertising;
4644 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004646 advmsk = ADVERTISE_ALL;
4647 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004648 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004652 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4653 return false;
4654
4655 if ((*lcladv & advmsk) != tgtadv)
4656 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004657
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004658 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 u32 tg3_ctrl;
4660
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004661 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004662
Matt Carlson221c5632011-06-13 13:39:01 +00004663 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004664 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Matt Carlson3198e072012-02-13 15:20:10 +00004666 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004667 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4668 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004669 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4670 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4671 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4672 } else {
4673 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4674 }
4675
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004676 if (tg3_ctrl != tgtadv)
4677 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004679
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004680 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004681}
4682
Matt Carlson859edb22011-12-08 14:40:16 +00004683static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4684{
4685 u32 lpeth = 0;
4686
4687 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4688 u32 val;
4689
4690 if (tg3_readphy(tp, MII_STAT1000, &val))
4691 return false;
4692
4693 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4694 }
4695
4696 if (tg3_readphy(tp, MII_LPA, rmtadv))
4697 return false;
4698
4699 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4700 tp->link_config.rmt_adv = lpeth;
4701
4702 return true;
4703}
4704
Joe Perches953c96e2013-04-09 10:18:14 +00004705static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004706{
4707 if (curr_link_up != tp->link_up) {
4708 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004709 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004710 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004711 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004712 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4713 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4714 }
4715
4716 tg3_link_report(tp);
4717 return true;
4718 }
4719
4720 return false;
4721}
4722
Michael Chan3310e242013-04-09 08:48:05 +00004723static void tg3_clear_mac_status(struct tg3 *tp)
4724{
4725 tw32(MAC_EVENT, 0);
4726
4727 tw32_f(MAC_STATUS,
4728 MAC_STATUS_SYNC_CHANGED |
4729 MAC_STATUS_CFG_CHANGED |
4730 MAC_STATUS_MI_COMPLETION |
4731 MAC_STATUS_LNKSTATE_CHANGED);
4732 udelay(40);
4733}
4734
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004735static void tg3_setup_eee(struct tg3 *tp)
4736{
4737 u32 val;
4738
4739 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4740 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4741 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4742 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4743
4744 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4745
4746 tw32_f(TG3_CPMU_EEE_CTRL,
4747 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4748
4749 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4750 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4751 TG3_CPMU_EEEMD_LPI_IN_RX |
4752 TG3_CPMU_EEEMD_EEE_ENABLE;
4753
4754 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4755 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4756
4757 if (tg3_flag(tp, ENABLE_APE))
4758 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4759
4760 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4761
4762 tw32_f(TG3_CPMU_EEE_DBTMR1,
4763 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4764 (tp->eee.tx_lpi_timer & 0xffff));
4765
4766 tw32_f(TG3_CPMU_EEE_DBTMR2,
4767 TG3_CPMU_DBTMR2_APE_TX_2047US |
4768 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4769}
4770
Joe Perches953c96e2013-04-09 10:18:14 +00004771static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Joe Perches953c96e2013-04-09 10:18:14 +00004773 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004774 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004775 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 u16 current_speed;
4777 u8 current_duplex;
4778 int i, err;
4779
Michael Chan3310e242013-04-09 08:48:05 +00004780 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Matt Carlson8ef21422008-05-02 16:47:53 -07004782 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4783 tw32_f(MAC_MI_MODE,
4784 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4785 udelay(80);
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004788 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 /* Some third-party PHYs need to be reset on link going
4791 * down.
4792 */
Joe Perches41535772013-02-16 11:20:04 +00004793 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4794 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4795 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004796 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 tg3_readphy(tp, MII_BMSR, &bmsr);
4798 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4799 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004800 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 }
4802 if (force_reset)
4803 tg3_phy_reset(tp);
4804
Matt Carlson79eb6902010-02-17 15:17:03 +00004805 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 tg3_readphy(tp, MII_BMSR, &bmsr);
4807 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004808 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 bmsr = 0;
4810
4811 if (!(bmsr & BMSR_LSTATUS)) {
4812 err = tg3_init_5401phy_dsp(tp);
4813 if (err)
4814 return err;
4815
4816 tg3_readphy(tp, MII_BMSR, &bmsr);
4817 for (i = 0; i < 1000; i++) {
4818 udelay(10);
4819 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4820 (bmsr & BMSR_LSTATUS)) {
4821 udelay(40);
4822 break;
4823 }
4824 }
4825
Matt Carlson79eb6902010-02-17 15:17:03 +00004826 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4827 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 !(bmsr & BMSR_LSTATUS) &&
4829 tp->link_config.active_speed == SPEED_1000) {
4830 err = tg3_phy_reset(tp);
4831 if (!err)
4832 err = tg3_init_5401phy_dsp(tp);
4833 if (err)
4834 return err;
4835 }
4836 }
Joe Perches41535772013-02-16 11:20:04 +00004837 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4838 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 /* 5701 {A0,B0} CRC bug workaround */
4840 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004841 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4842 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4843 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
4845
4846 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004847 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4848 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004850 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004852 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4854
Joe Perches41535772013-02-16 11:20:04 +00004855 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4856 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4858 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4859 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4860 else
4861 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4862 }
4863
Joe Perches953c96e2013-04-09 10:18:14 +00004864 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004865 current_speed = SPEED_UNKNOWN;
4866 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004867 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004868 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004870 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004871 err = tg3_phy_auxctl_read(tp,
4872 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4873 &val);
4874 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004875 tg3_phy_auxctl_write(tp,
4876 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4877 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 goto relink;
4879 }
4880 }
4881
4882 bmsr = 0;
4883 for (i = 0; i < 100; i++) {
4884 tg3_readphy(tp, MII_BMSR, &bmsr);
4885 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4886 (bmsr & BMSR_LSTATUS))
4887 break;
4888 udelay(40);
4889 }
4890
4891 if (bmsr & BMSR_LSTATUS) {
4892 u32 aux_stat, bmcr;
4893
4894 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4895 for (i = 0; i < 2000; i++) {
4896 udelay(10);
4897 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4898 aux_stat)
4899 break;
4900 }
4901
4902 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4903 &current_speed,
4904 &current_duplex);
4905
4906 bmcr = 0;
4907 for (i = 0; i < 200; i++) {
4908 tg3_readphy(tp, MII_BMCR, &bmcr);
4909 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4910 continue;
4911 if (bmcr && bmcr != 0x7fff)
4912 break;
4913 udelay(10);
4914 }
4915
Matt Carlsonef167e22007-12-20 20:10:01 -08004916 lcl_adv = 0;
4917 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
Matt Carlsonef167e22007-12-20 20:10:01 -08004919 tp->link_config.active_speed = current_speed;
4920 tp->link_config.active_duplex = current_duplex;
4921
4922 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004923 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4924
Matt Carlsonef167e22007-12-20 20:10:01 -08004925 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004926 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004927 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004928 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004929 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004930
4931 /* EEE settings changes take effect only after a phy
4932 * reset. If we have skipped a reset due to Link Flap
4933 * Avoidance being enabled, do it now.
4934 */
4935 if (!eee_config_ok &&
4936 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004937 !force_reset) {
4938 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004939 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 } else {
4942 if (!(bmcr & BMCR_ANENABLE) &&
4943 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004944 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004945 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947 }
4948
Joe Perches953c96e2013-04-09 10:18:14 +00004949 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004950 tp->link_config.active_duplex == DUPLEX_FULL) {
4951 u32 reg, bit;
4952
4953 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4954 reg = MII_TG3_FET_GEN_STAT;
4955 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4956 } else {
4957 reg = MII_TG3_EXT_STAT;
4958 bit = MII_TG3_EXT_STAT_MDIX;
4959 }
4960
4961 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4962 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4963
Matt Carlsonef167e22007-12-20 20:10:01 -08004964 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004969 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 tg3_phy_copper_begin(tp);
4971
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004972 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004973 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004974 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4975 current_speed = SPEED_1000;
4976 current_duplex = DUPLEX_FULL;
4977 tp->link_config.active_speed = current_speed;
4978 tp->link_config.active_duplex = current_duplex;
4979 }
4980
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004981 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004982 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4983 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004984 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
4986
4987 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004988 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (tp->link_config.active_speed == SPEED_100 ||
4990 tp->link_config.active_speed == SPEED_10)
4991 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4992 else
4993 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004994 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004995 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4996 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4998
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004999 /* In order for the 5750 core in BCM4785 chip to work properly
5000 * in RGMII mode, the Led Control Register must be set up.
5001 */
5002 if (tg3_flag(tp, RGMII_MODE)) {
5003 u32 led_ctrl = tr32(MAC_LED_CTRL);
5004 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5005
5006 if (tp->link_config.active_speed == SPEED_10)
5007 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5008 else if (tp->link_config.active_speed == SPEED_100)
5009 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5010 LED_CTRL_100MBPS_ON);
5011 else if (tp->link_config.active_speed == SPEED_1000)
5012 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5013 LED_CTRL_1000MBPS_ON);
5014
5015 tw32(MAC_LED_CTRL, led_ctrl);
5016 udelay(40);
5017 }
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5020 if (tp->link_config.active_duplex == DUPLEX_HALF)
5021 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5022
Joe Perches41535772013-02-16 11:20:04 +00005023 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005024 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005025 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005027 else
5028 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 }
5030
5031 /* ??? Without this setting Netgear GA302T PHY does not
5032 * ??? send/receive packets...
5033 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005034 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005035 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5037 tw32_f(MAC_MI_MODE, tp->mi_mode);
5038 udelay(80);
5039 }
5040
5041 tw32_f(MAC_MODE, tp->mac_mode);
5042 udelay(40);
5043
Matt Carlson52b02d02010-10-14 10:37:41 +00005044 tg3_phy_eee_adjust(tp, current_link_up);
5045
Joe Perches63c3a662011-04-26 08:12:10 +00005046 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 /* Polled via timer. */
5048 tw32_f(MAC_EVENT, 0);
5049 } else {
5050 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5051 }
5052 udelay(40);
5053
Joe Perches41535772013-02-16 11:20:04 +00005054 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005055 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005057 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 udelay(120);
5059 tw32_f(MAC_STATUS,
5060 (MAC_STATUS_SYNC_CHANGED |
5061 MAC_STATUS_CFG_CHANGED));
5062 udelay(40);
5063 tg3_write_mem(tp,
5064 NIC_SRAM_FIRMWARE_MBOX,
5065 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5066 }
5067
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005068 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005069 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005070 if (tp->link_config.active_speed == SPEED_100 ||
5071 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005072 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5073 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005074 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005075 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5076 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005077 }
5078
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005079 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081 return 0;
5082}
5083
5084struct tg3_fiber_aneginfo {
5085 int state;
5086#define ANEG_STATE_UNKNOWN 0
5087#define ANEG_STATE_AN_ENABLE 1
5088#define ANEG_STATE_RESTART_INIT 2
5089#define ANEG_STATE_RESTART 3
5090#define ANEG_STATE_DISABLE_LINK_OK 4
5091#define ANEG_STATE_ABILITY_DETECT_INIT 5
5092#define ANEG_STATE_ABILITY_DETECT 6
5093#define ANEG_STATE_ACK_DETECT_INIT 7
5094#define ANEG_STATE_ACK_DETECT 8
5095#define ANEG_STATE_COMPLETE_ACK_INIT 9
5096#define ANEG_STATE_COMPLETE_ACK 10
5097#define ANEG_STATE_IDLE_DETECT_INIT 11
5098#define ANEG_STATE_IDLE_DETECT 12
5099#define ANEG_STATE_LINK_OK 13
5100#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5101#define ANEG_STATE_NEXT_PAGE_WAIT 15
5102
5103 u32 flags;
5104#define MR_AN_ENABLE 0x00000001
5105#define MR_RESTART_AN 0x00000002
5106#define MR_AN_COMPLETE 0x00000004
5107#define MR_PAGE_RX 0x00000008
5108#define MR_NP_LOADED 0x00000010
5109#define MR_TOGGLE_TX 0x00000020
5110#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5111#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5112#define MR_LP_ADV_SYM_PAUSE 0x00000100
5113#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5114#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5115#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5116#define MR_LP_ADV_NEXT_PAGE 0x00001000
5117#define MR_TOGGLE_RX 0x00002000
5118#define MR_NP_RX 0x00004000
5119
5120#define MR_LINK_OK 0x80000000
5121
5122 unsigned long link_time, cur_time;
5123
5124 u32 ability_match_cfg;
5125 int ability_match_count;
5126
5127 char ability_match, idle_match, ack_match;
5128
5129 u32 txconfig, rxconfig;
5130#define ANEG_CFG_NP 0x00000080
5131#define ANEG_CFG_ACK 0x00000040
5132#define ANEG_CFG_RF2 0x00000020
5133#define ANEG_CFG_RF1 0x00000010
5134#define ANEG_CFG_PS2 0x00000001
5135#define ANEG_CFG_PS1 0x00008000
5136#define ANEG_CFG_HD 0x00004000
5137#define ANEG_CFG_FD 0x00002000
5138#define ANEG_CFG_INVAL 0x00001f06
5139
5140};
5141#define ANEG_OK 0
5142#define ANEG_DONE 1
5143#define ANEG_TIMER_ENAB 2
5144#define ANEG_FAILED -1
5145
5146#define ANEG_STATE_SETTLE_TIME 10000
5147
5148static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5149 struct tg3_fiber_aneginfo *ap)
5150{
Matt Carlson5be73b42007-12-20 20:09:29 -08005151 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 unsigned long delta;
5153 u32 rx_cfg_reg;
5154 int ret;
5155
5156 if (ap->state == ANEG_STATE_UNKNOWN) {
5157 ap->rxconfig = 0;
5158 ap->link_time = 0;
5159 ap->cur_time = 0;
5160 ap->ability_match_cfg = 0;
5161 ap->ability_match_count = 0;
5162 ap->ability_match = 0;
5163 ap->idle_match = 0;
5164 ap->ack_match = 0;
5165 }
5166 ap->cur_time++;
5167
5168 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5169 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5170
5171 if (rx_cfg_reg != ap->ability_match_cfg) {
5172 ap->ability_match_cfg = rx_cfg_reg;
5173 ap->ability_match = 0;
5174 ap->ability_match_count = 0;
5175 } else {
5176 if (++ap->ability_match_count > 1) {
5177 ap->ability_match = 1;
5178 ap->ability_match_cfg = rx_cfg_reg;
5179 }
5180 }
5181 if (rx_cfg_reg & ANEG_CFG_ACK)
5182 ap->ack_match = 1;
5183 else
5184 ap->ack_match = 0;
5185
5186 ap->idle_match = 0;
5187 } else {
5188 ap->idle_match = 1;
5189 ap->ability_match_cfg = 0;
5190 ap->ability_match_count = 0;
5191 ap->ability_match = 0;
5192 ap->ack_match = 0;
5193
5194 rx_cfg_reg = 0;
5195 }
5196
5197 ap->rxconfig = rx_cfg_reg;
5198 ret = ANEG_OK;
5199
Matt Carlson33f401a2010-04-05 10:19:27 +00005200 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 case ANEG_STATE_UNKNOWN:
5202 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5203 ap->state = ANEG_STATE_AN_ENABLE;
5204
5205 /* fallthru */
5206 case ANEG_STATE_AN_ENABLE:
5207 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5208 if (ap->flags & MR_AN_ENABLE) {
5209 ap->link_time = 0;
5210 ap->cur_time = 0;
5211 ap->ability_match_cfg = 0;
5212 ap->ability_match_count = 0;
5213 ap->ability_match = 0;
5214 ap->idle_match = 0;
5215 ap->ack_match = 0;
5216
5217 ap->state = ANEG_STATE_RESTART_INIT;
5218 } else {
5219 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5220 }
5221 break;
5222
5223 case ANEG_STATE_RESTART_INIT:
5224 ap->link_time = ap->cur_time;
5225 ap->flags &= ~(MR_NP_LOADED);
5226 ap->txconfig = 0;
5227 tw32(MAC_TX_AUTO_NEG, 0);
5228 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5229 tw32_f(MAC_MODE, tp->mac_mode);
5230 udelay(40);
5231
5232 ret = ANEG_TIMER_ENAB;
5233 ap->state = ANEG_STATE_RESTART;
5234
5235 /* fallthru */
5236 case ANEG_STATE_RESTART:
5237 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005238 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005240 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 break;
5243
5244 case ANEG_STATE_DISABLE_LINK_OK:
5245 ret = ANEG_DONE;
5246 break;
5247
5248 case ANEG_STATE_ABILITY_DETECT_INIT:
5249 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005250 ap->txconfig = ANEG_CFG_FD;
5251 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5252 if (flowctrl & ADVERTISE_1000XPAUSE)
5253 ap->txconfig |= ANEG_CFG_PS1;
5254 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5255 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5257 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5258 tw32_f(MAC_MODE, tp->mac_mode);
5259 udelay(40);
5260
5261 ap->state = ANEG_STATE_ABILITY_DETECT;
5262 break;
5263
5264 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005265 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 break;
5268
5269 case ANEG_STATE_ACK_DETECT_INIT:
5270 ap->txconfig |= ANEG_CFG_ACK;
5271 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5272 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5273 tw32_f(MAC_MODE, tp->mac_mode);
5274 udelay(40);
5275
5276 ap->state = ANEG_STATE_ACK_DETECT;
5277
5278 /* fallthru */
5279 case ANEG_STATE_ACK_DETECT:
5280 if (ap->ack_match != 0) {
5281 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5282 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5283 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5284 } else {
5285 ap->state = ANEG_STATE_AN_ENABLE;
5286 }
5287 } else if (ap->ability_match != 0 &&
5288 ap->rxconfig == 0) {
5289 ap->state = ANEG_STATE_AN_ENABLE;
5290 }
5291 break;
5292
5293 case ANEG_STATE_COMPLETE_ACK_INIT:
5294 if (ap->rxconfig & ANEG_CFG_INVAL) {
5295 ret = ANEG_FAILED;
5296 break;
5297 }
5298 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5299 MR_LP_ADV_HALF_DUPLEX |
5300 MR_LP_ADV_SYM_PAUSE |
5301 MR_LP_ADV_ASYM_PAUSE |
5302 MR_LP_ADV_REMOTE_FAULT1 |
5303 MR_LP_ADV_REMOTE_FAULT2 |
5304 MR_LP_ADV_NEXT_PAGE |
5305 MR_TOGGLE_RX |
5306 MR_NP_RX);
5307 if (ap->rxconfig & ANEG_CFG_FD)
5308 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5309 if (ap->rxconfig & ANEG_CFG_HD)
5310 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5311 if (ap->rxconfig & ANEG_CFG_PS1)
5312 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5313 if (ap->rxconfig & ANEG_CFG_PS2)
5314 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5315 if (ap->rxconfig & ANEG_CFG_RF1)
5316 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5317 if (ap->rxconfig & ANEG_CFG_RF2)
5318 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5319 if (ap->rxconfig & ANEG_CFG_NP)
5320 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5321
5322 ap->link_time = ap->cur_time;
5323
5324 ap->flags ^= (MR_TOGGLE_TX);
5325 if (ap->rxconfig & 0x0008)
5326 ap->flags |= MR_TOGGLE_RX;
5327 if (ap->rxconfig & ANEG_CFG_NP)
5328 ap->flags |= MR_NP_RX;
5329 ap->flags |= MR_PAGE_RX;
5330
5331 ap->state = ANEG_STATE_COMPLETE_ACK;
5332 ret = ANEG_TIMER_ENAB;
5333 break;
5334
5335 case ANEG_STATE_COMPLETE_ACK:
5336 if (ap->ability_match != 0 &&
5337 ap->rxconfig == 0) {
5338 ap->state = ANEG_STATE_AN_ENABLE;
5339 break;
5340 }
5341 delta = ap->cur_time - ap->link_time;
5342 if (delta > ANEG_STATE_SETTLE_TIME) {
5343 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5344 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5345 } else {
5346 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5347 !(ap->flags & MR_NP_RX)) {
5348 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5349 } else {
5350 ret = ANEG_FAILED;
5351 }
5352 }
5353 }
5354 break;
5355
5356 case ANEG_STATE_IDLE_DETECT_INIT:
5357 ap->link_time = ap->cur_time;
5358 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5359 tw32_f(MAC_MODE, tp->mac_mode);
5360 udelay(40);
5361
5362 ap->state = ANEG_STATE_IDLE_DETECT;
5363 ret = ANEG_TIMER_ENAB;
5364 break;
5365
5366 case ANEG_STATE_IDLE_DETECT:
5367 if (ap->ability_match != 0 &&
5368 ap->rxconfig == 0) {
5369 ap->state = ANEG_STATE_AN_ENABLE;
5370 break;
5371 }
5372 delta = ap->cur_time - ap->link_time;
5373 if (delta > ANEG_STATE_SETTLE_TIME) {
5374 /* XXX another gem from the Broadcom driver :( */
5375 ap->state = ANEG_STATE_LINK_OK;
5376 }
5377 break;
5378
5379 case ANEG_STATE_LINK_OK:
5380 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5381 ret = ANEG_DONE;
5382 break;
5383
5384 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5385 /* ??? unimplemented */
5386 break;
5387
5388 case ANEG_STATE_NEXT_PAGE_WAIT:
5389 /* ??? unimplemented */
5390 break;
5391
5392 default:
5393 ret = ANEG_FAILED;
5394 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 return ret;
5398}
5399
Matt Carlson5be73b42007-12-20 20:09:29 -08005400static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
5402 int res = 0;
5403 struct tg3_fiber_aneginfo aninfo;
5404 int status = ANEG_FAILED;
5405 unsigned int tick;
5406 u32 tmp;
5407
5408 tw32_f(MAC_TX_AUTO_NEG, 0);
5409
5410 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5411 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5412 udelay(40);
5413
5414 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5415 udelay(40);
5416
5417 memset(&aninfo, 0, sizeof(aninfo));
5418 aninfo.flags |= MR_AN_ENABLE;
5419 aninfo.state = ANEG_STATE_UNKNOWN;
5420 aninfo.cur_time = 0;
5421 tick = 0;
5422 while (++tick < 195000) {
5423 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5424 if (status == ANEG_DONE || status == ANEG_FAILED)
5425 break;
5426
5427 udelay(1);
5428 }
5429
5430 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5431 tw32_f(MAC_MODE, tp->mac_mode);
5432 udelay(40);
5433
Matt Carlson5be73b42007-12-20 20:09:29 -08005434 *txflags = aninfo.txconfig;
5435 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
5437 if (status == ANEG_DONE &&
5438 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5439 MR_LP_ADV_FULL_DUPLEX)))
5440 res = 1;
5441
5442 return res;
5443}
5444
5445static void tg3_init_bcm8002(struct tg3 *tp)
5446{
5447 u32 mac_status = tr32(MAC_STATUS);
5448 int i;
5449
5450 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005451 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 !(mac_status & MAC_STATUS_PCS_SYNCED))
5453 return;
5454
5455 /* Set PLL lock range. */
5456 tg3_writephy(tp, 0x16, 0x8007);
5457
5458 /* SW reset */
5459 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5460
5461 /* Wait for reset to complete. */
5462 /* XXX schedule_timeout() ... */
5463 for (i = 0; i < 500; i++)
5464 udelay(10);
5465
5466 /* Config mode; select PMA/Ch 1 regs. */
5467 tg3_writephy(tp, 0x10, 0x8411);
5468
5469 /* Enable auto-lock and comdet, select txclk for tx. */
5470 tg3_writephy(tp, 0x11, 0x0a10);
5471
5472 tg3_writephy(tp, 0x18, 0x00a0);
5473 tg3_writephy(tp, 0x16, 0x41ff);
5474
5475 /* Assert and deassert POR. */
5476 tg3_writephy(tp, 0x13, 0x0400);
5477 udelay(40);
5478 tg3_writephy(tp, 0x13, 0x0000);
5479
5480 tg3_writephy(tp, 0x11, 0x0a50);
5481 udelay(40);
5482 tg3_writephy(tp, 0x11, 0x0a10);
5483
5484 /* Wait for signal to stabilize */
5485 /* XXX schedule_timeout() ... */
5486 for (i = 0; i < 15000; i++)
5487 udelay(10);
5488
5489 /* Deselect the channel register so we can read the PHYID
5490 * later.
5491 */
5492 tg3_writephy(tp, 0x10, 0x8011);
5493}
5494
Joe Perches953c96e2013-04-09 10:18:14 +00005495static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005497 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005498 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 u32 sg_dig_ctrl, sg_dig_status;
5500 u32 serdes_cfg, expected_sg_dig_ctrl;
5501 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
5503 serdes_cfg = 0;
5504 expected_sg_dig_ctrl = 0;
5505 workaround = 0;
5506 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005507 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Joe Perches41535772013-02-16 11:20:04 +00005509 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5510 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 workaround = 1;
5512 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5513 port_a = 0;
5514
5515 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5516 /* preserve bits 20-23 for voltage regulator */
5517 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5518 }
5519
5520 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5521
5522 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005523 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 if (workaround) {
5525 u32 val = serdes_cfg;
5526
5527 if (port_a)
5528 val |= 0xc010000;
5529 else
5530 val |= 0x4010000;
5531 tw32_f(MAC_SERDES_CFG, val);
5532 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005533
5534 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 }
5536 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5537 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005538 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 }
5540 goto out;
5541 }
5542
5543 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005544 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Matt Carlson82cd3d12007-12-20 20:09:00 -08005546 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5547 if (flowctrl & ADVERTISE_1000XPAUSE)
5548 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5549 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5550 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
5552 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005553 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005554 tp->serdes_counter &&
5555 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5556 MAC_STATUS_RCVD_CFG)) ==
5557 MAC_STATUS_PCS_SYNCED)) {
5558 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005559 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005560 goto out;
5561 }
5562restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 if (workaround)
5564 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005565 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 udelay(5);
5567 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5568
Michael Chan3d3ebe72006-09-27 15:59:15 -07005569 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005570 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5572 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005573 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 mac_status = tr32(MAC_STATUS);
5575
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005576 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005578 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Matt Carlson82cd3d12007-12-20 20:09:00 -08005580 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5581 local_adv |= ADVERTISE_1000XPAUSE;
5582 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5583 local_adv |= ADVERTISE_1000XPSE_ASYM;
5584
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005585 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005586 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005587 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005588 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Matt Carlson859edb22011-12-08 14:40:16 +00005590 tp->link_config.rmt_adv =
5591 mii_adv_to_ethtool_adv_x(remote_adv);
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005594 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005595 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005596 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005597 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005598 if (tp->serdes_counter)
5599 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 else {
5601 if (workaround) {
5602 u32 val = serdes_cfg;
5603
5604 if (port_a)
5605 val |= 0xc010000;
5606 else
5607 val |= 0x4010000;
5608
5609 tw32_f(MAC_SERDES_CFG, val);
5610 }
5611
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005612 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 udelay(40);
5614
5615 /* Link parallel detection - link is up */
5616 /* only if we have PCS_SYNC and not */
5617 /* receiving config code words */
5618 mac_status = tr32(MAC_STATUS);
5619 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5620 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5621 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005622 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005623 tp->phy_flags |=
5624 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005625 tp->serdes_counter =
5626 SERDES_PARALLEL_DET_TIMEOUT;
5627 } else
5628 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 }
5630 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005631 } else {
5632 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005633 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
5635
5636out:
5637 return current_link_up;
5638}
5639
Joe Perches953c96e2013-04-09 10:18:14 +00005640static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Joe Perches953c96e2013-04-09 10:18:14 +00005642 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005644 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
5647 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005648 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005650
Matt Carlson5be73b42007-12-20 20:09:29 -08005651 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5652 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Matt Carlson5be73b42007-12-20 20:09:29 -08005654 if (txflags & ANEG_CFG_PS1)
5655 local_adv |= ADVERTISE_1000XPAUSE;
5656 if (txflags & ANEG_CFG_PS2)
5657 local_adv |= ADVERTISE_1000XPSE_ASYM;
5658
5659 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5660 remote_adv |= LPA_1000XPAUSE;
5661 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5662 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
Matt Carlson859edb22011-12-08 14:40:16 +00005664 tp->link_config.rmt_adv =
5665 mii_adv_to_ethtool_adv_x(remote_adv);
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 tg3_setup_flow_control(tp, local_adv, remote_adv);
5668
Joe Perches953c96e2013-04-09 10:18:14 +00005669 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
5671 for (i = 0; i < 30; i++) {
5672 udelay(20);
5673 tw32_f(MAC_STATUS,
5674 (MAC_STATUS_SYNC_CHANGED |
5675 MAC_STATUS_CFG_CHANGED));
5676 udelay(40);
5677 if ((tr32(MAC_STATUS) &
5678 (MAC_STATUS_SYNC_CHANGED |
5679 MAC_STATUS_CFG_CHANGED)) == 0)
5680 break;
5681 }
5682
5683 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005684 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5686 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005687 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005689 tg3_setup_flow_control(tp, 0, 0);
5690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005692 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5695 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005696
5697 tw32_f(MAC_MODE, tp->mac_mode);
5698 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
5700
5701out:
5702 return current_link_up;
5703}
5704
Joe Perches953c96e2013-04-09 10:18:14 +00005705static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
5707 u32 orig_pause_cfg;
5708 u16 orig_active_speed;
5709 u8 orig_active_duplex;
5710 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005711 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 int i;
5713
Matt Carlson8d018622007-12-20 20:05:44 -08005714 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 orig_active_speed = tp->link_config.active_speed;
5716 orig_active_duplex = tp->link_config.active_duplex;
5717
Joe Perches63c3a662011-04-26 08:12:10 +00005718 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005719 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005720 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 mac_status = tr32(MAC_STATUS);
5722 mac_status &= (MAC_STATUS_PCS_SYNCED |
5723 MAC_STATUS_SIGNAL_DET |
5724 MAC_STATUS_CFG_CHANGED |
5725 MAC_STATUS_RCVD_CFG);
5726 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5727 MAC_STATUS_SIGNAL_DET)) {
5728 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5729 MAC_STATUS_CFG_CHANGED));
5730 return 0;
5731 }
5732 }
5733
5734 tw32_f(MAC_TX_AUTO_NEG, 0);
5735
5736 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5737 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5738 tw32_f(MAC_MODE, tp->mac_mode);
5739 udelay(40);
5740
Matt Carlson79eb6902010-02-17 15:17:03 +00005741 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 tg3_init_bcm8002(tp);
5743
5744 /* Enable link change event even when serdes polling. */
5745 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5746 udelay(40);
5747
Joe Perches953c96e2013-04-09 10:18:14 +00005748 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005749 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 mac_status = tr32(MAC_STATUS);
5751
Joe Perches63c3a662011-04-26 08:12:10 +00005752 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5754 else
5755 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5756
Matt Carlson898a56f2009-08-28 14:02:40 +00005757 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005759 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
5761 for (i = 0; i < 100; i++) {
5762 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5763 MAC_STATUS_CFG_CHANGED));
5764 udelay(5);
5765 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005766 MAC_STATUS_CFG_CHANGED |
5767 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 break;
5769 }
5770
5771 mac_status = tr32(MAC_STATUS);
5772 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005773 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005774 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5775 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 tw32_f(MAC_MODE, (tp->mac_mode |
5777 MAC_MODE_SEND_CONFIGS));
5778 udelay(1);
5779 tw32_f(MAC_MODE, tp->mac_mode);
5780 }
5781 }
5782
Joe Perches953c96e2013-04-09 10:18:14 +00005783 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 tp->link_config.active_speed = SPEED_1000;
5785 tp->link_config.active_duplex = DUPLEX_FULL;
5786 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5787 LED_CTRL_LNKLED_OVERRIDE |
5788 LED_CTRL_1000MBPS_ON));
5789 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005790 tp->link_config.active_speed = SPEED_UNKNOWN;
5791 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5793 LED_CTRL_LNKLED_OVERRIDE |
5794 LED_CTRL_TRAFFIC_OVERRIDE));
5795 }
5796
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005797 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005798 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 if (orig_pause_cfg != now_pause_cfg ||
5800 orig_active_speed != tp->link_config.active_speed ||
5801 orig_active_duplex != tp->link_config.active_duplex)
5802 tg3_link_report(tp);
5803 }
5804
5805 return 0;
5806}
5807
Joe Perches953c96e2013-04-09 10:18:14 +00005808static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005809{
Joe Perches953c96e2013-04-09 10:18:14 +00005810 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005811 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005812 u16 current_speed = SPEED_UNKNOWN;
5813 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005814 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005815 u32 local_adv, remote_adv, sgsr;
5816
5817 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5818 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5819 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5820 (sgsr & SERDES_TG3_SGMII_MODE)) {
5821
5822 if (force_reset)
5823 tg3_phy_reset(tp);
5824
5825 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5826
5827 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5828 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5829 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005830 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005831 if (sgsr & SERDES_TG3_SPEED_1000) {
5832 current_speed = SPEED_1000;
5833 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5834 } else if (sgsr & SERDES_TG3_SPEED_100) {
5835 current_speed = SPEED_100;
5836 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5837 } else {
5838 current_speed = SPEED_10;
5839 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5840 }
5841
5842 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5843 current_duplex = DUPLEX_FULL;
5844 else
5845 current_duplex = DUPLEX_HALF;
5846 }
5847
5848 tw32_f(MAC_MODE, tp->mac_mode);
5849 udelay(40);
5850
5851 tg3_clear_mac_status(tp);
5852
5853 goto fiber_setup_done;
5854 }
Michael Chan747e8f82005-07-25 12:33:22 -07005855
5856 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5857 tw32_f(MAC_MODE, tp->mac_mode);
5858 udelay(40);
5859
Michael Chan3310e242013-04-09 08:48:05 +00005860 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005861
5862 if (force_reset)
5863 tg3_phy_reset(tp);
5864
Matt Carlson859edb22011-12-08 14:40:16 +00005865 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005866
5867 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5868 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005869 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005870 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5871 bmsr |= BMSR_LSTATUS;
5872 else
5873 bmsr &= ~BMSR_LSTATUS;
5874 }
Michael Chan747e8f82005-07-25 12:33:22 -07005875
5876 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5877
5878 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005879 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005880 /* do nothing, just check for link up at the end */
5881 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005882 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005883
5884 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005885 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5886 ADVERTISE_1000XPAUSE |
5887 ADVERTISE_1000XPSE_ASYM |
5888 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005889
Matt Carlson28011cf2011-11-16 18:36:59 -05005890 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005891 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005892
Matt Carlson28011cf2011-11-16 18:36:59 -05005893 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5894 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005895 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5896 tg3_writephy(tp, MII_BMCR, bmcr);
5897
5898 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005899 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005900 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005901
5902 return err;
5903 }
5904 } else {
5905 u32 new_bmcr;
5906
5907 bmcr &= ~BMCR_SPEED1000;
5908 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5909
5910 if (tp->link_config.duplex == DUPLEX_FULL)
5911 new_bmcr |= BMCR_FULLDPLX;
5912
5913 if (new_bmcr != bmcr) {
5914 /* BMCR_SPEED1000 is a reserved bit that needs
5915 * to be set on write.
5916 */
5917 new_bmcr |= BMCR_SPEED1000;
5918
5919 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005920 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005921 u32 adv;
5922
5923 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5924 adv &= ~(ADVERTISE_1000XFULL |
5925 ADVERTISE_1000XHALF |
5926 ADVERTISE_SLCT);
5927 tg3_writephy(tp, MII_ADVERTISE, adv);
5928 tg3_writephy(tp, MII_BMCR, bmcr |
5929 BMCR_ANRESTART |
5930 BMCR_ANENABLE);
5931 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005932 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005933 }
5934 tg3_writephy(tp, MII_BMCR, new_bmcr);
5935 bmcr = new_bmcr;
5936 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5937 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005938 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005939 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5940 bmsr |= BMSR_LSTATUS;
5941 else
5942 bmsr &= ~BMSR_LSTATUS;
5943 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005944 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005945 }
5946 }
5947
5948 if (bmsr & BMSR_LSTATUS) {
5949 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005950 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005951 if (bmcr & BMCR_FULLDPLX)
5952 current_duplex = DUPLEX_FULL;
5953 else
5954 current_duplex = DUPLEX_HALF;
5955
Matt Carlsonef167e22007-12-20 20:10:01 -08005956 local_adv = 0;
5957 remote_adv = 0;
5958
Michael Chan747e8f82005-07-25 12:33:22 -07005959 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005960 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005961
5962 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5963 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5964 common = local_adv & remote_adv;
5965 if (common & (ADVERTISE_1000XHALF |
5966 ADVERTISE_1000XFULL)) {
5967 if (common & ADVERTISE_1000XFULL)
5968 current_duplex = DUPLEX_FULL;
5969 else
5970 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005971
5972 tp->link_config.rmt_adv =
5973 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005974 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005975 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005976 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005977 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005978 }
Michael Chan747e8f82005-07-25 12:33:22 -07005979 }
5980 }
5981
Michael Chan85730a62013-04-09 08:48:06 +00005982fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005983 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005984 tg3_setup_flow_control(tp, local_adv, remote_adv);
5985
Michael Chan747e8f82005-07-25 12:33:22 -07005986 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5987 if (tp->link_config.active_duplex == DUPLEX_HALF)
5988 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5989
5990 tw32_f(MAC_MODE, tp->mac_mode);
5991 udelay(40);
5992
5993 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5994
5995 tp->link_config.active_speed = current_speed;
5996 tp->link_config.active_duplex = current_duplex;
5997
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005998 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005999 return err;
6000}
6001
6002static void tg3_serdes_parallel_detect(struct tg3 *tp)
6003{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006004 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006005 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006006 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006007 return;
6008 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006009
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006010 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006011 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6012 u32 bmcr;
6013
6014 tg3_readphy(tp, MII_BMCR, &bmcr);
6015 if (bmcr & BMCR_ANENABLE) {
6016 u32 phy1, phy2;
6017
6018 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006019 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6020 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006021
6022 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006023 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6024 MII_TG3_DSP_EXP1_INT_STAT);
6025 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6026 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006027
6028 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6029 /* We have signal detect and not receiving
6030 * config code words, link is up by parallel
6031 * detection.
6032 */
6033
6034 bmcr &= ~BMCR_ANENABLE;
6035 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6036 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006037 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006038 }
6039 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006040 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006041 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006042 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006043 u32 phy2;
6044
6045 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006046 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6047 MII_TG3_DSP_EXP1_INT_STAT);
6048 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006049 if (phy2 & 0x20) {
6050 u32 bmcr;
6051
6052 /* Config code words received, turn on autoneg. */
6053 tg3_readphy(tp, MII_BMCR, &bmcr);
6054 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6055
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006056 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006057
6058 }
6059 }
6060}
6061
Joe Perches953c96e2013-04-09 10:18:14 +00006062static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006064 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 int err;
6066
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006067 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006069 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006070 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006071 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Joe Perches41535772013-02-16 11:20:04 +00006074 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006075 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006076
6077 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6078 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6079 scale = 65;
6080 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6081 scale = 6;
6082 else
6083 scale = 12;
6084
6085 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6086 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6087 tw32(GRC_MISC_CFG, val);
6088 }
6089
Matt Carlsonf2096f92011-04-05 14:22:48 +00006090 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6091 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006092 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6093 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006094 val |= tr32(MAC_TX_LENGTHS) &
6095 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6096 TX_LENGTHS_CNT_DWN_VAL_MSK);
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 if (tp->link_config.active_speed == SPEED_1000 &&
6099 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006100 tw32(MAC_TX_LENGTHS, val |
6101 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006103 tw32(MAC_TX_LENGTHS, val |
6104 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Joe Perches63c3a662011-04-26 08:12:10 +00006106 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006107 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006109 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 } else {
6111 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6112 }
6113 }
6114
Joe Perches63c3a662011-04-26 08:12:10 +00006115 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006116 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006117 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006118 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6119 tp->pwrmgmt_thresh;
6120 else
6121 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6122 tw32(PCIE_PWR_MGMT_THRESH, val);
6123 }
6124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return err;
6126}
6127
Matt Carlsonbe947302012-12-03 19:36:57 +00006128/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006129static u64 tg3_refclk_read(struct tg3 *tp)
6130{
6131 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6132 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6133}
6134
6135/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006136static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6137{
Nithin Sujir92e64572013-07-29 13:58:38 -07006138 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6139
6140 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006141 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6142 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006143 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006144}
6145
Matt Carlson7d41e492012-12-03 19:36:58 +00006146static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6147static inline void tg3_full_unlock(struct tg3 *tp);
6148static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6149{
6150 struct tg3 *tp = netdev_priv(dev);
6151
6152 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6153 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006154 SOF_TIMESTAMPING_SOFTWARE;
6155
6156 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006157 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006158 SOF_TIMESTAMPING_RX_HARDWARE |
6159 SOF_TIMESTAMPING_RAW_HARDWARE;
6160 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006161
6162 if (tp->ptp_clock)
6163 info->phc_index = ptp_clock_index(tp->ptp_clock);
6164 else
6165 info->phc_index = -1;
6166
6167 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6168
6169 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6170 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6171 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6172 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6173 return 0;
6174}
6175
6176static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6177{
6178 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6179 bool neg_adj = false;
6180 u32 correction = 0;
6181
6182 if (ppb < 0) {
6183 neg_adj = true;
6184 ppb = -ppb;
6185 }
6186
6187 /* Frequency adjustment is performed using hardware with a 24 bit
6188 * accumulator and a programmable correction value. On each clk, the
6189 * correction value gets added to the accumulator and when it
6190 * overflows, the time counter is incremented/decremented.
6191 *
6192 * So conversion from ppb to correction value is
6193 * ppb * (1 << 24) / 1000000000
6194 */
6195 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6196 TG3_EAV_REF_CLK_CORRECT_MASK;
6197
6198 tg3_full_lock(tp, 0);
6199
6200 if (correction)
6201 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6202 TG3_EAV_REF_CLK_CORRECT_EN |
6203 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6204 else
6205 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6206
6207 tg3_full_unlock(tp);
6208
6209 return 0;
6210}
6211
6212static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6213{
6214 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6215
6216 tg3_full_lock(tp, 0);
6217 tp->ptp_adjust += delta;
6218 tg3_full_unlock(tp);
6219
6220 return 0;
6221}
6222
Richard Cochranf578b412015-03-29 23:11:57 +02006223static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006224{
6225 u64 ns;
Matt Carlson7d41e492012-12-03 19:36:58 +00006226 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6227
6228 tg3_full_lock(tp, 0);
6229 ns = tg3_refclk_read(tp);
6230 ns += tp->ptp_adjust;
6231 tg3_full_unlock(tp);
6232
Richard Cochran7a20efb2015-03-31 23:08:08 +02006233 *ts = ns_to_timespec64(ns);
Matt Carlson7d41e492012-12-03 19:36:58 +00006234
6235 return 0;
6236}
6237
6238static int tg3_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochranf578b412015-03-29 23:11:57 +02006239 const struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006240{
6241 u64 ns;
6242 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6243
Richard Cochranf578b412015-03-29 23:11:57 +02006244 ns = timespec64_to_ns(ts);
Matt Carlson7d41e492012-12-03 19:36:58 +00006245
6246 tg3_full_lock(tp, 0);
6247 tg3_refclk_write(tp, ns);
6248 tp->ptp_adjust = 0;
6249 tg3_full_unlock(tp);
6250
6251 return 0;
6252}
6253
6254static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6255 struct ptp_clock_request *rq, int on)
6256{
Nithin Sujir92e64572013-07-29 13:58:38 -07006257 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6258 u32 clock_ctl;
6259 int rval = 0;
6260
6261 switch (rq->type) {
6262 case PTP_CLK_REQ_PEROUT:
6263 if (rq->perout.index != 0)
6264 return -EINVAL;
6265
6266 tg3_full_lock(tp, 0);
6267 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6268 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6269
6270 if (on) {
6271 u64 nsec;
6272
6273 nsec = rq->perout.start.sec * 1000000000ULL +
6274 rq->perout.start.nsec;
6275
6276 if (rq->perout.period.sec || rq->perout.period.nsec) {
6277 netdev_warn(tp->dev,
6278 "Device supports only a one-shot timesync output, period must be 0\n");
6279 rval = -EINVAL;
6280 goto err_out;
6281 }
6282
6283 if (nsec & (1ULL << 63)) {
6284 netdev_warn(tp->dev,
6285 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6286 rval = -EINVAL;
6287 goto err_out;
6288 }
6289
6290 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6291 tw32(TG3_EAV_WATCHDOG0_MSB,
6292 TG3_EAV_WATCHDOG0_EN |
6293 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6294
6295 tw32(TG3_EAV_REF_CLCK_CTL,
6296 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6297 } else {
6298 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6299 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6300 }
6301
6302err_out:
6303 tg3_full_unlock(tp);
6304 return rval;
6305
6306 default:
6307 break;
6308 }
6309
Matt Carlson7d41e492012-12-03 19:36:58 +00006310 return -EOPNOTSUPP;
6311}
6312
6313static const struct ptp_clock_info tg3_ptp_caps = {
6314 .owner = THIS_MODULE,
6315 .name = "tg3 clock",
6316 .max_adj = 250000000,
6317 .n_alarm = 0,
6318 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006319 .n_per_out = 1,
Richard Cochran4986b4f02014-03-20 22:21:55 +01006320 .n_pins = 0,
Matt Carlson7d41e492012-12-03 19:36:58 +00006321 .pps = 0,
6322 .adjfreq = tg3_ptp_adjfreq,
6323 .adjtime = tg3_ptp_adjtime,
Richard Cochranf578b412015-03-29 23:11:57 +02006324 .gettime64 = tg3_ptp_gettime,
6325 .settime64 = tg3_ptp_settime,
Matt Carlson7d41e492012-12-03 19:36:58 +00006326 .enable = tg3_ptp_enable,
6327};
6328
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006329static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6330 struct skb_shared_hwtstamps *timestamp)
6331{
6332 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6333 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6334 tp->ptp_adjust);
6335}
6336
Matt Carlsonbe947302012-12-03 19:36:57 +00006337/* tp->lock must be held */
6338static void tg3_ptp_init(struct tg3 *tp)
6339{
6340 if (!tg3_flag(tp, PTP_CAPABLE))
6341 return;
6342
6343 /* Initialize the hardware clock to the system time. */
6344 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6345 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006346 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006347}
6348
6349/* tp->lock must be held */
6350static void tg3_ptp_resume(struct tg3 *tp)
6351{
6352 if (!tg3_flag(tp, PTP_CAPABLE))
6353 return;
6354
6355 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6356 tp->ptp_adjust = 0;
6357}
6358
6359static void tg3_ptp_fini(struct tg3 *tp)
6360{
6361 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6362 return;
6363
Matt Carlson7d41e492012-12-03 19:36:58 +00006364 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006365 tp->ptp_clock = NULL;
6366 tp->ptp_adjust = 0;
6367}
6368
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006369static inline int tg3_irq_sync(struct tg3 *tp)
6370{
6371 return tp->irq_sync;
6372}
6373
Matt Carlson97bd8e42011-04-13 11:05:04 +00006374static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6375{
6376 int i;
6377
6378 dst = (u32 *)((u8 *)dst + off);
6379 for (i = 0; i < len; i += sizeof(u32))
6380 *dst++ = tr32(off + i);
6381}
6382
6383static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6384{
6385 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6386 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6387 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6388 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6389 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6390 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6391 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6392 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6393 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6394 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6395 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6396 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6397 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6398 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6399 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6400 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6401 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6402 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6403 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6404
Joe Perches63c3a662011-04-26 08:12:10 +00006405 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006406 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6407
6408 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6409 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6410 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6411 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6412 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6413 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6414 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6415 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6416
Joe Perches63c3a662011-04-26 08:12:10 +00006417 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006418 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6419 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6420 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6421 }
6422
6423 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6424 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6425 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6426 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6427 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6428
Joe Perches63c3a662011-04-26 08:12:10 +00006429 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006430 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6431}
6432
6433static void tg3_dump_state(struct tg3 *tp)
6434{
6435 int i;
6436 u32 *regs;
6437
6438 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006439 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006440 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006441
Joe Perches63c3a662011-04-26 08:12:10 +00006442 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006443 /* Read up to but not including private PCI registers */
6444 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6445 regs[i / sizeof(u32)] = tr32(i);
6446 } else
6447 tg3_dump_legacy_regs(tp, regs);
6448
6449 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6450 if (!regs[i + 0] && !regs[i + 1] &&
6451 !regs[i + 2] && !regs[i + 3])
6452 continue;
6453
6454 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6455 i * 4,
6456 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6457 }
6458
6459 kfree(regs);
6460
6461 for (i = 0; i < tp->irq_cnt; i++) {
6462 struct tg3_napi *tnapi = &tp->napi[i];
6463
6464 /* SW status block */
6465 netdev_err(tp->dev,
6466 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6467 i,
6468 tnapi->hw_status->status,
6469 tnapi->hw_status->status_tag,
6470 tnapi->hw_status->rx_jumbo_consumer,
6471 tnapi->hw_status->rx_consumer,
6472 tnapi->hw_status->rx_mini_consumer,
6473 tnapi->hw_status->idx[0].rx_producer,
6474 tnapi->hw_status->idx[0].tx_consumer);
6475
6476 netdev_err(tp->dev,
6477 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6478 i,
6479 tnapi->last_tag, tnapi->last_irq_tag,
6480 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6481 tnapi->rx_rcb_ptr,
6482 tnapi->prodring.rx_std_prod_idx,
6483 tnapi->prodring.rx_std_cons_idx,
6484 tnapi->prodring.rx_jmb_prod_idx,
6485 tnapi->prodring.rx_jmb_cons_idx);
6486 }
6487}
6488
Michael Chandf3e6542006-05-26 17:48:07 -07006489/* This is called whenever we suspect that the system chipset is re-
6490 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6491 * is bogus tx completions. We try to recover by setting the
6492 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6493 * in the workqueue.
6494 */
6495static void tg3_tx_recover(struct tg3 *tp)
6496{
Joe Perches63c3a662011-04-26 08:12:10 +00006497 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006498 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6499
Matt Carlson5129c3a2010-04-05 10:19:23 +00006500 netdev_warn(tp->dev,
6501 "The system may be re-ordering memory-mapped I/O "
6502 "cycles to the network device, attempting to recover. "
6503 "Please report the problem to the driver maintainer "
6504 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006505
Joe Perches63c3a662011-04-26 08:12:10 +00006506 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006507}
6508
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006509static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006510{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006511 /* Tell compiler to fetch tx indices from memory. */
6512 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006513 return tnapi->tx_pending -
6514 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006515}
6516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517/* Tigon3 never reports partial packet sends. So we do not
6518 * need special logic to handle SKBs that have not had all
6519 * of their frags sent yet, like SunGEM does.
6520 */
Matt Carlson17375d22009-08-28 14:02:18 +00006521static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522{
Matt Carlson17375d22009-08-28 14:02:18 +00006523 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006524 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006525 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006526 struct netdev_queue *txq;
6527 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006528 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006529
Joe Perches63c3a662011-04-26 08:12:10 +00006530 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006531 index--;
6532
6533 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
6535 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006536 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006538 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
Michael Chandf3e6542006-05-26 17:48:07 -07006540 if (unlikely(skb == NULL)) {
6541 tg3_tx_recover(tp);
6542 return;
6543 }
6544
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006545 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6546 struct skb_shared_hwtstamps timestamp;
6547 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6548 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6549
6550 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6551
6552 skb_tstamp_tx(skb, &timestamp);
6553 }
6554
Alexander Duyckf4188d82009-12-02 16:48:38 +00006555 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006556 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006557 skb_headlen(skb),
6558 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
6560 ri->skb = NULL;
6561
Matt Carlsone01ee142011-07-27 14:20:50 +00006562 while (ri->fragmented) {
6563 ri->fragmented = false;
6564 sw_idx = NEXT_TX(sw_idx);
6565 ri = &tnapi->tx_buffers[sw_idx];
6566 }
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 sw_idx = NEXT_TX(sw_idx);
6569
6570 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006571 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006572 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6573 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006574
6575 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006576 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006577 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006578 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006579
6580 while (ri->fragmented) {
6581 ri->fragmented = false;
6582 sw_idx = NEXT_TX(sw_idx);
6583 ri = &tnapi->tx_buffers[sw_idx];
6584 }
6585
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 sw_idx = NEXT_TX(sw_idx);
6587 }
6588
Tom Herbert298376d2011-11-28 16:33:30 +00006589 pkts_compl++;
6590 bytes_compl += skb->len;
6591
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07006592 dev_consume_skb_any(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006593
6594 if (unlikely(tx_bug)) {
6595 tg3_tx_recover(tp);
6596 return;
6597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 }
6599
Tom Herbert5cb917b2012-03-05 19:53:50 +00006600 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006601
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006602 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
Michael Chan1b2a7202006-08-07 21:46:02 -07006604 /* Need to make the tx_cons update visible to tg3_start_xmit()
6605 * before checking for netif_queue_stopped(). Without the
6606 * memory barrier, there is a small possibility that tg3_start_xmit()
6607 * will miss it and cause the queue to be stopped forever.
6608 */
6609 smp_mb();
6610
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006611 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006612 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006613 __netif_tx_lock(txq, smp_processor_id());
6614 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006615 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006616 netif_tx_wake_queue(txq);
6617 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619}
6620
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006621static void tg3_frag_free(bool is_frag, void *data)
6622{
6623 if (is_frag)
Alexander Duycke51423d2015-05-06 21:12:31 -07006624 skb_free_frag(data);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006625 else
6626 kfree(data);
6627}
6628
Eric Dumazet9205fd92011-11-18 06:47:01 +00006629static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006630{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006631 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6632 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6633
Eric Dumazet9205fd92011-11-18 06:47:01 +00006634 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006635 return;
6636
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006637 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006638 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006639 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006640 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006641}
6642
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006643
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644/* Returns size of skb allocated or < 0 on error.
6645 *
6646 * We only need to fill in the address because the other members
6647 * of the RX descriptor are invariant, see tg3_init_rings.
6648 *
6649 * Note the purposeful assymetry of cpu vs. chip accesses. For
6650 * posting buffers we only dirty the first cache line of the RX
6651 * descriptor (containing the address). Whereas for the RX status
6652 * buffers the cpu only reads the last cacheline of the RX descriptor
6653 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6654 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006655static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006656 u32 opaque_key, u32 dest_idx_unmasked,
6657 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658{
6659 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006660 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006661 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006663 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 switch (opaque_key) {
6666 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006667 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006668 desc = &tpr->rx_std[dest_idx];
6669 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006670 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 break;
6672
6673 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006674 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006675 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006676 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006677 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 break;
6679
6680 default:
6681 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
6684 /* Do not overwrite any of the map or rp information
6685 * until we are sure we can commit to a new buffer.
6686 *
6687 * Callers depend upon this behavior and assume that
6688 * we leave everything unchanged if we fail.
6689 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006690 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6691 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006692 if (skb_size <= PAGE_SIZE) {
6693 data = netdev_alloc_frag(skb_size);
6694 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006695 } else {
6696 data = kmalloc(skb_size, GFP_ATOMIC);
6697 *frag_size = 0;
6698 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006699 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 return -ENOMEM;
6701
Eric Dumazet9205fd92011-11-18 06:47:01 +00006702 mapping = pci_map_single(tp->pdev,
6703 data + TG3_RX_OFFSET(tp),
6704 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006706 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006707 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006708 return -EIO;
6709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
Eric Dumazet9205fd92011-11-18 06:47:01 +00006711 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006712 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 desc->addr_hi = ((u64)mapping >> 32);
6715 desc->addr_lo = ((u64)mapping & 0xffffffff);
6716
Eric Dumazet9205fd92011-11-18 06:47:01 +00006717 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718}
6719
6720/* We only need to move over in the address because the other
6721 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006722 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 */
Matt Carlsona3896162009-11-13 13:03:44 +00006724static void tg3_recycle_rx(struct tg3_napi *tnapi,
6725 struct tg3_rx_prodring_set *dpr,
6726 u32 opaque_key, int src_idx,
6727 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728{
Matt Carlson17375d22009-08-28 14:02:18 +00006729 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6731 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006732 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006733 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 switch (opaque_key) {
6736 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006737 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006738 dest_desc = &dpr->rx_std[dest_idx];
6739 dest_map = &dpr->rx_std_buffers[dest_idx];
6740 src_desc = &spr->rx_std[src_idx];
6741 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 break;
6743
6744 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006745 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006746 dest_desc = &dpr->rx_jmb[dest_idx].std;
6747 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6748 src_desc = &spr->rx_jmb[src_idx].std;
6749 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 break;
6751
6752 default:
6753 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755
Eric Dumazet9205fd92011-11-18 06:47:01 +00006756 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006757 dma_unmap_addr_set(dest_map, mapping,
6758 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 dest_desc->addr_hi = src_desc->addr_hi;
6760 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006761
6762 /* Ensure that the update to the skb happens after the physical
6763 * addresses have been transferred to the new BD location.
6764 */
6765 smp_wmb();
6766
Eric Dumazet9205fd92011-11-18 06:47:01 +00006767 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768}
6769
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770/* The RX ring scheme is composed of multiple rings which post fresh
6771 * buffers to the chip, and one special ring the chip uses to report
6772 * status back to the host.
6773 *
6774 * The special ring reports the status of received packets to the
6775 * host. The chip does not write into the original descriptor the
6776 * RX buffer was obtained from. The chip simply takes the original
6777 * descriptor as provided by the host, updates the status and length
6778 * field, then writes this into the next status ring entry.
6779 *
6780 * Each ring the host uses to post buffers to the chip is described
6781 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6782 * it is first placed into the on-chip ram. When the packet's length
6783 * is known, it walks down the TG3_BDINFO entries to select the ring.
6784 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6785 * which is within the range of the new packet's length is chosen.
6786 *
6787 * The "separate ring for rx status" scheme may sound queer, but it makes
6788 * sense from a cache coherency perspective. If only the host writes
6789 * to the buffer post rings, and only the chip writes to the rx status
6790 * rings, then cache lines never move beyond shared-modified state.
6791 * If both the host and chip were to write into the same ring, cache line
6792 * eviction could occur since both entities want it in an exclusive state.
6793 */
Matt Carlson17375d22009-08-28 14:02:18 +00006794static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795{
Matt Carlson17375d22009-08-28 14:02:18 +00006796 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006797 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006798 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006799 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006800 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006802 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006804 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 /*
6806 * We need to order the read of hw_idx and the read of
6807 * the opaque cookie.
6808 */
6809 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 work_mask = 0;
6811 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006812 std_prod_idx = tpr->rx_std_prod_idx;
6813 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006815 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006816 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 unsigned int len;
6818 struct sk_buff *skb;
6819 dma_addr_t dma_addr;
6820 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006821 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006822 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
6824 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6825 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6826 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006827 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006828 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006829 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006830 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006831 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006833 ri = &tp->napi[0].prodring.rx_jmb_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 = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006837 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
6840 work_mask |= opaque_key;
6841
Michael Chand7b95312014-02-28 15:05:10 -08006842 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006844 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 desc_idx, *post_ptr);
6846 drop_it_no_recycle:
6847 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006848 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 goto next_pkt;
6850 }
6851
Eric Dumazet9205fd92011-11-18 06:47:01 +00006852 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006853 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6854 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006856 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6857 RXD_FLAG_PTPSTAT_PTPV1 ||
6858 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6859 RXD_FLAG_PTPSTAT_PTPV2) {
6860 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6861 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6862 }
6863
Matt Carlsond2757fc2010-04-12 06:58:27 +00006864 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006866 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Eric Dumazet9205fd92011-11-18 06:47:01 +00006868 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006869 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 if (skb_size < 0)
6871 goto drop_it;
6872
Matt Carlson287be122009-08-28 13:58:46 +00006873 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 PCI_DMA_FROMDEVICE);
6875
Eric Dumazet9205fd92011-11-18 06:47:01 +00006876 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006877 * after the usage of the old DMA mapping.
6878 */
6879 smp_wmb();
6880
Eric Dumazet9205fd92011-11-18 06:47:01 +00006881 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006882
Ivan Vecera85aec732013-11-06 14:02:36 +01006883 skb = build_skb(data, frag_size);
6884 if (!skb) {
6885 tg3_frag_free(frag_size != 0, data);
6886 goto drop_it_no_recycle;
6887 }
6888 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006890 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 desc_idx, *post_ptr);
6892
Eric Dumazet9205fd92011-11-18 06:47:01 +00006893 skb = netdev_alloc_skb(tp->dev,
6894 len + TG3_RAW_IP_ALIGN);
6895 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 goto drop_it_no_recycle;
6897
Eric Dumazet9205fd92011-11-18 06:47:01 +00006898 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006900 memcpy(skb->data,
6901 data + TG3_RX_OFFSET(tp),
6902 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 }
6905
Eric Dumazet9205fd92011-11-18 06:47:01 +00006906 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006907 if (tstamp)
6908 tg3_hwclock_to_timestamp(tp, tstamp,
6909 skb_hwtstamps(skb));
6910
Michał Mirosławdc668912011-04-07 03:35:07 +00006911 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6913 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6914 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6915 skb->ip_summed = CHECKSUM_UNNECESSARY;
6916 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006917 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
6919 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006920
6921 if (len > (tp->dev->mtu + ETH_HLEN) &&
Vlad Yasevich7d3083e2014-09-30 19:39:36 -04006922 skb->protocol != htons(ETH_P_8021Q) &&
6923 skb->protocol != htons(ETH_P_8021AD)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006924 dev_kfree_skb_any(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006925 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006926 }
6927
Matt Carlson9dc7a112010-04-12 06:58:28 +00006928 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006929 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006930 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006931 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006932
Matt Carlsonbf933c82011-01-25 15:58:49 +00006933 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 received++;
6936 budget--;
6937
6938next_pkt:
6939 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006940
6941 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006942 tpr->rx_std_prod_idx = std_prod_idx &
6943 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006944 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6945 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006946 work_mask &= ~RXD_OPAQUE_RING_STD;
6947 rx_std_posted = 0;
6948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006950 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006951 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006952
6953 /* Refresh hw_idx to see if there is new work */
6954 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006955 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006956 rmb();
6957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 }
6959
6960 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006961 tnapi->rx_rcb_ptr = sw_idx;
6962 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
6964 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006965 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006966 /* Sync BD data before updating mailbox */
6967 wmb();
6968
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006969 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006970 tpr->rx_std_prod_idx = std_prod_idx &
6971 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006972 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6973 tpr->rx_std_prod_idx);
6974 }
6975 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006976 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6977 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006978 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6979 tpr->rx_jmb_prod_idx);
6980 }
6981 mmiowb();
6982 } else if (work_mask) {
6983 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6984 * updated before the producer indices can be updated.
6985 */
6986 smp_wmb();
6987
Matt Carlson2c49a442010-09-30 10:34:35 +00006988 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6989 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006990
Michael Chan7ae52892012-03-21 15:38:33 +00006991 if (tnapi != &tp->napi[1]) {
6992 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006993 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
6997 return received;
6998}
6999
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007000static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007003 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007004 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7005
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 if (sblk->status & SD_STATUS_LINK_CHG) {
7007 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007008 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007009 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007010 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007011 tw32_f(MAC_STATUS,
7012 (MAC_STATUS_SYNC_CHANGED |
7013 MAC_STATUS_CFG_CHANGED |
7014 MAC_STATUS_MI_COMPLETION |
7015 MAC_STATUS_LNKSTATE_CHANGED));
7016 udelay(40);
7017 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007018 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007019 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 }
7021 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007022}
7023
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007024static int tg3_rx_prodring_xfer(struct tg3 *tp,
7025 struct tg3_rx_prodring_set *dpr,
7026 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007027{
7028 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007029 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007030
7031 while (1) {
7032 src_prod_idx = spr->rx_std_prod_idx;
7033
7034 /* Make sure updates to the rx_std_buffers[] entries and the
7035 * standard producer index are seen in the correct order.
7036 */
7037 smp_rmb();
7038
7039 if (spr->rx_std_cons_idx == src_prod_idx)
7040 break;
7041
7042 if (spr->rx_std_cons_idx < src_prod_idx)
7043 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7044 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007045 cpycnt = tp->rx_std_ring_mask + 1 -
7046 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007047
Matt Carlson2c49a442010-09-30 10:34:35 +00007048 cpycnt = min(cpycnt,
7049 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007050
7051 si = spr->rx_std_cons_idx;
7052 di = dpr->rx_std_prod_idx;
7053
Matt Carlsone92967b2010-02-12 14:47:06 +00007054 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007055 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007056 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007057 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007058 break;
7059 }
7060 }
7061
7062 if (!cpycnt)
7063 break;
7064
7065 /* Ensure that updates to the rx_std_buffers ring and the
7066 * shadowed hardware producer ring from tg3_recycle_skb() are
7067 * ordered correctly WRT the skb check above.
7068 */
7069 smp_rmb();
7070
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007071 memcpy(&dpr->rx_std_buffers[di],
7072 &spr->rx_std_buffers[si],
7073 cpycnt * sizeof(struct ring_info));
7074
7075 for (i = 0; i < cpycnt; i++, di++, si++) {
7076 struct tg3_rx_buffer_desc *sbd, *dbd;
7077 sbd = &spr->rx_std[si];
7078 dbd = &dpr->rx_std[di];
7079 dbd->addr_hi = sbd->addr_hi;
7080 dbd->addr_lo = sbd->addr_lo;
7081 }
7082
Matt Carlson2c49a442010-09-30 10:34:35 +00007083 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7084 tp->rx_std_ring_mask;
7085 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7086 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007087 }
7088
7089 while (1) {
7090 src_prod_idx = spr->rx_jmb_prod_idx;
7091
7092 /* Make sure updates to the rx_jmb_buffers[] entries and
7093 * the jumbo producer index are seen in the correct order.
7094 */
7095 smp_rmb();
7096
7097 if (spr->rx_jmb_cons_idx == src_prod_idx)
7098 break;
7099
7100 if (spr->rx_jmb_cons_idx < src_prod_idx)
7101 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7102 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007103 cpycnt = tp->rx_jmb_ring_mask + 1 -
7104 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007105
7106 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007107 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007108
7109 si = spr->rx_jmb_cons_idx;
7110 di = dpr->rx_jmb_prod_idx;
7111
Matt Carlsone92967b2010-02-12 14:47:06 +00007112 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007113 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007114 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007115 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007116 break;
7117 }
7118 }
7119
7120 if (!cpycnt)
7121 break;
7122
7123 /* Ensure that updates to the rx_jmb_buffers ring and the
7124 * shadowed hardware producer ring from tg3_recycle_skb() are
7125 * ordered correctly WRT the skb check above.
7126 */
7127 smp_rmb();
7128
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007129 memcpy(&dpr->rx_jmb_buffers[di],
7130 &spr->rx_jmb_buffers[si],
7131 cpycnt * sizeof(struct ring_info));
7132
7133 for (i = 0; i < cpycnt; i++, di++, si++) {
7134 struct tg3_rx_buffer_desc *sbd, *dbd;
7135 sbd = &spr->rx_jmb[si].std;
7136 dbd = &dpr->rx_jmb[di].std;
7137 dbd->addr_hi = sbd->addr_hi;
7138 dbd->addr_lo = sbd->addr_lo;
7139 }
7140
Matt Carlson2c49a442010-09-30 10:34:35 +00007141 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7142 tp->rx_jmb_ring_mask;
7143 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7144 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007145 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007146
7147 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007148}
7149
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007150static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7151{
7152 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
7154 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007155 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007156 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007157 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007158 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 }
7160
Matt Carlsonf891ea12012-04-24 13:37:01 +00007161 if (!tnapi->rx_rcb_prod_idx)
7162 return work_done;
7163
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 /* run RX thread, within the bounds set by NAPI.
7165 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007166 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007168 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007169 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
Joe Perches63c3a662011-04-26 08:12:10 +00007171 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007172 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007173 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007174 u32 std_prod_idx = dpr->rx_std_prod_idx;
7175 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007176
Michael Chan7ae52892012-03-21 15:38:33 +00007177 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007178 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007179 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007180 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007181
7182 wmb();
7183
Matt Carlsone4af1af2010-02-12 14:47:05 +00007184 if (std_prod_idx != dpr->rx_std_prod_idx)
7185 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7186 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007187
Matt Carlsone4af1af2010-02-12 14:47:05 +00007188 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7189 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7190 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007191
7192 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007193
7194 if (err)
7195 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007196 }
7197
David S. Miller6f535762007-10-11 18:08:29 -07007198 return work_done;
7199}
David S. Millerf7383c22005-05-18 22:50:53 -07007200
Matt Carlsondb219972011-11-04 09:15:03 +00007201static inline void tg3_reset_task_schedule(struct tg3 *tp)
7202{
7203 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7204 schedule_work(&tp->reset_task);
7205}
7206
7207static inline void tg3_reset_task_cancel(struct tg3 *tp)
7208{
7209 cancel_work_sync(&tp->reset_task);
7210 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007211 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007212}
7213
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007214static int tg3_poll_msix(struct napi_struct *napi, int budget)
7215{
7216 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7217 struct tg3 *tp = tnapi->tp;
7218 int work_done = 0;
7219 struct tg3_hw_status *sblk = tnapi->hw_status;
7220
7221 while (1) {
7222 work_done = tg3_poll_work(tnapi, work_done, budget);
7223
Joe Perches63c3a662011-04-26 08:12:10 +00007224 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007225 goto tx_recovery;
7226
7227 if (unlikely(work_done >= budget))
7228 break;
7229
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007230 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007231 * to tell the hw how much work has been processed,
7232 * so we must read it before checking for more work.
7233 */
7234 tnapi->last_tag = sblk->status_tag;
7235 tnapi->last_irq_tag = tnapi->last_tag;
7236 rmb();
7237
7238 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007239 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7240 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007241
7242 /* This test here is not race free, but will reduce
7243 * the number of interrupts by looping again.
7244 */
7245 if (tnapi == &tp->napi[1] && tp->rx_refill)
7246 continue;
7247
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007248 napi_complete_done(napi, work_done);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007249 /* Reenable interrupts. */
7250 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007251
7252 /* This test here is synchronized by napi_schedule()
7253 * and napi_complete() to close the race condition.
7254 */
7255 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7256 tw32(HOSTCC_MODE, tp->coalesce_mode |
7257 HOSTCC_MODE_ENABLE |
7258 tnapi->coal_now);
7259 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007260 mmiowb();
7261 break;
7262 }
7263 }
7264
7265 return work_done;
7266
7267tx_recovery:
7268 /* work_done is guaranteed to be less than budget. */
7269 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007270 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007271 return work_done;
7272}
7273
Matt Carlsone64de4e2011-04-13 11:05:05 +00007274static void tg3_process_error(struct tg3 *tp)
7275{
7276 u32 val;
7277 bool real_error = false;
7278
Joe Perches63c3a662011-04-26 08:12:10 +00007279 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007280 return;
7281
7282 /* Check Flow Attention register */
7283 val = tr32(HOSTCC_FLOW_ATTN);
7284 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7285 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7286 real_error = true;
7287 }
7288
7289 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7290 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7291 real_error = true;
7292 }
7293
7294 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7295 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7296 real_error = true;
7297 }
7298
7299 if (!real_error)
7300 return;
7301
7302 tg3_dump_state(tp);
7303
Joe Perches63c3a662011-04-26 08:12:10 +00007304 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007305 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007306}
7307
David S. Miller6f535762007-10-11 18:08:29 -07007308static int tg3_poll(struct napi_struct *napi, int budget)
7309{
Matt Carlson8ef04422009-08-28 14:01:37 +00007310 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7311 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007312 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007313 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007314
7315 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007316 if (sblk->status & SD_STATUS_ERROR)
7317 tg3_process_error(tp);
7318
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007319 tg3_poll_link(tp);
7320
Matt Carlson17375d22009-08-28 14:02:18 +00007321 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007322
Joe Perches63c3a662011-04-26 08:12:10 +00007323 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007324 goto tx_recovery;
7325
7326 if (unlikely(work_done >= budget))
7327 break;
7328
Joe Perches63c3a662011-04-26 08:12:10 +00007329 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007330 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007331 * to tell the hw how much work has been processed,
7332 * so we must read it before checking for more work.
7333 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007334 tnapi->last_tag = sblk->status_tag;
7335 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007336 rmb();
7337 } else
7338 sblk->status &= ~SD_STATUS_UPDATED;
7339
Matt Carlson17375d22009-08-28 14:02:18 +00007340 if (likely(!tg3_has_work(tnapi))) {
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007341 napi_complete_done(napi, work_done);
Matt Carlson17375d22009-08-28 14:02:18 +00007342 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007343 break;
7344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 }
7346
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007347 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007348
7349tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007350 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007351 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007352 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007353 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354}
7355
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007356static void tg3_napi_disable(struct tg3 *tp)
7357{
7358 int i;
7359
7360 for (i = tp->irq_cnt - 1; i >= 0; i--)
7361 napi_disable(&tp->napi[i].napi);
7362}
7363
7364static void tg3_napi_enable(struct tg3 *tp)
7365{
7366 int i;
7367
7368 for (i = 0; i < tp->irq_cnt; i++)
7369 napi_enable(&tp->napi[i].napi);
7370}
7371
7372static void tg3_napi_init(struct tg3 *tp)
7373{
7374 int i;
7375
7376 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7377 for (i = 1; i < tp->irq_cnt; i++)
7378 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7379}
7380
7381static void tg3_napi_fini(struct tg3 *tp)
7382{
7383 int i;
7384
7385 for (i = 0; i < tp->irq_cnt; i++)
7386 netif_napi_del(&tp->napi[i].napi);
7387}
7388
7389static inline void tg3_netif_stop(struct tg3 *tp)
7390{
Florian Westphal860e9532016-05-03 16:33:13 +02007391 netif_trans_update(tp->dev); /* prevent tx timeout */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007392 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007393 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007394 netif_tx_disable(tp->dev);
7395}
7396
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007397/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007398static inline void tg3_netif_start(struct tg3 *tp)
7399{
Matt Carlsonbe947302012-12-03 19:36:57 +00007400 tg3_ptp_resume(tp);
7401
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007402 /* NOTE: unconditional netif_tx_wake_all_queues is only
7403 * appropriate so long as all callers are assured to
7404 * have free tx slots (such as after tg3_init_hw)
7405 */
7406 netif_tx_wake_all_queues(tp->dev);
7407
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007408 if (tp->link_up)
7409 netif_carrier_on(tp->dev);
7410
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007411 tg3_napi_enable(tp);
7412 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7413 tg3_enable_ints(tp);
7414}
7415
David S. Millerf47c11e2005-06-24 20:18:35 -07007416static void tg3_irq_quiesce(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007417 __releases(tp->lock)
7418 __acquires(tp->lock)
David S. Millerf47c11e2005-06-24 20:18:35 -07007419{
Matt Carlson4f125f42009-09-01 12:55:02 +00007420 int i;
7421
David S. Millerf47c11e2005-06-24 20:18:35 -07007422 BUG_ON(tp->irq_sync);
7423
7424 tp->irq_sync = 1;
7425 smp_mb();
7426
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007427 spin_unlock_bh(&tp->lock);
7428
Matt Carlson4f125f42009-09-01 12:55:02 +00007429 for (i = 0; i < tp->irq_cnt; i++)
7430 synchronize_irq(tp->napi[i].irq_vec);
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007431
7432 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007433}
7434
David S. Millerf47c11e2005-06-24 20:18:35 -07007435/* Fully shutdown all tg3 driver activity elsewhere in the system.
7436 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7437 * with as well. Most of the time, this is not necessary except when
7438 * shutting down the device.
7439 */
7440static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7441{
Michael Chan46966542007-07-11 19:47:19 -07007442 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007443 if (irq_sync)
7444 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007445}
7446
7447static inline void tg3_full_unlock(struct tg3 *tp)
7448{
David S. Millerf47c11e2005-06-24 20:18:35 -07007449 spin_unlock_bh(&tp->lock);
7450}
7451
Michael Chanfcfa0a32006-03-20 22:28:41 -08007452/* One-shot MSI handler - Chip automatically disables interrupt
7453 * after sending MSI so driver doesn't have to do it.
7454 */
David Howells7d12e782006-10-05 14:55:46 +01007455static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007456{
Matt Carlson09943a12009-08-28 14:01:57 +00007457 struct tg3_napi *tnapi = dev_id;
7458 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007459
Matt Carlson898a56f2009-08-28 14:02:40 +00007460 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007461 if (tnapi->rx_rcb)
7462 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007463
7464 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007465 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007466
7467 return IRQ_HANDLED;
7468}
7469
Michael Chan88b06bc22005-04-21 17:13:25 -07007470/* MSI ISR - No need to check for interrupt sharing and no need to
7471 * flush status block and interrupt mailbox. PCI ordering rules
7472 * guarantee that MSI will arrive after the status block.
7473 */
David Howells7d12e782006-10-05 14:55:46 +01007474static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007475{
Matt Carlson09943a12009-08-28 14:01:57 +00007476 struct tg3_napi *tnapi = dev_id;
7477 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007478
Matt Carlson898a56f2009-08-28 14:02:40 +00007479 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007480 if (tnapi->rx_rcb)
7481 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007482 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007483 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007484 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007485 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007486 * NIC to stop sending us irqs, engaging "in-intr-handler"
7487 * event coalescing.
7488 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007489 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007490 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007491 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007492
Michael Chan88b06bc22005-04-21 17:13:25 -07007493 return IRQ_RETVAL(1);
7494}
7495
David Howells7d12e782006-10-05 14:55:46 +01007496static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497{
Matt Carlson09943a12009-08-28 14:01:57 +00007498 struct tg3_napi *tnapi = dev_id;
7499 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007500 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 unsigned int handled = 1;
7502
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 /* In INTx mode, it is possible for the interrupt to arrive at
7504 * the CPU before the status block posted prior to the interrupt.
7505 * Reading the PCI State register will confirm whether the
7506 * interrupt is ours and will flush the status block.
7507 */
Michael Chand18edcb2007-03-24 20:57:11 -07007508 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007509 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007510 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7511 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007512 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007513 }
Michael Chand18edcb2007-03-24 20:57:11 -07007514 }
7515
7516 /*
7517 * Writing any value to intr-mbox-0 clears PCI INTA# and
7518 * chip-internal interrupt pending events.
7519 * Writing non-zero to intr-mbox-0 additional tells the
7520 * NIC to stop sending us irqs, engaging "in-intr-handler"
7521 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007522 *
7523 * Flush the mailbox to de-assert the IRQ immediately to prevent
7524 * spurious interrupts. The flush impacts performance but
7525 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007526 */
Michael Chanc04cb342007-05-07 00:26:15 -07007527 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007528 if (tg3_irq_sync(tp))
7529 goto out;
7530 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007531 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007532 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007533 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007534 } else {
7535 /* No work, shared interrupt perhaps? re-enable
7536 * interrupts, and flush that PCI write
7537 */
7538 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7539 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007540 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007541out:
David S. Millerfac9b832005-05-18 22:46:34 -07007542 return IRQ_RETVAL(handled);
7543}
7544
David Howells7d12e782006-10-05 14:55:46 +01007545static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007546{
Matt Carlson09943a12009-08-28 14:01:57 +00007547 struct tg3_napi *tnapi = dev_id;
7548 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007549 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007550 unsigned int handled = 1;
7551
David S. Millerfac9b832005-05-18 22:46:34 -07007552 /* In INTx mode, it is possible for the interrupt to arrive at
7553 * the CPU before the status block posted prior to the interrupt.
7554 * Reading the PCI State register will confirm whether the
7555 * interrupt is ours and will flush the status block.
7556 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007557 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007558 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007559 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7560 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
Michael Chand18edcb2007-03-24 20:57:11 -07007563 }
7564
7565 /*
7566 * writing any value to intr-mbox-0 clears PCI INTA# and
7567 * chip-internal interrupt pending events.
7568 * writing non-zero to intr-mbox-0 additional tells the
7569 * NIC to stop sending us irqs, engaging "in-intr-handler"
7570 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007571 *
7572 * Flush the mailbox to de-assert the IRQ immediately to prevent
7573 * spurious interrupts. The flush impacts performance but
7574 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007575 */
Michael Chanc04cb342007-05-07 00:26:15 -07007576 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007577
7578 /*
7579 * In a shared interrupt configuration, sometimes other devices'
7580 * interrupts will scream. We record the current status tag here
7581 * so that the above check can report that the screaming interrupts
7582 * are unhandled. Eventually they will be silenced.
7583 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007584 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007585
Michael Chand18edcb2007-03-24 20:57:11 -07007586 if (tg3_irq_sync(tp))
7587 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007588
Matt Carlson72334482009-08-28 14:03:01 +00007589 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007590
Matt Carlson09943a12009-08-28 14:01:57 +00007591 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007592
David S. Millerf47c11e2005-06-24 20:18:35 -07007593out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 return IRQ_RETVAL(handled);
7595}
7596
Michael Chan79381092005-04-21 17:13:59 -07007597/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007598static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007599{
Matt Carlson09943a12009-08-28 14:01:57 +00007600 struct tg3_napi *tnapi = dev_id;
7601 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007602 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007603
Michael Chanf9804dd2005-09-27 12:13:10 -07007604 if ((sblk->status & SD_STATUS_UPDATED) ||
7605 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007606 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007607 return IRQ_RETVAL(1);
7608 }
7609 return IRQ_RETVAL(0);
7610}
7611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612#ifdef CONFIG_NET_POLL_CONTROLLER
7613static void tg3_poll_controller(struct net_device *dev)
7614{
Matt Carlson4f125f42009-09-01 12:55:02 +00007615 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007616 struct tg3 *tp = netdev_priv(dev);
7617
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007618 if (tg3_irq_sync(tp))
7619 return;
7620
Matt Carlson4f125f42009-09-01 12:55:02 +00007621 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007622 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623}
7624#endif
7625
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626static void tg3_tx_timeout(struct net_device *dev)
7627{
7628 struct tg3 *tp = netdev_priv(dev);
7629
Michael Chanb0408752007-02-13 12:18:30 -08007630 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007631 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007632 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634
Matt Carlsondb219972011-11-04 09:15:03 +00007635 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636}
7637
Michael Chanc58ec932005-09-17 00:46:27 -07007638/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7639static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7640{
7641 u32 base = (u32) mapping & 0xffffffff;
7642
Nithin Sujir37567912013-12-19 17:44:11 -08007643 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007644}
7645
Michael Chan0f0d1512013-05-13 11:04:16 +00007646/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7647 * of any 4GB boundaries: 4G, 8G, etc
7648 */
7649static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7650 u32 len, u32 mss)
7651{
7652 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7653 u32 base = (u32) mapping & 0xffffffff;
7654
7655 return ((base + len + (mss & 0x3fff)) < base);
7656 }
7657 return 0;
7658}
7659
Michael Chan72f2afb2006-03-06 19:28:35 -08007660/* Test for DMA addresses > 40-bit */
7661static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7662 int len)
7663{
7664#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007665 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007666 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007667 return 0;
7668#else
7669 return 0;
7670#endif
7671}
7672
Matt Carlsond1a3b732011-07-27 14:20:51 +00007673static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007674 dma_addr_t mapping, u32 len, u32 flags,
7675 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007676{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007677 txbd->addr_hi = ((u64) mapping >> 32);
7678 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7679 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7680 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
Matt Carlson84b67b22011-07-27 14:20:52 +00007683static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007684 dma_addr_t map, u32 len, u32 flags,
7685 u32 mss, u32 vlan)
7686{
7687 struct tg3 *tp = tnapi->tp;
7688 bool hwbug = false;
7689
7690 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007691 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007692
7693 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007694 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007695
Michael Chan0f0d1512013-05-13 11:04:16 +00007696 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7697 hwbug = true;
7698
Matt Carlsond1a3b732011-07-27 14:20:51 +00007699 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007700 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007701
Matt Carlsona4cb4282011-12-14 11:09:58 +00007702 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007703 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007704 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007705 while (len > tp->dma_limit && *budget) {
7706 u32 frag_len = tp->dma_limit;
7707 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007708
Matt Carlsonb9e45482011-11-04 09:14:59 +00007709 /* Avoid the 8byte DMA problem */
7710 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007711 len += tp->dma_limit / 2;
7712 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007713 }
7714
Matt Carlsonb9e45482011-11-04 09:14:59 +00007715 tnapi->tx_buffers[*entry].fragmented = true;
7716
7717 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7718 frag_len, tmp_flag, mss, vlan);
7719 *budget -= 1;
7720 prvidx = *entry;
7721 *entry = NEXT_TX(*entry);
7722
Matt Carlsone31aa982011-07-27 14:20:53 +00007723 map += frag_len;
7724 }
7725
7726 if (len) {
7727 if (*budget) {
7728 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7729 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007730 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007731 *entry = NEXT_TX(*entry);
7732 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007733 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007734 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007735 }
7736 }
7737 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007738 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7739 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007740 *entry = NEXT_TX(*entry);
7741 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007742
7743 return hwbug;
7744}
7745
Matt Carlson0d681b22011-07-27 14:20:49 +00007746static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007747{
7748 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007749 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007750 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007751
Matt Carlson0d681b22011-07-27 14:20:49 +00007752 skb = txb->skb;
7753 txb->skb = NULL;
7754
Matt Carlson432aa7e2011-05-19 12:12:45 +00007755 pci_unmap_single(tnapi->tp->pdev,
7756 dma_unmap_addr(txb, mapping),
7757 skb_headlen(skb),
7758 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007759
7760 while (txb->fragmented) {
7761 txb->fragmented = false;
7762 entry = NEXT_TX(entry);
7763 txb = &tnapi->tx_buffers[entry];
7764 }
7765
Matt Carlsonba1142e2011-11-04 09:15:00 +00007766 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007767 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007768
7769 entry = NEXT_TX(entry);
7770 txb = &tnapi->tx_buffers[entry];
7771
7772 pci_unmap_page(tnapi->tp->pdev,
7773 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007774 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007775
7776 while (txb->fragmented) {
7777 txb->fragmented = false;
7778 entry = NEXT_TX(entry);
7779 txb = &tnapi->tx_buffers[entry];
7780 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007781 }
7782}
7783
Michael Chan72f2afb2006-03-06 19:28:35 -08007784/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007785static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007786 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007787 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007788 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007789{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007790 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007791 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007792 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007793 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794
Joe Perches41535772013-02-16 11:20:04 +00007795 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007796 new_skb = skb_copy(skb, GFP_ATOMIC);
7797 else {
7798 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7799
7800 new_skb = skb_copy_expand(skb,
7801 skb_headroom(skb) + more_headroom,
7802 skb_tailroom(skb), GFP_ATOMIC);
7803 }
7804
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007806 ret = -1;
7807 } else {
7808 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007809 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7810 PCI_DMA_TODEVICE);
7811 /* Make sure the mapping succeeded */
7812 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007813 dev_kfree_skb_any(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007814 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007815 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007816 u32 save_entry = *entry;
7817
Matt Carlson92cd3a12011-07-27 14:20:47 +00007818 base_flags |= TXD_FLAG_END;
7819
Matt Carlson84b67b22011-07-27 14:20:52 +00007820 tnapi->tx_buffers[*entry].skb = new_skb;
7821 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007822 mapping, new_addr);
7823
Matt Carlson84b67b22011-07-27 14:20:52 +00007824 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007825 new_skb->len, base_flags,
7826 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007827 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007828 dev_kfree_skb_any(new_skb);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007829 ret = -1;
7830 }
Michael Chanc58ec932005-09-17 00:46:27 -07007831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 }
7833
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007834 dev_consume_skb_any(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007835 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837}
7838
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307839static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
7840{
7841 /* Check if we will never have enough descriptors,
7842 * as gso_segs can be more than current ring size
7843 */
7844 return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
7845}
7846
Matt Carlson2ffcc982011-05-19 12:12:44 +00007847static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007848
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007849/* Use GSO to workaround all TSO packets that meet HW bug conditions
7850 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007851 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007852static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7853 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007854{
7855 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007856 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007857
7858 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007859 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7860 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007861
7862 /* netif_tx_stop_queue() must be done before checking
7863 * checking tx index in tg3_tx_avail() below, because in
7864 * tg3_tx(), we update tx index before checking for
7865 * netif_tx_queue_stopped().
7866 */
7867 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007868 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007869 return NETDEV_TX_BUSY;
7870
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007871 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007872 }
7873
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007874 segs = skb_gso_segment(skb, tp->dev->features &
7875 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007876 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007877 goto tg3_tso_bug_end;
7878
7879 do {
7880 nskb = segs;
7881 segs = segs->next;
7882 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007883 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007884 } while (segs);
7885
7886tg3_tso_bug_end:
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007887 dev_consume_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007888
7889 return NETDEV_TX_OK;
7890}
Michael Chan52c0fd82006-06-29 20:15:54 -07007891
Michael Chand71c0dc2014-05-11 20:22:53 -07007892/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007893static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007894{
7895 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007896 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007897 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007898 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007899 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007900 struct tg3_napi *tnapi;
7901 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007902 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007903 struct iphdr *iph = NULL;
7904 struct tcphdr *tcph = NULL;
7905 __sum16 tcp_csum = 0, ip_csum = 0;
7906 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007907
Matt Carlson24f4efd2009-11-13 13:03:35 +00007908 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7909 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007910 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007911 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912
Matt Carlson84b67b22011-07-27 14:20:52 +00007913 budget = tg3_tx_avail(tnapi);
7914
Michael Chan00b70502006-06-17 21:58:45 -07007915 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007916 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007917 * interrupt. Furthermore, IRQ processing runs lockless so we have
7918 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007920 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007921 if (!netif_tx_queue_stopped(txq)) {
7922 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007923
7924 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007925 netdev_err(dev,
7926 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 return NETDEV_TX_BUSY;
7929 }
7930
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007931 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007933
Matt Carlsonbe98da62010-07-11 09:31:46 +00007934 mss = skb_shinfo(skb)->gso_size;
7935 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007936 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
françois romieu105dcb52014-03-29 12:26:29 +01007938 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007939 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
Matt Carlson34195c32010-07-11 09:31:42 +00007941 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007942 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943
Eric Dumazeta5a11952012-01-23 01:22:09 +00007944 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007945
Vlad Yasevich476c1882014-09-18 10:31:17 -04007946 /* HW/FW can not correctly segment packets that have been
7947 * vlan encapsulated.
7948 */
7949 if (skb->protocol == htons(ETH_P_8021Q) ||
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307950 skb->protocol == htons(ETH_P_8021AD)) {
7951 if (tg3_tso_bug_gso_check(tnapi, skb))
7952 return tg3_tso_bug(tp, tnapi, txq, skb);
7953 goto drop;
7954 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007955
Eric Dumazeta5a11952012-01-23 01:22:09 +00007956 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007957 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307958 tg3_flag(tp, TSO_BUG)) {
7959 if (tg3_tso_bug_gso_check(tnapi, skb))
7960 return tg3_tso_bug(tp, tnapi, txq, skb);
7961 goto drop;
7962 }
Michael Chand3f6f3a2014-05-11 20:22:54 -07007963 ip_csum = iph->check;
7964 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007965 iph->check = 0;
7966 iph->tot_len = htons(mss + hdr_len);
7967 }
7968
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7970 TXD_FLAG_CPU_POST_DMA);
7971
Michael Chand3f6f3a2014-05-11 20:22:54 -07007972 tcph = tcp_hdr(skb);
7973 tcp_csum = tcph->check;
7974
Joe Perches63c3a662011-04-26 08:12:10 +00007975 if (tg3_flag(tp, HW_TSO_1) ||
7976 tg3_flag(tp, HW_TSO_2) ||
7977 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007978 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007979 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007980 } else {
7981 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7982 0, IPPROTO_TCP, 0);
7983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984
Joe Perches63c3a662011-04-26 08:12:10 +00007985 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007986 mss |= (hdr_len & 0xc) << 12;
7987 if (hdr_len & 0x10)
7988 base_flags |= 0x00000010;
7989 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007990 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007991 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007992 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007993 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007994 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995 int tsflags;
7996
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007997 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 mss |= (tsflags << 11);
7999 }
8000 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008001 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 int tsflags;
8003
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008004 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 base_flags |= tsflags << 12;
8006 }
8007 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04008008 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
8009 /* HW/FW can not correctly checksum packets that have been
8010 * vlan encapsulated.
8011 */
8012 if (skb->protocol == htons(ETH_P_8021Q) ||
8013 skb->protocol == htons(ETH_P_8021AD)) {
8014 if (skb_checksum_help(skb))
8015 goto drop;
8016 } else {
8017 base_flags |= TXD_FLAG_TCPUDP_CSUM;
8018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008020
Matt Carlson93a700a2011-08-31 11:44:54 +00008021 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8022 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8023 base_flags |= TXD_FLAG_JMB_PKT;
8024
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008025 if (skb_vlan_tag_present(skb)) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008026 base_flags |= TXD_FLAG_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008027 vlan = skb_vlan_tag_get(skb);
Matt Carlson92cd3a12011-07-27 14:20:47 +00008028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008030 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8031 tg3_flag(tp, TX_TSTAMP_EN)) {
8032 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8033 base_flags |= TXD_FLAG_HWTSTAMP;
8034 }
8035
Alexander Duyckf4188d82009-12-02 16:48:38 +00008036 len = skb_headlen(skb);
8037
8038 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008039 if (pci_dma_mapping_error(tp->pdev, mapping))
8040 goto drop;
8041
David S. Miller90079ce2008-09-11 04:52:51 -07008042
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008043 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008044 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045
8046 would_hit_hwbug = 0;
8047
Joe Perches63c3a662011-04-26 08:12:10 +00008048 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008049 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
Matt Carlson84b67b22011-07-27 14:20:52 +00008051 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008052 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008053 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008054 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008055 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008056 u32 tmp_mss = mss;
8057
8058 if (!tg3_flag(tp, HW_TSO_1) &&
8059 !tg3_flag(tp, HW_TSO_2) &&
8060 !tg3_flag(tp, HW_TSO_3))
8061 tmp_mss = 0;
8062
Matt Carlsonc5665a52012-02-13 10:20:12 +00008063 /* Now loop through additional data
8064 * fragments, and queue them.
8065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066 last = skb_shinfo(skb)->nr_frags - 1;
8067 for (i = 0; i <= last; i++) {
8068 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8069
Eric Dumazet9e903e02011-10-18 21:00:24 +00008070 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008071 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008072 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008074 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008075 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008076 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008077 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008078 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079
Matt Carlsonb9e45482011-11-04 09:14:59 +00008080 if (!budget ||
8081 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008082 len, base_flags |
8083 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008084 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008085 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008086 break;
8087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 }
8089 }
8090
8091 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008092 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05308094 if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07008095 /* If it's a TSO packet, do GSO instead of
8096 * allocating and copying to a large linear SKB
8097 */
8098 if (ip_tot_len) {
8099 iph->check = ip_csum;
8100 iph->tot_len = ip_tot_len;
8101 }
8102 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008103 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008104 }
8105
Linus Torvalds1da177e2005-04-16 15:20:36 -07008106 /* If the workaround fails due to memory/mapping
8107 * failure, silently drop this packet.
8108 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008109 entry = tnapi->tx_prod;
8110 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008111 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008112 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008113 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 }
8115
Richard Cochrand515b452011-06-19 03:31:41 +00008116 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008117 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008118
Michael Chan6541b802012-03-04 14:48:14 +00008119 /* Sync BD data before updating mailbox */
8120 wmb();
8121
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008122 tnapi->tx_prod = entry;
8123 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008124 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008125
8126 /* netif_tx_stop_queue() must be done before checking
8127 * checking tx index in tg3_tx_avail() below, because in
8128 * tg3_tx(), we update tx index before checking for
8129 * netif_tx_queue_stopped().
8130 */
8131 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008132 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008133 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008136 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8137 /* Packets are ready, update Tx producer idx on card. */
8138 tw32_tx_mbox(tnapi->prodmbox, entry);
8139 mmiowb();
8140 }
8141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008143
8144dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008145 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008146 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008147drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008148 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008149drop_nofree:
8150 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008151 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152}
8153
Matt Carlson6e01b202011-08-19 13:58:20 +00008154static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8155{
8156 if (enable) {
8157 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8158 MAC_MODE_PORT_MODE_MASK);
8159
8160 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8161
8162 if (!tg3_flag(tp, 5705_PLUS))
8163 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8164
8165 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8166 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8167 else
8168 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8169 } else {
8170 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8171
8172 if (tg3_flag(tp, 5705_PLUS) ||
8173 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008174 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008175 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8176 }
8177
8178 tw32(MAC_MODE, tp->mac_mode);
8179 udelay(40);
8180}
8181
Matt Carlson941ec902011-08-19 13:58:23 +00008182static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008183{
Matt Carlson941ec902011-08-19 13:58:23 +00008184 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008185
8186 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008187 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008188
Matt Carlson941ec902011-08-19 13:58:23 +00008189 if (extlpbk && tg3_phy_set_extloopbk(tp))
8190 return -EIO;
8191
8192 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008193 switch (speed) {
8194 case SPEED_10:
8195 break;
8196 case SPEED_100:
8197 bmcr |= BMCR_SPEED100;
8198 break;
8199 case SPEED_1000:
8200 default:
8201 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8202 speed = SPEED_100;
8203 bmcr |= BMCR_SPEED100;
8204 } else {
8205 speed = SPEED_1000;
8206 bmcr |= BMCR_SPEED1000;
8207 }
8208 }
8209
Matt Carlson941ec902011-08-19 13:58:23 +00008210 if (extlpbk) {
8211 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8212 tg3_readphy(tp, MII_CTRL1000, &val);
8213 val |= CTL1000_AS_MASTER |
8214 CTL1000_ENABLE_MASTER;
8215 tg3_writephy(tp, MII_CTRL1000, val);
8216 } else {
8217 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8218 MII_TG3_FET_PTEST_TRIM_2;
8219 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8220 }
8221 } else
8222 bmcr |= BMCR_LOOPBACK;
8223
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008224 tg3_writephy(tp, MII_BMCR, bmcr);
8225
8226 /* The write needs to be flushed for the FETs */
8227 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8228 tg3_readphy(tp, MII_BMCR, &bmcr);
8229
8230 udelay(40);
8231
8232 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008233 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008234 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008235 MII_TG3_FET_PTEST_FRC_TX_LINK |
8236 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8237
8238 /* The write needs to be flushed for the AC131 */
8239 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8240 }
8241
8242 /* Reset to prevent losing 1st rx packet intermittently */
8243 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8244 tg3_flag(tp, 5780_CLASS)) {
8245 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8246 udelay(10);
8247 tw32_f(MAC_RX_MODE, tp->rx_mode);
8248 }
8249
8250 mac_mode = tp->mac_mode &
8251 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8252 if (speed == SPEED_1000)
8253 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8254 else
8255 mac_mode |= MAC_MODE_PORT_MODE_MII;
8256
Joe Perches41535772013-02-16 11:20:04 +00008257 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008258 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8259
8260 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8261 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8262 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8263 mac_mode |= MAC_MODE_LINK_POLARITY;
8264
8265 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8266 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8267 }
8268
8269 tw32(MAC_MODE, mac_mode);
8270 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008271
8272 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008273}
8274
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008275static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008276{
8277 struct tg3 *tp = netdev_priv(dev);
8278
8279 if (features & NETIF_F_LOOPBACK) {
8280 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8281 return;
8282
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008283 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008284 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008285 netif_carrier_on(tp->dev);
8286 spin_unlock_bh(&tp->lock);
8287 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8288 } else {
8289 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8290 return;
8291
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008292 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008293 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008294 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008295 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008296 spin_unlock_bh(&tp->lock);
8297 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8298 }
8299}
8300
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008301static netdev_features_t tg3_fix_features(struct net_device *dev,
8302 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008303{
8304 struct tg3 *tp = netdev_priv(dev);
8305
Joe Perches63c3a662011-04-26 08:12:10 +00008306 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008307 features &= ~NETIF_F_ALL_TSO;
8308
8309 return features;
8310}
8311
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008312static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008313{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008314 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008315
8316 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8317 tg3_set_loopback(dev, features);
8318
8319 return 0;
8320}
8321
Matt Carlson21f581a2009-08-28 14:00:25 +00008322static void tg3_rx_prodring_free(struct tg3 *tp,
8323 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 int i;
8326
Matt Carlson8fea32b2010-09-15 08:59:58 +00008327 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008328 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008329 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008330 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008331 tp->rx_pkt_map_sz);
8332
Joe Perches63c3a662011-04-26 08:12:10 +00008333 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008334 for (i = tpr->rx_jmb_cons_idx;
8335 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008336 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008337 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008338 TG3_RX_JMB_MAP_SZ);
8339 }
8340 }
8341
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008342 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344
Matt Carlson2c49a442010-09-30 10:34:35 +00008345 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008346 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008347 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348
Joe Perches63c3a662011-04-26 08:12:10 +00008349 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008350 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008351 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008352 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 }
8354}
8355
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008356/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008357 *
8358 * The chip has been shut down and the driver detached from
8359 * the networking, so no interrupts or new tx packets will
8360 * end up in the driver. tp->{tx,}lock are held and thus
8361 * we may not sleep.
8362 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008363static int tg3_rx_prodring_alloc(struct tg3 *tp,
8364 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365{
Matt Carlson287be122009-08-28 13:58:46 +00008366 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008368 tpr->rx_std_cons_idx = 0;
8369 tpr->rx_std_prod_idx = 0;
8370 tpr->rx_jmb_cons_idx = 0;
8371 tpr->rx_jmb_prod_idx = 0;
8372
Matt Carlson8fea32b2010-09-15 08:59:58 +00008373 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008374 memset(&tpr->rx_std_buffers[0], 0,
8375 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008376 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008377 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008378 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008379 goto done;
8380 }
8381
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008383 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384
Matt Carlson287be122009-08-28 13:58:46 +00008385 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008386 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008387 tp->dev->mtu > ETH_DATA_LEN)
8388 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8389 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008390
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 /* Initialize invariants of the rings, we only set this
8392 * stuff once. This works because the card does not
8393 * write into the rx buffer posting rings.
8394 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008395 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396 struct tg3_rx_buffer_desc *rxd;
8397
Matt Carlson21f581a2009-08-28 14:00:25 +00008398 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008399 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8401 rxd->opaque = (RXD_OPAQUE_RING_STD |
8402 (i << RXD_OPAQUE_INDEX_SHIFT));
8403 }
8404
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008405 /* Now allocate fresh SKBs for each rx ring. */
8406 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008407 unsigned int frag_size;
8408
8409 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8410 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008411 netdev_warn(tp->dev,
8412 "Using a smaller RX standard ring. Only "
8413 "%d out of %d buffers were allocated "
8414 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008415 if (i == 0)
8416 goto initfail;
8417 tp->rx_pending = i;
8418 break;
8419 }
8420 }
8421
Joe Perches63c3a662011-04-26 08:12:10 +00008422 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008423 goto done;
8424
Matt Carlson2c49a442010-09-30 10:34:35 +00008425 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008426
Joe Perches63c3a662011-04-26 08:12:10 +00008427 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008428 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429
Matt Carlson2c49a442010-09-30 10:34:35 +00008430 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008431 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432
Matt Carlson0d86df82010-02-17 15:17:00 +00008433 rxd = &tpr->rx_jmb[i].std;
8434 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8435 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8436 RXD_FLAG_JUMBO;
8437 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8438 (i << RXD_OPAQUE_INDEX_SHIFT));
8439 }
8440
8441 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008442 unsigned int frag_size;
8443
8444 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8445 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008446 netdev_warn(tp->dev,
8447 "Using a smaller RX jumbo ring. Only %d "
8448 "out of %d buffers were allocated "
8449 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008450 if (i == 0)
8451 goto initfail;
8452 tp->rx_jumbo_pending = i;
8453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 }
8455 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008456
8457done:
Michael Chan32d8c572006-07-25 16:38:29 -07008458 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008459
8460initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008461 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008462 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463}
8464
Matt Carlson21f581a2009-08-28 14:00:25 +00008465static void tg3_rx_prodring_fini(struct tg3 *tp,
8466 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467{
Matt Carlson21f581a2009-08-28 14:00:25 +00008468 kfree(tpr->rx_std_buffers);
8469 tpr->rx_std_buffers = NULL;
8470 kfree(tpr->rx_jmb_buffers);
8471 tpr->rx_jmb_buffers = NULL;
8472 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008473 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8474 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008475 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008476 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008477 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008478 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8479 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008480 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008481 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008482}
8483
Matt Carlson21f581a2009-08-28 14:00:25 +00008484static int tg3_rx_prodring_init(struct tg3 *tp,
8485 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008486{
Matt Carlson2c49a442010-09-30 10:34:35 +00008487 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8488 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008489 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008490 return -ENOMEM;
8491
Matt Carlson4bae65c2010-11-24 08:31:52 +00008492 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8493 TG3_RX_STD_RING_BYTES(tp),
8494 &tpr->rx_std_mapping,
8495 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008496 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008497 goto err_out;
8498
Joe Perches63c3a662011-04-26 08:12:10 +00008499 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008500 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008501 GFP_KERNEL);
8502 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008503 goto err_out;
8504
Matt Carlson4bae65c2010-11-24 08:31:52 +00008505 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8506 TG3_RX_JMB_RING_BYTES(tp),
8507 &tpr->rx_jmb_mapping,
8508 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008509 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008510 goto err_out;
8511 }
8512
8513 return 0;
8514
8515err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008516 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008517 return -ENOMEM;
8518}
8519
8520/* Free up pending packets in all rx/tx rings.
8521 *
8522 * The chip has been shut down and the driver detached from
8523 * the networking, so no interrupts or new tx packets will
8524 * end up in the driver. tp->{tx,}lock is not held and we are not
8525 * in an interrupt context and thus may sleep.
8526 */
8527static void tg3_free_rings(struct tg3 *tp)
8528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008529 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008530
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008531 for (j = 0; j < tp->irq_cnt; j++) {
8532 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008533
Matt Carlson8fea32b2010-09-15 08:59:58 +00008534 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008535
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008536 if (!tnapi->tx_buffers)
8537 continue;
8538
Matt Carlson0d681b22011-07-27 14:20:49 +00008539 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8540 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008541
Matt Carlson0d681b22011-07-27 14:20:49 +00008542 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008543 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008544
Matt Carlsonba1142e2011-11-04 09:15:00 +00008545 tg3_tx_skb_unmap(tnapi, i,
8546 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008547
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07008548 dev_consume_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008549 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008550 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008551 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008552}
8553
8554/* Initialize tx/rx rings for packet processing.
8555 *
8556 * The chip has been shut down and the driver detached from
8557 * the networking, so no interrupts or new tx packets will
8558 * end up in the driver. tp->{tx,}lock are held and thus
8559 * we may not sleep.
8560 */
8561static int tg3_init_rings(struct tg3 *tp)
8562{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008563 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008564
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008565 /* Free up all the SKBs. */
8566 tg3_free_rings(tp);
8567
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008568 for (i = 0; i < tp->irq_cnt; i++) {
8569 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008570
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008571 tnapi->last_tag = 0;
8572 tnapi->last_irq_tag = 0;
8573 tnapi->hw_status->status = 0;
8574 tnapi->hw_status->status_tag = 0;
8575 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8576
8577 tnapi->tx_prod = 0;
8578 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008579 if (tnapi->tx_ring)
8580 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008581
8582 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008583 if (tnapi->rx_rcb)
8584 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008585
Thadeu Lima de Souza Cascardoa620a6b2014-11-25 14:21:11 -02008586 if (tnapi->prodring.rx_std &&
8587 tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008588 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008589 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008590 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008591 }
Matt Carlson72334482009-08-28 14:03:01 +00008592
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008593 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008594}
8595
Michael Chan49a359e2012-09-28 07:12:37 +00008596static void tg3_mem_tx_release(struct tg3 *tp)
8597{
8598 int i;
8599
8600 for (i = 0; i < tp->irq_max; i++) {
8601 struct tg3_napi *tnapi = &tp->napi[i];
8602
8603 if (tnapi->tx_ring) {
8604 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8605 tnapi->tx_ring, tnapi->tx_desc_mapping);
8606 tnapi->tx_ring = NULL;
8607 }
8608
8609 kfree(tnapi->tx_buffers);
8610 tnapi->tx_buffers = NULL;
8611 }
8612}
8613
8614static int tg3_mem_tx_acquire(struct tg3 *tp)
8615{
8616 int i;
8617 struct tg3_napi *tnapi = &tp->napi[0];
8618
8619 /* If multivector TSS is enabled, vector 0 does not handle
8620 * tx interrupts. Don't allocate any resources for it.
8621 */
8622 if (tg3_flag(tp, ENABLE_TSS))
8623 tnapi++;
8624
8625 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8626 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8627 TG3_TX_RING_SIZE, GFP_KERNEL);
8628 if (!tnapi->tx_buffers)
8629 goto err_out;
8630
8631 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8632 TG3_TX_RING_BYTES,
8633 &tnapi->tx_desc_mapping,
8634 GFP_KERNEL);
8635 if (!tnapi->tx_ring)
8636 goto err_out;
8637 }
8638
8639 return 0;
8640
8641err_out:
8642 tg3_mem_tx_release(tp);
8643 return -ENOMEM;
8644}
8645
8646static void tg3_mem_rx_release(struct tg3 *tp)
8647{
8648 int i;
8649
8650 for (i = 0; i < tp->irq_max; i++) {
8651 struct tg3_napi *tnapi = &tp->napi[i];
8652
8653 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8654
8655 if (!tnapi->rx_rcb)
8656 continue;
8657
8658 dma_free_coherent(&tp->pdev->dev,
8659 TG3_RX_RCB_RING_BYTES(tp),
8660 tnapi->rx_rcb,
8661 tnapi->rx_rcb_mapping);
8662 tnapi->rx_rcb = NULL;
8663 }
8664}
8665
8666static int tg3_mem_rx_acquire(struct tg3 *tp)
8667{
8668 unsigned int i, limit;
8669
8670 limit = tp->rxq_cnt;
8671
8672 /* If RSS is enabled, we need a (dummy) producer ring
8673 * set on vector zero. This is the true hw prodring.
8674 */
8675 if (tg3_flag(tp, ENABLE_RSS))
8676 limit++;
8677
8678 for (i = 0; i < limit; i++) {
8679 struct tg3_napi *tnapi = &tp->napi[i];
8680
8681 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8682 goto err_out;
8683
8684 /* If multivector RSS is enabled, vector 0
8685 * does not handle rx or tx interrupts.
8686 * Don't allocate any resources for it.
8687 */
8688 if (!i && tg3_flag(tp, ENABLE_RSS))
8689 continue;
8690
Joe Perchesede23fa82013-08-26 22:45:23 -07008691 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8692 TG3_RX_RCB_RING_BYTES(tp),
8693 &tnapi->rx_rcb_mapping,
8694 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008695 if (!tnapi->rx_rcb)
8696 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008697 }
8698
8699 return 0;
8700
8701err_out:
8702 tg3_mem_rx_release(tp);
8703 return -ENOMEM;
8704}
8705
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008706/*
8707 * Must not be invoked with interrupt sources disabled and
8708 * the hardware shutdown down.
8709 */
8710static void tg3_free_consistent(struct tg3 *tp)
8711{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008712 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008713
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008714 for (i = 0; i < tp->irq_cnt; i++) {
8715 struct tg3_napi *tnapi = &tp->napi[i];
8716
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008717 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008718 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8719 tnapi->hw_status,
8720 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008721 tnapi->hw_status = NULL;
8722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008724
Michael Chan49a359e2012-09-28 07:12:37 +00008725 tg3_mem_rx_release(tp);
8726 tg3_mem_tx_release(tp);
8727
Michael Chanf5992b72017-01-06 16:18:53 -05008728 /* Protect tg3_get_stats64() from reading freed tp->hw_stats. */
8729 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008731 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8732 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 tp->hw_stats = NULL;
8734 }
Michael Chanf5992b72017-01-06 16:18:53 -05008735 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736}
8737
8738/*
8739 * Must not be invoked with interrupt sources disabled and
8740 * the hardware shutdown down. Can sleep.
8741 */
8742static int tg3_alloc_consistent(struct tg3 *tp)
8743{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008744 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008745
Joe Perchesede23fa82013-08-26 22:45:23 -07008746 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8747 sizeof(struct tg3_hw_stats),
8748 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008749 if (!tp->hw_stats)
8750 goto err_out;
8751
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008752 for (i = 0; i < tp->irq_cnt; i++) {
8753 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008754 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008755
Joe Perchesede23fa82013-08-26 22:45:23 -07008756 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8757 TG3_HW_STATUS_SIZE,
8758 &tnapi->status_mapping,
8759 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008760 if (!tnapi->hw_status)
8761 goto err_out;
8762
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008763 sblk = tnapi->hw_status;
8764
Michael Chan49a359e2012-09-28 07:12:37 +00008765 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008766 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008767
Michael Chan49a359e2012-09-28 07:12:37 +00008768 /*
8769 * When RSS is enabled, the status block format changes
8770 * slightly. The "rx_jumbo_consumer", "reserved",
8771 * and "rx_mini_consumer" members get mapped to the
8772 * other three rx return ring producer indexes.
8773 */
8774 switch (i) {
8775 case 1:
8776 prodptr = &sblk->idx[0].rx_producer;
8777 break;
8778 case 2:
8779 prodptr = &sblk->rx_jumbo_consumer;
8780 break;
8781 case 3:
8782 prodptr = &sblk->reserved;
8783 break;
8784 case 4:
8785 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008786 break;
8787 }
Michael Chan49a359e2012-09-28 07:12:37 +00008788 tnapi->rx_rcb_prod_idx = prodptr;
8789 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008790 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008791 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008792 }
8793
Michael Chan49a359e2012-09-28 07:12:37 +00008794 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8795 goto err_out;
8796
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797 return 0;
8798
8799err_out:
8800 tg3_free_consistent(tp);
8801 return -ENOMEM;
8802}
8803
8804#define MAX_WAIT_CNT 1000
8805
8806/* To stop a block, clear the enable bit and poll till it
8807 * clears. tp->lock is held.
8808 */
Joe Perches953c96e2013-04-09 10:18:14 +00008809static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810{
8811 unsigned int i;
8812 u32 val;
8813
Joe Perches63c3a662011-04-26 08:12:10 +00008814 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 switch (ofs) {
8816 case RCVLSC_MODE:
8817 case DMAC_MODE:
8818 case MBFREE_MODE:
8819 case BUFMGR_MODE:
8820 case MEMARB_MODE:
8821 /* We can't enable/disable these bits of the
8822 * 5705/5750, just say success.
8823 */
8824 return 0;
8825
8826 default:
8827 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 }
8830
8831 val = tr32(ofs);
8832 val &= ~enable_bit;
8833 tw32_f(ofs, val);
8834
8835 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008836 if (pci_channel_offline(tp->pdev)) {
8837 dev_err(&tp->pdev->dev,
8838 "tg3_stop_block device offline, "
8839 "ofs=%lx enable_bit=%x\n",
8840 ofs, enable_bit);
8841 return -ENODEV;
8842 }
8843
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 udelay(100);
8845 val = tr32(ofs);
8846 if ((val & enable_bit) == 0)
8847 break;
8848 }
8849
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008850 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008851 dev_err(&tp->pdev->dev,
8852 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8853 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 return -ENODEV;
8855 }
8856
8857 return 0;
8858}
8859
8860/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008861static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862{
8863 int i, err;
8864
8865 tg3_disable_ints(tp);
8866
Gavin Shan6d446ec2013-06-25 15:24:32 +08008867 if (pci_channel_offline(tp->pdev)) {
8868 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8869 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8870 err = -ENODEV;
8871 goto err_no_dev;
8872 }
8873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 tp->rx_mode &= ~RX_MODE_ENABLE;
8875 tw32_f(MAC_RX_MODE, tp->rx_mode);
8876 udelay(10);
8877
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008878 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8879 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8880 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8881 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8882 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8883 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008885 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8886 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8887 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8888 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8889 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8890 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8891 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892
8893 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8894 tw32_f(MAC_MODE, tp->mac_mode);
8895 udelay(40);
8896
8897 tp->tx_mode &= ~TX_MODE_ENABLE;
8898 tw32_f(MAC_TX_MODE, tp->tx_mode);
8899
8900 for (i = 0; i < MAX_WAIT_CNT; i++) {
8901 udelay(100);
8902 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8903 break;
8904 }
8905 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008906 dev_err(&tp->pdev->dev,
8907 "%s timed out, TX_MODE_ENABLE will not clear "
8908 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008909 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910 }
8911
Michael Chane6de8ad2005-05-05 14:42:41 -07008912 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008913 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8914 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
8916 tw32(FTQ_RESET, 0xffffffff);
8917 tw32(FTQ_RESET, 0x00000000);
8918
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008919 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8920 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921
Gavin Shan6d446ec2013-06-25 15:24:32 +08008922err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008923 for (i = 0; i < tp->irq_cnt; i++) {
8924 struct tg3_napi *tnapi = &tp->napi[i];
8925 if (tnapi->hw_status)
8926 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 return err;
8930}
8931
Michael Chanee6a99b2007-07-18 21:49:10 -07008932/* Save PCI command register before chip reset */
8933static void tg3_save_pci_state(struct tg3 *tp)
8934{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008935 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008936}
8937
8938/* Restore PCI state after chip reset */
8939static void tg3_restore_pci_state(struct tg3 *tp)
8940{
8941 u32 val;
8942
8943 /* Re-enable indirect register accesses. */
8944 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8945 tp->misc_host_ctrl);
8946
8947 /* Set MAX PCI retry to zero. */
8948 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008949 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008950 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008951 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008952 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008953 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008954 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008955 PCISTATE_ALLOW_APE_SHMEM_WR |
8956 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008957 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8958
Matt Carlson8a6eac92007-10-21 16:17:55 -07008959 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008960
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008961 if (!tg3_flag(tp, PCI_EXPRESS)) {
8962 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8963 tp->pci_cacheline_sz);
8964 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8965 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008966 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008967
Michael Chanee6a99b2007-07-18 21:49:10 -07008968 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008969 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008970 u16 pcix_cmd;
8971
8972 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8973 &pcix_cmd);
8974 pcix_cmd &= ~PCI_X_CMD_ERO;
8975 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8976 pcix_cmd);
8977 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008978
Joe Perches63c3a662011-04-26 08:12:10 +00008979 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008980
8981 /* Chip reset on 5780 will reset MSI enable bit,
8982 * so need to restore it.
8983 */
Joe Perches63c3a662011-04-26 08:12:10 +00008984 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008985 u16 ctrl;
8986
8987 pci_read_config_word(tp->pdev,
8988 tp->msi_cap + PCI_MSI_FLAGS,
8989 &ctrl);
8990 pci_write_config_word(tp->pdev,
8991 tp->msi_cap + PCI_MSI_FLAGS,
8992 ctrl | PCI_MSI_FLAGS_ENABLE);
8993 val = tr32(MSGINT_MODE);
8994 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8995 }
8996 }
8997}
8998
Nithin Sujirf82995b2014-01-03 10:09:13 -08008999static void tg3_override_clk(struct tg3 *tp)
9000{
9001 u32 val;
9002
9003 switch (tg3_asic_rev(tp)) {
9004 case ASIC_REV_5717:
9005 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9006 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
9007 TG3_CPMU_MAC_ORIDE_ENABLE);
9008 break;
9009
9010 case ASIC_REV_5719:
9011 case ASIC_REV_5720:
9012 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9013 break;
9014
9015 default:
9016 return;
9017 }
9018}
9019
9020static void tg3_restore_clk(struct tg3 *tp)
9021{
9022 u32 val;
9023
9024 switch (tg3_asic_rev(tp)) {
9025 case ASIC_REV_5717:
9026 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9027 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9028 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9029 break;
9030
9031 case ASIC_REV_5719:
9032 case ASIC_REV_5720:
9033 val = tr32(TG3_CPMU_CLCK_ORIDE);
9034 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9035 break;
9036
9037 default:
9038 return;
9039 }
9040}
9041
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042/* tp->lock is held. */
9043static int tg3_chip_reset(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009044 __releases(tp->lock)
9045 __acquires(tp->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046{
9047 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009048 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009049 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009051 if (!pci_device_is_present(tp->pdev))
9052 return -ENODEV;
9053
David S. Millerf49639e2006-06-09 11:58:36 -07009054 tg3_nvram_lock(tp);
9055
Matt Carlson77b483f2008-08-15 14:07:24 -07009056 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9057
David S. Millerf49639e2006-06-09 11:58:36 -07009058 /* No matching tg3_nvram_unlock() after this because
9059 * chip reset below will undo the nvram lock.
9060 */
9061 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062
Michael Chanee6a99b2007-07-18 21:49:10 -07009063 /* GRC_MISC_CFG core clock reset will clear the memory
9064 * enable bit in PCI register 4 and the MSI enable bit
9065 * on some chips, so we save relevant registers here.
9066 */
9067 tg3_save_pci_state(tp);
9068
Joe Perches41535772013-02-16 11:20:04 +00009069 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009070 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009071 tw32(GRC_FASTBOOT_PC, 0);
9072
Linus Torvalds1da177e2005-04-16 15:20:36 -07009073 /*
9074 * We must avoid the readl() that normally takes place.
9075 * It locks machines, causes machine checks, and other
9076 * fun things. So, temporarily disable the 5701
9077 * hardware workaround, while we do the reset.
9078 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009079 write_op = tp->write32;
9080 if (write_op == tg3_write_flush_reg32)
9081 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082
Michael Chand18edcb2007-03-24 20:57:11 -07009083 /* Prevent the irq handler from reading or writing PCI registers
9084 * during chip reset when the memory enable bit in the PCI command
9085 * register may be cleared. The chip does not generate interrupt
9086 * at this time, but the irq handler may still be called due to irq
9087 * sharing or irqpoll.
9088 */
Joe Perches63c3a662011-04-26 08:12:10 +00009089 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009090 for (i = 0; i < tp->irq_cnt; i++) {
9091 struct tg3_napi *tnapi = &tp->napi[i];
9092 if (tnapi->hw_status) {
9093 tnapi->hw_status->status = 0;
9094 tnapi->hw_status->status_tag = 0;
9095 }
9096 tnapi->last_tag = 0;
9097 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009098 }
Michael Chand18edcb2007-03-24 20:57:11 -07009099 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009100
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009101 tg3_full_unlock(tp);
9102
Matt Carlson4f125f42009-09-01 12:55:02 +00009103 for (i = 0; i < tp->irq_cnt; i++)
9104 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009105
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009106 tg3_full_lock(tp, 0);
9107
Joe Perches41535772013-02-16 11:20:04 +00009108 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009109 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9110 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9111 }
9112
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 /* do the reset */
9114 val = GRC_MISC_CFG_CORECLK_RESET;
9115
Joe Perches63c3a662011-04-26 08:12:10 +00009116 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009117 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009118 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009119 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009120 tr32(TG3_PCIE_PHY_TSTCTL) ==
9121 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9122 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9123
Joe Perches41535772013-02-16 11:20:04 +00009124 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 tw32(GRC_MISC_CFG, (1 << 29));
9126 val |= (1 << 29);
9127 }
9128 }
9129
Joe Perches41535772013-02-16 11:20:04 +00009130 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009131 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9132 tw32(GRC_VCPU_EXT_CTRL,
9133 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9134 }
9135
Nithin Sujirf82995b2014-01-03 10:09:13 -08009136 /* Set the clock to the highest frequency to avoid timeouts. With link
9137 * aware mode, the clock speed could be slow and bootcode does not
9138 * complete within the expected time. Override the clock to allow the
9139 * bootcode to finish sooner and then restore it.
9140 */
9141 tg3_override_clk(tp);
9142
Matt Carlsonf37500d2010-08-02 11:25:59 +00009143 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009144 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009146
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147 tw32(GRC_MISC_CFG, val);
9148
Michael Chan1ee582d2005-08-09 20:16:46 -07009149 /* restore 5701 hardware bug workaround write method */
9150 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009151
9152 /* Unfortunately, we have to delay before the PCI read back.
9153 * Some 575X chips even will not respond to a PCI cfg access
9154 * when the reset command is given to the chip.
9155 *
9156 * How do these hardware designers expect things to work
9157 * properly if the PCI write is posted for a long period
9158 * of time? It is always necessary to have some method by
9159 * which a register read back can occur to push the write
9160 * out which does the reset.
9161 *
9162 * For most tg3 variants the trick below was working.
9163 * Ho hum...
9164 */
9165 udelay(120);
9166
9167 /* Flush PCI posted writes. The normal MMIO registers
9168 * are inaccessible at this time so this is the only
9169 * way to make this reliably (actually, this is no longer
9170 * the case, see above). I tried to use indirect
9171 * register read/write but this upset some 5701 variants.
9172 */
9173 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9174
9175 udelay(120);
9176
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009177 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009178 u16 val16;
9179
Joe Perches41535772013-02-16 11:20:04 +00009180 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009181 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 u32 cfg_val;
9183
9184 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009185 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186 udelay(100);
9187
9188 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9189 pci_write_config_dword(tp->pdev, 0xc4,
9190 cfg_val | (1 << 15));
9191 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009192
Matt Carlsone7126992009-08-25 10:08:16 +00009193 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009194 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009195 /*
9196 * Older PCIe devices only support the 128 byte
9197 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009198 */
Joe Perches63c3a662011-04-26 08:12:10 +00009199 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009200 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9201 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009202
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009203 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009204 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009205 PCI_EXP_DEVSTA_CED |
9206 PCI_EXP_DEVSTA_NFED |
9207 PCI_EXP_DEVSTA_FED |
9208 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 }
9210
Michael Chanee6a99b2007-07-18 21:49:10 -07009211 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212
Joe Perches63c3a662011-04-26 08:12:10 +00009213 tg3_flag_clear(tp, CHIP_RESETTING);
9214 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009215
Michael Chanee6a99b2007-07-18 21:49:10 -07009216 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009217 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009218 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009219 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220
Joe Perches41535772013-02-16 11:20:04 +00009221 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 tg3_stop_fw(tp);
9223 tw32(0x5000, 0x400);
9224 }
9225
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009226 if (tg3_flag(tp, IS_SSB_CORE)) {
9227 /*
9228 * BCM4785: In order to avoid repercussions from using
9229 * potentially defective internal ROM, stop the Rx RISC CPU,
9230 * which is not required.
9231 */
9232 tg3_stop_fw(tp);
9233 tg3_halt_cpu(tp, RX_CPU_BASE);
9234 }
9235
Nithin Sujirfb03a432013-05-21 12:57:32 +00009236 err = tg3_poll_fw(tp);
9237 if (err)
9238 return err;
9239
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240 tw32(GRC_MODE, tp->grc_mode);
9241
Joe Perches41535772013-02-16 11:20:04 +00009242 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009243 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244
9245 tw32(0xc4, val | (1 << 15));
9246 }
9247
9248 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009249 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009251 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9253 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9254 }
9255
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009256 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009257 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009258 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009259 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009260 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009261 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009263 val = 0;
9264
9265 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 udelay(40);
9267
Matt Carlson77b483f2008-08-15 14:07:24 -07009268 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9269
Matt Carlson0a9140c2009-08-28 12:27:50 +00009270 tg3_mdio_start(tp);
9271
Joe Perches63c3a662011-04-26 08:12:10 +00009272 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009273 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9274 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009275 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009276 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009277
9278 tw32(0x7c00, val | (1 << 25));
9279 }
9280
Nithin Sujirf82995b2014-01-03 10:09:13 -08009281 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009282
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009284 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009285 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9286 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9287
Joe Perches63c3a662011-04-26 08:12:10 +00009288 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9290 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9291 u32 nic_cfg;
9292
9293 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9294 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009295 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009296 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009297 if (tg3_flag(tp, 5750_PLUS))
9298 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009299
9300 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9301 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9302 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9303 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9304 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 }
9306 }
9307
9308 return 0;
9309}
9310
Matt Carlson65ec6982012-02-28 23:33:37 +00009311static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9312static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009313static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009314
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009316static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317{
9318 int err;
9319
9320 tg3_stop_fw(tp);
9321
Michael Chan944d9802005-05-29 14:57:48 -07009322 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009324 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325 err = tg3_chip_reset(tp);
9326
Joe Perches953c96e2013-04-09 10:18:14 +00009327 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009328
Michael Chan944d9802005-05-29 14:57:48 -07009329 tg3_write_sig_legacy(tp, kind);
9330 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331
Matt Carlson92feeab2011-12-08 14:40:14 +00009332 if (tp->hw_stats) {
9333 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009334 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009335 tg3_get_estats(tp, &tp->estats_prev);
9336
9337 /* And make sure the next sample is new data */
9338 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9339 }
9340
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342}
9343
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344static int tg3_set_mac_addr(struct net_device *dev, void *p)
9345{
9346 struct tg3 *tp = netdev_priv(dev);
9347 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009348 int err = 0;
9349 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350
Michael Chanf9804dd2005-09-27 12:13:10 -07009351 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009352 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009353
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9355
Michael Chane75f7c92006-03-20 21:33:26 -08009356 if (!netif_running(dev))
9357 return 0;
9358
Joe Perches63c3a662011-04-26 08:12:10 +00009359 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009360 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009361
Michael Chan986e0ae2007-05-05 12:10:20 -07009362 addr0_high = tr32(MAC_ADDR_0_HIGH);
9363 addr0_low = tr32(MAC_ADDR_0_LOW);
9364 addr1_high = tr32(MAC_ADDR_1_HIGH);
9365 addr1_low = tr32(MAC_ADDR_1_LOW);
9366
9367 /* Skip MAC addr 1 if ASF is using it. */
9368 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9369 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009370 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009371 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009372 spin_lock_bh(&tp->lock);
9373 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009374 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009375 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
Michael Chanb9ec6c12006-07-25 16:37:27 -07009377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378}
9379
9380/* tp->lock is held. */
9381static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9382 dma_addr_t mapping, u32 maxlen_flags,
9383 u32 nic_addr)
9384{
9385 tg3_write_mem(tp,
9386 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9387 ((u64) mapping >> 32));
9388 tg3_write_mem(tp,
9389 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9390 ((u64) mapping & 0xffffffff));
9391 tg3_write_mem(tp,
9392 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9393 maxlen_flags);
9394
Joe Perches63c3a662011-04-26 08:12:10 +00009395 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 tg3_write_mem(tp,
9397 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9398 nic_addr);
9399}
9400
Michael Chana489b6d2012-09-28 07:12:39 +00009401
9402static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009403{
Michael Chana489b6d2012-09-28 07:12:39 +00009404 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009405
Joe Perches63c3a662011-04-26 08:12:10 +00009406 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009407 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9408 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9409 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009410 } else {
9411 tw32(HOSTCC_TXCOL_TICKS, 0);
9412 tw32(HOSTCC_TXMAX_FRAMES, 0);
9413 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009414
9415 for (; i < tp->txq_cnt; i++) {
9416 u32 reg;
9417
9418 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9419 tw32(reg, ec->tx_coalesce_usecs);
9420 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9421 tw32(reg, ec->tx_max_coalesced_frames);
9422 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9423 tw32(reg, ec->tx_max_coalesced_frames_irq);
9424 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009425 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009426
Michael Chana489b6d2012-09-28 07:12:39 +00009427 for (; i < tp->irq_max - 1; i++) {
9428 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9429 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9430 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9431 }
9432}
9433
9434static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9435{
9436 int i = 0;
9437 u32 limit = tp->rxq_cnt;
9438
Joe Perches63c3a662011-04-26 08:12:10 +00009439 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009440 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9441 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9442 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009443 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009444 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009445 tw32(HOSTCC_RXCOL_TICKS, 0);
9446 tw32(HOSTCC_RXMAX_FRAMES, 0);
9447 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009448 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009449
Michael Chana489b6d2012-09-28 07:12:39 +00009450 for (; i < limit; i++) {
9451 u32 reg;
9452
9453 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9454 tw32(reg, ec->rx_coalesce_usecs);
9455 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9456 tw32(reg, ec->rx_max_coalesced_frames);
9457 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9458 tw32(reg, ec->rx_max_coalesced_frames_irq);
9459 }
9460
9461 for (; i < tp->irq_max - 1; i++) {
9462 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9463 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9464 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9465 }
9466}
9467
9468static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9469{
9470 tg3_coal_tx_init(tp, ec);
9471 tg3_coal_rx_init(tp, ec);
9472
Joe Perches63c3a662011-04-26 08:12:10 +00009473 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009474 u32 val = ec->stats_block_coalesce_usecs;
9475
Matt Carlsonb6080e12009-09-01 13:12:00 +00009476 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9477 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9478
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009479 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009480 val = 0;
9481
9482 tw32(HOSTCC_STAT_COAL_TICKS, val);
9483 }
9484}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485
9486/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009487static void tg3_tx_rcbs_disable(struct tg3 *tp)
9488{
9489 u32 txrcb, limit;
9490
9491 /* Disable all transmit rings but the first. */
9492 if (!tg3_flag(tp, 5705_PLUS))
9493 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9494 else if (tg3_flag(tp, 5717_PLUS))
9495 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9496 else if (tg3_flag(tp, 57765_CLASS) ||
9497 tg3_asic_rev(tp) == ASIC_REV_5762)
9498 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9499 else
9500 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9501
9502 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9503 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9504 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9505 BDINFO_FLAGS_DISABLED);
9506}
9507
9508/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009509static void tg3_tx_rcbs_init(struct tg3 *tp)
9510{
9511 int i = 0;
9512 u32 txrcb = NIC_SRAM_SEND_RCB;
9513
9514 if (tg3_flag(tp, ENABLE_TSS))
9515 i++;
9516
9517 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9518 struct tg3_napi *tnapi = &tp->napi[i];
9519
9520 if (!tnapi->tx_ring)
9521 continue;
9522
9523 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9524 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9525 NIC_SRAM_TX_BUFFER_DESC);
9526 }
9527}
9528
9529/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009530static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9531{
9532 u32 rxrcb, limit;
9533
9534 /* Disable all receive return rings but the first. */
9535 if (tg3_flag(tp, 5717_PLUS))
9536 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9537 else if (!tg3_flag(tp, 5705_PLUS))
9538 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9539 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9540 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9541 tg3_flag(tp, 57765_CLASS))
9542 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9543 else
9544 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9545
9546 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9547 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9548 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9549 BDINFO_FLAGS_DISABLED);
9550}
9551
9552/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009553static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9554{
9555 int i = 0;
9556 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9557
9558 if (tg3_flag(tp, ENABLE_RSS))
9559 i++;
9560
9561 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9562 struct tg3_napi *tnapi = &tp->napi[i];
9563
9564 if (!tnapi->rx_rcb)
9565 continue;
9566
9567 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9568 (tp->rx_ret_ring_mask + 1) <<
9569 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9570 }
9571}
9572
9573/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009574static void tg3_rings_reset(struct tg3 *tp)
9575{
9576 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009577 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009578 struct tg3_napi *tnapi = &tp->napi[0];
9579
Nithin Sujir328947f2013-05-23 11:11:24 +00009580 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009581
Nithin Sujir328947f2013-05-23 11:11:24 +00009582 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009583
9584 /* Disable interrupts */
9585 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009586 tp->napi[0].chk_msi_cnt = 0;
9587 tp->napi[0].last_rx_cons = 0;
9588 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009589
9590 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009591 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009592 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009593 tp->napi[i].tx_prod = 0;
9594 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009595 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009596 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009597 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9598 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009599 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009600 tp->napi[i].last_rx_cons = 0;
9601 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009602 }
Joe Perches63c3a662011-04-26 08:12:10 +00009603 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009604 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009605 } else {
9606 tp->napi[0].tx_prod = 0;
9607 tp->napi[0].tx_cons = 0;
9608 tw32_mailbox(tp->napi[0].prodmbox, 0);
9609 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9610 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009611
9612 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009613 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009614 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9615 for (i = 0; i < 16; i++)
9616 tw32_tx_mbox(mbox + i * 8, 0);
9617 }
9618
Matt Carlson2d31eca2009-09-01 12:53:31 +00009619 /* Clear status block in ram. */
9620 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9621
9622 /* Set status block DMA address */
9623 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9624 ((u64) tnapi->status_mapping >> 32));
9625 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9626 ((u64) tnapi->status_mapping & 0xffffffff));
9627
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009628 stblk = HOSTCC_STATBLCK_RING1;
9629
9630 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9631 u64 mapping = (u64)tnapi->status_mapping;
9632 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9633 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009634 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009635
9636 /* Clear status block in ram. */
9637 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009638 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009639
9640 tg3_tx_rcbs_init(tp);
9641 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009642}
9643
Matt Carlsoneb07a942011-04-20 07:57:36 +00009644static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9645{
9646 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9647
Joe Perches63c3a662011-04-26 08:12:10 +00009648 if (!tg3_flag(tp, 5750_PLUS) ||
9649 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009650 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9651 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009652 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009653 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009654 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9655 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009656 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9657 else
9658 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9659
9660 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9661 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9662
9663 val = min(nic_rep_thresh, host_rep_thresh);
9664 tw32(RCVBDI_STD_THRESH, val);
9665
Joe Perches63c3a662011-04-26 08:12:10 +00009666 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009667 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9668
Joe Perches63c3a662011-04-26 08:12:10 +00009669 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009670 return;
9671
Matt Carlson513aa6e2011-11-21 15:01:18 +00009672 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009673
9674 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9675
9676 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9677 tw32(RCVBDI_JUMBO_THRESH, val);
9678
Joe Perches63c3a662011-04-26 08:12:10 +00009679 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009680 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9681}
9682
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009683static inline u32 calc_crc(unsigned char *buf, int len)
9684{
9685 u32 reg;
9686 u32 tmp;
9687 int j, k;
9688
9689 reg = 0xffffffff;
9690
9691 for (j = 0; j < len; j++) {
9692 reg ^= buf[j];
9693
9694 for (k = 0; k < 8; k++) {
9695 tmp = reg & 0x01;
9696
9697 reg >>= 1;
9698
9699 if (tmp)
9700 reg ^= 0xedb88320;
9701 }
9702 }
9703
9704 return ~reg;
9705}
9706
9707static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9708{
9709 /* accept or reject all multicast frames */
9710 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9711 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9712 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9713 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9714}
9715
9716static void __tg3_set_rx_mode(struct net_device *dev)
9717{
9718 struct tg3 *tp = netdev_priv(dev);
9719 u32 rx_mode;
9720
9721 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9722 RX_MODE_KEEP_VLAN_TAG);
9723
9724#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9725 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9726 * flag clear.
9727 */
9728 if (!tg3_flag(tp, ENABLE_ASF))
9729 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9730#endif
9731
9732 if (dev->flags & IFF_PROMISC) {
9733 /* Promiscuous mode. */
9734 rx_mode |= RX_MODE_PROMISC;
9735 } else if (dev->flags & IFF_ALLMULTI) {
9736 /* Accept all multicast. */
9737 tg3_set_multi(tp, 1);
9738 } else if (netdev_mc_empty(dev)) {
9739 /* Reject all multicast. */
9740 tg3_set_multi(tp, 0);
9741 } else {
9742 /* Accept one or more multicast(s). */
9743 struct netdev_hw_addr *ha;
9744 u32 mc_filter[4] = { 0, };
9745 u32 regidx;
9746 u32 bit;
9747 u32 crc;
9748
9749 netdev_for_each_mc_addr(ha, dev) {
9750 crc = calc_crc(ha->addr, ETH_ALEN);
9751 bit = ~crc & 0x7f;
9752 regidx = (bit & 0x60) >> 5;
9753 bit &= 0x1f;
9754 mc_filter[regidx] |= (1 << bit);
9755 }
9756
9757 tw32(MAC_HASH_REG_0, mc_filter[0]);
9758 tw32(MAC_HASH_REG_1, mc_filter[1]);
9759 tw32(MAC_HASH_REG_2, mc_filter[2]);
9760 tw32(MAC_HASH_REG_3, mc_filter[3]);
9761 }
9762
Michael Chane565eec2014-01-03 10:09:12 -08009763 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9764 rx_mode |= RX_MODE_PROMISC;
9765 } else if (!(dev->flags & IFF_PROMISC)) {
9766 /* Add all entries into to the mac addr filter list */
9767 int i = 0;
9768 struct netdev_hw_addr *ha;
9769
9770 netdev_for_each_uc_addr(ha, dev) {
9771 __tg3_set_one_mac_addr(tp, ha->addr,
9772 i + TG3_UCAST_ADDR_IDX(tp));
9773 i++;
9774 }
9775 }
9776
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009777 if (rx_mode != tp->rx_mode) {
9778 tp->rx_mode = rx_mode;
9779 tw32_f(MAC_RX_MODE, rx_mode);
9780 udelay(10);
9781 }
9782}
9783
Michael Chan91024262012-09-28 07:12:38 +00009784static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009785{
9786 int i;
9787
9788 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009789 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009790}
9791
9792static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009793{
9794 int i;
9795
9796 if (!tg3_flag(tp, SUPPORT_MSIX))
9797 return;
9798
Michael Chan0b3ba052012-11-14 14:44:29 +00009799 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009800 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009801 return;
9802 }
9803
9804 /* Validate table against current IRQ count */
9805 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009806 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009807 break;
9808 }
9809
9810 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009811 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009812}
9813
Matt Carlson90415472011-12-16 13:33:23 +00009814static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009815{
9816 int i = 0;
9817 u32 reg = MAC_RSS_INDIR_TBL_0;
9818
9819 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9820 u32 val = tp->rss_ind_tbl[i];
9821 i++;
9822 for (; i % 8; i++) {
9823 val <<= 4;
9824 val |= tp->rss_ind_tbl[i];
9825 }
9826 tw32(reg, val);
9827 reg += 4;
9828 }
9829}
9830
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009831static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9832{
9833 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9834 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9835 else
9836 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9837}
9838
Matt Carlson2d31eca2009-09-01 12:53:31 +00009839/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009840static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841{
9842 u32 val, rdmac_mode;
9843 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009844 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845
9846 tg3_disable_ints(tp);
9847
9848 tg3_stop_fw(tp);
9849
9850 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9851
Joe Perches63c3a662011-04-26 08:12:10 +00009852 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009853 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009855 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9856 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9857 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009858 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009859 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9860 }
9861
Nithin Sujir400dfba2013-05-18 06:26:53 +00009862 /* Enable MAC control of LPI */
9863 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9864 tg3_setup_eee(tp);
9865
Matt Carlson603f1172010-02-12 14:47:10 +00009866 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009867 tg3_phy_reset(tp);
9868
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 err = tg3_chip_reset(tp);
9870 if (err)
9871 return err;
9872
9873 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9874
Joe Perches41535772013-02-16 11:20:04 +00009875 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009876 val = tr32(TG3_CPMU_CTRL);
9877 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9878 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009879
9880 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9881 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9882 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9883 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9884
9885 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9886 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9887 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9888 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9889
9890 val = tr32(TG3_CPMU_HST_ACC);
9891 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9892 val |= CPMU_HST_ACC_MACCLK_6_25;
9893 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009894 }
9895
Joe Perches41535772013-02-16 11:20:04 +00009896 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009897 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9898 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9899 PCIE_PWR_MGMT_L1_THRESH_4MS;
9900 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009901
9902 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9903 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9904
9905 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009906
Matt Carlsonf40386c2009-11-02 14:24:02 +00009907 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9908 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009909 }
9910
Joe Perches63c3a662011-04-26 08:12:10 +00009911 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009912 u32 grc_mode = tr32(GRC_MODE);
9913
9914 /* Access the lower 1K of PL PCIE block registers. */
9915 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9916 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9917
9918 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9919 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9920 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9921
9922 tw32(GRC_MODE, grc_mode);
9923 }
9924
Matt Carlson55086ad2011-12-14 11:09:59 +00009925 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009926 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009927 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009928
Matt Carlson5093eed2010-11-24 08:31:45 +00009929 /* Access the lower 1K of PL PCIE block registers. */
9930 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9931 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009932
Matt Carlson5093eed2010-11-24 08:31:45 +00009933 val = tr32(TG3_PCIE_TLDLPL_PORT +
9934 TG3_PCIE_PL_LO_PHYCTL5);
9935 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9936 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009937
Matt Carlson5093eed2010-11-24 08:31:45 +00009938 tw32(GRC_MODE, grc_mode);
9939 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009940
Joe Perches41535772013-02-16 11:20:04 +00009941 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009942 u32 grc_mode;
9943
9944 /* Fix transmit hangs */
9945 val = tr32(TG3_CPMU_PADRNG_CTL);
9946 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9947 tw32(TG3_CPMU_PADRNG_CTL, val);
9948
9949 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009950
9951 /* Access the lower 1K of DL PCIE block registers. */
9952 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9953 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9954
9955 val = tr32(TG3_PCIE_TLDLPL_PORT +
9956 TG3_PCIE_DL_LO_FTSMAX);
9957 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9958 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9959 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9960
9961 tw32(GRC_MODE, grc_mode);
9962 }
9963
Matt Carlsona977dbe2010-04-12 06:58:26 +00009964 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9965 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9966 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9967 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009968 }
9969
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970 /* This works around an issue with Athlon chipsets on
9971 * B3 tigon3 silicon. This bit has no effect on any
9972 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009973 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 */
Joe Perches63c3a662011-04-26 08:12:10 +00009975 if (!tg3_flag(tp, CPMU_PRESENT)) {
9976 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009977 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9978 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980
Joe Perches41535772013-02-16 11:20:04 +00009981 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009982 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 val = tr32(TG3PCI_PCISTATE);
9984 val |= PCISTATE_RETRY_SAME_DMA;
9985 tw32(TG3PCI_PCISTATE, val);
9986 }
9987
Joe Perches63c3a662011-04-26 08:12:10 +00009988 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009989 /* Allow reads and writes to the
9990 * APE register and memory space.
9991 */
9992 val = tr32(TG3PCI_PCISTATE);
9993 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009994 PCISTATE_ALLOW_APE_SHMEM_WR |
9995 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009996 tw32(TG3PCI_PCISTATE, val);
9997 }
9998
Joe Perches41535772013-02-16 11:20:04 +00009999 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 /* Enable some hw fixes. */
10001 val = tr32(TG3PCI_MSI_DATA);
10002 val |= (1 << 26) | (1 << 28) | (1 << 29);
10003 tw32(TG3PCI_MSI_DATA, val);
10004 }
10005
10006 /* Descriptor ring init may make accesses to the
10007 * NIC SRAM area to setup the TX descriptors, so we
10008 * can only do this after the hardware has been
10009 * successfully reset.
10010 */
Michael Chan32d8c572006-07-25 16:38:29 -070010011 err = tg3_init_rings(tp);
10012 if (err)
10013 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014
Joe Perches63c3a662011-04-26 08:12:10 +000010015 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010016 val = tr32(TG3PCI_DMA_RW_CTRL) &
10017 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +000010018 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +000010019 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +000010020 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +000010021 tg3_asic_rev(tp) != ASIC_REV_5717 &&
10022 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +000010023 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010024 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +000010025 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
10026 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -070010027 /* This value is determined during the probe time DMA
10028 * engine test, tg3_test_dma.
10029 */
10030 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032
10033 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10034 GRC_MODE_4X_NIC_SEND_RINGS |
10035 GRC_MODE_NO_TX_PHDR_CSUM |
10036 GRC_MODE_NO_RX_PHDR_CSUM);
10037 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010038
10039 /* Pseudo-header checksum is done by hardware logic and not
10040 * the offload processers, so make the chip do the pseudo-
10041 * header checksums on receive. For transmit it is more
10042 * convenient to do the pseudo-header checksum in software
10043 * as Linux does that on transmit for us in all cases.
10044 */
10045 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010047 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10048 if (tp->rxptpctl)
10049 tw32(TG3_RX_PTP_CTL,
10050 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10051
10052 if (tg3_flag(tp, PTP_CAPABLE))
10053 val |= GRC_MODE_TIME_SYNC_ENABLE;
10054
10055 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056
Siva Reddy Kallam4419bb12017-12-22 16:05:28 +053010057 /* On one of the AMD platform, MRRS is restricted to 4000 because of
10058 * south bridge limitation. As a workaround, Driver is setting MRRS
10059 * to 2048 instead of default 4096.
10060 */
10061 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
10062 tp->pdev->subsystem_device == TG3PCI_SUBDEVICE_ID_DELL_5762) {
10063 val = tr32(TG3PCI_DEV_STATUS_CTRL) & ~MAX_READ_REQ_MASK;
10064 tw32(TG3PCI_DEV_STATUS_CTRL, val | MAX_READ_REQ_SIZE_2048);
10065 }
10066
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10068 val = tr32(GRC_MISC_CFG);
10069 val &= ~0xff;
10070 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10071 tw32(GRC_MISC_CFG, val);
10072
10073 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010074 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010076 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010078 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10080 else
10081 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10082 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10083 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010084 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010085 int fw_len;
10086
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010087 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10089 tw32(BUFMGR_MB_POOL_ADDR,
10090 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10091 tw32(BUFMGR_MB_POOL_SIZE,
10092 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094
Michael Chan0f893dc2005-07-25 12:30:38 -070010095 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10097 tp->bufmgr_config.mbuf_read_dma_low_water);
10098 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10099 tp->bufmgr_config.mbuf_mac_rx_low_water);
10100 tw32(BUFMGR_MB_HIGH_WATER,
10101 tp->bufmgr_config.mbuf_high_water);
10102 } else {
10103 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10104 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10105 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10106 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10107 tw32(BUFMGR_MB_HIGH_WATER,
10108 tp->bufmgr_config.mbuf_high_water_jumbo);
10109 }
10110 tw32(BUFMGR_DMA_LOW_WATER,
10111 tp->bufmgr_config.dma_low_water);
10112 tw32(BUFMGR_DMA_HIGH_WATER,
10113 tp->bufmgr_config.dma_high_water);
10114
Matt Carlsond309a462010-09-30 10:34:31 +000010115 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010116 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010117 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010118 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010119 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010120 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10121 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010122 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010123 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124 for (i = 0; i < 2000; i++) {
10125 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10126 break;
10127 udelay(10);
10128 }
10129 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010130 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131 return -ENODEV;
10132 }
10133
Joe Perches41535772013-02-16 11:20:04 +000010134 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010135 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010136
Matt Carlsoneb07a942011-04-20 07:57:36 +000010137 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138
10139 /* Initialize TG3_BDINFO's at:
10140 * RCVDBDI_STD_BD: standard eth size rx ring
10141 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10142 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10143 *
10144 * like so:
10145 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10146 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10147 * ring attribute flags
10148 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10149 *
10150 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10151 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10152 *
10153 * The size of each ring is fixed in the firmware, but the location is
10154 * configurable.
10155 */
10156 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010157 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010159 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010160 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010161 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10162 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010164 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010165 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10167 BDINFO_FLAGS_DISABLED);
10168
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010169 /* Program the jumbo buffer descriptor ring control
10170 * blocks on those devices that have them.
10171 */
Joe Perches41535772013-02-16 11:20:04 +000010172 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010173 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174
Joe Perches63c3a662011-04-26 08:12:10 +000010175 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010177 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010179 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010180 val = TG3_RX_JMB_RING_SIZE(tp) <<
10181 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010183 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010184 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010185 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010186 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010187 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10188 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 } else {
10190 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10191 BDINFO_FLAGS_DISABLED);
10192 }
10193
Joe Perches63c3a662011-04-26 08:12:10 +000010194 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010195 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010196 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10197 val |= (TG3_RX_STD_DMA_SZ << 2);
10198 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010199 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010200 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010201 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010202
10203 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204
Matt Carlson411da642009-11-13 13:03:46 +000010205 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010206 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207
Joe Perches63c3a662011-04-26 08:12:10 +000010208 tpr->rx_jmb_prod_idx =
10209 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010210 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211
Matt Carlson2d31eca2009-09-01 12:53:31 +000010212 tg3_rings_reset(tp);
10213
Linus Torvalds1da177e2005-04-16 15:20:36 -070010214 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010215 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010216
10217 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010218 tw32(MAC_RX_MTU_SIZE,
10219 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
10221 /* The slot time is changed by tg3_setup_phy if we
10222 * run at gigabit with half duplex.
10223 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010224 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10225 (6 << TX_LENGTHS_IPG_SHIFT) |
10226 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10227
Joe Perches41535772013-02-16 11:20:04 +000010228 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10229 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010230 val |= tr32(MAC_TX_LENGTHS) &
10231 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10232 TX_LENGTHS_CNT_DWN_VAL_MSK);
10233
10234 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235
10236 /* Receive rules. */
10237 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10238 tw32(RCVLPC_CONFIG, 0x0181);
10239
10240 /* Calculate RDMAC_MODE setting early, we need it to determine
10241 * the RCVLPC_STATE_ENABLE mask.
10242 */
10243 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10244 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10245 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10246 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10247 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010248
Joe Perches41535772013-02-16 11:20:04 +000010249 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010250 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10251
Joe Perches41535772013-02-16 11:20:04 +000010252 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10253 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10254 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010255 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10256 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10257 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10258
Joe Perches41535772013-02-16 11:20:04 +000010259 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10260 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010261 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010262 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10264 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010265 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010266 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10267 }
10268 }
10269
Joe Perches63c3a662011-04-26 08:12:10 +000010270 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010271 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10272
Joe Perches41535772013-02-16 11:20:04 +000010273 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010274 tp->dma_limit = 0;
10275 if (tp->dev->mtu <= ETH_DATA_LEN) {
10276 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10277 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10278 }
10279 }
10280
Joe Perches63c3a662011-04-26 08:12:10 +000010281 if (tg3_flag(tp, HW_TSO_1) ||
10282 tg3_flag(tp, HW_TSO_2) ||
10283 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010284 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10285
Matt Carlson108a6c12011-05-19 12:12:47 +000010286 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010287 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10288 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010289 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290
Joe Perches41535772013-02-16 11:20:04 +000010291 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10292 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010293 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10294
Joe Perches41535772013-02-16 11:20:04 +000010295 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10296 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10297 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10298 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010299 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010300 u32 tgtreg;
10301
Joe Perches41535772013-02-16 11:20:04 +000010302 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010303 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10304 else
10305 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10306
10307 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010308 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10309 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010310 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10311 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10312 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10313 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10314 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10315 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010316 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010317 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010318 }
10319
Joe Perches41535772013-02-16 11:20:04 +000010320 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10321 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10322 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010323 u32 tgtreg;
10324
Joe Perches41535772013-02-16 11:20:04 +000010325 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010326 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10327 else
10328 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10329
10330 val = tr32(tgtreg);
10331 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010332 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10333 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10334 }
10335
Linus Torvalds1da177e2005-04-16 15:20:36 -070010336 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010337 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010338 val = tr32(RCVLPC_STATS_ENABLE);
10339 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10340 tw32(RCVLPC_STATS_ENABLE, val);
10341 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010342 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010343 val = tr32(RCVLPC_STATS_ENABLE);
10344 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10345 tw32(RCVLPC_STATS_ENABLE, val);
10346 } else {
10347 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10348 }
10349 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10350 tw32(SNDDATAI_STATSENAB, 0xffffff);
10351 tw32(SNDDATAI_STATSCTRL,
10352 (SNDDATAI_SCTRL_ENABLE |
10353 SNDDATAI_SCTRL_FASTUPD));
10354
10355 /* Setup host coalescing engine. */
10356 tw32(HOSTCC_MODE, 0);
10357 for (i = 0; i < 2000; i++) {
10358 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10359 break;
10360 udelay(10);
10361 }
10362
Michael Chand244c892005-07-05 14:42:33 -070010363 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010364
Joe Perches63c3a662011-04-26 08:12:10 +000010365 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 /* Status/statistics block address. See tg3_timer,
10367 * the tg3_periodic_fetch_stats call there, and
10368 * tg3_get_stats to see how this works for 5705/5750 chips.
10369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10371 ((u64) tp->stats_mapping >> 32));
10372 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10373 ((u64) tp->stats_mapping & 0xffffffff));
10374 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010375
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010377
10378 /* Clear statistics and status block memory areas */
10379 for (i = NIC_SRAM_STATS_BLK;
10380 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10381 i += sizeof(u32)) {
10382 tg3_write_mem(tp, i, 0);
10383 udelay(40);
10384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010385 }
10386
10387 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10388
10389 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10390 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010391 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10393
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010394 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10395 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010396 /* reset to prevent losing 1st rx packet intermittently */
10397 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10398 udelay(10);
10399 }
10400
Matt Carlson3bda1252008-08-15 14:08:22 -070010401 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010402 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10403 MAC_MODE_FHDE_ENABLE;
10404 if (tg3_flag(tp, ENABLE_APE))
10405 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010406 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010407 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010408 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010409 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10411 udelay(40);
10412
Michael Chan314fba32005-04-21 17:07:04 -070010413 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010414 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010415 * register to preserve the GPIO settings for LOMs. The GPIOs,
10416 * whether used as inputs or outputs, are set by boot code after
10417 * reset.
10418 */
Joe Perches63c3a662011-04-26 08:12:10 +000010419 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010420 u32 gpio_mask;
10421
Michael Chan9d26e212006-12-07 00:21:14 -080010422 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10423 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10424 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010425
Joe Perches41535772013-02-16 11:20:04 +000010426 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010427 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10428 GRC_LCLCTRL_GPIO_OUTPUT3;
10429
Joe Perches41535772013-02-16 11:20:04 +000010430 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010431 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10432
Gary Zambranoaaf84462007-05-05 11:51:45 -070010433 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010434 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10435
10436 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010437 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010438 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10439 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10442 udelay(100);
10443
Matt Carlsonc3b50032012-01-17 15:27:23 +000010444 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010445 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010446 val |= MSGINT_MODE_ENABLE;
10447 if (tp->irq_cnt > 1)
10448 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010449 if (!tg3_flag(tp, 1SHOT_MSI))
10450 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010451 tw32(MSGINT_MODE, val);
10452 }
10453
Joe Perches63c3a662011-04-26 08:12:10 +000010454 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10456 udelay(40);
10457 }
10458
10459 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10460 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10461 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10462 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10463 WDMAC_MODE_LNGREAD_ENAB);
10464
Joe Perches41535772013-02-16 11:20:04 +000010465 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10466 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010467 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010468 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10469 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010470 /* nothing */
10471 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010472 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 val |= WDMAC_MODE_RX_ACCEL;
10474 }
10475 }
10476
Michael Chand9ab5ad12006-03-20 22:27:35 -080010477 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010478 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010479 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010480
Joe Perches41535772013-02-16 11:20:04 +000010481 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010482 val |= WDMAC_MODE_BURST_ALL_DATA;
10483
Linus Torvalds1da177e2005-04-16 15:20:36 -070010484 tw32_f(WDMAC_MODE, val);
10485 udelay(40);
10486
Joe Perches63c3a662011-04-26 08:12:10 +000010487 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010488 u16 pcix_cmd;
10489
10490 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10491 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010492 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010493 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10494 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010495 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010496 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10497 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010498 }
Matt Carlson9974a352007-10-07 23:27:28 -070010499 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10500 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010501 }
10502
10503 tw32_f(RDMAC_MODE, rdmac_mode);
10504 udelay(40);
10505
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010506 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10507 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010508 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10509 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10510 break;
10511 }
10512 if (i < TG3_NUM_RDMA_CHANNELS) {
10513 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010514 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010515 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010516 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010517 }
10518 }
10519
Linus Torvalds1da177e2005-04-16 15:20:36 -070010520 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010521 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010522 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010523
Joe Perches41535772013-02-16 11:20:04 +000010524 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010525 tw32(SNDDATAC_MODE,
10526 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10527 else
10528 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10529
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10531 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010532 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010533 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010534 val |= RCVDBDI_MODE_LRG_RING_SZ;
10535 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010537 if (tg3_flag(tp, HW_TSO_1) ||
10538 tg3_flag(tp, HW_TSO_2) ||
10539 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010540 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010541 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010542 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010543 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10544 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10546
Joe Perches41535772013-02-16 11:20:04 +000010547 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548 err = tg3_load_5701_a0_firmware_fix(tp);
10549 if (err)
10550 return err;
10551 }
10552
Nithin Sujirc4dab502013-03-06 17:02:34 +000010553 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10554 /* Ignore any errors for the firmware download. If download
10555 * fails, the device will operate with EEE disabled
10556 */
10557 tg3_load_57766_firmware(tp);
10558 }
10559
Joe Perches63c3a662011-04-26 08:12:10 +000010560 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561 err = tg3_load_tso_firmware(tp);
10562 if (err)
10563 return err;
10564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565
10566 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010567
Joe Perches63c3a662011-04-26 08:12:10 +000010568 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010569 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010570 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010571
Joe Perches41535772013-02-16 11:20:04 +000010572 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10573 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010574 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10575 tp->tx_mode &= ~val;
10576 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10577 }
10578
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579 tw32_f(MAC_TX_MODE, tp->tx_mode);
10580 udelay(100);
10581
Joe Perches63c3a662011-04-26 08:12:10 +000010582 if (tg3_flag(tp, ENABLE_RSS)) {
Eric Dumazet39648352014-11-16 06:23:08 -080010583 u32 rss_key[10];
10584
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010585 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010586
Eric Dumazet39648352014-11-16 06:23:08 -080010587 netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
10588
10589 for (i = 0; i < 10 ; i++)
10590 tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010591 }
10592
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010594 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010595 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10596
Nithin Sujir378b72c2013-07-29 13:58:39 -070010597 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10598 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10599
Joe Perches63c3a662011-04-26 08:12:10 +000010600 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010601 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10602 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10603 RX_MODE_RSS_IPV6_HASH_EN |
10604 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10605 RX_MODE_RSS_IPV4_HASH_EN |
10606 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10607
Linus Torvalds1da177e2005-04-16 15:20:36 -070010608 tw32_f(MAC_RX_MODE, tp->rx_mode);
10609 udelay(10);
10610
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611 tw32(MAC_LED_CTRL, tp->led_ctrl);
10612
10613 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010614 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10616 udelay(10);
10617 }
10618 tw32_f(MAC_RX_MODE, tp->rx_mode);
10619 udelay(10);
10620
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010621 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010622 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10623 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624 /* Set drive transmission level to 1.2V */
10625 /* only if the signal pre-emphasis bit is not set */
10626 val = tr32(MAC_SERDES_CFG);
10627 val &= 0xfffff000;
10628 val |= 0x880;
10629 tw32(MAC_SERDES_CFG, val);
10630 }
Joe Perches41535772013-02-16 11:20:04 +000010631 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010632 tw32(MAC_SERDES_CFG, 0x616000);
10633 }
10634
10635 /* Prevent chip from dropping frames when flow control
10636 * is enabled.
10637 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010638 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010639 val = 1;
10640 else
10641 val = 2;
10642 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010643
Joe Perches41535772013-02-16 11:20:04 +000010644 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010645 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010647 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010648 }
10649
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010650 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010651 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010652 u32 tmp;
10653
10654 tmp = tr32(SERDES_RX_CTRL);
10655 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10656 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10657 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10658 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10659 }
10660
Joe Perches63c3a662011-04-26 08:12:10 +000010661 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010662 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010663 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664
Joe Perches953c96e2013-04-09 10:18:14 +000010665 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010666 if (err)
10667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010668
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010669 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10670 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010671 u32 tmp;
10672
10673 /* Clear CRC stats. */
10674 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10675 tg3_writephy(tp, MII_TG3_TEST1,
10676 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010677 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679 }
10680 }
10681
10682 __tg3_set_rx_mode(tp->dev);
10683
10684 /* Initialize receive rules. */
10685 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10686 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10687 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10688 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10689
Joe Perches63c3a662011-04-26 08:12:10 +000010690 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691 limit = 8;
10692 else
10693 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010694 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010695 limit -= 4;
10696 switch (limit) {
10697 case 16:
10698 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10699 case 15:
10700 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10701 case 14:
10702 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10703 case 13:
10704 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10705 case 12:
10706 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10707 case 11:
10708 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10709 case 10:
10710 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10711 case 9:
10712 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10713 case 8:
10714 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10715 case 7:
10716 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10717 case 6:
10718 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10719 case 5:
10720 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10721 case 4:
10722 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10723 case 3:
10724 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10725 case 2:
10726 case 1:
10727
10728 default:
10729 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
Joe Perches63c3a662011-04-26 08:12:10 +000010732 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010733 /* Write our heartbeat update interval to APE. */
10734 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10735 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010736
Linus Torvalds1da177e2005-04-16 15:20:36 -070010737 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10738
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739 return 0;
10740}
10741
10742/* Called at device open time to get the chip ready for
10743 * packet processing. Invoked with tp->lock held.
10744 */
Joe Perches953c96e2013-04-09 10:18:14 +000010745static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010747 /* Chip may have been just powered on. If so, the boot code may still
10748 * be running initialization. Wait for it to finish to avoid races in
10749 * accessing the hardware.
10750 */
10751 tg3_enable_register_access(tp);
10752 tg3_poll_fw(tp);
10753
Linus Torvalds1da177e2005-04-16 15:20:36 -070010754 tg3_switch_clocks(tp);
10755
10756 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10757
Matt Carlson2f751b62008-08-04 23:17:34 -070010758 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759}
10760
Florian Fainelli038e8932017-03-06 12:56:02 -080010761#ifdef CONFIG_TIGON3_HWMON
Michael Chanaed93e02012-07-16 16:24:02 +000010762static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10763{
10764 int i;
10765
10766 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10767 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10768
10769 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10770 off += len;
10771
10772 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10773 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10774 memset(ocir, 0, TG3_OCIR_LEN);
10775 }
10776}
10777
10778/* sysfs attributes for hwmon */
10779static ssize_t tg3_show_temp(struct device *dev,
10780 struct device_attribute *devattr, char *buf)
10781{
Michael Chanaed93e02012-07-16 16:24:02 +000010782 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010783 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010784 u32 temperature;
10785
10786 spin_lock_bh(&tp->lock);
10787 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10788 sizeof(temperature));
10789 spin_unlock_bh(&tp->lock);
Jean Delvared3d11fe2015-09-01 18:07:41 +020010790 return sprintf(buf, "%u\n", temperature * 1000);
Michael Chanaed93e02012-07-16 16:24:02 +000010791}
10792
10793
10794static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10795 TG3_TEMP_SENSOR_OFFSET);
10796static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10797 TG3_TEMP_CAUTION_OFFSET);
10798static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10799 TG3_TEMP_MAX_OFFSET);
10800
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010801static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010802 &sensor_dev_attr_temp1_input.dev_attr.attr,
10803 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10804 &sensor_dev_attr_temp1_max.dev_attr.attr,
10805 NULL
10806};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010807ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010808
Michael Chanaed93e02012-07-16 16:24:02 +000010809static void tg3_hwmon_close(struct tg3 *tp)
10810{
Michael Chanaed93e02012-07-16 16:24:02 +000010811 if (tp->hwmon_dev) {
10812 hwmon_device_unregister(tp->hwmon_dev);
10813 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010814 }
Michael Chanaed93e02012-07-16 16:24:02 +000010815}
10816
10817static void tg3_hwmon_open(struct tg3 *tp)
10818{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010819 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010820 u32 size = 0;
10821 struct pci_dev *pdev = tp->pdev;
10822 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10823
10824 tg3_sd_scan_scratchpad(tp, ocirs);
10825
10826 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10827 if (!ocirs[i].src_data_length)
10828 continue;
10829
10830 size += ocirs[i].src_hdr_length;
10831 size += ocirs[i].src_data_length;
10832 }
10833
10834 if (!size)
10835 return;
10836
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010837 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10838 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010839 if (IS_ERR(tp->hwmon_dev)) {
10840 tp->hwmon_dev = NULL;
10841 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010842 }
Michael Chanaed93e02012-07-16 16:24:02 +000010843}
Florian Fainelli038e8932017-03-06 12:56:02 -080010844#else
10845static inline void tg3_hwmon_close(struct tg3 *tp) { }
10846static inline void tg3_hwmon_open(struct tg3 *tp) { }
10847#endif /* CONFIG_TIGON3_HWMON */
Michael Chanaed93e02012-07-16 16:24:02 +000010848
10849
Linus Torvalds1da177e2005-04-16 15:20:36 -070010850#define TG3_STAT_ADD32(PSTAT, REG) \
10851do { u32 __val = tr32(REG); \
10852 (PSTAT)->low += __val; \
10853 if ((PSTAT)->low < __val) \
10854 (PSTAT)->high += 1; \
10855} while (0)
10856
10857static void tg3_periodic_fetch_stats(struct tg3 *tp)
10858{
10859 struct tg3_hw_stats *sp = tp->hw_stats;
10860
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010861 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010862 return;
10863
10864 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10865 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10866 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10867 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10868 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10869 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10870 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10871 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10872 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10873 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10874 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10875 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10876 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010877 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010878 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10879 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10880 u32 val;
10881
10882 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010883 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010884 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010885 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010887
10888 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10889 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10890 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10891 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10892 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10893 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10894 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10895 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10896 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10897 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10898 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10899 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10900 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10901 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010902
10903 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010904 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010905 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010906 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10907 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010908 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10909 } else {
10910 u32 val = tr32(HOSTCC_FLOW_ATTN);
10911 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10912 if (val) {
10913 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10914 sp->rx_discards.low += val;
10915 if (sp->rx_discards.low < val)
10916 sp->rx_discards.high += 1;
10917 }
10918 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10919 }
Michael Chan463d3052006-05-22 16:36:27 -070010920 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921}
10922
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010923static void tg3_chk_missed_msi(struct tg3 *tp)
10924{
10925 u32 i;
10926
10927 for (i = 0; i < tp->irq_cnt; i++) {
10928 struct tg3_napi *tnapi = &tp->napi[i];
10929
10930 if (tg3_has_work(tnapi)) {
10931 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10932 tnapi->last_tx_cons == tnapi->tx_cons) {
10933 if (tnapi->chk_msi_cnt < 1) {
10934 tnapi->chk_msi_cnt++;
10935 return;
10936 }
Matt Carlson7f230732011-08-31 11:44:48 +000010937 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010938 }
10939 }
10940 tnapi->chk_msi_cnt = 0;
10941 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10942 tnapi->last_tx_cons = tnapi->tx_cons;
10943 }
10944}
10945
Kees Cooke99e88a2017-10-16 14:43:17 -070010946static void tg3_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010947{
Kees Cooke99e88a2017-10-16 14:43:17 -070010948 struct tg3 *tp = from_timer(tp, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010949
David S. Millerf47c11e2005-06-24 20:18:35 -070010950 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010951
Prashant Sreedharan4fd190a2015-01-14 11:33:49 -080010952 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
10953 spin_unlock(&tp->lock);
10954 goto restart_timer;
10955 }
10956
Joe Perches41535772013-02-16 11:20:04 +000010957 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010958 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010959 tg3_chk_missed_msi(tp);
10960
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010961 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10962 /* BCM4785: Flush posted writes from GbE to host memory. */
10963 tr32(HOSTCC_MODE);
10964 }
10965
Joe Perches63c3a662011-04-26 08:12:10 +000010966 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010967 /* All of this garbage is because when using non-tagged
10968 * IRQ status the mailbox/status_block protocol the chip
10969 * uses with the cpu is race prone.
10970 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010971 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010972 tw32(GRC_LOCAL_CTRL,
10973 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10974 } else {
10975 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010976 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978
David S. Millerfac9b832005-05-18 22:46:34 -070010979 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010980 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010981 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010982 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010984 }
10985
Linus Torvalds1da177e2005-04-16 15:20:36 -070010986 /* This part only runs once per second. */
10987 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010988 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010989 tg3_periodic_fetch_stats(tp);
10990
Matt Carlsonb0c59432011-05-19 12:12:48 +000010991 if (tp->setlpicnt && !--tp->setlpicnt)
10992 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010993
Joe Perches63c3a662011-04-26 08:12:10 +000010994 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995 u32 mac_stat;
10996 int phy_event;
10997
10998 mac_stat = tr32(MAC_STATUS);
10999
11000 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011001 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011002 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
11003 phy_event = 1;
11004 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
11005 phy_event = 1;
11006
11007 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000011008 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000011009 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011010 u32 mac_stat = tr32(MAC_STATUS);
11011 int need_setup = 0;
11012
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011013 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011014 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
11015 need_setup = 1;
11016 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011017 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011018 (mac_stat & (MAC_STATUS_PCS_SYNCED |
11019 MAC_STATUS_SIGNAL_DET))) {
11020 need_setup = 1;
11021 }
11022 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070011023 if (!tp->serdes_counter) {
11024 tw32_f(MAC_MODE,
11025 (tp->mac_mode &
11026 ~MAC_MODE_PORT_MODE_MASK));
11027 udelay(40);
11028 tw32_f(MAC_MODE, tp->mac_mode);
11029 udelay(40);
11030 }
Joe Perches953c96e2013-04-09 10:18:14 +000011031 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011032 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011033 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011034 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070011035 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080011036 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
11037 u32 cpmu = tr32(TG3_CPMU_STATUS);
11038 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
11039 TG3_CPMU_STATUS_LINK_MASK);
11040
11041 if (link_up != tp->link_up)
11042 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011044
11045 tp->timer_counter = tp->timer_multiplier;
11046 }
11047
Michael Chan130b8e42006-09-27 16:00:40 -070011048 /* Heartbeat is only sent once every 2 seconds.
11049 *
11050 * The heartbeat is to tell the ASF firmware that the host
11051 * driver is still alive. In the event that the OS crashes,
11052 * ASF needs to reset the hardware to free up the FIFO space
11053 * that may be filled with rx packets destined for the host.
11054 * If the FIFO is full, ASF will no longer function properly.
11055 *
11056 * Unintended resets have been reported on real time kernels
11057 * where the timer doesn't run on time. Netpoll will also have
11058 * same problem.
11059 *
11060 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11061 * to check the ring condition when the heartbeat is expiring
11062 * before doing the reset. This will prevent most unintended
11063 * resets.
11064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011065 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011066 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011067 tg3_wait_for_event_ack(tp);
11068
Michael Chanbbadf502006-04-06 21:46:34 -070011069 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011070 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011071 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011072 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11073 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011074
11075 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011076 }
11077 tp->asf_counter = tp->asf_multiplier;
11078 }
11079
David S. Millerf47c11e2005-06-24 20:18:35 -070011080 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081
Michael Chanf475f162006-03-27 23:20:14 -080011082restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011083 tp->timer.expires = jiffies + tp->timer_offset;
11084 add_timer(&tp->timer);
11085}
11086
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011087static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011088{
11089 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011090 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011091 !tg3_flag(tp, 57765_CLASS))
11092 tp->timer_offset = HZ;
11093 else
11094 tp->timer_offset = HZ / 10;
11095
11096 BUG_ON(tp->timer_offset > HZ);
11097
11098 tp->timer_multiplier = (HZ / tp->timer_offset);
11099 tp->asf_multiplier = (HZ / tp->timer_offset) *
11100 TG3_FW_UPDATE_FREQ_SEC;
11101
Kees Cooke99e88a2017-10-16 14:43:17 -070011102 timer_setup(&tp->timer, tg3_timer, 0);
Matt Carlson21f76382012-02-22 12:35:21 +000011103}
11104
11105static void tg3_timer_start(struct tg3 *tp)
11106{
11107 tp->asf_counter = tp->asf_multiplier;
11108 tp->timer_counter = tp->timer_multiplier;
11109
11110 tp->timer.expires = jiffies + tp->timer_offset;
11111 add_timer(&tp->timer);
11112}
11113
11114static void tg3_timer_stop(struct tg3 *tp)
11115{
11116 del_timer_sync(&tp->timer);
11117}
11118
11119/* Restart hardware after configuration changes, self-test, etc.
11120 * Invoked with tp->lock held.
11121 */
Joe Perches953c96e2013-04-09 10:18:14 +000011122static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011123 __releases(tp->lock)
11124 __acquires(tp->lock)
11125{
11126 int err;
11127
11128 err = tg3_init_hw(tp, reset_phy);
11129 if (err) {
11130 netdev_err(tp->dev,
11131 "Failed to re-initialize device, aborting\n");
11132 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11133 tg3_full_unlock(tp);
11134 tg3_timer_stop(tp);
11135 tp->irq_sync = 0;
11136 tg3_napi_enable(tp);
11137 dev_close(tp->dev);
11138 tg3_full_lock(tp, 0);
11139 }
11140 return err;
11141}
11142
11143static void tg3_reset_task(struct work_struct *work)
11144{
11145 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11146 int err;
11147
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011148 rtnl_lock();
Matt Carlson21f76382012-02-22 12:35:21 +000011149 tg3_full_lock(tp, 0);
11150
11151 if (!netif_running(tp->dev)) {
11152 tg3_flag_clear(tp, RESET_TASK_PENDING);
11153 tg3_full_unlock(tp);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011154 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011155 return;
11156 }
11157
11158 tg3_full_unlock(tp);
11159
11160 tg3_phy_stop(tp);
11161
11162 tg3_netif_stop(tp);
11163
11164 tg3_full_lock(tp, 1);
11165
11166 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11167 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11168 tp->write32_rx_mbox = tg3_write_flush_reg32;
11169 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11170 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11171 }
11172
11173 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011174 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011175 if (err)
11176 goto out;
11177
11178 tg3_netif_start(tp);
11179
11180out:
11181 tg3_full_unlock(tp);
11182
11183 if (!err)
11184 tg3_phy_start(tp);
11185
11186 tg3_flag_clear(tp, RESET_TASK_PENDING);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011187 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011188}
11189
Matt Carlson4f125f42009-09-01 12:55:02 +000011190static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011191{
David Howells7d12e782006-10-05 14:55:46 +010011192 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011193 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011194 char *name;
11195 struct tg3_napi *tnapi = &tp->napi[irq_num];
11196
11197 if (tp->irq_cnt == 1)
11198 name = tp->dev->name;
11199 else {
11200 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011201 if (tnapi->tx_buffers && tnapi->rx_rcb)
11202 snprintf(name, IFNAMSIZ,
11203 "%s-txrx-%d", tp->dev->name, irq_num);
11204 else if (tnapi->tx_buffers)
11205 snprintf(name, IFNAMSIZ,
11206 "%s-tx-%d", tp->dev->name, irq_num);
11207 else if (tnapi->rx_rcb)
11208 snprintf(name, IFNAMSIZ,
11209 "%s-rx-%d", tp->dev->name, irq_num);
11210 else
11211 snprintf(name, IFNAMSIZ,
11212 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011213 name[IFNAMSIZ-1] = 0;
11214 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011215
Joe Perches63c3a662011-04-26 08:12:10 +000011216 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011217 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011218 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011219 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011220 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011221 } else {
11222 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011223 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011224 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011225 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011226 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011227
11228 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011229}
11230
Michael Chan79381092005-04-21 17:13:59 -070011231static int tg3_test_interrupt(struct tg3 *tp)
11232{
Matt Carlson09943a12009-08-28 14:01:57 +000011233 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011234 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011235 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011236 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011237
Michael Chand4bc3922005-05-29 14:59:20 -070011238 if (!netif_running(dev))
11239 return -ENODEV;
11240
Michael Chan79381092005-04-21 17:13:59 -070011241 tg3_disable_ints(tp);
11242
Matt Carlson4f125f42009-09-01 12:55:02 +000011243 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011244
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011245 /*
11246 * Turn off MSI one shot mode. Otherwise this test has no
11247 * observable way to know whether the interrupt was delivered.
11248 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011249 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011250 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11251 tw32(MSGINT_MODE, val);
11252 }
11253
Matt Carlson4f125f42009-09-01 12:55:02 +000011254 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011255 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011256 if (err)
11257 return err;
11258
Matt Carlson898a56f2009-08-28 14:02:40 +000011259 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011260 tg3_enable_ints(tp);
11261
11262 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011263 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011264
11265 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011266 u32 int_mbox, misc_host_ctrl;
11267
Matt Carlson898a56f2009-08-28 14:02:40 +000011268 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011269 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11270
11271 if ((int_mbox != 0) ||
11272 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11273 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011274 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011275 }
11276
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011277 if (tg3_flag(tp, 57765_PLUS) &&
11278 tnapi->hw_status->status_tag != tnapi->last_tag)
11279 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11280
Michael Chan79381092005-04-21 17:13:59 -070011281 msleep(10);
11282 }
11283
11284 tg3_disable_ints(tp);
11285
Matt Carlson4f125f42009-09-01 12:55:02 +000011286 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011287
Matt Carlson4f125f42009-09-01 12:55:02 +000011288 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011289
11290 if (err)
11291 return err;
11292
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011293 if (intr_ok) {
11294 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011295 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011296 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11297 tw32(MSGINT_MODE, val);
11298 }
Michael Chan79381092005-04-21 17:13:59 -070011299 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011300 }
Michael Chan79381092005-04-21 17:13:59 -070011301
11302 return -EIO;
11303}
11304
11305/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11306 * successfully restored
11307 */
11308static int tg3_test_msi(struct tg3 *tp)
11309{
Michael Chan79381092005-04-21 17:13:59 -070011310 int err;
11311 u16 pci_cmd;
11312
Joe Perches63c3a662011-04-26 08:12:10 +000011313 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011314 return 0;
11315
11316 /* Turn off SERR reporting in case MSI terminates with Master
11317 * Abort.
11318 */
11319 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11320 pci_write_config_word(tp->pdev, PCI_COMMAND,
11321 pci_cmd & ~PCI_COMMAND_SERR);
11322
11323 err = tg3_test_interrupt(tp);
11324
11325 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11326
11327 if (!err)
11328 return 0;
11329
11330 /* other failures */
11331 if (err != -EIO)
11332 return err;
11333
11334 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011335 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11336 "to INTx mode. Please report this failure to the PCI "
11337 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011338
Matt Carlson4f125f42009-09-01 12:55:02 +000011339 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011340
Michael Chan79381092005-04-21 17:13:59 -070011341 pci_disable_msi(tp->pdev);
11342
Joe Perches63c3a662011-04-26 08:12:10 +000011343 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011344 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011345
Matt Carlson4f125f42009-09-01 12:55:02 +000011346 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011347 if (err)
11348 return err;
11349
11350 /* Need to reset the chip because the MSI cycle may have terminated
11351 * with Master Abort.
11352 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011353 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011354
Michael Chan944d9802005-05-29 14:57:48 -070011355 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011356 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011357
David S. Millerf47c11e2005-06-24 20:18:35 -070011358 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011359
11360 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011361 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011362
11363 return err;
11364}
11365
Matt Carlson9e9fd122009-01-19 16:57:45 -080011366static int tg3_request_firmware(struct tg3 *tp)
11367{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011368 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011369
11370 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011371 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11372 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011373 return -ENOENT;
11374 }
11375
Nithin Sujir77997ea2013-03-06 17:02:32 +000011376 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011377
11378 /* Firmware blob starts with version numbers, followed by
11379 * start address and _full_ length including BSS sections
11380 * (which must be longer than the actual data, of course
11381 */
11382
Nithin Sujir77997ea2013-03-06 17:02:32 +000011383 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11384 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011385 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11386 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011387 release_firmware(tp->fw);
11388 tp->fw = NULL;
11389 return -EINVAL;
11390 }
11391
11392 /* We no longer need firmware; we have it. */
11393 tp->fw_needed = NULL;
11394 return 0;
11395}
11396
Michael Chan91024262012-09-28 07:12:38 +000011397static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011398{
Michael Chan91024262012-09-28 07:12:38 +000011399 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011400
Michael Chan91024262012-09-28 07:12:38 +000011401 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011402 /* We want as many rx rings enabled as there are cpus.
11403 * In multiqueue MSI-X mode, the first MSI-X vector
11404 * only deals with link interrupts, etc, so we add
11405 * one to the number of vectors we are requesting.
11406 */
Michael Chan91024262012-09-28 07:12:38 +000011407 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011408 }
Matt Carlson679563f2009-09-01 12:55:46 +000011409
Michael Chan91024262012-09-28 07:12:38 +000011410 return irq_cnt;
11411}
11412
11413static bool tg3_enable_msix(struct tg3 *tp)
11414{
11415 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011416 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011417
Michael Chan09681692012-09-28 07:12:42 +000011418 tp->txq_cnt = tp->txq_req;
11419 tp->rxq_cnt = tp->rxq_req;
11420 if (!tp->rxq_cnt)
11421 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011422 if (tp->rxq_cnt > tp->rxq_max)
11423 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011424
11425 /* Disable multiple TX rings by default. Simple round-robin hardware
11426 * scheduling of the TX rings can cause starvation of rings with
11427 * small packets when other rings have TSO or jumbo packets.
11428 */
11429 if (!tp->txq_req)
11430 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011431
11432 tp->irq_cnt = tg3_irq_count(tp);
11433
Matt Carlson679563f2009-09-01 12:55:46 +000011434 for (i = 0; i < tp->irq_max; i++) {
11435 msix_ent[i].entry = i;
11436 msix_ent[i].vector = 0;
11437 }
11438
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011439 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011440 if (rc < 0) {
11441 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011442 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011443 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11444 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011445 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011446 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011447 if (tp->txq_cnt)
11448 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011449 }
11450
11451 for (i = 0; i < tp->irq_max; i++)
11452 tp->napi[i].irq_vec = msix_ent[i].vector;
11453
Michael Chan49a359e2012-09-28 07:12:37 +000011454 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011455 pci_disable_msix(tp->pdev);
11456 return false;
11457 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011458
Michael Chan91024262012-09-28 07:12:38 +000011459 if (tp->irq_cnt == 1)
11460 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011461
Michael Chan91024262012-09-28 07:12:38 +000011462 tg3_flag_set(tp, ENABLE_RSS);
11463
11464 if (tp->txq_cnt > 1)
11465 tg3_flag_set(tp, ENABLE_TSS);
11466
11467 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011468
Matt Carlson679563f2009-09-01 12:55:46 +000011469 return true;
11470}
11471
Matt Carlson07b01732009-08-28 14:01:15 +000011472static void tg3_ints_init(struct tg3 *tp)
11473{
Joe Perches63c3a662011-04-26 08:12:10 +000011474 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11475 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011476 /* All MSI supporting chips should support tagged
11477 * status. Assert that this is the case.
11478 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011479 netdev_warn(tp->dev,
11480 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011481 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011482 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011483
Joe Perches63c3a662011-04-26 08:12:10 +000011484 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11485 tg3_flag_set(tp, USING_MSIX);
11486 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11487 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011488
Joe Perches63c3a662011-04-26 08:12:10 +000011489 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011490 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011491 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011492 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011493 if (!tg3_flag(tp, 1SHOT_MSI))
11494 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011495 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11496 }
11497defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011498 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011499 tp->irq_cnt = 1;
11500 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011501 }
11502
11503 if (tp->irq_cnt == 1) {
11504 tp->txq_cnt = 1;
11505 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011506 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011507 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011508 }
Matt Carlson07b01732009-08-28 14:01:15 +000011509}
11510
11511static void tg3_ints_fini(struct tg3 *tp)
11512{
Joe Perches63c3a662011-04-26 08:12:10 +000011513 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011514 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011515 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011516 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011517 tg3_flag_clear(tp, USING_MSI);
11518 tg3_flag_clear(tp, USING_MSIX);
11519 tg3_flag_clear(tp, ENABLE_RSS);
11520 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011521}
11522
Matt Carlsonbe947302012-12-03 19:36:57 +000011523static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11524 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011525{
Michael Chand8f4cd32012-09-28 07:12:40 +000011526 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011527 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011528
Matt Carlson679563f2009-09-01 12:55:46 +000011529 /*
11530 * Setup interrupts first so we know how
11531 * many NAPI resources to allocate
11532 */
11533 tg3_ints_init(tp);
11534
Matt Carlson90415472011-12-16 13:33:23 +000011535 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011536
Linus Torvalds1da177e2005-04-16 15:20:36 -070011537 /* The placement of this call is tied
11538 * to the setup and use of Host TX descriptors.
11539 */
11540 err = tg3_alloc_consistent(tp);
11541 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011542 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011543
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011544 tg3_napi_init(tp);
11545
Matt Carlsonfed97812009-09-01 13:10:19 +000011546 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011547
Matt Carlson4f125f42009-09-01 12:55:02 +000011548 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson4f125f42009-09-01 12:55:02 +000011549 err = tg3_request_irq(tp, i);
11550 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011551 for (i--; i >= 0; i--) {
Colin Ian King23f48222017-09-14 17:01:25 +010011552 struct tg3_napi *tnapi = &tp->napi[i];
11553
Matt Carlson4f125f42009-09-01 12:55:02 +000011554 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011555 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011556 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011557 }
11558 }
Matt Carlson07b01732009-08-28 14:01:15 +000011559
David S. Millerf47c11e2005-06-24 20:18:35 -070011560 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011562 if (init)
11563 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11564
Michael Chand8f4cd32012-09-28 07:12:40 +000011565 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011567 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569 }
11570
David S. Millerf47c11e2005-06-24 20:18:35 -070011571 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572
Matt Carlson07b01732009-08-28 14:01:15 +000011573 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011574 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575
Michael Chand8f4cd32012-09-28 07:12:40 +000011576 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011577 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011578
Michael Chan79381092005-04-21 17:13:59 -070011579 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011580 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011581 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011582 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011583 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011584
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011585 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011586 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011587
Joe Perches63c3a662011-04-26 08:12:10 +000011588 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011589 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011590
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011591 tw32(PCIE_TRANSACTION_CFG,
11592 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011593 }
Michael Chan79381092005-04-21 17:13:59 -070011594 }
11595
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011596 tg3_phy_start(tp);
11597
Michael Chanaed93e02012-07-16 16:24:02 +000011598 tg3_hwmon_open(tp);
11599
David S. Millerf47c11e2005-06-24 20:18:35 -070011600 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601
Matt Carlson21f76382012-02-22 12:35:21 +000011602 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011603 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011604 tg3_enable_ints(tp);
11605
Ivan Vecera20d14a52015-01-08 16:13:07 +010011606 tg3_ptp_resume(tp);
Matt Carlsonbe947302012-12-03 19:36:57 +000011607
David S. Millerf47c11e2005-06-24 20:18:35 -070011608 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011610 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011612 /*
11613 * Reset loopback feature if it was turned on while the device was down
11614 * make sure that it's installed properly now.
11615 */
11616 if (dev->features & NETIF_F_LOOPBACK)
11617 tg3_set_loopback(dev, dev->features);
11618
Linus Torvalds1da177e2005-04-16 15:20:36 -070011619 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011620
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011621out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011622 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11623 struct tg3_napi *tnapi = &tp->napi[i];
11624 free_irq(tnapi->irq_vec, tnapi);
11625 }
Matt Carlson07b01732009-08-28 14:01:15 +000011626
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011627out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011628 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011629 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011630 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011631
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011632out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011633 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011634
Matt Carlson07b01732009-08-28 14:01:15 +000011635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011636}
11637
Michael Chan65138592012-09-28 07:12:41 +000011638static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639{
Matt Carlson4f125f42009-09-01 12:55:02 +000011640 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011641
Matt Carlsondb219972011-11-04 09:15:03 +000011642 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011643 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011644
Matt Carlson21f76382012-02-22 12:35:21 +000011645 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011646
Michael Chanaed93e02012-07-16 16:24:02 +000011647 tg3_hwmon_close(tp);
11648
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011649 tg3_phy_stop(tp);
11650
David S. Millerf47c11e2005-06-24 20:18:35 -070011651 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011652
11653 tg3_disable_ints(tp);
11654
Michael Chan944d9802005-05-29 14:57:48 -070011655 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011656 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011657 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011658
David S. Millerf47c11e2005-06-24 20:18:35 -070011659 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011660
Matt Carlson4f125f42009-09-01 12:55:02 +000011661 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11662 struct tg3_napi *tnapi = &tp->napi[i];
11663 free_irq(tnapi->irq_vec, tnapi);
11664 }
Matt Carlson07b01732009-08-28 14:01:15 +000011665
11666 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011668 tg3_napi_fini(tp);
11669
Linus Torvalds1da177e2005-04-16 15:20:36 -070011670 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011671}
11672
Michael Chand8f4cd32012-09-28 07:12:40 +000011673static int tg3_open(struct net_device *dev)
11674{
11675 struct tg3 *tp = netdev_priv(dev);
11676 int err;
11677
Ivan Vecera0486a062014-09-01 14:21:57 +020011678 if (tp->pcierr_recovery) {
11679 netdev_err(dev, "Failed to open device. PCI error recovery "
11680 "in progress\n");
11681 return -EAGAIN;
11682 }
11683
Michael Chand8f4cd32012-09-28 07:12:40 +000011684 if (tp->fw_needed) {
11685 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011686 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11687 if (err) {
11688 netdev_warn(tp->dev, "EEE capability disabled\n");
11689 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11690 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11691 netdev_warn(tp->dev, "EEE capability restored\n");
11692 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11693 }
11694 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011695 if (err)
11696 return err;
11697 } else if (err) {
11698 netdev_warn(tp->dev, "TSO capability disabled\n");
11699 tg3_flag_clear(tp, TSO_CAPABLE);
11700 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11701 netdev_notice(tp->dev, "TSO capability restored\n");
11702 tg3_flag_set(tp, TSO_CAPABLE);
11703 }
11704 }
11705
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011706 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011707
11708 err = tg3_power_up(tp);
11709 if (err)
11710 return err;
11711
11712 tg3_full_lock(tp, 0);
11713
11714 tg3_disable_ints(tp);
11715 tg3_flag_clear(tp, INIT_COMPLETE);
11716
11717 tg3_full_unlock(tp);
11718
Nithin Sujir942d1af2013-04-09 08:48:07 +000011719 err = tg3_start(tp,
11720 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11721 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011722 if (err) {
11723 tg3_frob_aux_power(tp, false);
11724 pci_set_power_state(tp->pdev, PCI_D3hot);
11725 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011726
Linus Torvalds1da177e2005-04-16 15:20:36 -070011727 return err;
11728}
11729
11730static int tg3_close(struct net_device *dev)
11731{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 struct tg3 *tp = netdev_priv(dev);
11733
Ivan Vecera0486a062014-09-01 14:21:57 +020011734 if (tp->pcierr_recovery) {
11735 netdev_err(dev, "Failed to close device. PCI error recovery "
11736 "in progress\n");
11737 return -EAGAIN;
11738 }
11739
Michael Chan65138592012-09-28 07:12:41 +000011740 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011741
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011742 if (pci_device_is_present(tp->pdev)) {
11743 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011744
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011745 tg3_carrier_off(tp);
11746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011747 return 0;
11748}
11749
11750static inline u64 get_stat64(tg3_stat64_t *val)
11751{
11752 return ((u64)val->high << 32) | ((u64)val->low);
11753}
11754
11755static u64 tg3_calc_crc_errors(struct tg3 *tp)
11756{
11757 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11758
11759 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011760 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11761 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011762 u32 val;
11763
11764 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11765 tg3_writephy(tp, MII_TG3_TEST1,
11766 val | MII_TG3_TEST1_CRC_EN);
11767 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11768 } else
11769 val = 0;
11770
11771 tp->phy_crc_errors += val;
11772
11773 return tp->phy_crc_errors;
11774 }
11775
11776 return get_stat64(&hw_stats->rx_fcs_errors);
11777}
11778
11779#define ESTAT_ADD(member) \
11780 estats->member = old_estats->member + \
11781 get_stat64(&hw_stats->member)
11782
11783static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11784{
11785 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11786 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11787
11788 ESTAT_ADD(rx_octets);
11789 ESTAT_ADD(rx_fragments);
11790 ESTAT_ADD(rx_ucast_packets);
11791 ESTAT_ADD(rx_mcast_packets);
11792 ESTAT_ADD(rx_bcast_packets);
11793 ESTAT_ADD(rx_fcs_errors);
11794 ESTAT_ADD(rx_align_errors);
11795 ESTAT_ADD(rx_xon_pause_rcvd);
11796 ESTAT_ADD(rx_xoff_pause_rcvd);
11797 ESTAT_ADD(rx_mac_ctrl_rcvd);
11798 ESTAT_ADD(rx_xoff_entered);
11799 ESTAT_ADD(rx_frame_too_long_errors);
11800 ESTAT_ADD(rx_jabbers);
11801 ESTAT_ADD(rx_undersize_packets);
11802 ESTAT_ADD(rx_in_length_errors);
11803 ESTAT_ADD(rx_out_length_errors);
11804 ESTAT_ADD(rx_64_or_less_octet_packets);
11805 ESTAT_ADD(rx_65_to_127_octet_packets);
11806 ESTAT_ADD(rx_128_to_255_octet_packets);
11807 ESTAT_ADD(rx_256_to_511_octet_packets);
11808 ESTAT_ADD(rx_512_to_1023_octet_packets);
11809 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11810 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11811 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11812 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11813 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11814
11815 ESTAT_ADD(tx_octets);
11816 ESTAT_ADD(tx_collisions);
11817 ESTAT_ADD(tx_xon_sent);
11818 ESTAT_ADD(tx_xoff_sent);
11819 ESTAT_ADD(tx_flow_control);
11820 ESTAT_ADD(tx_mac_errors);
11821 ESTAT_ADD(tx_single_collisions);
11822 ESTAT_ADD(tx_mult_collisions);
11823 ESTAT_ADD(tx_deferred);
11824 ESTAT_ADD(tx_excessive_collisions);
11825 ESTAT_ADD(tx_late_collisions);
11826 ESTAT_ADD(tx_collide_2times);
11827 ESTAT_ADD(tx_collide_3times);
11828 ESTAT_ADD(tx_collide_4times);
11829 ESTAT_ADD(tx_collide_5times);
11830 ESTAT_ADD(tx_collide_6times);
11831 ESTAT_ADD(tx_collide_7times);
11832 ESTAT_ADD(tx_collide_8times);
11833 ESTAT_ADD(tx_collide_9times);
11834 ESTAT_ADD(tx_collide_10times);
11835 ESTAT_ADD(tx_collide_11times);
11836 ESTAT_ADD(tx_collide_12times);
11837 ESTAT_ADD(tx_collide_13times);
11838 ESTAT_ADD(tx_collide_14times);
11839 ESTAT_ADD(tx_collide_15times);
11840 ESTAT_ADD(tx_ucast_packets);
11841 ESTAT_ADD(tx_mcast_packets);
11842 ESTAT_ADD(tx_bcast_packets);
11843 ESTAT_ADD(tx_carrier_sense_errors);
11844 ESTAT_ADD(tx_discards);
11845 ESTAT_ADD(tx_errors);
11846
11847 ESTAT_ADD(dma_writeq_full);
11848 ESTAT_ADD(dma_write_prioq_full);
11849 ESTAT_ADD(rxbds_empty);
11850 ESTAT_ADD(rx_discards);
11851 ESTAT_ADD(rx_errors);
11852 ESTAT_ADD(rx_threshold_hit);
11853
11854 ESTAT_ADD(dma_readq_full);
11855 ESTAT_ADD(dma_read_prioq_full);
11856 ESTAT_ADD(tx_comp_queue_full);
11857
11858 ESTAT_ADD(ring_set_send_prod_index);
11859 ESTAT_ADD(ring_status_update);
11860 ESTAT_ADD(nic_irqs);
11861 ESTAT_ADD(nic_avoided_irqs);
11862 ESTAT_ADD(nic_tx_threshold_hit);
11863
Matt Carlson4452d092011-05-19 12:12:51 +000011864 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011865}
11866
Matt Carlson65ec6982012-02-28 23:33:37 +000011867static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011868{
Eric Dumazet511d2222010-07-07 20:44:24 +000011869 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011870 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11871
Linus Torvalds1da177e2005-04-16 15:20:36 -070011872 stats->rx_packets = old_stats->rx_packets +
11873 get_stat64(&hw_stats->rx_ucast_packets) +
11874 get_stat64(&hw_stats->rx_mcast_packets) +
11875 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011876
Linus Torvalds1da177e2005-04-16 15:20:36 -070011877 stats->tx_packets = old_stats->tx_packets +
11878 get_stat64(&hw_stats->tx_ucast_packets) +
11879 get_stat64(&hw_stats->tx_mcast_packets) +
11880 get_stat64(&hw_stats->tx_bcast_packets);
11881
11882 stats->rx_bytes = old_stats->rx_bytes +
11883 get_stat64(&hw_stats->rx_octets);
11884 stats->tx_bytes = old_stats->tx_bytes +
11885 get_stat64(&hw_stats->tx_octets);
11886
11887 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011888 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 stats->tx_errors = old_stats->tx_errors +
11890 get_stat64(&hw_stats->tx_errors) +
11891 get_stat64(&hw_stats->tx_mac_errors) +
11892 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11893 get_stat64(&hw_stats->tx_discards);
11894
11895 stats->multicast = old_stats->multicast +
11896 get_stat64(&hw_stats->rx_mcast_packets);
11897 stats->collisions = old_stats->collisions +
11898 get_stat64(&hw_stats->tx_collisions);
11899
11900 stats->rx_length_errors = old_stats->rx_length_errors +
11901 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11902 get_stat64(&hw_stats->rx_undersize_packets);
11903
Linus Torvalds1da177e2005-04-16 15:20:36 -070011904 stats->rx_frame_errors = old_stats->rx_frame_errors +
11905 get_stat64(&hw_stats->rx_align_errors);
11906 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11907 get_stat64(&hw_stats->tx_discards);
11908 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11909 get_stat64(&hw_stats->tx_carrier_sense_errors);
11910
11911 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011912 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913
John W. Linville4f63b872005-09-12 14:43:18 -070011914 stats->rx_missed_errors = old_stats->rx_missed_errors +
11915 get_stat64(&hw_stats->rx_discards);
11916
Eric Dumazetb0057c52010-10-10 19:55:52 +000011917 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011918 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919}
11920
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921static int tg3_get_regs_len(struct net_device *dev)
11922{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011923 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924}
11925
11926static void tg3_get_regs(struct net_device *dev,
11927 struct ethtool_regs *regs, void *_p)
11928{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930
11931 regs->version = 0;
11932
Matt Carlson97bd8e42011-04-13 11:05:04 +000011933 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934
Matt Carlson80096062010-08-02 11:26:06 +000011935 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011936 return;
11937
David S. Millerf47c11e2005-06-24 20:18:35 -070011938 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939
Matt Carlson97bd8e42011-04-13 11:05:04 +000011940 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941
David S. Millerf47c11e2005-06-24 20:18:35 -070011942 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011943}
11944
11945static int tg3_get_eeprom_len(struct net_device *dev)
11946{
11947 struct tg3 *tp = netdev_priv(dev);
11948
11949 return tp->nvram_size;
11950}
11951
Linus Torvalds1da177e2005-04-16 15:20:36 -070011952static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11953{
11954 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011955 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011957 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011958 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011959
Joe Perches63c3a662011-04-26 08:12:10 +000011960 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011961 return -EINVAL;
11962
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963 offset = eeprom->offset;
11964 len = eeprom->len;
11965 eeprom->len = 0;
11966
11967 eeprom->magic = TG3_EEPROM_MAGIC;
11968
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011969 /* Override clock, link aware and link idle modes */
11970 if (tg3_flag(tp, CPMU_PRESENT)) {
11971 cpmu_val = tr32(TG3_CPMU_CTRL);
11972 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11973 CPMU_CTRL_LINK_IDLE_MODE)) {
11974 tw32(TG3_CPMU_CTRL, cpmu_val &
11975 ~(CPMU_CTRL_LINK_AWARE_MODE |
11976 CPMU_CTRL_LINK_IDLE_MODE));
11977 cpmu_restore = 1;
11978 }
11979 }
11980 tg3_override_clk(tp);
11981
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 if (offset & 3) {
11983 /* adjustments to start on required 4 byte boundary */
11984 b_offset = offset & 3;
11985 b_count = 4 - b_offset;
11986 if (b_count > len) {
11987 /* i.e. offset=1 len=2 */
11988 b_count = len;
11989 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011990 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011991 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011992 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011993 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011994 len -= b_count;
11995 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011996 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997 }
11998
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011999 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000 pd = &data[eeprom->len];
12001 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012002 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012004 if (i)
12005 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012006 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012007 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012010 if (need_resched()) {
12011 if (signal_pending(current)) {
12012 eeprom->len += i;
12013 ret = -EINTR;
12014 goto eeprom_done;
12015 }
12016 cond_resched();
12017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012018 }
12019 eeprom->len += i;
12020
12021 if (len & 3) {
12022 /* read last bytes not ending on 4 byte boundary */
12023 pd = &data[eeprom->len];
12024 b_count = len & 3;
12025 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012026 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012027 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012028 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080012029 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012030 eeprom->len += b_count;
12031 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012032 ret = 0;
12033
12034eeprom_done:
12035 /* Restore clock, link aware and link idle modes */
12036 tg3_restore_clk(tp);
12037 if (cpmu_restore)
12038 tw32(TG3_CPMU_CTRL, cpmu_val);
12039
12040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041}
12042
Linus Torvalds1da177e2005-04-16 15:20:36 -070012043static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12044{
12045 struct tg3 *tp = netdev_priv(dev);
12046 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012047 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012048 u8 *buf;
Arnd Bergmanne434e042016-01-29 12:39:15 +010012049 __be32 start = 0, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012050
Joe Perches63c3a662011-04-26 08:12:10 +000012051 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012052 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 return -EINVAL;
12054
12055 offset = eeprom->offset;
12056 len = eeprom->len;
12057
12058 if ((b_offset = (offset & 3))) {
12059 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012060 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012061 if (ret)
12062 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012063 len += b_offset;
12064 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070012065 if (len < 4)
12066 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067 }
12068
12069 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070012070 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012071 /* adjustments to end on required 4 byte boundary */
12072 odd_len = 1;
12073 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012074 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012075 if (ret)
12076 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012077 }
12078
12079 buf = data;
12080 if (b_offset || odd_len) {
12081 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012082 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012083 return -ENOMEM;
12084 if (b_offset)
12085 memcpy(buf, &start, 4);
12086 if (odd_len)
12087 memcpy(buf+len-4, &end, 4);
12088 memcpy(buf + b_offset, data, eeprom->len);
12089 }
12090
12091 ret = tg3_nvram_write_block(tp, offset, len, buf);
12092
12093 if (buf != data)
12094 kfree(buf);
12095
12096 return ret;
12097}
12098
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012099static int tg3_get_link_ksettings(struct net_device *dev,
12100 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012101{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012102 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012103 u32 supported, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012104
Joe Perches63c3a662011-04-26 08:12:10 +000012105 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012106 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012107 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012108 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012109 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +030012110 phy_ethtool_ksettings_get(phydev, cmd);
12111
12112 return 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012113 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012114
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012115 supported = (SUPPORTED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012116
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012117 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012118 supported |= (SUPPORTED_1000baseT_Half |
12119 SUPPORTED_1000baseT_Full);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012120
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012121 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012122 supported |= (SUPPORTED_100baseT_Half |
12123 SUPPORTED_100baseT_Full |
12124 SUPPORTED_10baseT_Half |
12125 SUPPORTED_10baseT_Full |
12126 SUPPORTED_TP);
12127 cmd->base.port = PORT_TP;
Karsten Keilef348142006-05-12 12:49:08 -070012128 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012129 supported |= SUPPORTED_FIBRE;
12130 cmd->base.port = PORT_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012131 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012132 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
12133 supported);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012134
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012135 advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012136 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12137 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12138 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012139 advertising |= ADVERTISED_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012140 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012141 advertising |= ADVERTISED_Pause |
12142 ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012143 }
12144 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012145 advertising |= ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012146 }
12147 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012148 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
12149 advertising);
12150
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012151 if (netif_running(dev) && tp->link_up) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012152 cmd->base.speed = tp->link_config.active_speed;
12153 cmd->base.duplex = tp->link_config.active_duplex;
12154 ethtool_convert_legacy_u32_to_link_mode(
12155 cmd->link_modes.lp_advertising,
12156 tp->link_config.rmt_adv);
12157
Matt Carlsone348c5e2011-11-21 15:01:20 +000012158 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12159 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012160 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012161 else
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012162 cmd->base.eth_tp_mdix = ETH_TP_MDI;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012163 }
Matt Carlson64c22182010-10-14 10:37:44 +000012164 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012165 cmd->base.speed = SPEED_UNKNOWN;
12166 cmd->base.duplex = DUPLEX_UNKNOWN;
12167 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012168 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012169 cmd->base.phy_address = tp->phy_addr;
12170 cmd->base.autoneg = tp->link_config.autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 return 0;
12172}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012173
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012174static int tg3_set_link_ksettings(struct net_device *dev,
12175 const struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012176{
12177 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012178 u32 speed = cmd->base.speed;
12179 u32 advertising;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012180
Joe Perches63c3a662011-04-26 08:12:10 +000012181 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012182 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012183 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012184 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012185 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012186 return phy_ethtool_ksettings_set(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012187 }
12188
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012189 if (cmd->base.autoneg != AUTONEG_ENABLE &&
12190 cmd->base.autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012191 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012192
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012193 if (cmd->base.autoneg == AUTONEG_DISABLE &&
12194 cmd->base.duplex != DUPLEX_FULL &&
12195 cmd->base.duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012196 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012198 ethtool_convert_link_mode_to_legacy_u32(&advertising,
12199 cmd->link_modes.advertising);
12200
12201 if (cmd->base.autoneg == AUTONEG_ENABLE) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000012202 u32 mask = ADVERTISED_Autoneg |
12203 ADVERTISED_Pause |
12204 ADVERTISED_Asym_Pause;
12205
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012206 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012207 mask |= ADVERTISED_1000baseT_Half |
12208 ADVERTISED_1000baseT_Full;
12209
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012210 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012211 mask |= ADVERTISED_100baseT_Half |
12212 ADVERTISED_100baseT_Full |
12213 ADVERTISED_10baseT_Half |
12214 ADVERTISED_10baseT_Full |
12215 ADVERTISED_TP;
12216 else
12217 mask |= ADVERTISED_FIBRE;
12218
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012219 if (advertising & ~mask)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012220 return -EINVAL;
12221
12222 mask &= (ADVERTISED_1000baseT_Half |
12223 ADVERTISED_1000baseT_Full |
12224 ADVERTISED_100baseT_Half |
12225 ADVERTISED_100baseT_Full |
12226 ADVERTISED_10baseT_Half |
12227 ADVERTISED_10baseT_Full);
12228
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012229 advertising &= mask;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012230 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012231 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012232 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012233 return -EINVAL;
12234
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012235 if (cmd->base.duplex != DUPLEX_FULL)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012236 return -EINVAL;
12237 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012238 if (speed != SPEED_100 &&
12239 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012240 return -EINVAL;
12241 }
12242 }
12243
David S. Millerf47c11e2005-06-24 20:18:35 -070012244 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012245
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012246 tp->link_config.autoneg = cmd->base.autoneg;
12247 if (cmd->base.autoneg == AUTONEG_ENABLE) {
12248 tp->link_config.advertising = (advertising |
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012249 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012250 tp->link_config.speed = SPEED_UNKNOWN;
12251 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012252 } else {
12253 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012254 tp->link_config.speed = speed;
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012255 tp->link_config.duplex = cmd->base.duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012256 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012257
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012258 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12259
Nithin Sujirce20f162013-04-09 08:48:04 +000012260 tg3_warn_mgmt_link_flap(tp);
12261
Linus Torvalds1da177e2005-04-16 15:20:36 -070012262 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012263 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012264
David S. Millerf47c11e2005-06-24 20:18:35 -070012265 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012266
Linus Torvalds1da177e2005-04-16 15:20:36 -070012267 return 0;
12268}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012269
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12271{
12272 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012273
Rick Jones68aad782011-11-07 13:29:27 +000012274 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12275 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12276 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12277 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012278}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012279
Linus Torvalds1da177e2005-04-16 15:20:36 -070012280static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12281{
12282 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012283
Joe Perches63c3a662011-04-26 08:12:10 +000012284 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012285 wol->supported = WAKE_MAGIC;
12286 else
12287 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012289 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290 wol->wolopts = WAKE_MAGIC;
12291 memset(&wol->sopass, 0, sizeof(wol->sopass));
12292}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012293
Linus Torvalds1da177e2005-04-16 15:20:36 -070012294static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12295{
12296 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012297 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012298
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299 if (wol->wolopts & ~WAKE_MAGIC)
12300 return -EINVAL;
12301 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012302 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012303 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012304
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012305 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12306
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012307 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012308 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012309 else
Joe Perches63c3a662011-04-26 08:12:10 +000012310 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012311
Linus Torvalds1da177e2005-04-16 15:20:36 -070012312 return 0;
12313}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012314
Linus Torvalds1da177e2005-04-16 15:20:36 -070012315static u32 tg3_get_msglevel(struct net_device *dev)
12316{
12317 struct tg3 *tp = netdev_priv(dev);
12318 return tp->msg_enable;
12319}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012320
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321static void tg3_set_msglevel(struct net_device *dev, u32 value)
12322{
12323 struct tg3 *tp = netdev_priv(dev);
12324 tp->msg_enable = value;
12325}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012326
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327static int tg3_nway_reset(struct net_device *dev)
12328{
12329 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012330 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012331
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332 if (!netif_running(dev))
12333 return -EAGAIN;
12334
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012335 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012336 return -EINVAL;
12337
Nithin Sujirce20f162013-04-09 08:48:04 +000012338 tg3_warn_mgmt_link_flap(tp);
12339
Joe Perches63c3a662011-04-26 08:12:10 +000012340 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012341 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012342 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012343 r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012344 } else {
12345 u32 bmcr;
12346
12347 spin_lock_bh(&tp->lock);
12348 r = -EINVAL;
12349 tg3_readphy(tp, MII_BMCR, &bmcr);
12350 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12351 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012352 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012353 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12354 BMCR_ANENABLE);
12355 r = 0;
12356 }
12357 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012359
Linus Torvalds1da177e2005-04-16 15:20:36 -070012360 return r;
12361}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012362
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12364{
12365 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012366
Matt Carlson2c49a442010-09-30 10:34:35 +000012367 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012368 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012369 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012370 else
12371 ering->rx_jumbo_max_pending = 0;
12372
12373 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012374
12375 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012376 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012377 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12378 else
12379 ering->rx_jumbo_pending = 0;
12380
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012381 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012382}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012383
Linus Torvalds1da177e2005-04-16 15:20:36 -070012384static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12385{
12386 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012387 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012388
Matt Carlson2c49a442010-09-30 10:34:35 +000012389 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12390 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012391 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12392 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012393 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012394 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012395 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012396
Michael Chanbbe832c2005-06-24 20:20:04 -070012397 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012398 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012399 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012400 irq_sync = 1;
12401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012402
Michael Chanbbe832c2005-06-24 20:20:04 -070012403 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012404
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405 tp->rx_pending = ering->rx_pending;
12406
Joe Perches63c3a662011-04-26 08:12:10 +000012407 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408 tp->rx_pending > 63)
12409 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012410
12411 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12412 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012413
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012414 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012415 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416
12417 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012418 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012419 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012420 if (!err)
12421 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422 }
12423
David S. Millerf47c11e2005-06-24 20:18:35 -070012424 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012425
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012426 if (irq_sync && !err)
12427 tg3_phy_start(tp);
12428
Michael Chanb9ec6c12006-07-25 16:37:27 -070012429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012430}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012431
Linus Torvalds1da177e2005-04-16 15:20:36 -070012432static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12433{
12434 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012435
Joe Perches63c3a662011-04-26 08:12:10 +000012436 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012437
Matt Carlson4a2db502011-12-08 14:40:17 +000012438 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012439 epause->rx_pause = 1;
12440 else
12441 epause->rx_pause = 0;
12442
Matt Carlson4a2db502011-12-08 14:40:17 +000012443 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012444 epause->tx_pause = 1;
12445 else
12446 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012447}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012448
Linus Torvalds1da177e2005-04-16 15:20:36 -070012449static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12450{
12451 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012452 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012453
Nithin Sujirce20f162013-04-09 08:48:04 +000012454 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12455 tg3_warn_mgmt_link_flap(tp);
12456
Joe Perches63c3a662011-04-26 08:12:10 +000012457 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012458 u32 newadv;
12459 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012460
Andrew Lunn7f854422016-01-06 20:11:18 +010012461 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012462
Matt Carlson27121682010-02-17 15:16:57 +000012463 if (!(phydev->supported & SUPPORTED_Pause) ||
12464 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012465 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012466 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012467
Matt Carlson27121682010-02-17 15:16:57 +000012468 tp->link_config.flowctrl = 0;
12469 if (epause->rx_pause) {
12470 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012471
Matt Carlson27121682010-02-17 15:16:57 +000012472 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012473 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012474 newadv = ADVERTISED_Pause;
12475 } else
12476 newadv = ADVERTISED_Pause |
12477 ADVERTISED_Asym_Pause;
12478 } else if (epause->tx_pause) {
12479 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12480 newadv = ADVERTISED_Asym_Pause;
12481 } else
12482 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012483
Matt Carlson27121682010-02-17 15:16:57 +000012484 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012486 else
Joe Perches63c3a662011-04-26 08:12:10 +000012487 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012488
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012489 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012490 u32 oldadv = phydev->advertising &
12491 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12492 if (oldadv != newadv) {
12493 phydev->advertising &=
12494 ~(ADVERTISED_Pause |
12495 ADVERTISED_Asym_Pause);
12496 phydev->advertising |= newadv;
12497 if (phydev->autoneg) {
12498 /*
12499 * Always renegotiate the link to
12500 * inform our link partner of our
12501 * flow control settings, even if the
12502 * flow control is forced. Let
12503 * tg3_adjust_link() do the final
12504 * flow control setup.
12505 */
12506 return phy_start_aneg(phydev);
12507 }
12508 }
12509
12510 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012511 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012512 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012513 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012514 ~(ADVERTISED_Pause |
12515 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012516 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012517 }
12518 } else {
12519 int irq_sync = 0;
12520
12521 if (netif_running(dev)) {
12522 tg3_netif_stop(tp);
12523 irq_sync = 1;
12524 }
12525
12526 tg3_full_lock(tp, irq_sync);
12527
12528 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012529 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012530 else
Joe Perches63c3a662011-04-26 08:12:10 +000012531 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012532 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012533 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012534 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012535 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012536 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012537 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012538 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012539 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012540
12541 if (netif_running(dev)) {
12542 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012543 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012544 if (!err)
12545 tg3_netif_start(tp);
12546 }
12547
12548 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012550
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012551 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12552
Michael Chanb9ec6c12006-07-25 16:37:27 -070012553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012554}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012555
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012556static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012557{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012558 switch (sset) {
12559 case ETH_SS_TEST:
12560 return TG3_NUM_TEST;
12561 case ETH_SS_STATS:
12562 return TG3_NUM_STATS;
12563 default:
12564 return -EOPNOTSUPP;
12565 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012566}
12567
Matt Carlson90415472011-12-16 13:33:23 +000012568static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12569 u32 *rules __always_unused)
12570{
12571 struct tg3 *tp = netdev_priv(dev);
12572
12573 if (!tg3_flag(tp, SUPPORT_MSIX))
12574 return -EOPNOTSUPP;
12575
12576 switch (info->cmd) {
12577 case ETHTOOL_GRXRINGS:
12578 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012579 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012580 else {
12581 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012582 if (info->data > TG3_RSS_MAX_NUM_QS)
12583 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012584 }
12585
Matt Carlson90415472011-12-16 13:33:23 +000012586 return 0;
12587
12588 default:
12589 return -EOPNOTSUPP;
12590 }
12591}
12592
12593static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12594{
12595 u32 size = 0;
12596 struct tg3 *tp = netdev_priv(dev);
12597
12598 if (tg3_flag(tp, SUPPORT_MSIX))
12599 size = TG3_RSS_INDIR_TBL_SIZE;
12600
12601 return size;
12602}
12603
Eyal Perry892311f2014-12-02 18:12:10 +020012604static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012605{
12606 struct tg3 *tp = netdev_priv(dev);
12607 int i;
12608
Eyal Perry892311f2014-12-02 18:12:10 +020012609 if (hfunc)
12610 *hfunc = ETH_RSS_HASH_TOP;
12611 if (!indir)
12612 return 0;
12613
Matt Carlson90415472011-12-16 13:33:23 +000012614 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12615 indir[i] = tp->rss_ind_tbl[i];
12616
12617 return 0;
12618}
12619
Eyal Perry892311f2014-12-02 18:12:10 +020012620static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
12621 const u8 hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012622{
12623 struct tg3 *tp = netdev_priv(dev);
12624 size_t i;
12625
Eyal Perry892311f2014-12-02 18:12:10 +020012626 /* We require at least one supported parameter to be changed and no
12627 * change in any of the unsupported parameters
12628 */
12629 if (key ||
12630 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
12631 return -EOPNOTSUPP;
12632
12633 if (!indir)
12634 return 0;
12635
Matt Carlson90415472011-12-16 13:33:23 +000012636 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12637 tp->rss_ind_tbl[i] = indir[i];
12638
12639 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12640 return 0;
12641
12642 /* It is legal to write the indirection
12643 * table while the device is running.
12644 */
12645 tg3_full_lock(tp, 0);
12646 tg3_rss_write_indir_tbl(tp);
12647 tg3_full_unlock(tp);
12648
12649 return 0;
12650}
12651
Michael Chan09681692012-09-28 07:12:42 +000012652static void tg3_get_channels(struct net_device *dev,
12653 struct ethtool_channels *channel)
12654{
12655 struct tg3 *tp = netdev_priv(dev);
12656 u32 deflt_qs = netif_get_num_default_rss_queues();
12657
12658 channel->max_rx = tp->rxq_max;
12659 channel->max_tx = tp->txq_max;
12660
12661 if (netif_running(dev)) {
12662 channel->rx_count = tp->rxq_cnt;
12663 channel->tx_count = tp->txq_cnt;
12664 } else {
12665 if (tp->rxq_req)
12666 channel->rx_count = tp->rxq_req;
12667 else
12668 channel->rx_count = min(deflt_qs, tp->rxq_max);
12669
12670 if (tp->txq_req)
12671 channel->tx_count = tp->txq_req;
12672 else
12673 channel->tx_count = min(deflt_qs, tp->txq_max);
12674 }
12675}
12676
12677static int tg3_set_channels(struct net_device *dev,
12678 struct ethtool_channels *channel)
12679{
12680 struct tg3 *tp = netdev_priv(dev);
12681
12682 if (!tg3_flag(tp, SUPPORT_MSIX))
12683 return -EOPNOTSUPP;
12684
12685 if (channel->rx_count > tp->rxq_max ||
12686 channel->tx_count > tp->txq_max)
12687 return -EINVAL;
12688
12689 tp->rxq_req = channel->rx_count;
12690 tp->txq_req = channel->tx_count;
12691
12692 if (!netif_running(dev))
12693 return 0;
12694
12695 tg3_stop(tp);
12696
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012697 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012698
Matt Carlsonbe947302012-12-03 19:36:57 +000012699 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012700
12701 return 0;
12702}
12703
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012704static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705{
12706 switch (stringset) {
12707 case ETH_SS_STATS:
12708 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12709 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012710 case ETH_SS_TEST:
12711 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012713 default:
12714 WARN_ON(1); /* we need a WARN() */
12715 break;
12716 }
12717}
12718
stephen hemminger81b87092011-04-04 08:43:50 +000012719static int tg3_set_phys_id(struct net_device *dev,
12720 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012721{
12722 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012723
12724 if (!netif_running(tp->dev))
12725 return -EAGAIN;
12726
stephen hemminger81b87092011-04-04 08:43:50 +000012727 switch (state) {
12728 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012729 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012730
stephen hemminger81b87092011-04-04 08:43:50 +000012731 case ETHTOOL_ID_ON:
12732 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12733 LED_CTRL_1000MBPS_ON |
12734 LED_CTRL_100MBPS_ON |
12735 LED_CTRL_10MBPS_ON |
12736 LED_CTRL_TRAFFIC_OVERRIDE |
12737 LED_CTRL_TRAFFIC_BLINK |
12738 LED_CTRL_TRAFFIC_LED);
12739 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012740
stephen hemminger81b87092011-04-04 08:43:50 +000012741 case ETHTOOL_ID_OFF:
12742 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12743 LED_CTRL_TRAFFIC_OVERRIDE);
12744 break;
Michael Chan4009a932005-09-05 17:52:54 -070012745
stephen hemminger81b87092011-04-04 08:43:50 +000012746 case ETHTOOL_ID_INACTIVE:
12747 tw32(MAC_LED_CTRL, tp->led_ctrl);
12748 break;
Michael Chan4009a932005-09-05 17:52:54 -070012749 }
stephen hemminger81b87092011-04-04 08:43:50 +000012750
Michael Chan4009a932005-09-05 17:52:54 -070012751 return 0;
12752}
12753
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012754static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012755 struct ethtool_stats *estats, u64 *tmp_stats)
12756{
12757 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012758
Matt Carlsonb546e462012-02-13 15:20:09 +000012759 if (tp->hw_stats)
12760 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12761 else
12762 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012763}
12764
Matt Carlson535a4902011-07-20 10:20:56 +000012765static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012766{
12767 int i;
12768 __be32 *buf;
12769 u32 offset = 0, len = 0;
12770 u32 magic, val;
12771
Joe Perches63c3a662011-04-26 08:12:10 +000012772 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012773 return NULL;
12774
12775 if (magic == TG3_EEPROM_MAGIC) {
12776 for (offset = TG3_NVM_DIR_START;
12777 offset < TG3_NVM_DIR_END;
12778 offset += TG3_NVM_DIRENT_SIZE) {
12779 if (tg3_nvram_read(tp, offset, &val))
12780 return NULL;
12781
12782 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12783 TG3_NVM_DIRTYPE_EXTVPD)
12784 break;
12785 }
12786
12787 if (offset != TG3_NVM_DIR_END) {
12788 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12789 if (tg3_nvram_read(tp, offset + 4, &offset))
12790 return NULL;
12791
12792 offset = tg3_nvram_logical_addr(tp, offset);
12793 }
12794 }
12795
12796 if (!offset || !len) {
12797 offset = TG3_NVM_VPD_OFF;
12798 len = TG3_NVM_VPD_LEN;
12799 }
12800
12801 buf = kmalloc(len, GFP_KERNEL);
12802 if (buf == NULL)
12803 return NULL;
12804
12805 if (magic == TG3_EEPROM_MAGIC) {
12806 for (i = 0; i < len; i += 4) {
12807 /* The data is in little-endian format in NVRAM.
12808 * Use the big-endian read routines to preserve
12809 * the byte order as it exists in NVRAM.
12810 */
12811 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12812 goto error;
12813 }
12814 } else {
12815 u8 *ptr;
12816 ssize_t cnt;
12817 unsigned int pos = 0;
12818
12819 ptr = (u8 *)&buf[0];
12820 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12821 cnt = pci_read_vpd(tp->pdev, pos,
12822 len - pos, ptr);
12823 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12824 cnt = 0;
12825 else if (cnt < 0)
12826 goto error;
12827 }
12828 if (pos != len)
12829 goto error;
12830 }
12831
Matt Carlson535a4902011-07-20 10:20:56 +000012832 *vpdlen = len;
12833
Matt Carlsonc3e94502011-04-13 11:05:08 +000012834 return buf;
12835
12836error:
12837 kfree(buf);
12838 return NULL;
12839}
12840
Michael Chan566f86a2005-05-29 14:56:58 -070012841#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012842#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12843#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12844#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012845#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12846#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012847#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012848#define NVRAM_SELFBOOT_HW_SIZE 0x20
12849#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012850
12851static int tg3_test_nvram(struct tg3 *tp)
12852{
Matt Carlson535a4902011-07-20 10:20:56 +000012853 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012854 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012855 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012856
Joe Perches63c3a662011-04-26 08:12:10 +000012857 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012858 return 0;
12859
Matt Carlsone4f34112009-02-25 14:25:00 +000012860 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012861 return -EIO;
12862
Michael Chan1b277772006-03-20 22:27:48 -080012863 if (magic == TG3_EEPROM_MAGIC)
12864 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012865 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012866 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12867 TG3_EEPROM_SB_FORMAT_1) {
12868 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12869 case TG3_EEPROM_SB_REVISION_0:
12870 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12871 break;
12872 case TG3_EEPROM_SB_REVISION_2:
12873 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12874 break;
12875 case TG3_EEPROM_SB_REVISION_3:
12876 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12877 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012878 case TG3_EEPROM_SB_REVISION_4:
12879 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12880 break;
12881 case TG3_EEPROM_SB_REVISION_5:
12882 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12883 break;
12884 case TG3_EEPROM_SB_REVISION_6:
12885 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12886 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012887 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012888 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012889 }
12890 } else
Michael Chan1b277772006-03-20 22:27:48 -080012891 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012892 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12893 size = NVRAM_SELFBOOT_HW_SIZE;
12894 else
Michael Chan1b277772006-03-20 22:27:48 -080012895 return -EIO;
12896
12897 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012898 if (buf == NULL)
12899 return -ENOMEM;
12900
Michael Chan1b277772006-03-20 22:27:48 -080012901 err = -EIO;
12902 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012903 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12904 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012905 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012906 }
Michael Chan1b277772006-03-20 22:27:48 -080012907 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012908 goto out;
12909
Michael Chan1b277772006-03-20 22:27:48 -080012910 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012911 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012912 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012913 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012914 u8 *buf8 = (u8 *) buf, csum8 = 0;
12915
Al Virob9fc7dc2007-12-17 22:59:57 -080012916 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012917 TG3_EEPROM_SB_REVISION_2) {
12918 /* For rev 2, the csum doesn't include the MBA. */
12919 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12920 csum8 += buf8[i];
12921 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12922 csum8 += buf8[i];
12923 } else {
12924 for (i = 0; i < size; i++)
12925 csum8 += buf8[i];
12926 }
Michael Chan1b277772006-03-20 22:27:48 -080012927
Adrian Bunkad96b482006-04-05 22:21:04 -070012928 if (csum8 == 0) {
12929 err = 0;
12930 goto out;
12931 }
12932
12933 err = -EIO;
12934 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012935 }
Michael Chan566f86a2005-05-29 14:56:58 -070012936
Al Virob9fc7dc2007-12-17 22:59:57 -080012937 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012938 TG3_EEPROM_MAGIC_HW) {
12939 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012940 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012941 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012942
12943 /* Separate the parity bits and the data bytes. */
12944 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12945 if ((i == 0) || (i == 8)) {
12946 int l;
12947 u8 msk;
12948
12949 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12950 parity[k++] = buf8[i] & msk;
12951 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012952 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012953 int l;
12954 u8 msk;
12955
12956 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12957 parity[k++] = buf8[i] & msk;
12958 i++;
12959
12960 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12961 parity[k++] = buf8[i] & msk;
12962 i++;
12963 }
12964 data[j++] = buf8[i];
12965 }
12966
12967 err = -EIO;
12968 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12969 u8 hw8 = hweight8(data[i]);
12970
12971 if ((hw8 & 0x1) && parity[i])
12972 goto out;
12973 else if (!(hw8 & 0x1) && !parity[i])
12974 goto out;
12975 }
12976 err = 0;
12977 goto out;
12978 }
12979
Matt Carlson01c3a392011-03-09 16:58:20 +000012980 err = -EIO;
12981
Michael Chan566f86a2005-05-29 14:56:58 -070012982 /* Bootstrap checksum at offset 0x10 */
12983 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012984 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012985 goto out;
12986
12987 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12988 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012989 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012990 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012991
Matt Carlsonc3e94502011-04-13 11:05:08 +000012992 kfree(buf);
12993
Matt Carlson535a4902011-07-20 10:20:56 +000012994 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012995 if (!buf)
12996 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012997
Matt Carlson535a4902011-07-20 10:20:56 +000012998 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012999 if (i > 0) {
13000 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
13001 if (j < 0)
13002 goto out;
13003
Matt Carlson535a4902011-07-20 10:20:56 +000013004 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000013005 goto out;
13006
13007 i += PCI_VPD_LRDT_TAG_SIZE;
13008 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
13009 PCI_VPD_RO_KEYWORD_CHKSUM);
13010 if (j > 0) {
13011 u8 csum8 = 0;
13012
13013 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13014
13015 for (i = 0; i <= j; i++)
13016 csum8 += ((u8 *)buf)[i];
13017
13018 if (csum8)
13019 goto out;
13020 }
13021 }
13022
Michael Chan566f86a2005-05-29 14:56:58 -070013023 err = 0;
13024
13025out:
13026 kfree(buf);
13027 return err;
13028}
13029
Michael Chanca430072005-05-29 14:57:23 -070013030#define TG3_SERDES_TIMEOUT_SEC 2
13031#define TG3_COPPER_TIMEOUT_SEC 6
13032
13033static int tg3_test_link(struct tg3 *tp)
13034{
13035 int i, max;
13036
13037 if (!netif_running(tp->dev))
13038 return -ENODEV;
13039
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013040 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070013041 max = TG3_SERDES_TIMEOUT_SEC;
13042 else
13043 max = TG3_COPPER_TIMEOUT_SEC;
13044
13045 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000013046 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070013047 return 0;
13048
13049 if (msleep_interruptible(1000))
13050 break;
13051 }
13052
13053 return -EIO;
13054}
13055
Michael Chana71116d2005-05-29 14:58:11 -070013056/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013057static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013058{
Michael Chanb16250e2006-09-27 16:10:14 -070013059 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013060 u32 offset, read_mask, write_mask, val, save_val, read_val;
13061 static struct {
13062 u16 offset;
13063 u16 flags;
13064#define TG3_FL_5705 0x1
13065#define TG3_FL_NOT_5705 0x2
13066#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013067#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013068 u32 read_mask;
13069 u32 write_mask;
13070 } reg_tbl[] = {
13071 /* MAC Control Registers */
13072 { MAC_MODE, TG3_FL_NOT_5705,
13073 0x00000000, 0x00ef6f8c },
13074 { MAC_MODE, TG3_FL_5705,
13075 0x00000000, 0x01ef6b8c },
13076 { MAC_STATUS, TG3_FL_NOT_5705,
13077 0x03800107, 0x00000000 },
13078 { MAC_STATUS, TG3_FL_5705,
13079 0x03800100, 0x00000000 },
13080 { MAC_ADDR_0_HIGH, 0x0000,
13081 0x00000000, 0x0000ffff },
13082 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013083 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013084 { MAC_RX_MTU_SIZE, 0x0000,
13085 0x00000000, 0x0000ffff },
13086 { MAC_TX_MODE, 0x0000,
13087 0x00000000, 0x00000070 },
13088 { MAC_TX_LENGTHS, 0x0000,
13089 0x00000000, 0x00003fff },
13090 { MAC_RX_MODE, TG3_FL_NOT_5705,
13091 0x00000000, 0x000007fc },
13092 { MAC_RX_MODE, TG3_FL_5705,
13093 0x00000000, 0x000007dc },
13094 { MAC_HASH_REG_0, 0x0000,
13095 0x00000000, 0xffffffff },
13096 { MAC_HASH_REG_1, 0x0000,
13097 0x00000000, 0xffffffff },
13098 { MAC_HASH_REG_2, 0x0000,
13099 0x00000000, 0xffffffff },
13100 { MAC_HASH_REG_3, 0x0000,
13101 0x00000000, 0xffffffff },
13102
13103 /* Receive Data and Receive BD Initiator Control Registers. */
13104 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13105 0x00000000, 0xffffffff },
13106 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13107 0x00000000, 0xffffffff },
13108 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13109 0x00000000, 0x00000003 },
13110 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13111 0x00000000, 0xffffffff },
13112 { RCVDBDI_STD_BD+0, 0x0000,
13113 0x00000000, 0xffffffff },
13114 { RCVDBDI_STD_BD+4, 0x0000,
13115 0x00000000, 0xffffffff },
13116 { RCVDBDI_STD_BD+8, 0x0000,
13117 0x00000000, 0xffff0002 },
13118 { RCVDBDI_STD_BD+0xc, 0x0000,
13119 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013120
Michael Chana71116d2005-05-29 14:58:11 -070013121 /* Receive BD Initiator Control Registers. */
13122 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13123 0x00000000, 0xffffffff },
13124 { RCVBDI_STD_THRESH, TG3_FL_5705,
13125 0x00000000, 0x000003ff },
13126 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13127 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013128
Michael Chana71116d2005-05-29 14:58:11 -070013129 /* Host Coalescing Control Registers. */
13130 { HOSTCC_MODE, TG3_FL_NOT_5705,
13131 0x00000000, 0x00000004 },
13132 { HOSTCC_MODE, TG3_FL_5705,
13133 0x00000000, 0x000000f6 },
13134 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13135 0x00000000, 0xffffffff },
13136 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13137 0x00000000, 0x000003ff },
13138 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13139 0x00000000, 0xffffffff },
13140 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13141 0x00000000, 0x000003ff },
13142 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13143 0x00000000, 0xffffffff },
13144 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13145 0x00000000, 0x000000ff },
13146 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13147 0x00000000, 0xffffffff },
13148 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13149 0x00000000, 0x000000ff },
13150 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13151 0x00000000, 0xffffffff },
13152 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13153 0x00000000, 0xffffffff },
13154 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13155 0x00000000, 0xffffffff },
13156 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13157 0x00000000, 0x000000ff },
13158 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13159 0x00000000, 0xffffffff },
13160 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13161 0x00000000, 0x000000ff },
13162 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13163 0x00000000, 0xffffffff },
13164 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13165 0x00000000, 0xffffffff },
13166 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13167 0x00000000, 0xffffffff },
13168 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13169 0x00000000, 0xffffffff },
13170 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13171 0x00000000, 0xffffffff },
13172 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13173 0xffffffff, 0x00000000 },
13174 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13175 0xffffffff, 0x00000000 },
13176
13177 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013178 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013179 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013180 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013181 0x00000000, 0x007fffff },
13182 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13183 0x00000000, 0x0000003f },
13184 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13185 0x00000000, 0x000001ff },
13186 { BUFMGR_MB_HIGH_WATER, 0x0000,
13187 0x00000000, 0x000001ff },
13188 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13189 0xffffffff, 0x00000000 },
13190 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13191 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013192
Michael Chana71116d2005-05-29 14:58:11 -070013193 /* Mailbox Registers */
13194 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13195 0x00000000, 0x000001ff },
13196 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13197 0x00000000, 0x000001ff },
13198 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13199 0x00000000, 0x000007ff },
13200 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13201 0x00000000, 0x000001ff },
13202
13203 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13204 };
13205
Michael Chanb16250e2006-09-27 16:10:14 -070013206 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013207 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013208 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013209 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013210 is_5750 = 1;
13211 }
Michael Chana71116d2005-05-29 14:58:11 -070013212
13213 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13214 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13215 continue;
13216
13217 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13218 continue;
13219
Joe Perches63c3a662011-04-26 08:12:10 +000013220 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013221 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13222 continue;
13223
Michael Chanb16250e2006-09-27 16:10:14 -070013224 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13225 continue;
13226
Michael Chana71116d2005-05-29 14:58:11 -070013227 offset = (u32) reg_tbl[i].offset;
13228 read_mask = reg_tbl[i].read_mask;
13229 write_mask = reg_tbl[i].write_mask;
13230
13231 /* Save the original register content */
13232 save_val = tr32(offset);
13233
13234 /* Determine the read-only value. */
13235 read_val = save_val & read_mask;
13236
13237 /* Write zero to the register, then make sure the read-only bits
13238 * are not changed and the read/write bits are all zeros.
13239 */
13240 tw32(offset, 0);
13241
13242 val = tr32(offset);
13243
13244 /* Test the read-only and read/write bits. */
13245 if (((val & read_mask) != read_val) || (val & write_mask))
13246 goto out;
13247
13248 /* Write ones to all the bits defined by RdMask and WrMask, then
13249 * make sure the read-only bits are not changed and the
13250 * read/write bits are all ones.
13251 */
13252 tw32(offset, read_mask | write_mask);
13253
13254 val = tr32(offset);
13255
13256 /* Test the read-only bits. */
13257 if ((val & read_mask) != read_val)
13258 goto out;
13259
13260 /* Test the read/write bits. */
13261 if ((val & write_mask) != write_mask)
13262 goto out;
13263
13264 tw32(offset, save_val);
13265 }
13266
13267 return 0;
13268
13269out:
Michael Chan9f88f292006-12-07 00:22:54 -080013270 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013271 netdev_err(tp->dev,
13272 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013273 tw32(offset, save_val);
13274 return -EIO;
13275}
13276
Michael Chan7942e1d2005-05-29 14:58:36 -070013277static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13278{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013279 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013280 int i;
13281 u32 j;
13282
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013283 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013284 for (j = 0; j < len; j += 4) {
13285 u32 val;
13286
13287 tg3_write_mem(tp, offset + j, test_pattern[i]);
13288 tg3_read_mem(tp, offset + j, &val);
13289 if (val != test_pattern[i])
13290 return -EIO;
13291 }
13292 }
13293 return 0;
13294}
13295
13296static int tg3_test_memory(struct tg3 *tp)
13297{
13298 static struct mem_entry {
13299 u32 offset;
13300 u32 len;
13301 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013302 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013303 { 0x00002000, 0x1c000},
13304 { 0xffffffff, 0x00000}
13305 }, mem_tbl_5705[] = {
13306 { 0x00000100, 0x0000c},
13307 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013308 { 0x00004000, 0x00800},
13309 { 0x00006000, 0x01000},
13310 { 0x00008000, 0x02000},
13311 { 0x00010000, 0x0e000},
13312 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013313 }, mem_tbl_5755[] = {
13314 { 0x00000200, 0x00008},
13315 { 0x00004000, 0x00800},
13316 { 0x00006000, 0x00800},
13317 { 0x00008000, 0x02000},
13318 { 0x00010000, 0x0c000},
13319 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013320 }, mem_tbl_5906[] = {
13321 { 0x00000200, 0x00008},
13322 { 0x00004000, 0x00400},
13323 { 0x00006000, 0x00400},
13324 { 0x00008000, 0x01000},
13325 { 0x00010000, 0x01000},
13326 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013327 }, mem_tbl_5717[] = {
13328 { 0x00000200, 0x00008},
13329 { 0x00010000, 0x0a000},
13330 { 0x00020000, 0x13c00},
13331 { 0xffffffff, 0x00000}
13332 }, mem_tbl_57765[] = {
13333 { 0x00000200, 0x00008},
13334 { 0x00004000, 0x00800},
13335 { 0x00006000, 0x09800},
13336 { 0x00010000, 0x0a000},
13337 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013338 };
13339 struct mem_entry *mem_tbl;
13340 int err = 0;
13341 int i;
13342
Joe Perches63c3a662011-04-26 08:12:10 +000013343 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013344 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013345 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013346 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013347 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013348 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013349 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013350 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013351 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013352 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013353 mem_tbl = mem_tbl_5705;
13354 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013355 mem_tbl = mem_tbl_570x;
13356
13357 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013358 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13359 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013360 break;
13361 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013362
Michael Chan7942e1d2005-05-29 14:58:36 -070013363 return err;
13364}
13365
Matt Carlsonbb158d62011-04-25 12:42:47 +000013366#define TG3_TSO_MSS 500
13367
13368#define TG3_TSO_IP_HDR_LEN 20
13369#define TG3_TSO_TCP_HDR_LEN 20
13370#define TG3_TSO_TCP_OPT_LEN 12
13371
13372static const u8 tg3_tso_header[] = {
133730x08, 0x00,
133740x45, 0x00, 0x00, 0x00,
133750x00, 0x00, 0x40, 0x00,
133760x40, 0x06, 0x00, 0x00,
133770x0a, 0x00, 0x00, 0x01,
133780x0a, 0x00, 0x00, 0x02,
133790x0d, 0x00, 0xe0, 0x00,
133800x00, 0x00, 0x01, 0x00,
133810x00, 0x00, 0x02, 0x00,
133820x80, 0x10, 0x10, 0x00,
133830x14, 0x09, 0x00, 0x00,
133840x01, 0x01, 0x08, 0x0a,
133850x11, 0x11, 0x11, 0x11,
133860x11, 0x11, 0x11, 0x11,
13387};
Michael Chan9f40dea2005-09-05 17:53:06 -070013388
Matt Carlson28a45952011-08-19 13:58:22 +000013389static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013390{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013391 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013392 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013393 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013394 struct sk_buff *skb;
13395 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013396 dma_addr_t map;
13397 int num_pkts, tx_len, rx_len, i, err;
13398 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013399 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013400 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013401
Matt Carlsonc8873402010-02-12 14:47:11 +000013402 tnapi = &tp->napi[0];
13403 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013404 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013405 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013406 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013407 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013408 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013409 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013410 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013411
Michael Chanc76949a2005-05-29 14:58:59 -070013412 err = -EIO;
13413
Matt Carlson4852a862011-04-13 11:05:07 +000013414 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013415 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013416 if (!skb)
13417 return -ENOMEM;
13418
Michael Chanc76949a2005-05-29 14:58:59 -070013419 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013420 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13421 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013422
Matt Carlson4852a862011-04-13 11:05:07 +000013423 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013424
Matt Carlson28a45952011-08-19 13:58:22 +000013425 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013426 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13427
13428 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13429 TG3_TSO_TCP_OPT_LEN;
13430
13431 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13432 sizeof(tg3_tso_header));
13433 mss = TG3_TSO_MSS;
13434
13435 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13436 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13437
13438 /* Set the total length field in the IP header */
13439 iph->tot_len = htons((u16)(mss + hdr_len));
13440
13441 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13442 TXD_FLAG_CPU_POST_DMA);
13443
Joe Perches63c3a662011-04-26 08:12:10 +000013444 if (tg3_flag(tp, HW_TSO_1) ||
13445 tg3_flag(tp, HW_TSO_2) ||
13446 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013447 struct tcphdr *th;
13448 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13449 th = (struct tcphdr *)&tx_data[val];
13450 th->check = 0;
13451 } else
13452 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13453
Joe Perches63c3a662011-04-26 08:12:10 +000013454 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013455 mss |= (hdr_len & 0xc) << 12;
13456 if (hdr_len & 0x10)
13457 base_flags |= 0x00000010;
13458 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013459 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013460 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013461 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013462 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013463 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13464 } else {
13465 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13466 }
13467
13468 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13469 } else {
13470 num_pkts = 1;
13471 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013472
13473 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13474 tx_len > VLAN_ETH_FRAME_LEN)
13475 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013476 }
13477
13478 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013479 tx_data[i] = (u8) (i & 0xff);
13480
Alexander Duyckf4188d82009-12-02 16:48:38 +000013481 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13482 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013483 dev_kfree_skb(skb);
13484 return -EIO;
13485 }
Michael Chanc76949a2005-05-29 14:58:59 -070013486
Matt Carlson0d681b22011-07-27 14:20:49 +000013487 val = tnapi->tx_prod;
13488 tnapi->tx_buffers[val].skb = skb;
13489 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13490
Michael Chanc76949a2005-05-29 14:58:59 -070013491 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013492 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013493
13494 udelay(10);
13495
Matt Carlson898a56f2009-08-28 14:02:40 +000013496 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013497
Matt Carlson84b67b22011-07-27 14:20:52 +000013498 budget = tg3_tx_avail(tnapi);
13499 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013500 base_flags | TXD_FLAG_END, mss, 0)) {
13501 tnapi->tx_buffers[val].skb = NULL;
13502 dev_kfree_skb(skb);
13503 return -EIO;
13504 }
Michael Chanc76949a2005-05-29 14:58:59 -070013505
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013506 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013507
Michael Chan6541b802012-03-04 14:48:14 +000013508 /* Sync BD data before updating mailbox */
13509 wmb();
13510
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013511 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13512 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013513
13514 udelay(10);
13515
Matt Carlson303fc922009-11-02 14:27:34 +000013516 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13517 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013518 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013519 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013520
13521 udelay(10);
13522
Matt Carlson898a56f2009-08-28 14:02:40 +000013523 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13524 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013525 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013526 (rx_idx == (rx_start_idx + num_pkts)))
13527 break;
13528 }
13529
Matt Carlsonba1142e2011-11-04 09:15:00 +000013530 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013531 dev_kfree_skb(skb);
13532
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013533 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013534 goto out;
13535
13536 if (rx_idx != rx_start_idx + num_pkts)
13537 goto out;
13538
Matt Carlsonbb158d62011-04-25 12:42:47 +000013539 val = data_off;
13540 while (rx_idx != rx_start_idx) {
13541 desc = &rnapi->rx_rcb[rx_start_idx++];
13542 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13543 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013544
Matt Carlsonbb158d62011-04-25 12:42:47 +000013545 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13546 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013547 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013548
Matt Carlsonbb158d62011-04-25 12:42:47 +000013549 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13550 - ETH_FCS_LEN;
13551
Matt Carlson28a45952011-08-19 13:58:22 +000013552 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013553 if (rx_len != tx_len)
13554 goto out;
13555
13556 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13557 if (opaque_key != RXD_OPAQUE_RING_STD)
13558 goto out;
13559 } else {
13560 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13561 goto out;
13562 }
13563 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13564 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013565 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013566 goto out;
13567 }
13568
13569 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013570 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013571 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13572 mapping);
13573 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013574 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013575 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13576 mapping);
13577 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013578 goto out;
13579
Matt Carlsonbb158d62011-04-25 12:42:47 +000013580 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13581 PCI_DMA_FROMDEVICE);
13582
Eric Dumazet9205fd92011-11-18 06:47:01 +000013583 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013584 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013585 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013586 goto out;
13587 }
Matt Carlson4852a862011-04-13 11:05:07 +000013588 }
13589
Michael Chanc76949a2005-05-29 14:58:59 -070013590 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013591
Eric Dumazet9205fd92011-11-18 06:47:01 +000013592 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013593out:
13594 return err;
13595}
13596
Matt Carlson00c266b2011-04-25 12:42:46 +000013597#define TG3_STD_LOOPBACK_FAILED 1
13598#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013599#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013600#define TG3_LOOPBACK_FAILED \
13601 (TG3_STD_LOOPBACK_FAILED | \
13602 TG3_JMB_LOOPBACK_FAILED | \
13603 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013604
Matt Carlson941ec902011-08-19 13:58:23 +000013605static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013606{
Matt Carlson28a45952011-08-19 13:58:22 +000013607 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013608 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013609 u32 jmb_pkt_sz = 9000;
13610
13611 if (tp->dma_limit)
13612 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013613
Matt Carlsonab789042011-01-25 15:58:54 +000013614 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13615 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13616
Matt Carlson28a45952011-08-19 13:58:22 +000013617 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013618 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13619 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013620 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013621 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013622 goto done;
13623 }
13624
Joe Perches953c96e2013-04-09 10:18:14 +000013625 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013626 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013627 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13628 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013629 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013630 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013631 goto done;
13632 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013633
Joe Perches63c3a662011-04-26 08:12:10 +000013634 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013635 int i;
13636
13637 /* Reroute all rx packets to the 1st queue */
13638 for (i = MAC_RSS_INDIR_TBL_0;
13639 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13640 tw32(i, 0x0);
13641 }
13642
Matt Carlson6e01b202011-08-19 13:58:20 +000013643 /* HW errata - mac loopback fails in some cases on 5780.
13644 * Normal traffic and PHY loopback are not affected by
13645 * errata. Also, the MAC loopback test is deprecated for
13646 * all newer ASIC revisions.
13647 */
Joe Perches41535772013-02-16 11:20:04 +000013648 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013649 !tg3_flag(tp, CPMU_PRESENT)) {
13650 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013651
Matt Carlson28a45952011-08-19 13:58:22 +000013652 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013653 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013654
13655 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013656 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013657 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013658
13659 tg3_mac_loopback(tp, false);
13660 }
Matt Carlson4852a862011-04-13 11:05:07 +000013661
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013662 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013663 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013664 int i;
13665
Matt Carlson941ec902011-08-19 13:58:23 +000013666 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013667
13668 /* Wait for link */
13669 for (i = 0; i < 100; i++) {
13670 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13671 break;
13672 mdelay(1);
13673 }
13674
Matt Carlson28a45952011-08-19 13:58:22 +000013675 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013676 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013677 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013678 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013679 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013680 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013681 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013682 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013683
Matt Carlson941ec902011-08-19 13:58:23 +000013684 if (do_extlpbk) {
13685 tg3_phy_lpbk_set(tp, 0, true);
13686
13687 /* All link indications report up, but the hardware
13688 * isn't really ready for about 20 msec. Double it
13689 * to be sure.
13690 */
13691 mdelay(40);
13692
13693 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013694 data[TG3_EXT_LOOPB_TEST] |=
13695 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013696 if (tg3_flag(tp, TSO_CAPABLE) &&
13697 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013698 data[TG3_EXT_LOOPB_TEST] |=
13699 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013700 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013701 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013702 data[TG3_EXT_LOOPB_TEST] |=
13703 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013704 }
13705
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013706 /* Re-enable gphy autopowerdown. */
13707 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13708 tg3_phy_toggle_apd(tp, true);
13709 }
Matt Carlson6833c042008-11-21 17:18:59 -080013710
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013711 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13712 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013713
Matt Carlsonab789042011-01-25 15:58:54 +000013714done:
13715 tp->phy_flags |= eee_cap;
13716
Michael Chan9f40dea2005-09-05 17:53:06 -070013717 return err;
13718}
13719
Michael Chan4cafd3f2005-05-29 14:56:34 -070013720static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13721 u64 *data)
13722{
Michael Chan566f86a2005-05-29 14:56:58 -070013723 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013724 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013725
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013726 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13727 if (tg3_power_up(tp)) {
13728 etest->flags |= ETH_TEST_FL_FAILED;
13729 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13730 return;
13731 }
13732 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013733 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013734
Michael Chan566f86a2005-05-29 14:56:58 -070013735 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13736
13737 if (tg3_test_nvram(tp) != 0) {
13738 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013739 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013740 }
Matt Carlson941ec902011-08-19 13:58:23 +000013741 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013742 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013743 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013744 }
Michael Chana71116d2005-05-29 14:58:11 -070013745 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013746 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013747
Michael Chanbbe832c2005-06-24 20:20:04 -070013748 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013749 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013750 tg3_netif_stop(tp);
13751 irq_sync = 1;
13752 }
13753
13754 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013755 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013756 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013757 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013758 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013759 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013760 if (!err)
13761 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013762
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013763 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013764 tg3_phy_reset(tp);
13765
Michael Chana71116d2005-05-29 14:58:11 -070013766 if (tg3_test_registers(tp) != 0) {
13767 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013768 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013769 }
Matt Carlson28a45952011-08-19 13:58:22 +000013770
Michael Chan7942e1d2005-05-29 14:58:36 -070013771 if (tg3_test_memory(tp) != 0) {
13772 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013773 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013774 }
Matt Carlson28a45952011-08-19 13:58:22 +000013775
Matt Carlson941ec902011-08-19 13:58:23 +000013776 if (doextlpbk)
13777 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13778
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013779 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013780 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013781
David S. Millerf47c11e2005-06-24 20:18:35 -070013782 tg3_full_unlock(tp);
13783
Michael Chand4bc3922005-05-29 14:59:20 -070013784 if (tg3_test_interrupt(tp) != 0) {
13785 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013786 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013787 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013788
13789 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013790
Michael Chana71116d2005-05-29 14:58:11 -070013791 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13792 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013793 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013794 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013795 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013796 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013797 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013798
13799 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013800
13801 if (irq_sync && !err2)
13802 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013803 }
Matt Carlson80096062010-08-02 11:26:06 +000013804 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013805 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013806
Michael Chan4cafd3f2005-05-29 14:56:34 -070013807}
13808
Ben Hutchings72608992013-11-18 22:59:43 +000013809static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013810{
13811 struct tg3 *tp = netdev_priv(dev);
13812 struct hwtstamp_config stmpconf;
13813
13814 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013815 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013816
13817 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13818 return -EFAULT;
13819
13820 if (stmpconf.flags)
13821 return -EINVAL;
13822
Ben Hutchings58b187c2013-11-14 00:40:56 +000013823 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13824 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013825 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013826
13827 switch (stmpconf.rx_filter) {
13828 case HWTSTAMP_FILTER_NONE:
13829 tp->rxptpctl = 0;
13830 break;
13831 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13832 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13833 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13834 break;
13835 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13836 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13837 TG3_RX_PTP_CTL_SYNC_EVNT;
13838 break;
13839 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13840 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13841 TG3_RX_PTP_CTL_DELAY_REQ;
13842 break;
13843 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13844 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13845 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13846 break;
13847 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13848 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13849 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13850 break;
13851 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13852 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13853 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13854 break;
13855 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13856 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13857 TG3_RX_PTP_CTL_SYNC_EVNT;
13858 break;
13859 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13860 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13861 TG3_RX_PTP_CTL_SYNC_EVNT;
13862 break;
13863 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13864 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13865 TG3_RX_PTP_CTL_SYNC_EVNT;
13866 break;
13867 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13868 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13869 TG3_RX_PTP_CTL_DELAY_REQ;
13870 break;
13871 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13872 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13873 TG3_RX_PTP_CTL_DELAY_REQ;
13874 break;
13875 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13876 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13877 TG3_RX_PTP_CTL_DELAY_REQ;
13878 break;
13879 default:
13880 return -ERANGE;
13881 }
13882
13883 if (netif_running(dev) && tp->rxptpctl)
13884 tw32(TG3_RX_PTP_CTL,
13885 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13886
Ben Hutchings58b187c2013-11-14 00:40:56 +000013887 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13888 tg3_flag_set(tp, TX_TSTAMP_EN);
13889 else
13890 tg3_flag_clear(tp, TX_TSTAMP_EN);
13891
Matt Carlson0a633ac2012-12-03 19:36:59 +000013892 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13893 -EFAULT : 0;
13894}
13895
Ben Hutchings72608992013-11-18 22:59:43 +000013896static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13897{
13898 struct tg3 *tp = netdev_priv(dev);
13899 struct hwtstamp_config stmpconf;
13900
13901 if (!tg3_flag(tp, PTP_CAPABLE))
13902 return -EOPNOTSUPP;
13903
13904 stmpconf.flags = 0;
13905 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13906 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13907
13908 switch (tp->rxptpctl) {
13909 case 0:
13910 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13911 break;
13912 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13913 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13914 break;
13915 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13916 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13917 break;
13918 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13919 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13920 break;
13921 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13922 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13923 break;
13924 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13925 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13926 break;
13927 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13928 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13929 break;
13930 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13931 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13932 break;
13933 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13934 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13935 break;
13936 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13937 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13938 break;
13939 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13940 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13941 break;
13942 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13943 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13944 break;
13945 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13946 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13947 break;
13948 default:
13949 WARN_ON_ONCE(1);
13950 return -ERANGE;
13951 }
13952
13953 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13954 -EFAULT : 0;
13955}
13956
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13958{
13959 struct mii_ioctl_data *data = if_mii(ifr);
13960 struct tg3 *tp = netdev_priv(dev);
13961 int err;
13962
Joe Perches63c3a662011-04-26 08:12:10 +000013963 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013964 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013965 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013966 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010013967 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Richard Cochran28b04112010-07-17 08:48:55 +000013968 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013969 }
13970
Matt Carlson33f401a2010-04-05 10:19:27 +000013971 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013973 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013974
13975 /* fallthru */
13976 case SIOCGMIIREG: {
13977 u32 mii_regval;
13978
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013979 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 break; /* We have no PHY */
13981
Matt Carlson34eea5a2011-04-20 07:57:38 +000013982 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013983 return -EAGAIN;
13984
David S. Millerf47c11e2005-06-24 20:18:35 -070013985 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013986 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13987 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013988 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013989
13990 data->val_out = mii_regval;
13991
13992 return err;
13993 }
13994
13995 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013996 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013997 break; /* We have no PHY */
13998
Matt Carlson34eea5a2011-04-20 07:57:38 +000013999 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080014000 return -EAGAIN;
14001
David S. Millerf47c11e2005-06-24 20:18:35 -070014002 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000014003 err = __tg3_writephy(tp, data->phy_id & 0x1f,
14004 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070014005 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014006
14007 return err;
14008
Matt Carlson0a633ac2012-12-03 19:36:59 +000014009 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000014010 return tg3_hwtstamp_set(dev, ifr);
14011
14012 case SIOCGHWTSTAMP:
14013 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000014014
Linus Torvalds1da177e2005-04-16 15:20:36 -070014015 default:
14016 /* do nothing */
14017 break;
14018 }
14019 return -EOPNOTSUPP;
14020}
14021
David S. Miller15f98502005-05-18 22:49:26 -070014022static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14023{
14024 struct tg3 *tp = netdev_priv(dev);
14025
14026 memcpy(ec, &tp->coal, sizeof(*ec));
14027 return 0;
14028}
14029
Michael Chand244c892005-07-05 14:42:33 -070014030static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14031{
14032 struct tg3 *tp = netdev_priv(dev);
14033 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
14034 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
14035
Joe Perches63c3a662011-04-26 08:12:10 +000014036 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014037 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
14038 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
14039 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
14040 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
14041 }
14042
14043 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
Satish Baddipadige087d7a8c2016-08-03 09:43:59 +053014044 (!ec->rx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014045 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
Ivan Veceraaabdd092016-09-01 11:28:59 +020014046 (!ec->tx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014047 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
14048 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
14049 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
14050 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
14051 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
14052 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
14053 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14054 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14055 return -EINVAL;
14056
Michael Chand244c892005-07-05 14:42:33 -070014057 /* Only copy relevant parameters, ignore all others. */
14058 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14059 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14060 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14061 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14062 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14063 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14064 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14065 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14066 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14067
14068 if (netif_running(dev)) {
14069 tg3_full_lock(tp, 0);
14070 __tg3_set_coalesce(tp, &tp->coal);
14071 tg3_full_unlock(tp);
14072 }
14073 return 0;
14074}
14075
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014076static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14077{
14078 struct tg3 *tp = netdev_priv(dev);
14079
14080 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14081 netdev_warn(tp->dev, "Board does not support EEE!\n");
14082 return -EOPNOTSUPP;
14083 }
14084
14085 if (edata->advertised != tp->eee.advertised) {
14086 netdev_warn(tp->dev,
14087 "Direct manipulation of EEE advertisement is not supported\n");
14088 return -EINVAL;
14089 }
14090
14091 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14092 netdev_warn(tp->dev,
14093 "Maximal Tx Lpi timer supported is %#x(u)\n",
14094 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14095 return -EINVAL;
14096 }
14097
14098 tp->eee = *edata;
14099
14100 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14101 tg3_warn_mgmt_link_flap(tp);
14102
14103 if (netif_running(tp->dev)) {
14104 tg3_full_lock(tp, 0);
14105 tg3_setup_eee(tp);
14106 tg3_phy_reset(tp);
14107 tg3_full_unlock(tp);
14108 }
14109
14110 return 0;
14111}
14112
14113static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14114{
14115 struct tg3 *tp = netdev_priv(dev);
14116
14117 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14118 netdev_warn(tp->dev,
14119 "Board does not support EEE!\n");
14120 return -EOPNOTSUPP;
14121 }
14122
14123 *edata = tp->eee;
14124 return 0;
14125}
14126
Jeff Garzik7282d492006-09-13 14:30:00 -040014127static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 .get_drvinfo = tg3_get_drvinfo,
14129 .get_regs_len = tg3_get_regs_len,
14130 .get_regs = tg3_get_regs,
14131 .get_wol = tg3_get_wol,
14132 .set_wol = tg3_set_wol,
14133 .get_msglevel = tg3_get_msglevel,
14134 .set_msglevel = tg3_set_msglevel,
14135 .nway_reset = tg3_nway_reset,
14136 .get_link = ethtool_op_get_link,
14137 .get_eeprom_len = tg3_get_eeprom_len,
14138 .get_eeprom = tg3_get_eeprom,
14139 .set_eeprom = tg3_set_eeprom,
14140 .get_ringparam = tg3_get_ringparam,
14141 .set_ringparam = tg3_set_ringparam,
14142 .get_pauseparam = tg3_get_pauseparam,
14143 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014144 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014145 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014146 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014148 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014149 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014150 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014151 .get_rxnfc = tg3_get_rxnfc,
14152 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014153 .get_rxfh = tg3_get_rxfh,
14154 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014155 .get_channels = tg3_get_channels,
14156 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014157 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014158 .get_eee = tg3_get_eee,
14159 .set_eee = tg3_set_eee,
Philippe Reynesb6f5be22016-09-25 23:31:24 +020014160 .get_link_ksettings = tg3_get_link_ksettings,
14161 .set_link_ksettings = tg3_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162};
14163
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014164static void tg3_get_stats64(struct net_device *dev,
14165 struct rtnl_link_stats64 *stats)
David S. Millerb4017c52012-03-01 17:57:40 -050014166{
14167 struct tg3 *tp = netdev_priv(dev);
14168
David S. Millerb4017c52012-03-01 17:57:40 -050014169 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014170 if (!tp->hw_stats) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014171 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014172 spin_unlock_bh(&tp->lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014173 return;
Michael Chan0f566b22012-07-29 19:15:44 +000014174 }
14175
David S. Millerb4017c52012-03-01 17:57:40 -050014176 tg3_get_nstats(tp, stats);
14177 spin_unlock_bh(&tp->lock);
David S. Millerb4017c52012-03-01 17:57:40 -050014178}
14179
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014180static void tg3_set_rx_mode(struct net_device *dev)
14181{
14182 struct tg3 *tp = netdev_priv(dev);
14183
14184 if (!netif_running(dev))
14185 return;
14186
14187 tg3_full_lock(tp, 0);
14188 __tg3_set_rx_mode(dev);
14189 tg3_full_unlock(tp);
14190}
14191
Matt Carlsonfaf16272012-02-13 10:20:07 +000014192static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14193 int new_mtu)
14194{
14195 dev->mtu = new_mtu;
14196
14197 if (new_mtu > ETH_DATA_LEN) {
14198 if (tg3_flag(tp, 5780_CLASS)) {
14199 netdev_update_features(dev);
14200 tg3_flag_clear(tp, TSO_CAPABLE);
14201 } else {
14202 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14203 }
14204 } else {
14205 if (tg3_flag(tp, 5780_CLASS)) {
14206 tg3_flag_set(tp, TSO_CAPABLE);
14207 netdev_update_features(dev);
14208 }
14209 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14210 }
14211}
14212
14213static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14214{
14215 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014216 int err;
14217 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014218
Matt Carlsonfaf16272012-02-13 10:20:07 +000014219 if (!netif_running(dev)) {
14220 /* We'll just catch it later when the
14221 * device is up'd.
14222 */
14223 tg3_set_mtu(dev, tp, new_mtu);
14224 return 0;
14225 }
14226
14227 tg3_phy_stop(tp);
14228
14229 tg3_netif_stop(tp);
14230
Nithin Sujirc6993df2014-02-06 14:13:05 -080014231 tg3_set_mtu(dev, tp, new_mtu);
14232
Matt Carlsonfaf16272012-02-13 10:20:07 +000014233 tg3_full_lock(tp, 1);
14234
14235 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14236
Michael Chan2fae5e32012-03-04 14:48:15 +000014237 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14238 * breaks all requests to 256 bytes.
14239 */
Brian King748a2402017-12-15 15:21:50 -060014240 if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
14241 tg3_asic_rev(tp) == ASIC_REV_5717 ||
Siva Reddy Kallame60ee412017-12-22 16:05:29 +053014242 tg3_asic_rev(tp) == ASIC_REV_5719 ||
14243 tg3_asic_rev(tp) == ASIC_REV_5720)
Joe Perches953c96e2013-04-09 10:18:14 +000014244 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014245
14246 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014247
14248 if (!err)
14249 tg3_netif_start(tp);
14250
14251 tg3_full_unlock(tp);
14252
14253 if (!err)
14254 tg3_phy_start(tp);
14255
14256 return err;
14257}
14258
14259static const struct net_device_ops tg3_netdev_ops = {
14260 .ndo_open = tg3_open,
14261 .ndo_stop = tg3_close,
14262 .ndo_start_xmit = tg3_start_xmit,
14263 .ndo_get_stats64 = tg3_get_stats64,
14264 .ndo_validate_addr = eth_validate_addr,
14265 .ndo_set_rx_mode = tg3_set_rx_mode,
14266 .ndo_set_mac_address = tg3_set_mac_addr,
14267 .ndo_do_ioctl = tg3_ioctl,
14268 .ndo_tx_timeout = tg3_tx_timeout,
14269 .ndo_change_mtu = tg3_change_mtu,
14270 .ndo_fix_features = tg3_fix_features,
14271 .ndo_set_features = tg3_set_features,
14272#ifdef CONFIG_NET_POLL_CONTROLLER
14273 .ndo_poll_controller = tg3_poll_controller,
14274#endif
14275};
14276
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014277static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278{
Michael Chan1b277772006-03-20 22:27:48 -080014279 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280
14281 tp->nvram_size = EEPROM_CHIP_SIZE;
14282
Matt Carlsone4f34112009-02-25 14:25:00 +000014283 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 return;
14285
Michael Chanb16250e2006-09-27 16:10:14 -070014286 if ((magic != TG3_EEPROM_MAGIC) &&
14287 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14288 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014289 return;
14290
14291 /*
14292 * Size the chip by reading offsets at increasing powers of two.
14293 * When we encounter our validation signature, we know the addressing
14294 * has wrapped around, and thus have our chip size.
14295 */
Michael Chan1b277772006-03-20 22:27:48 -080014296 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014297
14298 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014299 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300 return;
14301
Michael Chan18201802006-03-20 22:29:15 -080014302 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 break;
14304
14305 cursize <<= 1;
14306 }
14307
14308 tp->nvram_size = cursize;
14309}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014310
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014311static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014312{
14313 u32 val;
14314
Joe Perches63c3a662011-04-26 08:12:10 +000014315 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014316 return;
14317
14318 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014319 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014320 tg3_get_eeprom_size(tp);
14321 return;
14322 }
14323
Matt Carlson6d348f22009-02-25 14:25:52 +000014324 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014326 /* This is confusing. We want to operate on the
14327 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14328 * call will read from NVRAM and byteswap the data
14329 * according to the byteswapping settings for all
14330 * other register accesses. This ensures the data we
14331 * want will always reside in the lower 16-bits.
14332 * However, the data in NVRAM is in LE format, which
14333 * means the data from the NVRAM read will always be
14334 * opposite the endianness of the CPU. The 16-bit
14335 * byteswap then brings the data to CPU endianness.
14336 */
14337 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338 return;
14339 }
14340 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014341 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342}
14343
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014344static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345{
14346 u32 nvcfg1;
14347
14348 nvcfg1 = tr32(NVRAM_CFG1);
14349 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014350 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014351 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14353 tw32(NVRAM_CFG1, nvcfg1);
14354 }
14355
Joe Perches41535772013-02-16 11:20:04 +000014356 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014357 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014358 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014359 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14360 tp->nvram_jedecnum = JEDEC_ATMEL;
14361 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014362 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014363 break;
14364 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14365 tp->nvram_jedecnum = JEDEC_ATMEL;
14366 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14367 break;
14368 case FLASH_VENDOR_ATMEL_EEPROM:
14369 tp->nvram_jedecnum = JEDEC_ATMEL;
14370 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014371 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014372 break;
14373 case FLASH_VENDOR_ST:
14374 tp->nvram_jedecnum = JEDEC_ST;
14375 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014376 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014377 break;
14378 case FLASH_VENDOR_SAIFUN:
14379 tp->nvram_jedecnum = JEDEC_SAIFUN;
14380 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14381 break;
14382 case FLASH_VENDOR_SST_SMALL:
14383 case FLASH_VENDOR_SST_LARGE:
14384 tp->nvram_jedecnum = JEDEC_SST;
14385 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387 }
Matt Carlson8590a602009-08-28 12:29:16 +000014388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389 tp->nvram_jedecnum = JEDEC_ATMEL;
14390 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014391 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392 }
14393}
14394
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014395static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014396{
14397 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14398 case FLASH_5752PAGE_SIZE_256:
14399 tp->nvram_pagesize = 256;
14400 break;
14401 case FLASH_5752PAGE_SIZE_512:
14402 tp->nvram_pagesize = 512;
14403 break;
14404 case FLASH_5752PAGE_SIZE_1K:
14405 tp->nvram_pagesize = 1024;
14406 break;
14407 case FLASH_5752PAGE_SIZE_2K:
14408 tp->nvram_pagesize = 2048;
14409 break;
14410 case FLASH_5752PAGE_SIZE_4K:
14411 tp->nvram_pagesize = 4096;
14412 break;
14413 case FLASH_5752PAGE_SIZE_264:
14414 tp->nvram_pagesize = 264;
14415 break;
14416 case FLASH_5752PAGE_SIZE_528:
14417 tp->nvram_pagesize = 528;
14418 break;
14419 }
14420}
14421
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014422static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014423{
14424 u32 nvcfg1;
14425
14426 nvcfg1 = tr32(NVRAM_CFG1);
14427
Michael Chane6af3012005-04-21 17:12:05 -070014428 /* NVRAM protection for TPM */
14429 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014430 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014431
Michael Chan361b4ac2005-04-21 17:11:21 -070014432 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014433 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14434 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14435 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014436 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014437 break;
14438 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14439 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014440 tg3_flag_set(tp, NVRAM_BUFFERED);
14441 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014442 break;
14443 case FLASH_5752VENDOR_ST_M45PE10:
14444 case FLASH_5752VENDOR_ST_M45PE20:
14445 case FLASH_5752VENDOR_ST_M45PE40:
14446 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014447 tg3_flag_set(tp, NVRAM_BUFFERED);
14448 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014449 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014450 }
14451
Joe Perches63c3a662011-04-26 08:12:10 +000014452 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014453 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014454 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014455 /* For eeprom, set pagesize to maximum eeprom size */
14456 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14457
14458 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14459 tw32(NVRAM_CFG1, nvcfg1);
14460 }
14461}
14462
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014463static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014464{
Matt Carlson989a9d22007-05-05 11:51:05 -070014465 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014466
14467 nvcfg1 = tr32(NVRAM_CFG1);
14468
14469 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014470 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014471 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014472 protect = 1;
14473 }
Michael Chand3c7b882006-03-23 01:28:25 -080014474
Matt Carlson989a9d22007-05-05 11:51:05 -070014475 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14476 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014477 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14478 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14479 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14480 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14481 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014482 tg3_flag_set(tp, NVRAM_BUFFERED);
14483 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014484 tp->nvram_pagesize = 264;
14485 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14486 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14487 tp->nvram_size = (protect ? 0x3e200 :
14488 TG3_NVRAM_SIZE_512KB);
14489 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14490 tp->nvram_size = (protect ? 0x1f200 :
14491 TG3_NVRAM_SIZE_256KB);
14492 else
14493 tp->nvram_size = (protect ? 0x1f200 :
14494 TG3_NVRAM_SIZE_128KB);
14495 break;
14496 case FLASH_5752VENDOR_ST_M45PE10:
14497 case FLASH_5752VENDOR_ST_M45PE20:
14498 case FLASH_5752VENDOR_ST_M45PE40:
14499 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014500 tg3_flag_set(tp, NVRAM_BUFFERED);
14501 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014502 tp->nvram_pagesize = 256;
14503 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14504 tp->nvram_size = (protect ?
14505 TG3_NVRAM_SIZE_64KB :
14506 TG3_NVRAM_SIZE_128KB);
14507 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14508 tp->nvram_size = (protect ?
14509 TG3_NVRAM_SIZE_64KB :
14510 TG3_NVRAM_SIZE_256KB);
14511 else
14512 tp->nvram_size = (protect ?
14513 TG3_NVRAM_SIZE_128KB :
14514 TG3_NVRAM_SIZE_512KB);
14515 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014516 }
14517}
14518
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014519static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014520{
14521 u32 nvcfg1;
14522
14523 nvcfg1 = tr32(NVRAM_CFG1);
14524
14525 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014526 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14527 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14528 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14529 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14530 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014531 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014532 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014533
Matt Carlson8590a602009-08-28 12:29:16 +000014534 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14535 tw32(NVRAM_CFG1, nvcfg1);
14536 break;
14537 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14538 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14539 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14540 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14541 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014542 tg3_flag_set(tp, NVRAM_BUFFERED);
14543 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014544 tp->nvram_pagesize = 264;
14545 break;
14546 case FLASH_5752VENDOR_ST_M45PE10:
14547 case FLASH_5752VENDOR_ST_M45PE20:
14548 case FLASH_5752VENDOR_ST_M45PE40:
14549 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014550 tg3_flag_set(tp, NVRAM_BUFFERED);
14551 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014552 tp->nvram_pagesize = 256;
14553 break;
Michael Chan1b277772006-03-20 22:27:48 -080014554 }
14555}
14556
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014557static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014558{
14559 u32 nvcfg1, protect = 0;
14560
14561 nvcfg1 = tr32(NVRAM_CFG1);
14562
14563 /* NVRAM protection for TPM */
14564 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014565 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014566 protect = 1;
14567 }
14568
14569 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14570 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014571 case FLASH_5761VENDOR_ATMEL_ADB021D:
14572 case FLASH_5761VENDOR_ATMEL_ADB041D:
14573 case FLASH_5761VENDOR_ATMEL_ADB081D:
14574 case FLASH_5761VENDOR_ATMEL_ADB161D:
14575 case FLASH_5761VENDOR_ATMEL_MDB021D:
14576 case FLASH_5761VENDOR_ATMEL_MDB041D:
14577 case FLASH_5761VENDOR_ATMEL_MDB081D:
14578 case FLASH_5761VENDOR_ATMEL_MDB161D:
14579 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014580 tg3_flag_set(tp, NVRAM_BUFFERED);
14581 tg3_flag_set(tp, FLASH);
14582 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014583 tp->nvram_pagesize = 256;
14584 break;
14585 case FLASH_5761VENDOR_ST_A_M45PE20:
14586 case FLASH_5761VENDOR_ST_A_M45PE40:
14587 case FLASH_5761VENDOR_ST_A_M45PE80:
14588 case FLASH_5761VENDOR_ST_A_M45PE16:
14589 case FLASH_5761VENDOR_ST_M_M45PE20:
14590 case FLASH_5761VENDOR_ST_M_M45PE40:
14591 case FLASH_5761VENDOR_ST_M_M45PE80:
14592 case FLASH_5761VENDOR_ST_M_M45PE16:
14593 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014594 tg3_flag_set(tp, NVRAM_BUFFERED);
14595 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014596 tp->nvram_pagesize = 256;
14597 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014598 }
14599
14600 if (protect) {
14601 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14602 } else {
14603 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014604 case FLASH_5761VENDOR_ATMEL_ADB161D:
14605 case FLASH_5761VENDOR_ATMEL_MDB161D:
14606 case FLASH_5761VENDOR_ST_A_M45PE16:
14607 case FLASH_5761VENDOR_ST_M_M45PE16:
14608 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14609 break;
14610 case FLASH_5761VENDOR_ATMEL_ADB081D:
14611 case FLASH_5761VENDOR_ATMEL_MDB081D:
14612 case FLASH_5761VENDOR_ST_A_M45PE80:
14613 case FLASH_5761VENDOR_ST_M_M45PE80:
14614 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14615 break;
14616 case FLASH_5761VENDOR_ATMEL_ADB041D:
14617 case FLASH_5761VENDOR_ATMEL_MDB041D:
14618 case FLASH_5761VENDOR_ST_A_M45PE40:
14619 case FLASH_5761VENDOR_ST_M_M45PE40:
14620 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14621 break;
14622 case FLASH_5761VENDOR_ATMEL_ADB021D:
14623 case FLASH_5761VENDOR_ATMEL_MDB021D:
14624 case FLASH_5761VENDOR_ST_A_M45PE20:
14625 case FLASH_5761VENDOR_ST_M_M45PE20:
14626 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14627 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014628 }
14629 }
14630}
14631
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014632static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014633{
14634 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014635 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014636 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14637}
14638
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014639static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014640{
14641 u32 nvcfg1;
14642
14643 nvcfg1 = tr32(NVRAM_CFG1);
14644
14645 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14646 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14647 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14648 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014649 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014650 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14651
14652 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14653 tw32(NVRAM_CFG1, nvcfg1);
14654 return;
14655 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14656 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14657 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14658 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14659 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14660 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14661 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14662 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014663 tg3_flag_set(tp, NVRAM_BUFFERED);
14664 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014665
14666 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14667 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14668 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14669 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14670 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14671 break;
14672 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14673 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14674 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14675 break;
14676 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14677 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14678 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14679 break;
14680 }
14681 break;
14682 case FLASH_5752VENDOR_ST_M45PE10:
14683 case FLASH_5752VENDOR_ST_M45PE20:
14684 case FLASH_5752VENDOR_ST_M45PE40:
14685 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014686 tg3_flag_set(tp, NVRAM_BUFFERED);
14687 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014688
14689 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14690 case FLASH_5752VENDOR_ST_M45PE10:
14691 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14692 break;
14693 case FLASH_5752VENDOR_ST_M45PE20:
14694 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14695 break;
14696 case FLASH_5752VENDOR_ST_M45PE40:
14697 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14698 break;
14699 }
14700 break;
14701 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014702 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014703 return;
14704 }
14705
Matt Carlsona1b950d2009-09-01 13:20:17 +000014706 tg3_nvram_get_pagesize(tp, nvcfg1);
14707 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014708 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014709}
14710
14711
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014712static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014713{
14714 u32 nvcfg1;
14715
14716 nvcfg1 = tr32(NVRAM_CFG1);
14717
14718 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14719 case FLASH_5717VENDOR_ATMEL_EEPROM:
14720 case FLASH_5717VENDOR_MICRO_EEPROM:
14721 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014722 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014723 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14724
14725 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14726 tw32(NVRAM_CFG1, nvcfg1);
14727 return;
14728 case FLASH_5717VENDOR_ATMEL_MDB011D:
14729 case FLASH_5717VENDOR_ATMEL_ADB011B:
14730 case FLASH_5717VENDOR_ATMEL_ADB011D:
14731 case FLASH_5717VENDOR_ATMEL_MDB021D:
14732 case FLASH_5717VENDOR_ATMEL_ADB021B:
14733 case FLASH_5717VENDOR_ATMEL_ADB021D:
14734 case FLASH_5717VENDOR_ATMEL_45USPT:
14735 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014736 tg3_flag_set(tp, NVRAM_BUFFERED);
14737 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014738
14739 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14740 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014741 /* Detect size with tg3_nvram_get_size() */
14742 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014743 case FLASH_5717VENDOR_ATMEL_ADB021B:
14744 case FLASH_5717VENDOR_ATMEL_ADB021D:
14745 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14746 break;
14747 default:
14748 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14749 break;
14750 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014751 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014752 case FLASH_5717VENDOR_ST_M_M25PE10:
14753 case FLASH_5717VENDOR_ST_A_M25PE10:
14754 case FLASH_5717VENDOR_ST_M_M45PE10:
14755 case FLASH_5717VENDOR_ST_A_M45PE10:
14756 case FLASH_5717VENDOR_ST_M_M25PE20:
14757 case FLASH_5717VENDOR_ST_A_M25PE20:
14758 case FLASH_5717VENDOR_ST_M_M45PE20:
14759 case FLASH_5717VENDOR_ST_A_M45PE20:
14760 case FLASH_5717VENDOR_ST_25USPT:
14761 case FLASH_5717VENDOR_ST_45USPT:
14762 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014763 tg3_flag_set(tp, NVRAM_BUFFERED);
14764 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014765
14766 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14767 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014768 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014769 /* Detect size with tg3_nvram_get_size() */
14770 break;
14771 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014772 case FLASH_5717VENDOR_ST_A_M45PE20:
14773 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14774 break;
14775 default:
14776 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14777 break;
14778 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014779 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014780 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014781 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014782 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014783 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014784
14785 tg3_nvram_get_pagesize(tp, nvcfg1);
14786 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014787 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014788}
14789
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014790static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014791{
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014792 u32 nvcfg1, nvmpinstrp, nv_status;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014793
14794 nvcfg1 = tr32(NVRAM_CFG1);
14795 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14796
Joe Perches41535772013-02-16 11:20:04 +000014797 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014798 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14799 tg3_flag_set(tp, NO_NVRAM);
14800 return;
14801 }
14802
14803 switch (nvmpinstrp) {
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014804 case FLASH_5762_MX25L_100:
14805 case FLASH_5762_MX25L_200:
14806 case FLASH_5762_MX25L_400:
14807 case FLASH_5762_MX25L_800:
14808 case FLASH_5762_MX25L_160_320:
14809 tp->nvram_pagesize = 4096;
14810 tp->nvram_jedecnum = JEDEC_MACRONIX;
14811 tg3_flag_set(tp, NVRAM_BUFFERED);
14812 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
14813 tg3_flag_set(tp, FLASH);
14814 nv_status = tr32(NVRAM_AUTOSENSE_STATUS);
14815 tp->nvram_size =
14816 (1 << (nv_status >> AUTOSENSE_DEVID &
14817 AUTOSENSE_DEVID_MASK)
14818 << AUTOSENSE_SIZE_IN_MB);
14819 return;
14820
Michael Chanc86a8562013-01-06 12:51:08 +000014821 case FLASH_5762_EEPROM_HD:
14822 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014823 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014824 case FLASH_5762_EEPROM_LD:
14825 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014826 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014827 case FLASH_5720VENDOR_M_ST_M45PE20:
14828 /* This pinstrap supports multiple sizes, so force it
14829 * to read the actual size from location 0xf0.
14830 */
14831 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14832 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014833 }
14834 }
14835
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014836 switch (nvmpinstrp) {
14837 case FLASH_5720_EEPROM_HD:
14838 case FLASH_5720_EEPROM_LD:
14839 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014840 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014841
14842 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14843 tw32(NVRAM_CFG1, nvcfg1);
14844 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14845 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14846 else
14847 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14848 return;
14849 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14850 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14851 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14852 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14853 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14854 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14855 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14856 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14857 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14858 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14859 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14860 case FLASH_5720VENDOR_ATMEL_45USPT:
14861 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014862 tg3_flag_set(tp, NVRAM_BUFFERED);
14863 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014864
14865 switch (nvmpinstrp) {
14866 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14867 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14868 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14869 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14870 break;
14871 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14872 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14873 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14874 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14875 break;
14876 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14877 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14878 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14879 break;
14880 default:
Joe Perches41535772013-02-16 11:20:04 +000014881 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014882 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014883 break;
14884 }
14885 break;
14886 case FLASH_5720VENDOR_M_ST_M25PE10:
14887 case FLASH_5720VENDOR_M_ST_M45PE10:
14888 case FLASH_5720VENDOR_A_ST_M25PE10:
14889 case FLASH_5720VENDOR_A_ST_M45PE10:
14890 case FLASH_5720VENDOR_M_ST_M25PE20:
14891 case FLASH_5720VENDOR_M_ST_M45PE20:
14892 case FLASH_5720VENDOR_A_ST_M25PE20:
14893 case FLASH_5720VENDOR_A_ST_M45PE20:
14894 case FLASH_5720VENDOR_M_ST_M25PE40:
14895 case FLASH_5720VENDOR_M_ST_M45PE40:
14896 case FLASH_5720VENDOR_A_ST_M25PE40:
14897 case FLASH_5720VENDOR_A_ST_M45PE40:
14898 case FLASH_5720VENDOR_M_ST_M25PE80:
14899 case FLASH_5720VENDOR_M_ST_M45PE80:
14900 case FLASH_5720VENDOR_A_ST_M25PE80:
14901 case FLASH_5720VENDOR_A_ST_M45PE80:
14902 case FLASH_5720VENDOR_ST_25USPT:
14903 case FLASH_5720VENDOR_ST_45USPT:
14904 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014905 tg3_flag_set(tp, NVRAM_BUFFERED);
14906 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014907
14908 switch (nvmpinstrp) {
14909 case FLASH_5720VENDOR_M_ST_M25PE20:
14910 case FLASH_5720VENDOR_M_ST_M45PE20:
14911 case FLASH_5720VENDOR_A_ST_M25PE20:
14912 case FLASH_5720VENDOR_A_ST_M45PE20:
14913 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14914 break;
14915 case FLASH_5720VENDOR_M_ST_M25PE40:
14916 case FLASH_5720VENDOR_M_ST_M45PE40:
14917 case FLASH_5720VENDOR_A_ST_M25PE40:
14918 case FLASH_5720VENDOR_A_ST_M45PE40:
14919 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14920 break;
14921 case FLASH_5720VENDOR_M_ST_M25PE80:
14922 case FLASH_5720VENDOR_M_ST_M45PE80:
14923 case FLASH_5720VENDOR_A_ST_M25PE80:
14924 case FLASH_5720VENDOR_A_ST_M45PE80:
14925 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14926 break;
14927 default:
Joe Perches41535772013-02-16 11:20:04 +000014928 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014929 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014930 break;
14931 }
14932 break;
14933 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014934 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014935 return;
14936 }
14937
14938 tg3_nvram_get_pagesize(tp, nvcfg1);
14939 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014940 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014941
Joe Perches41535772013-02-16 11:20:04 +000014942 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014943 u32 val;
14944
14945 if (tg3_nvram_read(tp, 0, &val))
14946 return;
14947
14948 if (val != TG3_EEPROM_MAGIC &&
14949 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14950 tg3_flag_set(tp, NO_NVRAM);
14951 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014952}
14953
Linus Torvalds1da177e2005-04-16 15:20:36 -070014954/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014955static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014956{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014957 if (tg3_flag(tp, IS_SSB_CORE)) {
14958 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14959 tg3_flag_clear(tp, NVRAM);
14960 tg3_flag_clear(tp, NVRAM_BUFFERED);
14961 tg3_flag_set(tp, NO_NVRAM);
14962 return;
14963 }
14964
Linus Torvalds1da177e2005-04-16 15:20:36 -070014965 tw32_f(GRC_EEPROM_ADDR,
14966 (EEPROM_ADDR_FSM_RESET |
14967 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14968 EEPROM_ADDR_CLKPERD_SHIFT)));
14969
Michael Chan9d57f012006-12-07 00:23:25 -080014970 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014971
14972 /* Enable seeprom accesses. */
14973 tw32_f(GRC_LOCAL_CTRL,
14974 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14975 udelay(100);
14976
Joe Perches41535772013-02-16 11:20:04 +000014977 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14978 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014979 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980
Michael Chanec41c7d2006-01-17 02:40:55 -080014981 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014982 netdev_warn(tp->dev,
14983 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014984 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014985 return;
14986 }
Michael Chane6af3012005-04-21 17:12:05 -070014987 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988
Matt Carlson989a9d22007-05-05 11:51:05 -070014989 tp->nvram_size = 0;
14990
Joe Perches41535772013-02-16 11:20:04 +000014991 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014992 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014993 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014994 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014995 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14996 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14997 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014998 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014999 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070015000 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015001 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015002 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015003 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015004 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080015005 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015006 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15007 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015008 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015009 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15010 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000015011 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070015012 else
15013 tg3_get_nvram_info(tp);
15014
Matt Carlson989a9d22007-05-05 11:51:05 -070015015 if (tp->nvram_size == 0)
15016 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015017
Michael Chane6af3012005-04-21 17:12:05 -070015018 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080015019 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015020
15021 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015022 tg3_flag_clear(tp, NVRAM);
15023 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015024
15025 tg3_get_eeprom_size(tp);
15026 }
15027}
15028
Linus Torvalds1da177e2005-04-16 15:20:36 -070015029struct subsys_tbl_ent {
15030 u16 subsys_vendor, subsys_devid;
15031 u32 phy_id;
15032};
15033
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015034static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015036 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015037 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015038 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015039 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015040 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015041 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015042 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15043 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
15044 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015045 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015046 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015047 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015048 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15049 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
15050 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015051 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, 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_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015054 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015055 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015056 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015057 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058
15059 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015060 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015061 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015062 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015063 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015064 { TG3PCI_SUBVENDOR_ID_3COM,
15065 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15066 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015067 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015068 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015069 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070
15071 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015072 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015073 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015074 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015075 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015076 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015077 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015078 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015079 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080
15081 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015082 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015083 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015084 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015085 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015086 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15087 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15088 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015089 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015090 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015091 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015092
15093 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015094 { TG3PCI_SUBVENDOR_ID_IBM,
15095 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096};
15097
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015098static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099{
15100 int i;
15101
15102 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15103 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15104 tp->pdev->subsystem_vendor) &&
15105 (subsys_id_to_phy_id[i].subsys_devid ==
15106 tp->pdev->subsystem_device))
15107 return &subsys_id_to_phy_id[i];
15108 }
15109 return NULL;
15110}
15111
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015112static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015113{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015114 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015115
Matt Carlson79eb6902010-02-17 15:17:03 +000015116 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015117 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15118
Gary Zambranoa85feb82007-05-05 11:52:19 -070015119 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015120 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15121 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015122
Joe Perches41535772013-02-16 11:20:04 +000015123 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015124 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015125 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15126 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015127 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015128 val = tr32(VCPU_CFGSHDW);
15129 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015130 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015131 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015132 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015133 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015134 device_set_wakeup_enable(&tp->pdev->dev, true);
15135 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015136 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015137 }
15138
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15140 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15141 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015142 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15143 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015144 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015145
15146 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15147 tp->nic_sram_data_cfg = nic_cfg;
15148
15149 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15150 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015151 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15152 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15153 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154 (ver > 0) && (ver < 0x100))
15155 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15156
Joe Perches41535772013-02-16 11:20:04 +000015157 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015158 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15159
Nithin Sujir7c786062013-12-06 09:53:17 -080015160 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15161 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15162 tg3_asic_rev(tp) == ASIC_REV_5720)
15163 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15164
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15166 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15167 eeprom_phy_serdes = 1;
15168
15169 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15170 if (nic_phy_id != 0) {
15171 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15172 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15173
15174 eeprom_phy_id = (id1 >> 16) << 10;
15175 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15176 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15177 } else
15178 eeprom_phy_id = 0;
15179
Michael Chan7d0c41e2005-04-21 17:06:20 -070015180 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015181 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015182 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015183 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015184 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015185 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015186 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015187
Joe Perches63c3a662011-04-26 08:12:10 +000015188 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015189 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15190 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015191 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15193
15194 switch (led_cfg) {
15195 default:
15196 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15197 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15198 break;
15199
15200 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15201 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15202 break;
15203
15204 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15205 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015206
15207 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15208 * read on some older 5700/5701 bootcode.
15209 */
Joe Perches41535772013-02-16 11:20:04 +000015210 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15211 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015212 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15213
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214 break;
15215
15216 case SHASTA_EXT_LED_SHARED:
15217 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015218 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15219 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015220 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15221 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015222
15223 if (tg3_flag(tp, 5717_PLUS) ||
15224 tg3_asic_rev(tp) == ASIC_REV_5762)
15225 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15226 LED_CTRL_BLINK_RATE_MASK;
15227
Linus Torvalds1da177e2005-04-16 15:20:36 -070015228 break;
15229
15230 case SHASTA_EXT_LED_MAC:
15231 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15232 break;
15233
15234 case SHASTA_EXT_LED_COMBO:
15235 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015236 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15238 LED_CTRL_MODE_PHY_2);
15239 break;
15240
Stephen Hemminger855e1112008-04-16 16:37:28 -070015241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015242
Joe Perches41535772013-02-16 11:20:04 +000015243 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15244 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15246 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15247
Joe Perches41535772013-02-16 11:20:04 +000015248 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015249 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015250
Michael Chan9d26e212006-12-07 00:21:14 -080015251 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015252 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015253 if ((tp->pdev->subsystem_vendor ==
15254 PCI_VENDOR_ID_ARIMA) &&
15255 (tp->pdev->subsystem_device == 0x205a ||
15256 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015257 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015258 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015259 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15260 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015262
15263 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015264 tg3_flag_set(tp, ENABLE_ASF);
15265 if (tg3_flag(tp, 5750_PLUS))
15266 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015267 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015268
15269 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015270 tg3_flag(tp, 5750_PLUS))
15271 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015272
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015273 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015274 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015275 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015276
Joe Perches63c3a662011-04-26 08:12:10 +000015277 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015278 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015279 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015280 device_set_wakeup_enable(&tp->pdev->dev, true);
15281 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015282
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015284 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015285
15286 /* serdes signal pre-emphasis in register 0x590 set by */
15287 /* bootcode if bit 18 is set */
15288 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015289 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015290
Joe Perches63c3a662011-04-26 08:12:10 +000015291 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015292 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15293 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015294 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015295 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015296
Nithin Sujir942d1af2013-04-09 08:48:07 +000015297 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015298 u32 cfg3;
15299
15300 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015301 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15302 !tg3_flag(tp, 57765_PLUS) &&
15303 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015304 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015305 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15306 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15307 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15308 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015309 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015310
Matt Carlson14417062010-02-17 15:16:59 +000015311 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015312 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015313 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015314 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015315 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015316 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015317
15318 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15319 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015321done:
Joe Perches63c3a662011-04-26 08:12:10 +000015322 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015323 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015324 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015325 else
15326 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015327}
15328
Michael Chanc86a8562013-01-06 12:51:08 +000015329static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15330{
15331 int i, err;
15332 u32 val2, off = offset * 8;
15333
15334 err = tg3_nvram_lock(tp);
15335 if (err)
15336 return err;
15337
15338 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15339 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15340 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15341 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15342 udelay(10);
15343
15344 for (i = 0; i < 100; i++) {
15345 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15346 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15347 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15348 break;
15349 }
15350 udelay(10);
15351 }
15352
15353 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15354
15355 tg3_nvram_unlock(tp);
15356 if (val2 & APE_OTP_STATUS_CMD_DONE)
15357 return 0;
15358
15359 return -EBUSY;
15360}
15361
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015362static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015363{
15364 int i;
15365 u32 val;
15366
15367 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15368 tw32(OTP_CTRL, cmd);
15369
15370 /* Wait for up to 1 ms for command to execute. */
15371 for (i = 0; i < 100; i++) {
15372 val = tr32(OTP_STATUS);
15373 if (val & OTP_STATUS_CMD_DONE)
15374 break;
15375 udelay(10);
15376 }
15377
15378 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15379}
15380
15381/* Read the gphy configuration from the OTP region of the chip. The gphy
15382 * configuration is a 32-bit value that straddles the alignment boundary.
15383 * We do two 32-bit reads and then shift and merge the results.
15384 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015385static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015386{
15387 u32 bhalf_otp, thalf_otp;
15388
15389 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15390
15391 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15392 return 0;
15393
15394 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15395
15396 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15397 return 0;
15398
15399 thalf_otp = tr32(OTP_READ_DATA);
15400
15401 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15402
15403 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15404 return 0;
15405
15406 bhalf_otp = tr32(OTP_READ_DATA);
15407
15408 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15409}
15410
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015411static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015412{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015413 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015414
Nithin Sujir7c786062013-12-06 09:53:17 -080015415 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15416 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15417 adv |= ADVERTISED_1000baseT_Half;
15418 adv |= ADVERTISED_1000baseT_Full;
15419 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015420
15421 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15422 adv |= ADVERTISED_100baseT_Half |
15423 ADVERTISED_100baseT_Full |
15424 ADVERTISED_10baseT_Half |
15425 ADVERTISED_10baseT_Full |
15426 ADVERTISED_TP;
15427 else
15428 adv |= ADVERTISED_FIBRE;
15429
15430 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015431 tp->link_config.speed = SPEED_UNKNOWN;
15432 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015433 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015434 tp->link_config.active_speed = SPEED_UNKNOWN;
15435 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015436
15437 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015438}
15439
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015440static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015441{
15442 u32 hw_phy_id_1, hw_phy_id_2;
15443 u32 hw_phy_id, hw_phy_id_masked;
15444 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445
Matt Carlsone256f8a2011-03-09 16:58:24 +000015446 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015447 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015448 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15449
Michael Chan8151ad52012-07-29 19:15:41 +000015450 if (tg3_flag(tp, ENABLE_APE)) {
15451 switch (tp->pci_fn) {
15452 case 0:
15453 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15454 break;
15455 case 1:
15456 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15457 break;
15458 case 2:
15459 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15460 break;
15461 case 3:
15462 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15463 break;
15464 }
15465 }
15466
Nithin Sujir942d1af2013-04-09 08:48:07 +000015467 if (!tg3_flag(tp, ENABLE_ASF) &&
15468 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15469 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15470 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15471 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15472
Joe Perches63c3a662011-04-26 08:12:10 +000015473 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015474 return tg3_phy_init(tp);
15475
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015477 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 */
15479 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015480 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015481 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 } else {
15483 /* Now read the physical PHY_ID from the chip and verify
15484 * that it is sane. If it doesn't look good, we fall back
15485 * to either the hard-coded table based PHY_ID and failing
15486 * that the value found in the eeprom area.
15487 */
15488 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15489 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15490
15491 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15492 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15493 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15494
Matt Carlson79eb6902010-02-17 15:17:03 +000015495 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015496 }
15497
Matt Carlson79eb6902010-02-17 15:17:03 +000015498 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015499 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015500 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015501 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015502 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015503 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015505 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015506 /* Do nothing, phy ID already set up in
15507 * tg3_get_eeprom_hw_cfg().
15508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509 } else {
15510 struct subsys_tbl_ent *p;
15511
15512 /* No eeprom signature? Try the hardcoded
15513 * subsys device table.
15514 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015515 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015516 if (p) {
15517 tp->phy_id = p->phy_id;
15518 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15519 /* For now we saw the IDs 0xbc050cd0,
15520 * 0xbc050f80 and 0xbc050c30 on devices
15521 * connected to an BCM4785 and there are
15522 * probably more. Just assume that the phy is
15523 * supported when it is connected to a SSB core
15524 * for now.
15525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015528
Linus Torvalds1da177e2005-04-16 15:20:36 -070015529 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015530 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015531 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015532 }
15533 }
15534
Matt Carlsona6b68da2010-12-06 08:28:52 +000015535 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015536 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15537 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015538 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015539 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15540 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15541 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15542 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015543 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015544 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15545
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015546 tp->eee.supported = SUPPORTED_100baseT_Full |
15547 SUPPORTED_1000baseT_Full;
15548 tp->eee.advertised = ADVERTISED_100baseT_Full |
15549 ADVERTISED_1000baseT_Full;
15550 tp->eee.eee_enabled = 1;
15551 tp->eee.tx_lpi_enabled = 1;
15552 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15553 }
15554
Matt Carlsone256f8a2011-03-09 16:58:24 +000015555 tg3_phy_init_link_config(tp);
15556
Nithin Sujir942d1af2013-04-09 08:48:07 +000015557 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15558 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015559 !tg3_flag(tp, ENABLE_APE) &&
15560 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015561 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015562
15563 tg3_readphy(tp, MII_BMSR, &bmsr);
15564 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15565 (bmsr & BMSR_LSTATUS))
15566 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015567
Linus Torvalds1da177e2005-04-16 15:20:36 -070015568 err = tg3_phy_reset(tp);
15569 if (err)
15570 return err;
15571
Matt Carlson42b64a42011-05-19 12:12:49 +000015572 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015573
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015574 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015575 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15576 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015577
15578 tg3_writephy(tp, MII_BMCR,
15579 BMCR_ANENABLE | BMCR_ANRESTART);
15580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015581 }
15582
15583skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015584 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 err = tg3_init_5401phy_dsp(tp);
15586 if (err)
15587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588
Linus Torvalds1da177e2005-04-16 15:20:36 -070015589 err = tg3_init_5401phy_dsp(tp);
15590 }
15591
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592 return err;
15593}
15594
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015595static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015596{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015597 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015598 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015599 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015600 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015601
Matt Carlson535a4902011-07-20 10:20:56 +000015602 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015603 if (!vpd_data)
15604 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015605
Matt Carlson535a4902011-07-20 10:20:56 +000015606 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015607 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015608 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015609
15610 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15611 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15612 i += PCI_VPD_LRDT_TAG_SIZE;
15613
Matt Carlson535a4902011-07-20 10:20:56 +000015614 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015615 goto out_not_found;
15616
Matt Carlson184b8902010-04-05 10:19:25 +000015617 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15618 PCI_VPD_RO_KEYWORD_MFR_ID);
15619 if (j > 0) {
15620 len = pci_vpd_info_field_size(&vpd_data[j]);
15621
15622 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15623 if (j + len > block_end || len != 4 ||
15624 memcmp(&vpd_data[j], "1028", 4))
15625 goto partno;
15626
15627 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15628 PCI_VPD_RO_KEYWORD_VENDOR0);
15629 if (j < 0)
15630 goto partno;
15631
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)
15636 goto partno;
15637
Kees Cook715230a2013-03-27 06:40:50 +000015638 if (len >= sizeof(tp->fw_ver))
15639 len = sizeof(tp->fw_ver) - 1;
15640 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15641 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15642 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015643 }
15644
15645partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015646 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15647 PCI_VPD_RO_KEYWORD_PARTNO);
15648 if (i < 0)
15649 goto out_not_found;
15650
15651 len = pci_vpd_info_field_size(&vpd_data[i]);
15652
15653 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15654 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015655 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015656 goto out_not_found;
15657
15658 memcpy(tp->board_part_number, &vpd_data[i], len);
15659
Linus Torvalds1da177e2005-04-16 15:20:36 -070015660out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015661 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015662 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015663 return;
15664
15665out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015666 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015667 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15668 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015669 strcpy(tp->board_part_number, "BCM5717");
15670 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15671 strcpy(tp->board_part_number, "BCM5718");
15672 else
15673 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015674 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015675 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15676 strcpy(tp->board_part_number, "BCM57780");
15677 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15678 strcpy(tp->board_part_number, "BCM57760");
15679 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15680 strcpy(tp->board_part_number, "BCM57790");
15681 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15682 strcpy(tp->board_part_number, "BCM57788");
15683 else
15684 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015685 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015686 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15687 strcpy(tp->board_part_number, "BCM57761");
15688 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15689 strcpy(tp->board_part_number, "BCM57765");
15690 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15691 strcpy(tp->board_part_number, "BCM57781");
15692 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15693 strcpy(tp->board_part_number, "BCM57785");
15694 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15695 strcpy(tp->board_part_number, "BCM57791");
15696 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15697 strcpy(tp->board_part_number, "BCM57795");
15698 else
15699 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015700 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015701 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15702 strcpy(tp->board_part_number, "BCM57762");
15703 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15704 strcpy(tp->board_part_number, "BCM57766");
15705 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15706 strcpy(tp->board_part_number, "BCM57782");
15707 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15708 strcpy(tp->board_part_number, "BCM57786");
15709 else
15710 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015711 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015712 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015713 } else {
15714nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015715 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015717}
15718
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015719static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015720{
15721 u32 val;
15722
Matt Carlsone4f34112009-02-25 14:25:00 +000015723 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015724 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015725 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015726 val != 0)
15727 return 0;
15728
15729 return 1;
15730}
15731
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015732static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015733{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015734 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015735 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015736 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015737
15738 if (tg3_nvram_read(tp, 0xc, &offset) ||
15739 tg3_nvram_read(tp, 0x4, &start))
15740 return;
15741
15742 offset = tg3_nvram_logical_addr(tp, offset);
15743
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015744 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015745 return;
15746
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015747 if ((val & 0xfc000000) == 0x0c000000) {
15748 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015749 return;
15750
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015751 if (val == 0)
15752 newver = true;
15753 }
15754
Matt Carlson75f99362010-04-05 10:19:24 +000015755 dst_off = strlen(tp->fw_ver);
15756
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015757 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015758 if (TG3_VER_SIZE - dst_off < 16 ||
15759 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015760 return;
15761
15762 offset = offset + ver_offset - start;
15763 for (i = 0; i < 16; i += 4) {
15764 __be32 v;
15765 if (tg3_nvram_read_be32(tp, offset + i, &v))
15766 return;
15767
Matt Carlson75f99362010-04-05 10:19:24 +000015768 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015769 }
15770 } else {
15771 u32 major, minor;
15772
15773 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15774 return;
15775
15776 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15777 TG3_NVM_BCVER_MAJSFT;
15778 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015779 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15780 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015781 }
15782}
15783
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015784static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015785{
15786 u32 val, major, minor;
15787
15788 /* Use native endian representation */
15789 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15790 return;
15791
15792 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15793 TG3_NVM_HWSB_CFG1_MAJSFT;
15794 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15795 TG3_NVM_HWSB_CFG1_MINSFT;
15796
15797 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15798}
15799
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015800static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015801{
15802 u32 offset, major, minor, build;
15803
Matt Carlson75f99362010-04-05 10:19:24 +000015804 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015805
15806 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15807 return;
15808
15809 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15810 case TG3_EEPROM_SB_REVISION_0:
15811 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15812 break;
15813 case TG3_EEPROM_SB_REVISION_2:
15814 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15815 break;
15816 case TG3_EEPROM_SB_REVISION_3:
15817 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15818 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015819 case TG3_EEPROM_SB_REVISION_4:
15820 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15821 break;
15822 case TG3_EEPROM_SB_REVISION_5:
15823 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15824 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015825 case TG3_EEPROM_SB_REVISION_6:
15826 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15827 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015828 default:
15829 return;
15830 }
15831
Matt Carlsone4f34112009-02-25 14:25:00 +000015832 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015833 return;
15834
15835 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15836 TG3_EEPROM_SB_EDH_BLD_SHFT;
15837 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15838 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15839 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15840
15841 if (minor > 99 || build > 26)
15842 return;
15843
Matt Carlson75f99362010-04-05 10:19:24 +000015844 offset = strlen(tp->fw_ver);
15845 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15846 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015847
15848 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015849 offset = strlen(tp->fw_ver);
15850 if (offset < TG3_VER_SIZE - 1)
15851 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015852 }
15853}
15854
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015855static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015856{
15857 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015858 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015859
15860 for (offset = TG3_NVM_DIR_START;
15861 offset < TG3_NVM_DIR_END;
15862 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015863 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015864 return;
15865
15866 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15867 break;
15868 }
15869
15870 if (offset == TG3_NVM_DIR_END)
15871 return;
15872
Joe Perches63c3a662011-04-26 08:12:10 +000015873 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015874 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015875 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015876 return;
15877
Matt Carlsone4f34112009-02-25 14:25:00 +000015878 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015879 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015880 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015881 return;
15882
15883 offset += val - start;
15884
Matt Carlsonacd9c112009-02-25 14:26:33 +000015885 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015886
Matt Carlsonacd9c112009-02-25 14:26:33 +000015887 tp->fw_ver[vlen++] = ',';
15888 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015889
15890 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015891 __be32 v;
15892 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015893 return;
15894
Al Virob9fc7dc2007-12-17 22:59:57 -080015895 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015896
Matt Carlsonacd9c112009-02-25 14:26:33 +000015897 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15898 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015899 break;
15900 }
15901
Matt Carlsonacd9c112009-02-25 14:26:33 +000015902 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15903 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015904 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015905}
15906
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015907static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015908{
Matt Carlson7fd76442009-02-25 14:27:20 +000015909 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015910
15911 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15912 if (apedata != APE_SEG_SIG_MAGIC)
15913 return;
15914
15915 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15916 if (!(apedata & APE_FW_STATUS_READY))
15917 return;
15918
Michael Chan165f4d12012-07-16 16:23:59 +000015919 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15920 tg3_flag_set(tp, APE_HAS_NCSI);
15921}
15922
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015923static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015924{
15925 int vlen;
15926 u32 apedata;
15927 char *fwtype;
15928
Matt Carlson7fd76442009-02-25 14:27:20 +000015929 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15930
Michael Chan165f4d12012-07-16 16:23:59 +000015931 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015932 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015933 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15934 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015935 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015936 fwtype = "DASH";
15937
Matt Carlson7fd76442009-02-25 14:27:20 +000015938 vlen = strlen(tp->fw_ver);
15939
Matt Carlsonecc79642010-08-02 11:26:01 +000015940 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15941 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015942 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15943 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15944 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15945 (apedata & APE_FW_VERSION_BLDMSK));
15946}
15947
Michael Chanc86a8562013-01-06 12:51:08 +000015948static void tg3_read_otp_ver(struct tg3 *tp)
15949{
15950 u32 val, val2;
15951
Joe Perches41535772013-02-16 11:20:04 +000015952 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015953 return;
15954
15955 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15956 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15957 TG3_OTP_MAGIC0_VALID(val)) {
15958 u64 val64 = (u64) val << 32 | val2;
15959 u32 ver = 0;
15960 int i, vlen;
15961
15962 for (i = 0; i < 7; i++) {
15963 if ((val64 & 0xff) == 0)
15964 break;
15965 ver = val64 & 0xff;
15966 val64 >>= 8;
15967 }
15968 vlen = strlen(tp->fw_ver);
15969 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15970 }
15971}
15972
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015973static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015974{
15975 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015976 bool vpd_vers = false;
15977
15978 if (tp->fw_ver[0] != 0)
15979 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015980
Joe Perches63c3a662011-04-26 08:12:10 +000015981 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015982 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015983 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015984 return;
15985 }
15986
Matt Carlsonacd9c112009-02-25 14:26:33 +000015987 if (tg3_nvram_read(tp, 0, &val))
15988 return;
15989
15990 if (val == TG3_EEPROM_MAGIC)
15991 tg3_read_bc_ver(tp);
15992 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15993 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015994 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15995 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015996
Michael Chan165f4d12012-07-16 16:23:59 +000015997 if (tg3_flag(tp, ENABLE_ASF)) {
15998 if (tg3_flag(tp, ENABLE_APE)) {
15999 tg3_probe_ncsi(tp);
16000 if (!vpd_vers)
16001 tg3_read_dash_ver(tp);
16002 } else if (!vpd_vers) {
16003 tg3_read_mgmtfw_ver(tp);
16004 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000016005 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070016006
16007 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080016008}
16009
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016010static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
16011{
Joe Perches63c3a662011-04-26 08:12:10 +000016012 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016013 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000016014 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016015 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016016 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000016017 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016018}
16019
Benoit Taine9baa3c32014-08-08 15:56:03 +020016020static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080016021 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
16022 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
16023 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
16024 { },
16025};
16026
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016027static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000016028{
16029 struct pci_dev *peer;
16030 unsigned int func, devnr = tp->pdev->devfn & ~7;
16031
16032 for (func = 0; func < 8; func++) {
16033 peer = pci_get_slot(tp->pdev->bus, devnr | func);
16034 if (peer && peer != tp->pdev)
16035 break;
16036 pci_dev_put(peer);
16037 }
16038 /* 5704 can be configured in single-port mode, set peer to
16039 * tp->pdev in that case.
16040 */
16041 if (!peer) {
16042 peer = tp->pdev;
16043 return peer;
16044 }
16045
16046 /*
16047 * We don't need to keep the refcount elevated; there's no way
16048 * to remove one half of this device without removing the other
16049 */
16050 pci_dev_put(peer);
16051
16052 return peer;
16053}
16054
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016055static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000016056{
16057 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000016058 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016059 u32 reg;
16060
16061 /* All devices that use the alternate
16062 * ASIC REV location have a CPMU.
16063 */
16064 tg3_flag_set(tp, CPMU_PRESENT);
16065
16066 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016067 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016068 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16069 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016070 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016071 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16072 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016073 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16074 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016075 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16076 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016077 reg = TG3PCI_GEN2_PRODID_ASICREV;
16078 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16079 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16080 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16081 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16082 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16083 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16084 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16085 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16086 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16087 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16088 reg = TG3PCI_GEN15_PRODID_ASICREV;
16089 else
16090 reg = TG3PCI_PRODID_ASICREV;
16091
16092 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16093 }
16094
16095 /* Wrong chip ID in 5752 A0. This code can be removed later
16096 * as A0 is not in production.
16097 */
Joe Perches41535772013-02-16 11:20:04 +000016098 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016099 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16100
Joe Perches41535772013-02-16 11:20:04 +000016101 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016102 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16103
Joe Perches41535772013-02-16 11:20:04 +000016104 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16105 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16106 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016107 tg3_flag_set(tp, 5717_PLUS);
16108
Joe Perches41535772013-02-16 11:20:04 +000016109 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16110 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016111 tg3_flag_set(tp, 57765_CLASS);
16112
Michael Chanc65a17f2013-01-06 12:51:07 +000016113 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016114 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016115 tg3_flag_set(tp, 57765_PLUS);
16116
16117 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016118 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16119 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16120 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16121 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16122 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16123 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016124 tg3_flag(tp, 57765_PLUS))
16125 tg3_flag_set(tp, 5755_PLUS);
16126
Joe Perches41535772013-02-16 11:20:04 +000016127 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16128 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016129 tg3_flag_set(tp, 5780_CLASS);
16130
Joe Perches41535772013-02-16 11:20:04 +000016131 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16132 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16133 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016134 tg3_flag(tp, 5755_PLUS) ||
16135 tg3_flag(tp, 5780_CLASS))
16136 tg3_flag_set(tp, 5750_PLUS);
16137
Joe Perches41535772013-02-16 11:20:04 +000016138 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016139 tg3_flag(tp, 5750_PLUS))
16140 tg3_flag_set(tp, 5705_PLUS);
16141}
16142
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016143static bool tg3_10_100_only_device(struct tg3 *tp,
16144 const struct pci_device_id *ent)
16145{
16146 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16147
Joe Perches41535772013-02-16 11:20:04 +000016148 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16149 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016150 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16151 return true;
16152
16153 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016154 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016155 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16156 return true;
16157 } else {
16158 return true;
16159 }
16160 }
16161
16162 return false;
16163}
16164
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016165static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016166{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016167 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016168 u32 pci_state_reg, grc_misc_cfg;
16169 u32 val;
16170 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016171 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016172
Linus Torvalds1da177e2005-04-16 15:20:36 -070016173 /* Force memory write invalidate off. If we leave it on,
16174 * then on 5700_BX chips we have to enable a workaround.
16175 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16176 * to match the cacheline size. The Broadcom driver have this
16177 * workaround but turns MWI off all the times so never uses
16178 * it. This seems to suggest that the workaround is insufficient.
16179 */
16180 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16181 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16182 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16183
Matt Carlson16821282011-07-13 09:27:28 +000016184 /* Important! -- Make sure register accesses are byteswapped
16185 * correctly. Also, for those chips that require it, make
16186 * sure that indirect register accesses are enabled before
16187 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016188 */
16189 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16190 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016191 tp->misc_host_ctrl |= (misc_ctrl_reg &
16192 MISC_HOST_CTRL_CHIPREV);
16193 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16194 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016195
Matt Carlson42b123b2012-02-13 15:20:13 +000016196 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016197
Michael Chan68929142005-08-09 20:17:14 -070016198 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16199 * we need to disable memory and use config. cycles
16200 * only to access all registers. The 5702/03 chips
16201 * can mistakenly decode the special cycles from the
16202 * ICH chipsets as memory write cycles, causing corruption
16203 * of register and memory space. Only certain ICH bridges
16204 * will drive special cycles with non-zero data during the
16205 * address phase which can fall within the 5703's address
16206 * range. This is not an ICH bug as the PCI spec allows
16207 * non-zero address during special cycles. However, only
16208 * these ICH bridges are known to drive non-zero addresses
16209 * during special cycles.
16210 *
16211 * Since special cycles do not cross PCI bridges, we only
16212 * enable this workaround if the 5703 is on the secondary
16213 * bus of these ICH bridges.
16214 */
Joe Perches41535772013-02-16 11:20:04 +000016215 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16216 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016217 static struct tg3_dev_id {
16218 u32 vendor;
16219 u32 device;
16220 u32 rev;
16221 } ich_chipsets[] = {
16222 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16223 PCI_ANY_ID },
16224 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16225 PCI_ANY_ID },
16226 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16227 0xa },
16228 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16229 PCI_ANY_ID },
16230 { },
16231 };
16232 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16233 struct pci_dev *bridge = NULL;
16234
16235 while (pci_id->vendor != 0) {
16236 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16237 bridge);
16238 if (!bridge) {
16239 pci_id++;
16240 continue;
16241 }
16242 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016243 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016244 continue;
16245 }
16246 if (bridge->subordinate &&
16247 (bridge->subordinate->number ==
16248 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016249 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016250 pci_dev_put(bridge);
16251 break;
16252 }
16253 }
16254 }
16255
Joe Perches41535772013-02-16 11:20:04 +000016256 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016257 static struct tg3_dev_id {
16258 u32 vendor;
16259 u32 device;
16260 } bridge_chipsets[] = {
16261 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16262 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16263 { },
16264 };
16265 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16266 struct pci_dev *bridge = NULL;
16267
16268 while (pci_id->vendor != 0) {
16269 bridge = pci_get_device(pci_id->vendor,
16270 pci_id->device,
16271 bridge);
16272 if (!bridge) {
16273 pci_id++;
16274 continue;
16275 }
16276 if (bridge->subordinate &&
16277 (bridge->subordinate->number <=
16278 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016279 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016280 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016281 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016282 pci_dev_put(bridge);
16283 break;
16284 }
16285 }
16286 }
16287
Michael Chan4a29cc22006-03-19 13:21:12 -080016288 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16289 * DMA addresses > 40-bit. This bridge may have other additional
16290 * 57xx devices behind it in some 4-port NIC designs for example.
16291 * Any tg3 device found behind the bridge will also need the 40-bit
16292 * DMA workaround.
16293 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016294 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016295 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016296 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016297 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016298 struct pci_dev *bridge = NULL;
16299
16300 do {
16301 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16302 PCI_DEVICE_ID_SERVERWORKS_EPB,
16303 bridge);
16304 if (bridge && bridge->subordinate &&
16305 (bridge->subordinate->number <=
16306 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016307 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016308 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016309 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016310 pci_dev_put(bridge);
16311 break;
16312 }
16313 } while (bridge);
16314 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016315
Joe Perches41535772013-02-16 11:20:04 +000016316 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16317 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016318 tp->pdev_peer = tg3_find_peer(tp);
16319
Matt Carlson507399f2009-11-13 13:03:37 +000016320 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016321 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016322 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016323 else if (tg3_flag(tp, 57765_PLUS))
16324 tg3_flag_set(tp, HW_TSO_3);
16325 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016326 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016327 tg3_flag_set(tp, HW_TSO_2);
16328 else if (tg3_flag(tp, 5750_PLUS)) {
16329 tg3_flag_set(tp, HW_TSO_1);
16330 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016331 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16332 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016333 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016334 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16335 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16336 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016337 tg3_flag_set(tp, FW_TSO);
16338 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016339 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016340 tp->fw_needed = FIRMWARE_TG3TSO5;
16341 else
16342 tp->fw_needed = FIRMWARE_TG3TSO;
16343 }
16344
Matt Carlsondabc5c62011-05-19 12:12:52 +000016345 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016346 if (tg3_flag(tp, HW_TSO_1) ||
16347 tg3_flag(tp, HW_TSO_2) ||
16348 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016349 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016350 /* For firmware TSO, assume ASF is disabled.
16351 * We'll disable TSO later if we discover ASF
16352 * is enabled in tg3_get_eeprom_hw_cfg().
16353 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016354 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016355 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016356 tg3_flag_clear(tp, TSO_CAPABLE);
16357 tg3_flag_clear(tp, TSO_BUG);
16358 tp->fw_needed = NULL;
16359 }
16360
Joe Perches41535772013-02-16 11:20:04 +000016361 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016362 tp->fw_needed = FIRMWARE_TG3;
16363
Nithin Sujirc4dab502013-03-06 17:02:34 +000016364 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16365 tp->fw_needed = FIRMWARE_TG357766;
16366
Matt Carlson507399f2009-11-13 13:03:37 +000016367 tp->irq_max = 1;
16368
Joe Perches63c3a662011-04-26 08:12:10 +000016369 if (tg3_flag(tp, 5750_PLUS)) {
16370 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016371 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16372 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16373 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16374 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016375 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016376 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016377
Joe Perches63c3a662011-04-26 08:12:10 +000016378 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016379 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016380 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016382
Joe Perches63c3a662011-04-26 08:12:10 +000016383 if (tg3_flag(tp, 57765_PLUS)) {
16384 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016385 tp->irq_max = TG3_IRQ_MAX_VECS;
16386 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016387 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016388
Michael Chan91024262012-09-28 07:12:38 +000016389 tp->txq_max = 1;
16390 tp->rxq_max = 1;
16391 if (tp->irq_max > 1) {
16392 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16393 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16394
Joe Perches41535772013-02-16 11:20:04 +000016395 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16396 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016397 tp->txq_max = tp->irq_max - 1;
16398 }
16399
Matt Carlsonb7abee62012-06-07 12:56:54 +000016400 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016401 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016402 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016403
Joe Perches41535772013-02-16 11:20:04 +000016404 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016405 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016406
Joe Perches41535772013-02-16 11:20:04 +000016407 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16408 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16409 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16410 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016411 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016412
Joe Perches63c3a662011-04-26 08:12:10 +000016413 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016414 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016415 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016416
Joe Perches63c3a662011-04-26 08:12:10 +000016417 if (!tg3_flag(tp, 5705_PLUS) ||
16418 tg3_flag(tp, 5780_CLASS) ||
16419 tg3_flag(tp, USE_JUMBO_BDFLAG))
16420 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016421
Matt Carlson52f44902008-11-21 17:17:04 -080016422 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16423 &pci_state_reg);
16424
Jon Mason708ebb3a2011-06-27 12:56:50 +000016425 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016426 u16 lnkctl;
16427
Joe Perches63c3a662011-04-26 08:12:10 +000016428 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016429
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016430 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016431 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016432 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016433 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016434 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016435 }
Joe Perches41535772013-02-16 11:20:04 +000016436 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16437 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16438 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16439 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016440 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016441 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016442 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016443 }
Joe Perches41535772013-02-16 11:20:04 +000016444 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016445 /* BCM5785 devices are effectively PCIe devices, and should
16446 * follow PCIe codepaths, but do not have a PCIe capabilities
16447 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016448 */
Joe Perches63c3a662011-04-26 08:12:10 +000016449 tg3_flag_set(tp, PCI_EXPRESS);
16450 } else if (!tg3_flag(tp, 5705_PLUS) ||
16451 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016452 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16453 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016454 dev_err(&tp->pdev->dev,
16455 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016456 return -EIO;
16457 }
16458
16459 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016460 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016462
Michael Chan399de502005-10-03 14:02:39 -070016463 /* If we have an AMD 762 or VIA K8T800 chipset, write
16464 * reordering to the mailbox registers done by the host
16465 * controller can cause major troubles. We read back from
16466 * every mailbox register write to force the writes to be
16467 * posted to the chip in order.
16468 */
Matt Carlson41434702011-03-09 16:58:22 +000016469 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016470 !tg3_flag(tp, PCI_EXPRESS))
16471 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016472
Matt Carlson69fc4052008-12-21 20:19:57 -080016473 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16474 &tp->pci_cacheline_sz);
16475 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16476 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016477 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016478 tp->pci_lat_timer < 64) {
16479 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016480 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16481 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016482 }
16483
Matt Carlson16821282011-07-13 09:27:28 +000016484 /* Important! -- It is critical that the PCI-X hw workaround
16485 * situation is decided before the first MMIO register access.
16486 */
Joe Perches41535772013-02-16 11:20:04 +000016487 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016488 /* 5700 BX chips need to have their TX producer index
16489 * mailboxes written twice to workaround a bug.
16490 */
Joe Perches63c3a662011-04-26 08:12:10 +000016491 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016492
Matt Carlson52f44902008-11-21 17:17:04 -080016493 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016494 *
16495 * The workaround is to use indirect register accesses
16496 * for all chip writes not to mailbox registers.
16497 */
Joe Perches63c3a662011-04-26 08:12:10 +000016498 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016499 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016500
Joe Perches63c3a662011-04-26 08:12:10 +000016501 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016502
16503 /* The chip can have it's power management PCI config
16504 * space registers clobbered due to this bug.
16505 * So explicitly force the chip into D0 here.
16506 */
Matt Carlson9974a352007-10-07 23:27:28 -070016507 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016508 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016509 &pm_reg);
16510 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16511 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016512 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016513 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016514 pm_reg);
16515
16516 /* Also, force SERR#/PERR# in PCI command. */
16517 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16518 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16519 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16520 }
16521 }
16522
Linus Torvalds1da177e2005-04-16 15:20:36 -070016523 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016524 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016525 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016526 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016527
16528 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016529 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016530 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16531 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16532 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16533 }
16534
Michael Chan1ee582d2005-08-09 20:16:46 -070016535 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016536 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016537 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016538 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016539 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016540 tp->write32_tx_mbox = tg3_write32;
16541 tp->write32_rx_mbox = tg3_write32;
16542
16543 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016544 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016545 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016546 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016547 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016548 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016549 /*
16550 * Back to back register writes can cause problems on these
16551 * chips, the workaround is to read back all reg writes
16552 * except those to mailbox regs.
16553 *
16554 * See tg3_write_indirect_reg32().
16555 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016556 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016557 }
16558
Joe Perches63c3a662011-04-26 08:12:10 +000016559 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016560 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016561 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016562 tp->write32_rx_mbox = tg3_write_flush_reg32;
16563 }
Michael Chan20094932005-08-09 20:16:32 -070016564
Joe Perches63c3a662011-04-26 08:12:10 +000016565 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016566 tp->read32 = tg3_read_indirect_reg32;
16567 tp->write32 = tg3_write_indirect_reg32;
16568 tp->read32_mbox = tg3_read_indirect_mbox;
16569 tp->write32_mbox = tg3_write_indirect_mbox;
16570 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16571 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16572
16573 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016574 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016575
16576 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16577 pci_cmd &= ~PCI_COMMAND_MEMORY;
16578 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16579 }
Joe Perches41535772013-02-16 11:20:04 +000016580 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016581 tp->read32_mbox = tg3_read32_mbox_5906;
16582 tp->write32_mbox = tg3_write32_mbox_5906;
16583 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16584 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16585 }
Michael Chan68929142005-08-09 20:17:14 -070016586
Michael Chanbbadf502006-04-06 21:46:34 -070016587 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016588 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016589 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16590 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016591 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016592
Matt Carlson16821282011-07-13 09:27:28 +000016593 /* The memory arbiter has to be enabled in order for SRAM accesses
16594 * to succeed. Normally on powerup the tg3 chip firmware will make
16595 * sure it is enabled, but other entities such as system netboot
16596 * code might disable it.
16597 */
16598 val = tr32(MEMARB_MODE);
16599 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16600
Matt Carlson9dc5e342011-11-04 09:15:02 +000016601 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016602 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016603 tg3_flag(tp, 5780_CLASS)) {
16604 if (tg3_flag(tp, PCIX_MODE)) {
16605 pci_read_config_dword(tp->pdev,
16606 tp->pcix_cap + PCI_X_STATUS,
16607 &val);
16608 tp->pci_fn = val & 0x7;
16609 }
Joe Perches41535772013-02-16 11:20:04 +000016610 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16611 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16612 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016613 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016614 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16615 val = tr32(TG3_CPMU_STATUS);
16616
Joe Perches41535772013-02-16 11:20:04 +000016617 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016618 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16619 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016620 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16621 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016622 }
16623
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016624 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16625 tp->write32_tx_mbox = tg3_write_flush_reg32;
16626 tp->write32_rx_mbox = tg3_write_flush_reg32;
16627 }
16628
Michael Chan7d0c41e2005-04-21 17:06:20 -070016629 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016630 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016631 * determined before calling tg3_set_power_state() so that
16632 * we know whether or not to switch out of Vaux power.
16633 * When the flag is set, it means that GPIO1 is used for eeprom
16634 * write protect and also implies that it is a LOM where GPIOs
16635 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016636 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016637 tg3_get_eeprom_hw_cfg(tp);
16638
Matt Carlson1caf13e2013-03-06 17:02:29 +000016639 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016640 tg3_flag_clear(tp, TSO_CAPABLE);
16641 tg3_flag_clear(tp, TSO_BUG);
16642 tp->fw_needed = NULL;
16643 }
16644
Joe Perches63c3a662011-04-26 08:12:10 +000016645 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016646 /* Allow reads and writes to the
16647 * APE register and memory space.
16648 */
16649 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016650 PCISTATE_ALLOW_APE_SHMEM_WR |
16651 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016652 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16653 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016654
16655 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016656 }
16657
Matt Carlson16821282011-07-13 09:27:28 +000016658 /* Set up tp->grc_local_ctrl before calling
16659 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16660 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016661 * It is also used as eeprom write protect on LOMs.
16662 */
16663 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016664 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016665 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016666 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16667 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016668 /* Unused GPIO3 must be driven as output on 5752 because there
16669 * are no pull-up resistors on unused GPIO pins.
16670 */
Joe Perches41535772013-02-16 11:20:04 +000016671 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016672 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016673
Joe Perches41535772013-02-16 11:20:04 +000016674 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16675 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016676 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016677 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16678
Matt Carlson8d519ab2009-04-20 06:58:01 +000016679 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16680 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016681 /* Turn off the debug UART. */
16682 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016683 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016684 /* Keep VMain power. */
16685 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16686 GRC_LCLCTRL_GPIO_OUTPUT0;
16687 }
16688
Joe Perches41535772013-02-16 11:20:04 +000016689 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016690 tp->grc_local_ctrl |=
16691 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16692
Matt Carlson16821282011-07-13 09:27:28 +000016693 /* Switch out of Vaux if it is a NIC */
16694 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016695
Linus Torvalds1da177e2005-04-16 15:20:36 -070016696 /* Derive initial jumbo mode from MTU assigned in
16697 * ether_setup() via the alloc_etherdev() call
16698 */
Joe Perches63c3a662011-04-26 08:12:10 +000016699 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16700 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701
16702 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016703 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16704 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16705 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16706 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016707 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016708 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016709 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016710 }
16711
Joe Perches41535772013-02-16 11:20:04 +000016712 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016713 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016714
Linus Torvalds1da177e2005-04-16 15:20:36 -070016715 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016716 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16717 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16718 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16719 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016720 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16721 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16722 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016723
Joe Perches41535772013-02-16 11:20:04 +000016724 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16725 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016726 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016727 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016728 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729
Joe Perches63c3a662011-04-26 08:12:10 +000016730 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016731 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016732 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16733 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016734 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016735 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16736 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16737 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16738 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016739 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16740 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016741 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016742 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016743 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016744 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016745 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747
Joe Perches41535772013-02-16 11:20:04 +000016748 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16749 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016750 tp->phy_otp = tg3_read_otp_phycfg(tp);
16751 if (tp->phy_otp == 0)
16752 tp->phy_otp = TG3_OTP_DEFAULT;
16753 }
16754
Joe Perches63c3a662011-04-26 08:12:10 +000016755 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016756 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16757 else
16758 tp->mi_mode = MAC_MI_MODE_BASE;
16759
Linus Torvalds1da177e2005-04-16 15:20:36 -070016760 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016761 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16762 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016763 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16764
Matt Carlson4d958472011-04-20 07:57:35 +000016765 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016766 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016767 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016768 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16769 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016770 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16771 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16772 }
16773
Joe Perches41535772013-02-16 11:20:04 +000016774 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16775 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016776 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016777
Matt Carlson158d7ab2008-05-29 01:37:54 -070016778 err = tg3_mdio_init(tp);
16779 if (err)
16780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016781
16782 /* Initialize data/descriptor byte/word swapping. */
16783 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016784 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16785 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016786 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16787 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16788 GRC_MODE_B2HRX_ENABLE |
16789 GRC_MODE_HTX2B_ENABLE |
16790 GRC_MODE_HOST_STACKUP);
16791 else
16792 val &= GRC_MODE_HOST_STACKUP;
16793
Linus Torvalds1da177e2005-04-16 15:20:36 -070016794 tw32(GRC_MODE, val | tp->grc_mode);
16795
16796 tg3_switch_clocks(tp);
16797
16798 /* Clear this out for sanity. */
16799 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16800
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016801 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16802 tw32(TG3PCI_REG_BASE_ADDR, 0);
16803
Linus Torvalds1da177e2005-04-16 15:20:36 -070016804 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16805 &pci_state_reg);
16806 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016807 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016808 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16809 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16810 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16811 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016812 void __iomem *sram_base;
16813
16814 /* Write some dummy words into the SRAM status block
16815 * area, see if it reads back correctly. If the return
16816 * value is bad, force enable the PCIX workaround.
16817 */
16818 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16819
16820 writel(0x00000000, sram_base);
16821 writel(0x00000000, sram_base + 4);
16822 writel(0xffffffff, sram_base + 4);
16823 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016824 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016825 }
16826 }
16827
16828 udelay(50);
16829 tg3_nvram_init(tp);
16830
Nithin Sujirc4dab502013-03-06 17:02:34 +000016831 /* If the device has an NVRAM, no need to load patch firmware */
16832 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16833 !tg3_flag(tp, NO_NVRAM))
16834 tp->fw_needed = NULL;
16835
Linus Torvalds1da177e2005-04-16 15:20:36 -070016836 grc_misc_cfg = tr32(GRC_MISC_CFG);
16837 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16838
Joe Perches41535772013-02-16 11:20:04 +000016839 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016840 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16841 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016842 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843
Joe Perches63c3a662011-04-26 08:12:10 +000016844 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016845 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016846 tg3_flag_set(tp, TAGGED_STATUS);
16847 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016848 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16849 HOSTCC_MODE_CLRTICK_TXBD);
16850
16851 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16852 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16853 tp->misc_host_ctrl);
16854 }
16855
Matt Carlson3bda1252008-08-15 14:08:22 -070016856 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016857 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016858 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016859 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016860 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016861
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016862 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016863 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016864
16865 err = tg3_phy_probe(tp);
16866 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016867 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016868 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016869 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016870 }
16871
Matt Carlson184b8902010-04-05 10:19:25 +000016872 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016873 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016874
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016875 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16876 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016877 } else {
Joe Perches41535772013-02-16 11:20:04 +000016878 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016879 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016880 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016881 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016882 }
16883
16884 /* 5700 {AX,BX} chips have a broken status block link
16885 * change bit implementation, so we must use the
16886 * status register in those cases.
16887 */
Joe Perches41535772013-02-16 11:20:04 +000016888 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016889 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016890 else
Joe Perches63c3a662011-04-26 08:12:10 +000016891 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892
16893 /* The led_ctrl is set during tg3_phy_probe, here we might
16894 * have to force the link status polling mechanism based
16895 * upon subsystem IDs.
16896 */
16897 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016898 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016899 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16900 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016901 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016902 }
16903
16904 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016905 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016906 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016907 else
Joe Perches63c3a662011-04-26 08:12:10 +000016908 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016909
Nithin Sujir1743b832014-01-03 10:09:14 -080016910 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16911 tg3_flag_set(tp, POLL_CPMU_LINK);
16912
Eric Dumazet9205fd92011-11-18 06:47:01 +000016913 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016914 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016915 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016916 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016917 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016918#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016919 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016920#endif
16921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016922
Matt Carlson2c49a442010-09-30 10:34:35 +000016923 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16924 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016925 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16926
Matt Carlson2c49a442010-09-30 10:34:35 +000016927 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016928
16929 /* Increment the rx prod index on the rx std ring by at most
16930 * 8 for these chips to workaround hw errata.
16931 */
Joe Perches41535772013-02-16 11:20:04 +000016932 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16933 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16934 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016935 tp->rx_std_max_post = 8;
16936
Joe Perches63c3a662011-04-26 08:12:10 +000016937 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016938 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16939 PCIE_PWR_MGMT_L1_THRESH_MSK;
16940
Linus Torvalds1da177e2005-04-16 15:20:36 -070016941 return err;
16942}
16943
David S. Miller49b6e95f2007-03-29 01:38:42 -070016944#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016945static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016946{
16947 struct net_device *dev = tp->dev;
16948 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016949 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016950 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016951 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016952
David S. Miller49b6e95f2007-03-29 01:38:42 -070016953 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016954 if (addr && len == ETH_ALEN) {
16955 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957 }
16958 return -ENODEV;
16959}
16960
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016961static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016962{
16963 struct net_device *dev = tp->dev;
16964
Joe Perchesd458cdf2013-10-01 19:04:40 -070016965 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016966 return 0;
16967}
16968#endif
16969
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016970static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016971{
16972 struct net_device *dev = tp->dev;
16973 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016974 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016976
David S. Miller49b6e95f2007-03-29 01:38:42 -070016977#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016978 if (!tg3_get_macaddr_sparc(tp))
16979 return 0;
16980#endif
16981
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016982 if (tg3_flag(tp, IS_SSB_CORE)) {
16983 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16984 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16985 return 0;
16986 }
16987
Linus Torvalds1da177e2005-04-16 15:20:36 -070016988 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016989 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016990 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016991 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16992 mac_offset = 0xcc;
16993 if (tg3_nvram_lock(tp))
16994 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16995 else
16996 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016997 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016998 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016999 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000017000 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000017001 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000017002 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070017003 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017004
17005 /* First try to get it from MAC address mailbox. */
17006 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
17007 if ((hi >> 16) == 0x484b) {
17008 dev->dev_addr[0] = (hi >> 8) & 0xff;
17009 dev->dev_addr[1] = (hi >> 0) & 0xff;
17010
17011 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
17012 dev->dev_addr[2] = (lo >> 24) & 0xff;
17013 dev->dev_addr[3] = (lo >> 16) & 0xff;
17014 dev->dev_addr[4] = (lo >> 8) & 0xff;
17015 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017016
Michael Chan008652b2006-03-27 23:14:53 -080017017 /* Some old bootcode may report a 0 MAC address in SRAM */
17018 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
17019 }
17020 if (!addr_ok) {
17021 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000017022 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000017023 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000017024 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070017025 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
17026 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080017027 }
17028 /* Finally just fetch it out of the MAC control regs. */
17029 else {
17030 hi = tr32(MAC_ADDR_0_HIGH);
17031 lo = tr32(MAC_ADDR_0_LOW);
17032
17033 dev->dev_addr[5] = lo & 0xff;
17034 dev->dev_addr[4] = (lo >> 8) & 0xff;
17035 dev->dev_addr[3] = (lo >> 16) & 0xff;
17036 dev->dev_addr[2] = (lo >> 24) & 0xff;
17037 dev->dev_addr[1] = hi & 0xff;
17038 dev->dev_addr[0] = (hi >> 8) & 0xff;
17039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017040 }
17041
17042 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070017043#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070017044 if (!tg3_get_default_macaddr_sparc(tp))
17045 return 0;
17046#endif
17047 return -EINVAL;
17048 }
17049 return 0;
17050}
17051
David S. Miller59e6b432005-05-18 22:50:10 -070017052#define BOUNDARY_SINGLE_CACHELINE 1
17053#define BOUNDARY_MULTI_CACHELINE 2
17054
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017055static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070017056{
17057 int cacheline_size;
17058 u8 byte;
17059 int goal;
17060
17061 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17062 if (byte == 0)
17063 cacheline_size = 1024;
17064 else
17065 cacheline_size = (int) byte * 4;
17066
17067 /* On 5703 and later chips, the boundary bits have no
17068 * effect.
17069 */
Joe Perches41535772013-02-16 11:20:04 +000017070 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17071 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017072 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017073 goto out;
17074
17075#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17076 goal = BOUNDARY_MULTI_CACHELINE;
17077#else
17078#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17079 goal = BOUNDARY_SINGLE_CACHELINE;
17080#else
17081 goal = 0;
17082#endif
17083#endif
17084
Joe Perches63c3a662011-04-26 08:12:10 +000017085 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017086 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17087 goto out;
17088 }
17089
David S. Miller59e6b432005-05-18 22:50:10 -070017090 if (!goal)
17091 goto out;
17092
17093 /* PCI controllers on most RISC systems tend to disconnect
17094 * when a device tries to burst across a cache-line boundary.
17095 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17096 *
17097 * Unfortunately, for PCI-E there are only limited
17098 * write-side controls for this, and thus for reads
17099 * we will still get the disconnects. We'll also waste
17100 * these PCI cycles for both read and write for chips
17101 * other than 5700 and 5701 which do not implement the
17102 * boundary bits.
17103 */
Joe Perches63c3a662011-04-26 08:12:10 +000017104 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017105 switch (cacheline_size) {
17106 case 16:
17107 case 32:
17108 case 64:
17109 case 128:
17110 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17111 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17112 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17113 } else {
17114 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17115 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17116 }
17117 break;
17118
17119 case 256:
17120 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17121 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17122 break;
17123
17124 default:
17125 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17126 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17127 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017128 }
Joe Perches63c3a662011-04-26 08:12:10 +000017129 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017130 switch (cacheline_size) {
17131 case 16:
17132 case 32:
17133 case 64:
17134 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17135 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17136 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17137 break;
17138 }
17139 /* fallthrough */
17140 case 128:
17141 default:
17142 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17143 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17144 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017145 }
David S. Miller59e6b432005-05-18 22:50:10 -070017146 } else {
17147 switch (cacheline_size) {
17148 case 16:
17149 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17150 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17151 DMA_RWCTRL_WRITE_BNDRY_16);
17152 break;
17153 }
17154 /* fallthrough */
17155 case 32:
17156 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17157 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17158 DMA_RWCTRL_WRITE_BNDRY_32);
17159 break;
17160 }
17161 /* fallthrough */
17162 case 64:
17163 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17164 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17165 DMA_RWCTRL_WRITE_BNDRY_64);
17166 break;
17167 }
17168 /* fallthrough */
17169 case 128:
17170 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17171 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17172 DMA_RWCTRL_WRITE_BNDRY_128);
17173 break;
17174 }
17175 /* fallthrough */
17176 case 256:
17177 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17178 DMA_RWCTRL_WRITE_BNDRY_256);
17179 break;
17180 case 512:
17181 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17182 DMA_RWCTRL_WRITE_BNDRY_512);
17183 break;
17184 case 1024:
17185 default:
17186 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17187 DMA_RWCTRL_WRITE_BNDRY_1024);
17188 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017189 }
David S. Miller59e6b432005-05-18 22:50:10 -070017190 }
17191
17192out:
17193 return val;
17194}
17195
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017196static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017197 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017198{
17199 struct tg3_internal_buffer_desc test_desc;
17200 u32 sram_dma_descs;
17201 int i, ret;
17202
17203 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17204
17205 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17206 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17207 tw32(RDMAC_STATUS, 0);
17208 tw32(WDMAC_STATUS, 0);
17209
17210 tw32(BUFMGR_MODE, 0);
17211 tw32(FTQ_RESET, 0);
17212
17213 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17214 test_desc.addr_lo = buf_dma & 0xffffffff;
17215 test_desc.nic_mbuf = 0x00002100;
17216 test_desc.len = size;
17217
17218 /*
17219 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17220 * the *second* time the tg3 driver was getting loaded after an
17221 * initial scan.
17222 *
17223 * Broadcom tells me:
17224 * ...the DMA engine is connected to the GRC block and a DMA
17225 * reset may affect the GRC block in some unpredictable way...
17226 * The behavior of resets to individual blocks has not been tested.
17227 *
17228 * Broadcom noted the GRC reset will also reset all sub-components.
17229 */
17230 if (to_device) {
17231 test_desc.cqid_sqid = (13 << 8) | 2;
17232
17233 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17234 udelay(40);
17235 } else {
17236 test_desc.cqid_sqid = (16 << 8) | 7;
17237
17238 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17239 udelay(40);
17240 }
17241 test_desc.flags = 0x00000005;
17242
17243 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17244 u32 val;
17245
17246 val = *(((u32 *)&test_desc) + i);
17247 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17248 sram_dma_descs + (i * sizeof(u32)));
17249 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17250 }
17251 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17252
Matt Carlson859a588792010-04-05 10:19:28 +000017253 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017254 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017255 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017256 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017257
17258 ret = -ENODEV;
17259 for (i = 0; i < 40; i++) {
17260 u32 val;
17261
17262 if (to_device)
17263 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17264 else
17265 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17266 if ((val & 0xffff) == sram_dma_descs) {
17267 ret = 0;
17268 break;
17269 }
17270
17271 udelay(100);
17272 }
17273
17274 return ret;
17275}
17276
David S. Millerded73402005-05-23 13:59:47 -070017277#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017278
Benoit Taine9baa3c32014-08-08 15:56:03 +020017279static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017280 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17281 { },
17282};
17283
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017284static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017285{
17286 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017287 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017288 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017289
Matt Carlson4bae65c2010-11-24 08:31:52 +000017290 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17291 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017292 if (!buf) {
17293 ret = -ENOMEM;
17294 goto out_nofree;
17295 }
17296
17297 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17298 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17299
David S. Miller59e6b432005-05-18 22:50:10 -070017300 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017301
Joe Perches63c3a662011-04-26 08:12:10 +000017302 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017303 goto out;
17304
Joe Perches63c3a662011-04-26 08:12:10 +000017305 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017306 /* DMA read watermark not used on PCIE */
17307 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017308 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017309 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17310 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017311 tp->dma_rwctrl |= 0x003f0000;
17312 else
17313 tp->dma_rwctrl |= 0x003f000f;
17314 } else {
Joe Perches41535772013-02-16 11:20:04 +000017315 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17316 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017317 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017318 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017319
Michael Chan4a29cc22006-03-19 13:21:12 -080017320 /* If the 5704 is behind the EPB bridge, we can
17321 * do the less restrictive ONE_DMA workaround for
17322 * better performance.
17323 */
Joe Perches63c3a662011-04-26 08:12:10 +000017324 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017325 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017326 tp->dma_rwctrl |= 0x8000;
17327 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017328 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17329
Joe Perches41535772013-02-16 11:20:04 +000017330 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017331 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017332 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017333 tp->dma_rwctrl |=
17334 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17335 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17336 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017337 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017338 /* 5780 always in PCIX mode */
17339 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017340 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017341 /* 5714 always in PCIX mode */
17342 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017343 } else {
17344 tp->dma_rwctrl |= 0x001b000f;
17345 }
17346 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017347 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17348 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017349
Joe Perches41535772013-02-16 11:20:04 +000017350 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17351 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017352 tp->dma_rwctrl &= 0xfffffff0;
17353
Joe Perches41535772013-02-16 11:20:04 +000017354 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17355 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017356 /* Remove this if it causes problems for some boards. */
17357 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17358
17359 /* On 5700/5701 chips, we need to set this bit.
17360 * Otherwise the chip will issue cacheline transactions
17361 * to streamable DMA memory with not all the byte
17362 * enables turned on. This is an error on several
17363 * RISC PCI controllers, in particular sparc64.
17364 *
17365 * On 5703/5704 chips, this bit has been reassigned
17366 * a different meaning. In particular, it is used
17367 * on those chips to enable a PCI-X workaround.
17368 */
17369 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17370 }
17371
17372 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17373
Linus Torvalds1da177e2005-04-16 15:20:36 -070017374
Joe Perches41535772013-02-16 11:20:04 +000017375 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17376 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017377 goto out;
17378
David S. Miller59e6b432005-05-18 22:50:10 -070017379 /* It is best to perform DMA test with maximum write burst size
17380 * to expose the 5700/5701 write DMA bug.
17381 */
17382 saved_dma_rwctrl = tp->dma_rwctrl;
17383 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17384 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17385
Linus Torvalds1da177e2005-04-16 15:20:36 -070017386 while (1) {
17387 u32 *p = buf, i;
17388
17389 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17390 p[i] = i;
17391
17392 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017393 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017394 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017395 dev_err(&tp->pdev->dev,
17396 "%s: Buffer write failed. err = %d\n",
17397 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017398 break;
17399 }
17400
Linus Torvalds1da177e2005-04-16 15:20:36 -070017401 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017402 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017403 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017404 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17405 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017406 break;
17407 }
17408
17409 /* Verify it. */
17410 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17411 if (p[i] == i)
17412 continue;
17413
David S. Miller59e6b432005-05-18 22:50:10 -070017414 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17415 DMA_RWCTRL_WRITE_BNDRY_16) {
17416 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17418 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17419 break;
17420 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017421 dev_err(&tp->pdev->dev,
17422 "%s: Buffer corrupted on read back! "
17423 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017424 ret = -ENODEV;
17425 goto out;
17426 }
17427 }
17428
17429 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17430 /* Success. */
17431 ret = 0;
17432 break;
17433 }
17434 }
David S. Miller59e6b432005-05-18 22:50:10 -070017435 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17436 DMA_RWCTRL_WRITE_BNDRY_16) {
17437 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017438 * now look for chipsets that are known to expose the
17439 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017440 */
Matt Carlson41434702011-03-09 16:58:22 +000017441 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017442 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17443 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017444 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017445 /* Safe to use the calculated DMA boundary. */
17446 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017447 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017448
David S. Miller59e6b432005-05-18 22:50:10 -070017449 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017451
17452out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017453 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017454out_nofree:
17455 return ret;
17456}
17457
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017458static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017459{
Joe Perches63c3a662011-04-26 08:12:10 +000017460 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017461 tp->bufmgr_config.mbuf_read_dma_low_water =
17462 DEFAULT_MB_RDMA_LOW_WATER_5705;
17463 tp->bufmgr_config.mbuf_mac_rx_low_water =
17464 DEFAULT_MB_MACRX_LOW_WATER_57765;
17465 tp->bufmgr_config.mbuf_high_water =
17466 DEFAULT_MB_HIGH_WATER_57765;
17467
17468 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17469 DEFAULT_MB_RDMA_LOW_WATER_5705;
17470 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17471 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17472 tp->bufmgr_config.mbuf_high_water_jumbo =
17473 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017474 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017475 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_5705;
17479 tp->bufmgr_config.mbuf_high_water =
17480 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017481 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017482 tp->bufmgr_config.mbuf_mac_rx_low_water =
17483 DEFAULT_MB_MACRX_LOW_WATER_5906;
17484 tp->bufmgr_config.mbuf_high_water =
17485 DEFAULT_MB_HIGH_WATER_5906;
17486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017487
Michael Chanfdfec1722005-07-25 12:31:48 -070017488 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17489 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17490 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17491 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17492 tp->bufmgr_config.mbuf_high_water_jumbo =
17493 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17494 } else {
17495 tp->bufmgr_config.mbuf_read_dma_low_water =
17496 DEFAULT_MB_RDMA_LOW_WATER;
17497 tp->bufmgr_config.mbuf_mac_rx_low_water =
17498 DEFAULT_MB_MACRX_LOW_WATER;
17499 tp->bufmgr_config.mbuf_high_water =
17500 DEFAULT_MB_HIGH_WATER;
17501
17502 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17503 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17504 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17505 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17506 tp->bufmgr_config.mbuf_high_water_jumbo =
17507 DEFAULT_MB_HIGH_WATER_JUMBO;
17508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017509
17510 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17511 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17512}
17513
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017514static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017515{
Matt Carlson79eb6902010-02-17 15:17:03 +000017516 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17517 case TG3_PHY_ID_BCM5400: return "5400";
17518 case TG3_PHY_ID_BCM5401: return "5401";
17519 case TG3_PHY_ID_BCM5411: return "5411";
17520 case TG3_PHY_ID_BCM5701: return "5701";
17521 case TG3_PHY_ID_BCM5703: return "5703";
17522 case TG3_PHY_ID_BCM5704: return "5704";
17523 case TG3_PHY_ID_BCM5705: return "5705";
17524 case TG3_PHY_ID_BCM5750: return "5750";
17525 case TG3_PHY_ID_BCM5752: return "5752";
17526 case TG3_PHY_ID_BCM5714: return "5714";
17527 case TG3_PHY_ID_BCM5780: return "5780";
17528 case TG3_PHY_ID_BCM5755: return "5755";
17529 case TG3_PHY_ID_BCM5787: return "5787";
17530 case TG3_PHY_ID_BCM5784: return "5784";
17531 case TG3_PHY_ID_BCM5756: return "5722/5756";
17532 case TG3_PHY_ID_BCM5906: return "5906";
17533 case TG3_PHY_ID_BCM5761: return "5761";
17534 case TG3_PHY_ID_BCM5718C: return "5718C";
17535 case TG3_PHY_ID_BCM5718S: return "5718S";
17536 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017537 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017538 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017539 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017540 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017541 case 0: return "serdes";
17542 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017544}
17545
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017546static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017547{
Joe Perches63c3a662011-04-26 08:12:10 +000017548 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017549 strcpy(str, "PCI Express");
17550 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017551 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017552 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17553
17554 strcpy(str, "PCIX:");
17555
17556 if ((clock_ctrl == 7) ||
17557 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17558 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17559 strcat(str, "133MHz");
17560 else if (clock_ctrl == 0)
17561 strcat(str, "33MHz");
17562 else if (clock_ctrl == 2)
17563 strcat(str, "50MHz");
17564 else if (clock_ctrl == 4)
17565 strcat(str, "66MHz");
17566 else if (clock_ctrl == 6)
17567 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017568 } else {
17569 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017570 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017571 strcat(str, "66MHz");
17572 else
17573 strcat(str, "33MHz");
17574 }
Joe Perches63c3a662011-04-26 08:12:10 +000017575 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017576 strcat(str, ":32-bit");
17577 else
17578 strcat(str, ":64-bit");
17579 return str;
17580}
17581
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017582static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017583{
17584 struct ethtool_coalesce *ec = &tp->coal;
17585
17586 memset(ec, 0, sizeof(*ec));
17587 ec->cmd = ETHTOOL_GCOALESCE;
17588 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17589 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17590 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17591 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17592 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17593 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17594 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17595 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17596 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17597
17598 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17599 HOSTCC_MODE_CLRTICK_TXBD)) {
17600 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17601 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17602 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17603 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17604 }
Michael Chand244c892005-07-05 14:42:33 -070017605
Joe Perches63c3a662011-04-26 08:12:10 +000017606 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017607 ec->rx_coalesce_usecs_irq = 0;
17608 ec->tx_coalesce_usecs_irq = 0;
17609 ec->stats_block_coalesce_usecs = 0;
17610 }
David S. Miller15f98502005-05-18 22:49:26 -070017611}
17612
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017613static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017614 const struct pci_device_id *ent)
17615{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017616 struct net_device *dev;
17617 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017618 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017619 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017620 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017621 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017622 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017623
Joe Perches05dbe002010-02-17 19:44:19 +000017624 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017625
17626 err = pci_enable_device(pdev);
17627 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017628 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017629 return err;
17630 }
17631
Linus Torvalds1da177e2005-04-16 15:20:36 -070017632 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17633 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017634 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017635 goto err_out_disable_pdev;
17636 }
17637
17638 pci_set_master(pdev);
17639
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017640 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017641 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017642 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017643 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017644 }
17645
Linus Torvalds1da177e2005-04-16 15:20:36 -070017646 SET_NETDEV_DEV(dev, &pdev->dev);
17647
Linus Torvalds1da177e2005-04-16 15:20:36 -070017648 tp = netdev_priv(dev);
17649 tp->pdev = pdev;
17650 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017651 tp->rx_mode = TG3_DEF_RX_MODE;
17652 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017653 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017654 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017655
Linus Torvalds1da177e2005-04-16 15:20:36 -070017656 if (tg3_debug > 0)
17657 tp->msg_enable = tg3_debug;
17658 else
17659 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17660
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017661 if (pdev_is_ssb_gige_core(pdev)) {
17662 tg3_flag_set(tp, IS_SSB_CORE);
17663 if (ssb_gige_must_flush_posted_writes(pdev))
17664 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17665 if (ssb_gige_one_dma_at_once(pdev))
17666 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017667 if (ssb_gige_have_roboswitch(pdev)) {
17668 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017669 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017670 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017671 if (ssb_gige_is_rgmii(pdev))
17672 tg3_flag_set(tp, RGMII_MODE);
17673 }
17674
Linus Torvalds1da177e2005-04-16 15:20:36 -070017675 /* The word/byte swap controls here control register access byte
17676 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17677 * setting below.
17678 */
17679 tp->misc_host_ctrl =
17680 MISC_HOST_CTRL_MASK_PCI_INT |
17681 MISC_HOST_CTRL_WORD_SWAP |
17682 MISC_HOST_CTRL_INDIR_ACCESS |
17683 MISC_HOST_CTRL_PCISTATE_RW;
17684
17685 /* The NONFRM (non-frame) byte/word swap controls take effect
17686 * on descriptor entries, anything which isn't packet data.
17687 *
17688 * The StrongARM chips on the board (one for tx, one for rx)
17689 * are running in big-endian mode.
17690 */
17691 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17692 GRC_MODE_WSWAP_NONFRM_DATA);
17693#ifdef __BIG_ENDIAN
17694 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17695#endif
17696 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017697 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017698 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017699
Matt Carlsond5fe4882008-11-21 17:20:32 -080017700 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017701 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017702 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017703 err = -ENOMEM;
17704 goto err_out_free_dev;
17705 }
17706
Matt Carlsonc9cab242011-07-13 09:27:27 +000017707 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17708 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17709 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17710 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17711 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017712 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017713 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17714 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017715 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017716 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17717 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017718 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17719 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017720 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17721 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017722 tg3_flag_set(tp, ENABLE_APE);
17723 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17724 if (!tp->aperegs) {
17725 dev_err(&pdev->dev,
17726 "Cannot map APE registers, aborting\n");
17727 err = -ENOMEM;
17728 goto err_out_iounmap;
17729 }
17730 }
17731
Linus Torvalds1da177e2005-04-16 15:20:36 -070017732 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17733 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017734
Linus Torvalds1da177e2005-04-16 15:20:36 -070017735 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017736 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017737 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017738 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017739
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017740 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017741 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017742 dev_err(&pdev->dev,
17743 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017744 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017745 }
17746
Michael Chan4a29cc22006-03-19 13:21:12 -080017747 /* The EPB bridge inside 5714, 5715, and 5780 and any
17748 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017749 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17750 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17751 * do DMA address check in tg3_start_xmit().
17752 */
Joe Perches63c3a662011-04-26 08:12:10 +000017753 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017754 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017755 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017756 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017757#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017758 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017759#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017760 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017761 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017762
17763 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017764 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017765 err = pci_set_dma_mask(pdev, dma_mask);
17766 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017767 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017768 err = pci_set_consistent_dma_mask(pdev,
17769 persist_dma_mask);
17770 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017771 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17772 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017773 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017774 }
17775 }
17776 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017777 if (err || dma_mask == DMA_BIT_MASK(32)) {
17778 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017779 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017780 dev_err(&pdev->dev,
17781 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017782 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017783 }
17784 }
17785
Michael Chanfdfec1722005-07-25 12:31:48 -070017786 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017787
Matt Carlson0da06062011-05-19 12:12:53 +000017788 /* 5700 B0 chips do not support checksumming correctly due
17789 * to hardware bugs.
17790 */
Joe Perches41535772013-02-16 11:20:04 +000017791 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017792 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17793
17794 if (tg3_flag(tp, 5755_PLUS))
17795 features |= NETIF_F_IPV6_CSUM;
17796 }
17797
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017798 /* TSO is on by default on chips that support hardware TSO.
17799 * Firmware TSO on older chips gives lower performance, so it
17800 * is off by default, but can be enabled using ethtool.
17801 */
Joe Perches63c3a662011-04-26 08:12:10 +000017802 if ((tg3_flag(tp, HW_TSO_1) ||
17803 tg3_flag(tp, HW_TSO_2) ||
17804 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017805 (features & NETIF_F_IP_CSUM))
17806 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017807 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017808 if (features & NETIF_F_IPV6_CSUM)
17809 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017810 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017811 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17812 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17813 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17814 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17815 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017816 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017818
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017819 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17820 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017821 dev->vlan_features |= features;
17822
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017823 /*
17824 * Add loopback capability only for a subset of devices that support
17825 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17826 * loopback for the remaining devices.
17827 */
Joe Perches41535772013-02-16 11:20:04 +000017828 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017829 !tg3_flag(tp, CPMU_PRESENT))
17830 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017831 features |= NETIF_F_LOOPBACK;
17832
Matt Carlson0da06062011-05-19 12:12:53 +000017833 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017834 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017835
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -040017836 /* MTU range: 60 - 9000 or 1500, depending on hardware */
17837 dev->min_mtu = TG3_MIN_MTU;
17838 dev->max_mtu = TG3_MAX_MTU(tp);
17839
Joe Perches41535772013-02-16 11:20:04 +000017840 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017841 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017842 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017843 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017844 tp->rx_pending = 63;
17845 }
17846
Linus Torvalds1da177e2005-04-16 15:20:36 -070017847 err = tg3_get_device_address(tp);
17848 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017849 dev_err(&pdev->dev,
17850 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017851 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017852 }
17853
Matt Carlson78f90dc2009-11-13 13:03:42 +000017854 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17855 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17856 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017857 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017858 struct tg3_napi *tnapi = &tp->napi[i];
17859
17860 tnapi->tp = tp;
17861 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17862
17863 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017864 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017865 intmbx += 0x8;
17866 else
17867 intmbx += 0x4;
17868
17869 tnapi->consmbox = rcvmbx;
17870 tnapi->prodmbox = sndmbx;
17871
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017872 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017873 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017874 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017875 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017876
Joe Perches63c3a662011-04-26 08:12:10 +000017877 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017878 break;
17879
17880 /*
17881 * If we support MSIX, we'll be using RSS. If we're using
17882 * RSS, the first vector only handles link interrupts and the
17883 * remaining vectors handle rx and tx interrupts. Reuse the
17884 * mailbox values for the next iteration. The values we setup
17885 * above are still useful for the single vectored mode.
17886 */
17887 if (!i)
17888 continue;
17889
17890 rcvmbx += 0x8;
17891
17892 if (sndmbx & 0x4)
17893 sndmbx -= 0x4;
17894 else
17895 sndmbx += 0xc;
17896 }
17897
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017898 /*
17899 * Reset chip in case UNDI or EFI driver did not shutdown
17900 * DMA self test will enable WDMAC and we'll see (spurious)
17901 * pending DMA on the PCI bus at that point.
17902 */
17903 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17904 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017905 tg3_full_lock(tp, 0);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017906 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17907 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017908 tg3_full_unlock(tp);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017909 }
17910
17911 err = tg3_test_dma(tp);
17912 if (err) {
17913 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
17914 goto err_out_apeunmap;
17915 }
17916
Matt Carlsonc88864d2007-11-12 21:07:01 -080017917 tg3_init_coal(tp);
17918
Michael Chanc49a1562006-12-17 17:07:29 -080017919 pci_set_drvdata(pdev, dev);
17920
Joe Perches41535772013-02-16 11:20:04 +000017921 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17922 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17923 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017924 tg3_flag_set(tp, PTP_CAPABLE);
17925
Matt Carlson21f76382012-02-22 12:35:21 +000017926 tg3_timer_init(tp);
17927
Michael Chan402e1392013-02-14 12:13:41 +000017928 tg3_carrier_off(tp);
17929
Linus Torvalds1da177e2005-04-16 15:20:36 -070017930 err = register_netdev(dev);
17931 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017932 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017933 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017934 }
17935
Ivan Vecera20d14a52015-01-08 16:13:07 +010017936 if (tg3_flag(tp, PTP_CAPABLE)) {
17937 tg3_ptp_init(tp);
17938 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
17939 &tp->pdev->dev);
17940 if (IS_ERR(tp->ptp_clock))
17941 tp->ptp_clock = NULL;
17942 }
17943
Joe Perches05dbe002010-02-17 19:44:19 +000017944 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17945 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017946 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017947 tg3_bus_string(tp, str),
17948 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017949
Andrew Lunn22209432016-01-06 20:11:13 +010017950 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017951 char *ethtype;
17952
17953 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17954 ethtype = "10/100Base-TX";
17955 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17956 ethtype = "1000Base-SX";
17957 else
17958 ethtype = "10/100/1000Base-T";
17959
Matt Carlson5129c3a2010-04-05 10:19:23 +000017960 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017961 "(WireSpeed[%d], EEE[%d])\n",
17962 tg3_phy_string(tp), ethtype,
17963 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17964 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017965 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017966
Joe Perches05dbe002010-02-17 19:44:19 +000017967 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017968 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017969 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017970 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017971 tg3_flag(tp, ENABLE_ASF) != 0,
17972 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017973 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17974 tp->dma_rwctrl,
17975 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17976 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017977
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017978 pci_save_state(pdev);
17979
Linus Torvalds1da177e2005-04-16 15:20:36 -070017980 return 0;
17981
Matt Carlson0d3031d2007-10-10 18:02:43 -070017982err_out_apeunmap:
17983 if (tp->aperegs) {
17984 iounmap(tp->aperegs);
17985 tp->aperegs = NULL;
17986 }
17987
Linus Torvalds1da177e2005-04-16 15:20:36 -070017988err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017989 if (tp->regs) {
17990 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017991 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017993
17994err_out_free_dev:
17995 free_netdev(dev);
17996
17997err_out_free_res:
17998 pci_release_regions(pdev);
17999
18000err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080018001 if (pci_is_enabled(pdev))
18002 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018003 return err;
18004}
18005
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018006static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018007{
18008 struct net_device *dev = pci_get_drvdata(pdev);
18009
18010 if (dev) {
18011 struct tg3 *tp = netdev_priv(dev);
18012
Ivan Vecera20d14a52015-01-08 16:13:07 +010018013 tg3_ptp_fini(tp);
18014
Jesper Juhle3c55302012-04-09 22:50:15 +020018015 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080018016
Matt Carlsondb219972011-11-04 09:15:03 +000018017 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018018
David S. Miller1805b2f2011-10-24 18:18:09 -040018019 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018020 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018021 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018022 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070018023
Linus Torvalds1da177e2005-04-16 15:20:36 -070018024 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070018025 if (tp->aperegs) {
18026 iounmap(tp->aperegs);
18027 tp->aperegs = NULL;
18028 }
Michael Chan68929142005-08-09 20:17:14 -070018029 if (tp->regs) {
18030 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070018031 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070018032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018033 free_netdev(dev);
18034 pci_release_regions(pdev);
18035 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018036 }
18037}
18038
Eric Dumazetaa6027c2011-01-01 05:22:46 +000018039#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018040static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018041{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018042 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018043 struct net_device *dev = pci_get_drvdata(pdev);
18044 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018045 int err = 0;
18046
18047 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018048
18049 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018050 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018051
Matt Carlsondb219972011-11-04 09:15:03 +000018052 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018053 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018054 tg3_netif_stop(tp);
18055
Matt Carlson21f76382012-02-22 12:35:21 +000018056 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018057
David S. Millerf47c11e2005-06-24 20:18:35 -070018058 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018059 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070018060 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018061
18062 netif_device_detach(dev);
18063
David S. Millerf47c11e2005-06-24 20:18:35 -070018064 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070018065 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000018066 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070018067 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018068
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018069 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018070 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018071 int err2;
18072
David S. Millerf47c11e2005-06-24 20:18:35 -070018073 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018074
Joe Perches63c3a662011-04-26 08:12:10 +000018075 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018076 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018077 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018079
Matt Carlson21f76382012-02-22 12:35:21 +000018080 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018081
18082 netif_device_attach(dev);
18083 tg3_netif_start(tp);
18084
Michael Chanb9ec6c12006-07-25 16:37:27 -070018085out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018086 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018087
18088 if (!err2)
18089 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018090 }
18091
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018092unlock:
18093 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018094 return err;
18095}
18096
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018097static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018098{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018099 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018100 struct net_device *dev = pci_get_drvdata(pdev);
18101 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018102 int err = 0;
18103
18104 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018105
18106 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018107 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018108
Linus Torvalds1da177e2005-04-16 15:20:36 -070018109 netif_device_attach(dev);
18110
David S. Millerf47c11e2005-06-24 20:18:35 -070018111 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018112
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018113 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18114
Joe Perches63c3a662011-04-26 08:12:10 +000018115 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018116 err = tg3_restart_hw(tp,
18117 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018118 if (err)
18119 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018120
Matt Carlson21f76382012-02-22 12:35:21 +000018121 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018122
Linus Torvalds1da177e2005-04-16 15:20:36 -070018123 tg3_netif_start(tp);
18124
Michael Chanb9ec6c12006-07-25 16:37:27 -070018125out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018126 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018127
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018128 if (!err)
18129 tg3_phy_start(tp);
18130
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018131unlock:
18132 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018134}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018135#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018136
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018137static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18138
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018139static void tg3_shutdown(struct pci_dev *pdev)
18140{
18141 struct net_device *dev = pci_get_drvdata(pdev);
18142 struct tg3 *tp = netdev_priv(dev);
18143
18144 rtnl_lock();
18145 netif_device_detach(dev);
18146
18147 if (netif_running(dev))
18148 dev_close(dev);
18149
18150 if (system_state == SYSTEM_POWER_OFF)
18151 tg3_power_down(tp);
18152
18153 rtnl_unlock();
18154}
18155
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018156/**
18157 * tg3_io_error_detected - called when PCI error is detected
18158 * @pdev: Pointer to PCI device
18159 * @state: The current pci connection state
18160 *
18161 * This function is called after a PCI bus error affecting
18162 * this device has been detected.
18163 */
18164static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18165 pci_channel_state_t state)
18166{
18167 struct net_device *netdev = pci_get_drvdata(pdev);
18168 struct tg3 *tp = netdev_priv(netdev);
18169 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18170
18171 netdev_info(netdev, "PCI I/O error detected\n");
18172
18173 rtnl_lock();
18174
Gavin Shand8af4df2013-07-24 17:25:08 +080018175 /* We probably don't have netdev yet */
18176 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018177 goto done;
18178
Milton Miller1b0ff892016-09-29 13:24:08 -030018179 /* We needn't recover from permanent error */
18180 if (state == pci_channel_io_frozen)
18181 tp->pcierr_recovery = true;
18182
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018183 tg3_phy_stop(tp);
18184
18185 tg3_netif_stop(tp);
18186
Matt Carlson21f76382012-02-22 12:35:21 +000018187 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018188
18189 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018190 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018191
18192 netif_device_detach(netdev);
18193
18194 /* Clean up software state, even if MMIO is blocked */
18195 tg3_full_lock(tp, 0);
18196 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18197 tg3_full_unlock(tp);
18198
18199done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018200 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018201 if (netdev) {
18202 tg3_napi_enable(tp);
18203 dev_close(netdev);
18204 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018205 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018206 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018207 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018208 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018209
18210 rtnl_unlock();
18211
18212 return err;
18213}
18214
18215/**
18216 * tg3_io_slot_reset - called after the pci bus has been reset.
18217 * @pdev: Pointer to PCI device
18218 *
18219 * Restart the card from scratch, as if from a cold-boot.
18220 * At this point, the card has exprienced a hard reset,
18221 * followed by fixups by BIOS, and has its config space
18222 * set up identically to what it was at cold boot.
18223 */
18224static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18225{
18226 struct net_device *netdev = pci_get_drvdata(pdev);
18227 struct tg3 *tp = netdev_priv(netdev);
18228 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18229 int err;
18230
18231 rtnl_lock();
18232
18233 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018234 dev_err(&pdev->dev,
18235 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018236 goto done;
18237 }
18238
18239 pci_set_master(pdev);
18240 pci_restore_state(pdev);
18241 pci_save_state(pdev);
18242
Daniel Borkmann68293092013-08-13 11:45:13 -070018243 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018244 rc = PCI_ERS_RESULT_RECOVERED;
18245 goto done;
18246 }
18247
18248 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018249 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018250 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018251
18252 rc = PCI_ERS_RESULT_RECOVERED;
18253
18254done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018255 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018256 tg3_napi_enable(tp);
18257 dev_close(netdev);
18258 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018259 rtnl_unlock();
18260
18261 return rc;
18262}
18263
18264/**
18265 * tg3_io_resume - called when traffic can start flowing again.
18266 * @pdev: Pointer to PCI device
18267 *
18268 * This callback is called when the error recovery driver tells
18269 * us that its OK to resume normal operation.
18270 */
18271static void tg3_io_resume(struct pci_dev *pdev)
18272{
18273 struct net_device *netdev = pci_get_drvdata(pdev);
18274 struct tg3 *tp = netdev_priv(netdev);
18275 int err;
18276
18277 rtnl_lock();
18278
Milton Miller1b0ff892016-09-29 13:24:08 -030018279 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018280 goto done;
18281
18282 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018283 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018284 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018285 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018286 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018287 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018288 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18289 goto done;
18290 }
18291
18292 netif_device_attach(netdev);
18293
Matt Carlson21f76382012-02-22 12:35:21 +000018294 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018295
18296 tg3_netif_start(tp);
18297
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018298 tg3_full_unlock(tp);
18299
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018300 tg3_phy_start(tp);
18301
18302done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018303 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018304 rtnl_unlock();
18305}
18306
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018307static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018308 .error_detected = tg3_io_error_detected,
18309 .slot_reset = tg3_io_slot_reset,
18310 .resume = tg3_io_resume
18311};
18312
Linus Torvalds1da177e2005-04-16 15:20:36 -070018313static struct pci_driver tg3_driver = {
18314 .name = DRV_MODULE_NAME,
18315 .id_table = tg3_pci_tbl,
18316 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018317 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018318 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018319 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018320 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018321};
18322
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018323module_pci_driver(tg3_driver);