blob: aa1374d0af9313dfdbf6a7f8dfeea92e2fee7013 [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.
Siva Reddy Kallam0f2605f2018-07-16 11:13:31 +05309 * Copyright (C) 2018 Broadcom. All Rights Reserved. The term "Broadcom"
10 * refers to Broadcom Inc. and/or its subsidiaries.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070013 * Derived from proprietary unpublished source code,
Siva Reddy Kallam5a8bae92017-12-22 16:05:27 +053014 * Copyright (C) 2000-2016 Broadcom Corporation.
15 * Copyright (C) 2016-2017 Broadcom Ltd.
Siva Reddy Kallam0f2605f2018-07-16 11:13:31 +053016 * Copyright (C) 2018 Broadcom. All Rights Reserved. The term "Broadcom"
17 * refers to Broadcom Inc. and/or its subsidiaries.
Michael Chan49cabf42005-06-06 15:15:17 -070018 *
19 * Permission is hereby granted for the distribution of this firmware
20 * data in hexadecimal or equivalent format, provided this copyright
21 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <linux/module.h>
26#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000027#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010029#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
31#include <linux/compiler.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020034#include <linux/in.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000035#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
37#include <linux/pci.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/skbuff.h>
41#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000042#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070044#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070045#include <linux/brcmphy.h>
Michael Chane565eec2014-01-03 10:09:12 -080046#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/if_vlan.h>
48#include <linux/ip.h>
49#include <linux/tcp.h>
50#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070051#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020052#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080053#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000054#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000055#include <linux/hwmon.h>
56#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030059#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000061#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000063#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Matt Carlsonbe947302012-12-03 19:36:57 +000065#include <uapi/linux/net_tstamp.h>
66#include <linux/ptp_clock_kernel.h>
67
David S. Miller49b6e95f2007-03-29 01:38:42 -070068#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070070#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
Matt Carlson63532392008-11-03 16:49:57 -080073#define BAR_0 0
74#define BAR_2 2
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "tg3.h"
77
Joe Perches63c3a662011-04-26 08:12:10 +000078/* Functions & macros to verify TG3_FLAGS types */
79
80static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
81{
82 return test_bit(flag, bits);
83}
84
85static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
86{
87 set_bit(flag, bits);
88}
89
90static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
91{
92 clear_bit(flag, bits);
93}
94
95#define tg3_flag(tp, flag) \
96 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
97#define tg3_flag_set(tp, flag) \
98 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
99#define tg3_flag_clear(tp, flag) \
100 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +0000103#define TG3_MAJ_NUM 3
Michael Chande750e42014-05-11 20:22:55 -0700104#define TG3_MIN_NUM 137
Matt Carlson6867c842010-07-11 09:31:44 +0000105#define DRV_MODULE_VERSION \
106 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chande750e42014-05-11 20:22:55 -0700107#define DRV_MODULE_RELDATE "May 11, 2014"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000109#define RESET_KIND_SHUTDOWN 0
110#define RESET_KIND_INIT 1
111#define RESET_KIND_SUSPEND 2
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define TG3_DEF_RX_MODE 0
114#define TG3_DEF_TX_MODE 0
115#define TG3_DEF_MSG_ENABLE \
116 (NETIF_MSG_DRV | \
117 NETIF_MSG_PROBE | \
118 NETIF_MSG_LINK | \
119 NETIF_MSG_TIMER | \
120 NETIF_MSG_IFDOWN | \
121 NETIF_MSG_IFUP | \
122 NETIF_MSG_RX_ERR | \
123 NETIF_MSG_TX_ERR)
124
Matt Carlson520b2752011-06-13 13:39:02 +0000125#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* length of time before we decide the hardware is borked,
128 * and dev->tx_timeout() should be called to fix the problem
129 */
Joe Perches63c3a662011-04-26 08:12:10 +0000130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define TG3_TX_TIMEOUT (5 * HZ)
132
133/* hardware minimum and maximum for a single frame's data payload */
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -0400134#define TG3_MIN_MTU ETH_ZLEN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* These numbers seem to be hard coded in the NIC firmware somehow.
139 * You can't change the ring sizes, but you can change where you place
140 * them in the NIC onboard memory.
141 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000142#define TG3_RX_STD_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_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000146#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000147 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000148 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define TG3_DEF_RX_JUMBO_RING_PENDING 100
150
151/* Do not place this n-ring entries value into the tp struct itself,
152 * we really want to expose these constants to GCC so that modulo et
153 * al. operations are done with shifts and masks instead of with
154 * hw multiply/modulo instructions. Another solution would be to
155 * replace things like '% foo' with '& (foo - 1)'.
156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158#define TG3_TX_RING_SIZE 512
159#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
160
Matt Carlson2c49a442010-09-30 10:34:35 +0000161#define TG3_RX_STD_RING_BYTES(tp) \
162 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
163#define TG3_RX_JMB_RING_BYTES(tp) \
164 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
165#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000166 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
168 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
170
Matt Carlson287be122009-08-28 13:58:46 +0000171#define TG3_DMA_BYTE_ENAB 64
172
173#define TG3_RX_STD_DMA_SZ 1536
174#define TG3_RX_JMB_DMA_SZ 9046
175
176#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
177
178#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
179#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Matt Carlson2c49a442010-09-30 10:34:35 +0000181#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
182 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000183
Matt Carlson2c49a442010-09-30 10:34:35 +0000184#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
185 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000186
Matt Carlsond2757fc2010-04-12 06:58:27 +0000187/* Due to a hardware bug, the 5701 can only DMA to memory addresses
188 * that are at least dword aligned when used in PCIX mode. The driver
189 * works around this bug by double copying the packet. This workaround
190 * is built into the normal double copy length check for efficiency.
191 *
192 * However, the double copy is only necessary on those architectures
193 * where unaligned memory accesses are inefficient. For those architectures
194 * where unaligned memory accesses incur little penalty, we can reintegrate
195 * the 5701 in the normal rx path. Doing so saves a device structure
196 * dereference by hardcoding the double copy threshold in place.
197 */
198#define TG3_RX_COPY_THRESHOLD 256
199#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
200 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
201#else
202 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
203#endif
204
Matt Carlson81389f52011-08-31 11:44:49 +0000205#if (NET_IP_ALIGN != 0)
206#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
207#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000208#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000209#endif
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000212#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000213#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000214#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Matt Carlsonad829262008-11-21 17:16:16 -0800216#define TG3_RAW_IP_ALIGN 2
217
Michael Chane565eec2014-01-03 10:09:12 -0800218#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
219#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
220
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000221#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000222#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000223
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800224#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000225#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800226#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
227#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
228
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500229static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000230 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
233MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
234MODULE_LICENSE("GPL");
235MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800236MODULE_FIRMWARE(FIRMWARE_TG3);
237MODULE_FIRMWARE(FIRMWARE_TG3TSO);
238MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
241module_param(tg3_debug, int, 0);
242MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
243
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000244#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
245#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
246
Benoit Taine9baa3c32014-08-08 15:56:03 +0200247static const struct pci_device_id tg3_pci_tbl[] = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
267 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
268 TG3_DRV_DATA_FLAG_5705_10_100},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
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_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
274 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
275 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
282 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
288 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000296 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
297 PCI_VENDOR_ID_LENOVO,
298 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
299 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
302 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000321 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
322 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
323 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
324 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
325 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
326 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
330 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
340 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
342 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
348 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
349 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700350 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
351 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
352 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
353 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
354 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700355 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
356 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
357 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
358 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
359 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
360 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
361 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000362 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700363 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
366MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
367
Andreas Mohr50da8592006-08-14 23:54:30 -0700368static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000370} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 { "rx_octets" },
372 { "rx_fragments" },
373 { "rx_ucast_packets" },
374 { "rx_mcast_packets" },
375 { "rx_bcast_packets" },
376 { "rx_fcs_errors" },
377 { "rx_align_errors" },
378 { "rx_xon_pause_rcvd" },
379 { "rx_xoff_pause_rcvd" },
380 { "rx_mac_ctrl_rcvd" },
381 { "rx_xoff_entered" },
382 { "rx_frame_too_long_errors" },
383 { "rx_jabbers" },
384 { "rx_undersize_packets" },
385 { "rx_in_length_errors" },
386 { "rx_out_length_errors" },
387 { "rx_64_or_less_octet_packets" },
388 { "rx_65_to_127_octet_packets" },
389 { "rx_128_to_255_octet_packets" },
390 { "rx_256_to_511_octet_packets" },
391 { "rx_512_to_1023_octet_packets" },
392 { "rx_1024_to_1522_octet_packets" },
393 { "rx_1523_to_2047_octet_packets" },
394 { "rx_2048_to_4095_octet_packets" },
395 { "rx_4096_to_8191_octet_packets" },
396 { "rx_8192_to_9022_octet_packets" },
397
398 { "tx_octets" },
399 { "tx_collisions" },
400
401 { "tx_xon_sent" },
402 { "tx_xoff_sent" },
403 { "tx_flow_control" },
404 { "tx_mac_errors" },
405 { "tx_single_collisions" },
406 { "tx_mult_collisions" },
407 { "tx_deferred" },
408 { "tx_excessive_collisions" },
409 { "tx_late_collisions" },
410 { "tx_collide_2times" },
411 { "tx_collide_3times" },
412 { "tx_collide_4times" },
413 { "tx_collide_5times" },
414 { "tx_collide_6times" },
415 { "tx_collide_7times" },
416 { "tx_collide_8times" },
417 { "tx_collide_9times" },
418 { "tx_collide_10times" },
419 { "tx_collide_11times" },
420 { "tx_collide_12times" },
421 { "tx_collide_13times" },
422 { "tx_collide_14times" },
423 { "tx_collide_15times" },
424 { "tx_ucast_packets" },
425 { "tx_mcast_packets" },
426 { "tx_bcast_packets" },
427 { "tx_carrier_sense_errors" },
428 { "tx_discards" },
429 { "tx_errors" },
430
431 { "dma_writeq_full" },
432 { "dma_write_prioq_full" },
433 { "rxbds_empty" },
434 { "rx_discards" },
435 { "rx_errors" },
436 { "rx_threshold_hit" },
437
438 { "dma_readq_full" },
439 { "dma_read_prioq_full" },
440 { "tx_comp_queue_full" },
441
442 { "ring_set_send_prod_index" },
443 { "ring_status_update" },
444 { "nic_irqs" },
445 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000446 { "nic_tx_threshold_hit" },
447
448 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449};
450
Matt Carlson48fa55a2011-04-13 11:05:06 +0000451#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000452#define TG3_NVRAM_TEST 0
453#define TG3_LINK_TEST 1
454#define TG3_REGISTER_TEST 2
455#define TG3_MEMORY_TEST 3
456#define TG3_MAC_LOOPB_TEST 4
457#define TG3_PHY_LOOPB_TEST 5
458#define TG3_EXT_LOOPB_TEST 6
459#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000460
461
Andreas Mohr50da8592006-08-14 23:54:30 -0700462static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700463 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000464} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000465 [TG3_NVRAM_TEST] = { "nvram test (online) " },
466 [TG3_LINK_TEST] = { "link test (online) " },
467 [TG3_REGISTER_TEST] = { "register test (offline)" },
468 [TG3_MEMORY_TEST] = { "memory test (offline)" },
469 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
470 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
471 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
472 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700473};
474
Matt Carlson48fa55a2011-04-13 11:05:06 +0000475#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
476
477
Michael Chanb401e9e2005-12-19 16:27:04 -0800478static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
479{
480 writel(val, tp->regs + off);
481}
482
483static u32 tg3_read32(struct tg3 *tp, u32 off)
484{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000485 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800486}
487
Matt Carlson0d3031d2007-10-10 18:02:43 -0700488static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
489{
490 writel(val, tp->aperegs + off);
491}
492
493static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
494{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000495 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
499{
Michael Chan68929142005-08-09 20:17:14 -0700500 unsigned long flags;
501
502 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700503 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
504 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700505 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700506}
507
508static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
509{
510 writel(val, tp->regs + off);
511 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Michael Chan68929142005-08-09 20:17:14 -0700514static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
515{
516 unsigned long flags;
517 u32 val;
518
519 spin_lock_irqsave(&tp->indirect_lock, flags);
520 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
521 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
522 spin_unlock_irqrestore(&tp->indirect_lock, flags);
523 return val;
524}
525
526static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
527{
528 unsigned long flags;
529
530 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
531 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
532 TG3_64BIT_REG_LOW, val);
533 return;
534 }
Matt Carlson66711e662009-11-13 13:03:49 +0000535 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700536 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
537 TG3_64BIT_REG_LOW, val);
538 return;
539 }
540
541 spin_lock_irqsave(&tp->indirect_lock, flags);
542 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
543 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
544 spin_unlock_irqrestore(&tp->indirect_lock, flags);
545
546 /* In indirect mode when disabling interrupts, we also need
547 * to clear the interrupt bit in the GRC local ctrl register.
548 */
549 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
550 (val == 0x1)) {
551 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
552 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
553 }
554}
555
556static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
557{
558 unsigned long flags;
559 u32 val;
560
561 spin_lock_irqsave(&tp->indirect_lock, flags);
562 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
563 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
564 spin_unlock_irqrestore(&tp->indirect_lock, flags);
565 return val;
566}
567
Michael Chanb401e9e2005-12-19 16:27:04 -0800568/* usec_wait specifies the wait time in usec when writing to certain registers
569 * where it is unsafe to read back the register without some delay.
570 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
571 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
572 */
573static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Joe Perches63c3a662011-04-26 08:12:10 +0000575 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800576 /* Non-posted methods */
577 tp->write32(tp, off, val);
578 else {
579 /* Posted method */
580 tg3_write32(tp, off, val);
581 if (usec_wait)
582 udelay(usec_wait);
583 tp->read32(tp, off);
584 }
585 /* Wait again after the read for the posted method to guarantee that
586 * the wait time is met.
587 */
588 if (usec_wait)
589 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Michael Chan09ee9292005-08-09 20:17:00 -0700592static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
593{
594 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000595 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
596 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
597 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700598 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700599}
600
Michael Chan20094932005-08-09 20:16:32 -0700601static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 void __iomem *mbox = tp->regs + off;
604 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000605 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000607 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
608 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 readl(mbox);
610}
611
Michael Chanb5d37722006-09-27 16:06:21 -0700612static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
613{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000614 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700615}
616
617static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
618{
619 writel(val, tp->regs + off + GRCMBOX_BASE);
620}
621
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000622#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700623#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000624#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
625#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
626#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700627
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000628#define tw32(reg, val) tp->write32(tp, reg, val)
629#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
630#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
631#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
634{
Michael Chan68929142005-08-09 20:17:14 -0700635 unsigned long flags;
636
Joe Perches41535772013-02-16 11:20:04 +0000637 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700638 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
639 return;
640
Michael Chan68929142005-08-09 20:17:14 -0700641 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000642 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700643 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
644 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Michael Chanbbadf502006-04-06 21:46:34 -0700646 /* Always leave this as zero. */
647 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
648 } else {
649 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
650 tw32_f(TG3PCI_MEM_WIN_DATA, val);
651
652 /* Always leave this as zero. */
653 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
654 }
Michael Chan68929142005-08-09 20:17:14 -0700655 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
659{
Michael Chan68929142005-08-09 20:17:14 -0700660 unsigned long flags;
661
Joe Perches41535772013-02-16 11:20:04 +0000662 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700663 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
664 *val = 0;
665 return;
666 }
667
Michael Chan68929142005-08-09 20:17:14 -0700668 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000669 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700670 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
671 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Michael Chanbbadf502006-04-06 21:46:34 -0700673 /* Always leave this as zero. */
674 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
675 } else {
676 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
677 *val = tr32(TG3PCI_MEM_WIN_DATA);
678
679 /* Always leave this as zero. */
680 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
681 }
Michael Chan68929142005-08-09 20:17:14 -0700682 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685static void tg3_ape_lock_init(struct tg3 *tp)
686{
687 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000688 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000689
Joe Perches41535772013-02-16 11:20:04 +0000690 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000691 regbase = TG3_APE_LOCK_GRANT;
692 else
693 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700694
695 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000696 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
697 switch (i) {
698 case TG3_APE_LOCK_PHY0:
699 case TG3_APE_LOCK_PHY1:
700 case TG3_APE_LOCK_PHY2:
701 case TG3_APE_LOCK_PHY3:
702 bit = APE_LOCK_GRANT_DRIVER;
703 break;
704 default:
705 if (!tp->pci_fn)
706 bit = APE_LOCK_GRANT_DRIVER;
707 else
708 bit = 1 << tp->pci_fn;
709 }
710 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 }
712
Matt Carlson0d3031d2007-10-10 18:02:43 -0700713}
714
715static int tg3_ape_lock(struct tg3 *tp, int locknum)
716{
717 int i, off;
718 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000719 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700720
Joe Perches63c3a662011-04-26 08:12:10 +0000721 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700722 return 0;
723
724 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000725 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000726 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000727 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000728 case TG3_APE_LOCK_GRC:
729 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000730 if (!tp->pci_fn)
731 bit = APE_LOCK_REQ_DRIVER;
732 else
733 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000734 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000735 case TG3_APE_LOCK_PHY0:
736 case TG3_APE_LOCK_PHY1:
737 case TG3_APE_LOCK_PHY2:
738 case TG3_APE_LOCK_PHY3:
739 bit = APE_LOCK_REQ_DRIVER;
740 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000741 default:
742 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 }
744
Joe Perches41535772013-02-16 11:20:04 +0000745 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000746 req = TG3_APE_LOCK_REQ;
747 gnt = TG3_APE_LOCK_GRANT;
748 } else {
749 req = TG3_APE_PER_LOCK_REQ;
750 gnt = TG3_APE_PER_LOCK_GRANT;
751 }
752
Matt Carlson0d3031d2007-10-10 18:02:43 -0700753 off = 4 * locknum;
754
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000755 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700756
757 /* Wait for up to 1 millisecond to acquire lock. */
758 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000759 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800762 if (pci_channel_offline(tp->pdev))
763 break;
764
Matt Carlson0d3031d2007-10-10 18:02:43 -0700765 udelay(10);
766 }
767
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000768 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700769 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000770 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700771 ret = -EBUSY;
772 }
773
774 return ret;
775}
776
777static void tg3_ape_unlock(struct tg3 *tp, int locknum)
778{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000779 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700780
Joe Perches63c3a662011-04-26 08:12:10 +0000781 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700782 return;
783
784 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000785 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000786 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000787 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000788 case TG3_APE_LOCK_GRC:
789 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000790 if (!tp->pci_fn)
791 bit = APE_LOCK_GRANT_DRIVER;
792 else
793 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000794 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000795 case TG3_APE_LOCK_PHY0:
796 case TG3_APE_LOCK_PHY1:
797 case TG3_APE_LOCK_PHY2:
798 case TG3_APE_LOCK_PHY3:
799 bit = APE_LOCK_GRANT_DRIVER;
800 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000801 default:
802 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700803 }
804
Joe Perches41535772013-02-16 11:20:04 +0000805 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000806 gnt = TG3_APE_LOCK_GRANT;
807 else
808 gnt = TG3_APE_PER_LOCK_GRANT;
809
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000810 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700811}
812
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000814{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000815 u32 apedata;
816
Matt Carlsonb65a3722012-07-16 16:24:00 +0000817 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000818 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000819 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000820
821 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000822 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
823 break;
824
Matt Carlsonb65a3722012-07-16 16:24:00 +0000825 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
826
Jonathan Toppinsea91df62018-03-14 12:36:25 -0400827 udelay(10);
Matt Carlsonb65a3722012-07-16 16:24:00 +0000828 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000829 }
830
Matt Carlsonb65a3722012-07-16 16:24:00 +0000831 return timeout_us ? 0 : -EBUSY;
832}
833
Florian Fainelli038e8932017-03-06 12:56:02 -0800834#ifdef CONFIG_TIGON3_HWMON
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000835static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
836{
837 u32 i, apedata;
838
839 for (i = 0; i < timeout_us / 10; i++) {
840 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
841
842 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
843 break;
844
845 udelay(10);
846 }
847
848 return i == timeout_us / 10;
849}
850
Michael Chan86449942012-10-02 20:31:14 -0700851static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
852 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000853{
854 int err;
855 u32 i, bufoff, msgoff, maxlen, apedata;
856
857 if (!tg3_flag(tp, APE_HAS_NCSI))
858 return 0;
859
860 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
861 if (apedata != APE_SEG_SIG_MAGIC)
862 return -ENODEV;
863
864 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
865 if (!(apedata & APE_FW_STATUS_READY))
866 return -EAGAIN;
867
868 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
869 TG3_APE_SHMEM_BASE;
870 msgoff = bufoff + 2 * sizeof(u32);
871 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
872
873 while (len) {
874 u32 length;
875
876 /* Cap xfer sizes to scratchpad limits. */
877 length = (len > maxlen) ? maxlen : len;
878 len -= length;
879
880 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
881 if (!(apedata & APE_FW_STATUS_READY))
882 return -EAGAIN;
883
884 /* Wait for up to 1 msec for APE to service previous event. */
885 err = tg3_ape_event_lock(tp, 1000);
886 if (err)
887 return err;
888
889 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
890 APE_EVENT_STATUS_SCRTCHPD_READ |
891 APE_EVENT_STATUS_EVENT_PENDING;
892 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
893
894 tg3_ape_write32(tp, bufoff, base_off);
895 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
896
897 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
898 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
899
900 base_off += length;
901
902 if (tg3_ape_wait_for_event(tp, 30000))
903 return -EAGAIN;
904
905 for (i = 0; length; i += 4, length -= 4) {
906 u32 val = tg3_ape_read32(tp, msgoff + i);
907 memcpy(data, &val, sizeof(u32));
908 data++;
909 }
910 }
911
912 return 0;
913}
Florian Fainelli038e8932017-03-06 12:56:02 -0800914#endif
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000915
Matt Carlsonb65a3722012-07-16 16:24:00 +0000916static int tg3_ape_send_event(struct tg3 *tp, u32 event)
917{
918 int err;
919 u32 apedata;
920
921 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
922 if (apedata != APE_SEG_SIG_MAGIC)
923 return -EAGAIN;
924
925 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
926 if (!(apedata & APE_FW_STATUS_READY))
927 return -EAGAIN;
928
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530929 /* Wait for up to 20 millisecond for APE to service previous event. */
930 err = tg3_ape_event_lock(tp, 20000);
Matt Carlsonb65a3722012-07-16 16:24:00 +0000931 if (err)
932 return err;
933
934 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
935 event | APE_EVENT_STATUS_EVENT_PENDING);
936
937 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
938 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
939
940 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000941}
942
943static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
944{
945 u32 event;
946 u32 apedata;
947
948 if (!tg3_flag(tp, ENABLE_APE))
949 return;
950
951 switch (kind) {
952 case RESET_KIND_INIT:
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530953 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000954 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
955 APE_HOST_SEG_SIG_MAGIC);
956 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
957 APE_HOST_SEG_LEN_MAGIC);
958 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
959 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
960 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
961 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
962 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
963 APE_HOST_BEHAV_NO_PHYLOCK);
964 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
965 TG3_APE_HOST_DRVR_STATE_START);
966
967 event = APE_EVENT_STATUS_STATE_START;
968 break;
969 case RESET_KIND_SHUTDOWN:
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000970 if (device_may_wakeup(&tp->pdev->dev) &&
971 tg3_flag(tp, WOL_ENABLE)) {
972 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
973 TG3_APE_HOST_WOL_SPEED_AUTO);
974 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
975 } else
976 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
977
978 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
979
980 event = APE_EVENT_STATUS_STATE_UNLOAD;
981 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000982 default:
983 return;
984 }
985
986 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
987
988 tg3_ape_send_event(tp, event);
989}
990
Prashant Sreedharan506b0a32018-02-19 12:27:04 +0530991static void tg3_send_ape_heartbeat(struct tg3 *tp,
992 unsigned long interval)
993{
994 /* Check if hb interval has exceeded */
995 if (!tg3_flag(tp, ENABLE_APE) ||
996 time_before(jiffies, tp->ape_hb_jiffies + interval))
997 return;
998
999 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_COUNT, tp->ape_hb++);
1000 tp->ape_hb_jiffies = jiffies;
1001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003static void tg3_disable_ints(struct tg3 *tp)
1004{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001005 int i;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 tw32(TG3PCI_MISC_HOST_CTRL,
1008 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 for (i = 0; i < tp->irq_max; i++)
1010 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013static void tg3_enable_ints(struct tg3 *tp)
1014{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001015 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001016
Michael Chanbbe832c2005-06-24 20:20:04 -07001017 tp->irq_sync = 0;
1018 wmb();
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 tw32(TG3PCI_MISC_HOST_CTRL,
1021 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001022
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001023 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001024 for (i = 0; i < tp->irq_cnt; i++) {
1025 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001026
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001027 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001028 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001029 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1030
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001031 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001032 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001033
1034 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001035 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001036 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1037 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1038 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001039 tw32(HOSTCC_MODE, tp->coal_now);
1040
1041 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Matt Carlson17375d22009-08-28 14:02:18 +00001044static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001045{
Matt Carlson17375d22009-08-28 14:02:18 +00001046 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001047 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001048 unsigned int work_exists = 0;
1049
1050 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001051 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001052 if (sblk->status & SD_STATUS_LINK_CHG)
1053 work_exists = 1;
1054 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001055
1056 /* check for TX work to do */
1057 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1058 work_exists = 1;
1059
1060 /* check for RX work to do */
1061 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001062 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001063 work_exists = 1;
1064
1065 return work_exists;
1066}
1067
Matt Carlson17375d22009-08-28 14:02:18 +00001068/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001069 * similar to tg3_enable_ints, but it accurately determines whether there
1070 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001071 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Matt Carlson17375d22009-08-28 14:02:18 +00001073static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Matt Carlson17375d22009-08-28 14:02:18 +00001075 struct tg3 *tp = tnapi->tp;
1076
Matt Carlson898a56f2009-08-28 14:02:40 +00001077 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 mmiowb();
1079
David S. Millerfac9b832005-05-18 22:46:34 -07001080 /* When doing tagged status, this work check is unnecessary.
1081 * The last_tag we write above tells the chip which piece of
1082 * work we've completed.
1083 */
Joe Perches63c3a662011-04-26 08:12:10 +00001084 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001085 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001086 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089static void tg3_switch_clocks(struct tg3 *tp)
1090{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001091 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 u32 orig_clock_ctrl;
1093
Joe Perches63c3a662011-04-26 08:12:10 +00001094 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001095 return;
1096
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001097 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 orig_clock_ctrl = clock_ctrl;
1100 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1101 CLOCK_CTRL_CLKRUN_OENABLE |
1102 0x1f);
1103 tp->pci_clock_ctrl = clock_ctrl;
1104
Joe Perches63c3a662011-04-26 08:12:10 +00001105 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001107 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1108 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001111 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1112 clock_ctrl |
1113 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1114 40);
1115 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1116 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1117 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001119 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122#define PHY_BUSY_LOOPS 5000
1123
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001124static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1125 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
1127 u32 frame_val;
1128 unsigned int loops;
1129 int ret;
1130
1131 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1132 tw32_f(MAC_MI_MODE,
1133 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1134 udelay(80);
1135 }
1136
Michael Chan8151ad52012-07-29 19:15:41 +00001137 tg3_ape_lock(tp, tp->phy_ape_lock);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 *val = 0x0;
1140
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001141 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 MI_COM_PHY_ADDR_MASK);
1143 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1144 MI_COM_REG_ADDR_MASK);
1145 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 tw32_f(MAC_MI_COM, frame_val);
1148
1149 loops = PHY_BUSY_LOOPS;
1150 while (loops != 0) {
1151 udelay(10);
1152 frame_val = tr32(MAC_MI_COM);
1153
1154 if ((frame_val & MI_COM_BUSY) == 0) {
1155 udelay(5);
1156 frame_val = tr32(MAC_MI_COM);
1157 break;
1158 }
1159 loops -= 1;
1160 }
1161
1162 ret = -EBUSY;
1163 if (loops != 0) {
1164 *val = frame_val & MI_COM_DATA_MASK;
1165 ret = 0;
1166 }
1167
1168 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1169 tw32_f(MAC_MI_MODE, tp->mi_mode);
1170 udelay(80);
1171 }
1172
Michael Chan8151ad52012-07-29 19:15:41 +00001173 tg3_ape_unlock(tp, tp->phy_ape_lock);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return ret;
1176}
1177
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001178static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1179{
1180 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1181}
1182
1183static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1184 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 u32 frame_val;
1187 unsigned int loops;
1188 int ret;
1189
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001190 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001191 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001192 return 0;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1195 tw32_f(MAC_MI_MODE,
1196 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1197 udelay(80);
1198 }
1199
Michael Chan8151ad52012-07-29 19:15:41 +00001200 tg3_ape_lock(tp, tp->phy_ape_lock);
1201
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001202 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 MI_COM_PHY_ADDR_MASK);
1204 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1205 MI_COM_REG_ADDR_MASK);
1206 frame_val |= (val & MI_COM_DATA_MASK);
1207 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 tw32_f(MAC_MI_COM, frame_val);
1210
1211 loops = PHY_BUSY_LOOPS;
1212 while (loops != 0) {
1213 udelay(10);
1214 frame_val = tr32(MAC_MI_COM);
1215 if ((frame_val & MI_COM_BUSY) == 0) {
1216 udelay(5);
1217 frame_val = tr32(MAC_MI_COM);
1218 break;
1219 }
1220 loops -= 1;
1221 }
1222
1223 ret = -EBUSY;
1224 if (loops != 0)
1225 ret = 0;
1226
1227 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1228 tw32_f(MAC_MI_MODE, tp->mi_mode);
1229 udelay(80);
1230 }
1231
Michael Chan8151ad52012-07-29 19:15:41 +00001232 tg3_ape_unlock(tp, tp->phy_ape_lock);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return ret;
1235}
1236
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001237static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1238{
1239 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1240}
1241
Matt Carlsonb0988c12011-04-20 07:57:39 +00001242static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1243{
1244 int err;
1245
1246 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1247 if (err)
1248 goto done;
1249
1250 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1251 if (err)
1252 goto done;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1255 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1260
1261done:
1262 return err;
1263}
1264
1265static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1270 if (err)
1271 goto done;
1272
1273 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1274 if (err)
1275 goto done;
1276
1277 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1278 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1279 if (err)
1280 goto done;
1281
1282 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1283
1284done:
1285 return err;
1286}
1287
1288static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1289{
1290 int err;
1291
1292 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1293 if (!err)
1294 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1295
1296 return err;
1297}
1298
1299static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1300{
1301 int err;
1302
1303 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1304 if (!err)
1305 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1306
1307 return err;
1308}
1309
Matt Carlson15ee95c2011-04-20 07:57:40 +00001310static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1311{
1312 int err;
1313
1314 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1315 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1316 MII_TG3_AUXCTL_SHDWSEL_MISC);
1317 if (!err)
1318 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1319
1320 return err;
1321}
1322
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001323static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1324{
1325 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1326 set |= MII_TG3_AUXCTL_MISC_WREN;
1327
1328 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1329}
1330
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001331static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1332{
1333 u32 val;
1334 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001335
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001336 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1337
1338 if (err)
1339 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001340
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001341 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001342 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1343 else
1344 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1345
1346 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1347 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1348
1349 return err;
1350}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001351
Nithin Sujir3ab71072013-09-20 16:46:55 -07001352static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1353{
1354 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1355 reg | val | MII_TG3_MISC_SHDW_WREN);
1356}
1357
Matt Carlson95e28692008-05-25 23:44:14 -07001358static int tg3_bmcr_reset(struct tg3 *tp)
1359{
1360 u32 phy_control;
1361 int limit, err;
1362
1363 /* OK, reset it, and poll the BMCR_RESET bit until it
1364 * clears or we time out.
1365 */
1366 phy_control = BMCR_RESET;
1367 err = tg3_writephy(tp, MII_BMCR, phy_control);
1368 if (err != 0)
1369 return -EBUSY;
1370
1371 limit = 5000;
1372 while (limit--) {
1373 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1374 if (err != 0)
1375 return -EBUSY;
1376
1377 if ((phy_control & BMCR_RESET) == 0) {
1378 udelay(40);
1379 break;
1380 }
1381 udelay(10);
1382 }
Roel Kluind4675b52009-02-12 16:33:27 -08001383 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001384 return -EBUSY;
1385
1386 return 0;
1387}
1388
Matt Carlson158d7ab2008-05-29 01:37:54 -07001389static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1390{
Francois Romieu3d165432009-01-19 16:56:50 -08001391 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001392 u32 val;
1393
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Hauke Mehrtensead24022013-09-28 23:15:26 +02001396 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001397 val = -EIO;
1398
1399 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001400
1401 return val;
1402}
1403
1404static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1405{
Francois Romieu3d165432009-01-19 16:56:50 -08001406 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001407 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001408
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001409 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001410
Hauke Mehrtensead24022013-09-28 23:15:26 +02001411 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001412 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001413
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001414 spin_unlock_bh(&tp->lock);
1415
1416 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001417}
1418
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001419static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001420{
1421 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001422 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001423
Andrew Lunn7f854422016-01-06 20:11:18 +01001424 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001425 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001426 case PHY_ID_BCM50610:
1427 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001428 val = MAC_PHYCFG2_50610_LED_MODES;
1429 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001430 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001431 val = MAC_PHYCFG2_AC131_LED_MODES;
1432 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001433 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001434 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1435 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001436 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001437 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1438 break;
1439 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001440 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001441 }
1442
1443 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1444 tw32(MAC_PHYCFG2, val);
1445
1446 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001447 val &= ~(MAC_PHYCFG1_RGMII_INT |
1448 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1449 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001450 tw32(MAC_PHYCFG1, val);
1451
1452 return;
1453 }
1454
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001456 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1457 MAC_PHYCFG2_FMODE_MASK_MASK |
1458 MAC_PHYCFG2_GMODE_MASK_MASK |
1459 MAC_PHYCFG2_ACT_MASK_MASK |
1460 MAC_PHYCFG2_QUAL_MASK_MASK |
1461 MAC_PHYCFG2_INBAND_ENABLE;
1462
1463 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001464
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001465 val = tr32(MAC_PHYCFG1);
1466 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1467 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001468 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1469 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001470 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001471 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001472 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1473 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001474 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1475 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1476 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001477
Matt Carlsona9daf362008-05-25 23:49:44 -07001478 val = tr32(MAC_EXT_RGMII_MODE);
1479 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1480 MAC_RGMII_MODE_RX_QUALITY |
1481 MAC_RGMII_MODE_RX_ACTIVITY |
1482 MAC_RGMII_MODE_RX_ENG_DET |
1483 MAC_RGMII_MODE_TX_ENABLE |
1484 MAC_RGMII_MODE_TX_LOWPWR |
1485 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001486 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1487 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001488 val |= MAC_RGMII_MODE_RX_INT_B |
1489 MAC_RGMII_MODE_RX_QUALITY |
1490 MAC_RGMII_MODE_RX_ACTIVITY |
1491 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001492 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001493 val |= MAC_RGMII_MODE_TX_ENABLE |
1494 MAC_RGMII_MODE_TX_LOWPWR |
1495 MAC_RGMII_MODE_TX_RESET;
1496 }
1497 tw32(MAC_EXT_RGMII_MODE, val);
1498}
1499
Matt Carlson158d7ab2008-05-29 01:37:54 -07001500static void tg3_mdio_start(struct tg3 *tp)
1501{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001502 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1503 tw32_f(MAC_MI_MODE, tp->mi_mode);
1504 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001505
Joe Perches63c3a662011-04-26 08:12:10 +00001506 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001507 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001508 tg3_mdio_config_5785(tp);
1509}
1510
1511static int tg3_mdio_init(struct tg3 *tp)
1512{
1513 int i;
1514 u32 reg;
1515 struct phy_device *phydev;
1516
Joe Perches63c3a662011-04-26 08:12:10 +00001517 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001518 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001519
Matt Carlson69f11c92011-07-13 09:27:30 +00001520 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001521
Joe Perches41535772013-02-16 11:20:04 +00001522 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001523 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1524 else
1525 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1526 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001527 if (is_serdes)
1528 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001529 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1530 int addr;
1531
1532 addr = ssb_gige_get_phyaddr(tp->pdev);
1533 if (addr < 0)
1534 return addr;
1535 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001536 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001537 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001538
Matt Carlson158d7ab2008-05-29 01:37:54 -07001539 tg3_mdio_start(tp);
1540
Joe Perches63c3a662011-04-26 08:12:10 +00001541 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001542 return 0;
1543
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001544 tp->mdio_bus = mdiobus_alloc();
1545 if (tp->mdio_bus == NULL)
1546 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001547
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001548 tp->mdio_bus->name = "tg3 mdio bus";
1549 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001550 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001551 tp->mdio_bus->priv = tp;
1552 tp->mdio_bus->parent = &tp->pdev->dev;
1553 tp->mdio_bus->read = &tg3_mdio_read;
1554 tp->mdio_bus->write = &tg3_mdio_write;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001555 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001556
1557 /* The bus registration will look for all the PHYs on the mdio bus.
1558 * Unfortunately, it does not ensure the PHY is powered up before
1559 * accessing the PHY ID registers. A chip reset is the
1560 * quickest way to bring the device back to an operational state..
1561 */
1562 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1563 tg3_bmcr_reset(tp);
1564
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001565 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001566 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001567 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001568 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001569 return i;
1570 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001571
Andrew Lunn7f854422016-01-06 20:11:18 +01001572 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsona9daf362008-05-25 23:49:44 -07001573
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001574 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001575 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001576 mdiobus_unregister(tp->mdio_bus);
1577 mdiobus_free(tp->mdio_bus);
1578 return -ENODEV;
1579 }
1580
1581 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001582 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001583 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001584 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001585 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001586 case PHY_ID_BCM50610:
1587 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001588 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001589 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001590 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001591 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001592 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001593 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001594 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001595 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001596 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001597 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001598 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001599 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001600 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001601 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001602 case PHY_ID_RTL8201E:
1603 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001604 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001605 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001606 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001607 break;
1608 }
1609
Joe Perches63c3a662011-04-26 08:12:10 +00001610 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001611
Joe Perches41535772013-02-16 11:20:04 +00001612 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001613 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001614
1615 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001616}
1617
1618static void tg3_mdio_fini(struct tg3 *tp)
1619{
Joe Perches63c3a662011-04-26 08:12:10 +00001620 if (tg3_flag(tp, MDIOBUS_INITED)) {
1621 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001622 mdiobus_unregister(tp->mdio_bus);
1623 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001624 }
1625}
1626
Matt Carlson95e28692008-05-25 23:44:14 -07001627/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001628static inline void tg3_generate_fw_event(struct tg3 *tp)
1629{
1630 u32 val;
1631
1632 val = tr32(GRC_RX_CPU_EVENT);
1633 val |= GRC_RX_CPU_DRIVER_EVENT;
1634 tw32_f(GRC_RX_CPU_EVENT, val);
1635
1636 tp->last_event_jiffies = jiffies;
1637}
1638
1639#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1640
1641/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001642static void tg3_wait_for_event_ack(struct tg3 *tp)
1643{
1644 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001645 unsigned int delay_cnt;
1646 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001647
Matt Carlson4ba526c2008-08-15 14:10:04 -07001648 /* If enough time has passed, no wait is necessary. */
1649 time_remain = (long)(tp->last_event_jiffies + 1 +
1650 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1651 (long)jiffies;
1652 if (time_remain < 0)
1653 return;
1654
1655 /* Check if we can shorten the wait time. */
1656 delay_cnt = jiffies_to_usecs(time_remain);
1657 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1658 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1659 delay_cnt = (delay_cnt >> 3) + 1;
1660
1661 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001662 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1663 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001664 if (pci_channel_offline(tp->pdev))
1665 break;
1666
Matt Carlson4ba526c2008-08-15 14:10:04 -07001667 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001668 }
1669}
1670
1671/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001672static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001673{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001674 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001675
1676 val = 0;
1677 if (!tg3_readphy(tp, MII_BMCR, &reg))
1678 val = reg << 16;
1679 if (!tg3_readphy(tp, MII_BMSR, &reg))
1680 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001681 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001682
1683 val = 0;
1684 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1685 val = reg << 16;
1686 if (!tg3_readphy(tp, MII_LPA, &reg))
1687 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001688 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001689
1690 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001691 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001692 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1693 val = reg << 16;
1694 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1695 val |= (reg & 0xffff);
1696 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001697 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001698
1699 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1700 val = reg << 16;
1701 else
1702 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001703 *data++ = val;
1704}
1705
1706/* tp->lock is held. */
1707static void tg3_ump_link_report(struct tg3 *tp)
1708{
1709 u32 data[4];
1710
1711 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1712 return;
1713
1714 tg3_phy_gather_ump_data(tp, data);
1715
1716 tg3_wait_for_event_ack(tp);
1717
1718 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1719 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1720 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1721 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1722 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1723 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001724
Matt Carlson4ba526c2008-08-15 14:10:04 -07001725 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001726}
1727
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001728/* tp->lock is held. */
1729static void tg3_stop_fw(struct tg3 *tp)
1730{
1731 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1732 /* Wait for RX cpu to ACK the previous event. */
1733 tg3_wait_for_event_ack(tp);
1734
1735 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1736
1737 tg3_generate_fw_event(tp);
1738
1739 /* Wait for RX cpu to ACK this event. */
1740 tg3_wait_for_event_ack(tp);
1741 }
1742}
1743
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001744/* tp->lock is held. */
1745static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1746{
1747 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1748 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1749
1750 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1751 switch (kind) {
1752 case RESET_KIND_INIT:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_START);
1755 break;
1756
1757 case RESET_KIND_SHUTDOWN:
1758 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1759 DRV_STATE_UNLOAD);
1760 break;
1761
1762 case RESET_KIND_SUSPEND:
1763 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1764 DRV_STATE_SUSPEND);
1765 break;
1766
1767 default:
1768 break;
1769 }
1770 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001771}
1772
1773/* tp->lock is held. */
1774static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1775{
1776 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1777 switch (kind) {
1778 case RESET_KIND_INIT:
1779 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1780 DRV_STATE_START_DONE);
1781 break;
1782
1783 case RESET_KIND_SHUTDOWN:
1784 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1785 DRV_STATE_UNLOAD_DONE);
1786 break;
1787
1788 default:
1789 break;
1790 }
1791 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001792}
1793
1794/* tp->lock is held. */
1795static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1796{
1797 if (tg3_flag(tp, ENABLE_ASF)) {
1798 switch (kind) {
1799 case RESET_KIND_INIT:
1800 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1801 DRV_STATE_START);
1802 break;
1803
1804 case RESET_KIND_SHUTDOWN:
1805 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1806 DRV_STATE_UNLOAD);
1807 break;
1808
1809 case RESET_KIND_SUSPEND:
1810 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1811 DRV_STATE_SUSPEND);
1812 break;
1813
1814 default:
1815 break;
1816 }
1817 }
1818}
1819
1820static int tg3_poll_fw(struct tg3 *tp)
1821{
1822 int i;
1823 u32 val;
1824
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001825 if (tg3_flag(tp, NO_FWARE_REPORTED))
1826 return 0;
1827
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001828 if (tg3_flag(tp, IS_SSB_CORE)) {
1829 /* We don't use firmware. */
1830 return 0;
1831 }
1832
Joe Perches41535772013-02-16 11:20:04 +00001833 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001834 /* Wait up to 20ms for init done. */
1835 for (i = 0; i < 200; i++) {
1836 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1837 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001838 if (pci_channel_offline(tp->pdev))
1839 return -ENODEV;
1840
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001841 udelay(100);
1842 }
1843 return -ENODEV;
1844 }
1845
1846 /* Wait for firmware initialization to complete. */
1847 for (i = 0; i < 100000; i++) {
1848 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1849 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1850 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001851 if (pci_channel_offline(tp->pdev)) {
1852 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1853 tg3_flag_set(tp, NO_FWARE_REPORTED);
1854 netdev_info(tp->dev, "No firmware running\n");
1855 }
1856
1857 break;
1858 }
1859
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001860 udelay(10);
1861 }
1862
1863 /* Chip might not be fitted with firmware. Some Sun onboard
1864 * parts are configured like that. So don't signal the timeout
1865 * of the above loop as an error, but do report the lack of
1866 * running firmware once.
1867 */
1868 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1869 tg3_flag_set(tp, NO_FWARE_REPORTED);
1870
1871 netdev_info(tp->dev, "No firmware running\n");
1872 }
1873
Joe Perches41535772013-02-16 11:20:04 +00001874 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001875 /* The 57765 A0 needs a little more
1876 * time to do some important work.
1877 */
1878 mdelay(10);
1879 }
1880
1881 return 0;
1882}
1883
Matt Carlson95e28692008-05-25 23:44:14 -07001884static void tg3_link_report(struct tg3 *tp)
1885{
1886 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001887 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001888 tg3_ump_link_report(tp);
1889 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001890 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1891 (tp->link_config.active_speed == SPEED_1000 ?
1892 1000 :
1893 (tp->link_config.active_speed == SPEED_100 ?
1894 100 : 10)),
1895 (tp->link_config.active_duplex == DUPLEX_FULL ?
1896 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001897
Joe Perches05dbe002010-02-17 19:44:19 +00001898 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1899 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1900 "on" : "off",
1901 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1902 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001903
1904 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1905 netdev_info(tp->dev, "EEE is %s\n",
1906 tp->setlpicnt ? "enabled" : "disabled");
1907
Matt Carlson95e28692008-05-25 23:44:14 -07001908 tg3_ump_link_report(tp);
1909 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001910
1911 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001912}
1913
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001914static u32 tg3_decode_flowctrl_1000T(u32 adv)
1915{
1916 u32 flowctrl = 0;
1917
1918 if (adv & ADVERTISE_PAUSE_CAP) {
1919 flowctrl |= FLOW_CTRL_RX;
1920 if (!(adv & ADVERTISE_PAUSE_ASYM))
1921 flowctrl |= FLOW_CTRL_TX;
1922 } else if (adv & ADVERTISE_PAUSE_ASYM)
1923 flowctrl |= FLOW_CTRL_TX;
1924
1925 return flowctrl;
1926}
1927
Matt Carlson95e28692008-05-25 23:44:14 -07001928static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1929{
1930 u16 miireg;
1931
Steve Glendinninge18ce342008-12-16 02:00:00 -08001932 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001933 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001934 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001935 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001936 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001937 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1938 else
1939 miireg = 0;
1940
1941 return miireg;
1942}
1943
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001944static u32 tg3_decode_flowctrl_1000X(u32 adv)
1945{
1946 u32 flowctrl = 0;
1947
1948 if (adv & ADVERTISE_1000XPAUSE) {
1949 flowctrl |= FLOW_CTRL_RX;
1950 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1951 flowctrl |= FLOW_CTRL_TX;
1952 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1953 flowctrl |= FLOW_CTRL_TX;
1954
1955 return flowctrl;
1956}
1957
Matt Carlson95e28692008-05-25 23:44:14 -07001958static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1959{
1960 u8 cap = 0;
1961
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001962 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1963 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1964 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1965 if (lcladv & ADVERTISE_1000XPAUSE)
1966 cap = FLOW_CTRL_RX;
1967 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001968 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001969 }
1970
1971 return cap;
1972}
1973
Matt Carlsonf51f3562008-05-25 23:45:08 -07001974static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001975{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001977 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001978 u32 old_rx_mode = tp->rx_mode;
1979 u32 old_tx_mode = tp->tx_mode;
1980
Joe Perches63c3a662011-04-26 08:12:10 +00001981 if (tg3_flag(tp, USE_PHYLIB))
Andrew Lunn7f854422016-01-06 20:11:18 +01001982 autoneg = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr)->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001983 else
1984 autoneg = tp->link_config.autoneg;
1985
Joe Perches63c3a662011-04-26 08:12:10 +00001986 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001987 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001988 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001989 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001990 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001991 } else
1992 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001993
Matt Carlsonf51f3562008-05-25 23:45:08 -07001994 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001995
Steve Glendinninge18ce342008-12-16 02:00:00 -08001996 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001997 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1998 else
1999 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
2000
Matt Carlsonf51f3562008-05-25 23:45:08 -07002001 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002002 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002003
Steve Glendinninge18ce342008-12-16 02:00:00 -08002004 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07002005 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
2006 else
2007 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
2008
Matt Carlsonf51f3562008-05-25 23:45:08 -07002009 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002010 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002011}
2012
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002013static void tg3_adjust_link(struct net_device *dev)
2014{
2015 u8 oldflowctrl, linkmesg = 0;
2016 u32 mac_mode, lcl_adv, rmt_adv;
2017 struct tg3 *tp = netdev_priv(dev);
Andrew Lunn7f854422016-01-06 20:11:18 +01002018 struct phy_device *phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002019
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002020 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002021
2022 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2023 MAC_MODE_HALF_DUPLEX);
2024
2025 oldflowctrl = tp->link_config.active_flowctrl;
2026
2027 if (phydev->link) {
2028 lcl_adv = 0;
2029 rmt_adv = 0;
2030
2031 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2032 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002033 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002034 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002035 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002036 else
2037 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002038
2039 if (phydev->duplex == DUPLEX_HALF)
2040 mac_mode |= MAC_MODE_HALF_DUPLEX;
2041 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002042 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002043 tp->link_config.flowctrl);
2044
2045 if (phydev->pause)
2046 rmt_adv = LPA_PAUSE_CAP;
2047 if (phydev->asym_pause)
2048 rmt_adv |= LPA_PAUSE_ASYM;
2049 }
2050
2051 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2052 } else
2053 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2054
2055 if (mac_mode != tp->mac_mode) {
2056 tp->mac_mode = mac_mode;
2057 tw32_f(MAC_MODE, tp->mac_mode);
2058 udelay(40);
2059 }
2060
Joe Perches41535772013-02-16 11:20:04 +00002061 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002062 if (phydev->speed == SPEED_10)
2063 tw32(MAC_MI_STAT,
2064 MAC_MI_STAT_10MBPS_MODE |
2065 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2066 else
2067 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2068 }
2069
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002070 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2071 tw32(MAC_TX_LENGTHS,
2072 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2073 (6 << TX_LENGTHS_IPG_SHIFT) |
2074 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2075 else
2076 tw32(MAC_TX_LENGTHS,
2077 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2078 (6 << TX_LENGTHS_IPG_SHIFT) |
2079 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2080
Matt Carlson34655ad2012-02-22 12:35:18 +00002081 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002082 phydev->speed != tp->link_config.active_speed ||
2083 phydev->duplex != tp->link_config.active_duplex ||
2084 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002085 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002086
Matt Carlson34655ad2012-02-22 12:35:18 +00002087 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002088 tp->link_config.active_speed = phydev->speed;
2089 tp->link_config.active_duplex = phydev->duplex;
2090
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002091 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002092
2093 if (linkmesg)
2094 tg3_link_report(tp);
2095}
2096
2097static int tg3_phy_init(struct tg3 *tp)
2098{
2099 struct phy_device *phydev;
2100
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002101 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102 return 0;
2103
2104 /* Bring the PHY back to a known state. */
2105 tg3_bmcr_reset(tp);
2106
Andrew Lunn7f854422016-01-06 20:11:18 +01002107 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002108
2109 /* Attach the MAC to the PHY. */
Andrew Lunn84eff6d2016-01-06 20:11:10 +01002110 phydev = phy_connect(tp->dev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +00002111 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002112 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002113 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002114 return PTR_ERR(phydev);
2115 }
2116
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002117 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002118 switch (phydev->interface) {
2119 case PHY_INTERFACE_MODE_GMII:
2120 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002121 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002122 phydev->supported &= (PHY_GBIT_FEATURES |
2123 SUPPORTED_Pause |
2124 SUPPORTED_Asym_Pause);
2125 break;
2126 }
2127 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002128 case PHY_INTERFACE_MODE_MII:
2129 phydev->supported &= (PHY_BASIC_FEATURES |
2130 SUPPORTED_Pause |
2131 SUPPORTED_Asym_Pause);
2132 break;
2133 default:
Andrew Lunn7f854422016-01-06 20:11:18 +01002134 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002135 return -EINVAL;
2136 }
2137
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002138 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002139
2140 phydev->advertising = phydev->supported;
2141
Andrew Lunn22209432016-01-06 20:11:13 +01002142 phy_attached_info(phydev);
2143
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002144 return 0;
2145}
2146
2147static void tg3_phy_start(struct tg3 *tp)
2148{
2149 struct phy_device *phydev;
2150
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002151 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002152 return;
2153
Andrew Lunn7f854422016-01-06 20:11:18 +01002154 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002155
Matt Carlson80096062010-08-02 11:26:06 +00002156 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2157 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002158 phydev->speed = tp->link_config.speed;
2159 phydev->duplex = tp->link_config.duplex;
2160 phydev->autoneg = tp->link_config.autoneg;
2161 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002162 }
2163
2164 phy_start(phydev);
2165
2166 phy_start_aneg(phydev);
2167}
2168
2169static void tg3_phy_stop(struct tg3 *tp)
2170{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002171 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002172 return;
2173
Andrew Lunn7f854422016-01-06 20:11:18 +01002174 phy_stop(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002175}
2176
2177static void tg3_phy_fini(struct tg3 *tp)
2178{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002179 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Andrew Lunn7f854422016-01-06 20:11:18 +01002180 phy_disconnect(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002181 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002182 }
2183}
2184
Matt Carlson941ec902011-08-19 13:58:23 +00002185static int tg3_phy_set_extloopbk(struct tg3 *tp)
2186{
2187 int err;
2188 u32 val;
2189
2190 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2191 return 0;
2192
2193 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2194 /* Cannot do read-modify-write on 5401 */
2195 err = tg3_phy_auxctl_write(tp,
2196 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2197 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2198 0x4c20);
2199 goto done;
2200 }
2201
2202 err = tg3_phy_auxctl_read(tp,
2203 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2204 if (err)
2205 return err;
2206
2207 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2208 err = tg3_phy_auxctl_write(tp,
2209 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2210
2211done:
2212 return err;
2213}
2214
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002215static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2216{
2217 u32 phytest;
2218
2219 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2220 u32 phy;
2221
2222 tg3_writephy(tp, MII_TG3_FET_TEST,
2223 phytest | MII_TG3_FET_SHADOW_EN);
2224 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2225 if (enable)
2226 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2227 else
2228 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2229 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2230 }
2231 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2232 }
2233}
2234
Matt Carlson6833c042008-11-21 17:18:59 -08002235static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2236{
2237 u32 reg;
2238
Joe Perches63c3a662011-04-26 08:12:10 +00002239 if (!tg3_flag(tp, 5705_PLUS) ||
2240 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002241 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002242 return;
2243
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002244 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002245 tg3_phy_fet_toggle_apd(tp, enable);
2246 return;
2247 }
2248
Nithin Sujir3ab71072013-09-20 16:46:55 -07002249 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002250 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2251 MII_TG3_MISC_SHDW_SCR5_SDTL |
2252 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002253 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002254 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2255
Nithin Sujir3ab71072013-09-20 16:46:55 -07002256 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002257
2258
Nithin Sujir3ab71072013-09-20 16:46:55 -07002259 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002260 if (enable)
2261 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2262
Nithin Sujir3ab71072013-09-20 16:46:55 -07002263 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002264}
2265
Joe Perches953c96e2013-04-09 10:18:14 +00002266static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002267{
2268 u32 phy;
2269
Joe Perches63c3a662011-04-26 08:12:10 +00002270 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002271 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002272 return;
2273
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002274 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002275 u32 ephy;
2276
Matt Carlson535ef6e2009-08-25 10:09:36 +00002277 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2278 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2279
2280 tg3_writephy(tp, MII_TG3_FET_TEST,
2281 ephy | MII_TG3_FET_SHADOW_EN);
2282 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002283 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002284 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002285 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002286 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2287 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002288 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002289 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002290 }
2291 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002292 int ret;
2293
2294 ret = tg3_phy_auxctl_read(tp,
2295 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2296 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002297 if (enable)
2298 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2299 else
2300 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002301 tg3_phy_auxctl_write(tp,
2302 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002303 }
2304 }
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307static void tg3_phy_set_wirespeed(struct tg3 *tp)
2308{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002309 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 u32 val;
2311
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002312 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return;
2314
Matt Carlson15ee95c2011-04-20 07:57:40 +00002315 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2316 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002317 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2318 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002321static void tg3_phy_apply_otp(struct tg3 *tp)
2322{
2323 u32 otp, phy;
2324
2325 if (!tp->phy_otp)
2326 return;
2327
2328 otp = tp->phy_otp;
2329
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002330 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002331 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002332
2333 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2334 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2335 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2336
2337 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2338 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2339 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2340
2341 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2342 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2343 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2344
2345 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2346 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2347
2348 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2349 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2350
2351 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2352 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2353 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2354
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002355 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002356}
2357
Nithin Sujir400dfba2013-05-18 06:26:53 +00002358static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2359{
2360 u32 val;
2361 struct ethtool_eee *dest = &tp->eee;
2362
2363 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2364 return;
2365
2366 if (eee)
2367 dest = eee;
2368
2369 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2370 return;
2371
2372 /* Pull eee_active */
2373 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2374 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2375 dest->eee_active = 1;
2376 } else
2377 dest->eee_active = 0;
2378
2379 /* Pull lp advertised settings */
2380 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2381 return;
2382 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2383
2384 /* Pull advertised and eee_enabled settings */
2385 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2386 return;
2387 dest->eee_enabled = !!val;
2388 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2389
2390 /* Pull tx_lpi_enabled */
2391 val = tr32(TG3_CPMU_EEE_MODE);
2392 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2393
2394 /* Pull lpi timer value */
2395 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2396}
2397
Joe Perches953c96e2013-04-09 10:18:14 +00002398static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002399{
2400 u32 val;
2401
2402 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2403 return;
2404
2405 tp->setlpicnt = 0;
2406
2407 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002408 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002409 tp->link_config.active_duplex == DUPLEX_FULL &&
2410 (tp->link_config.active_speed == SPEED_100 ||
2411 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002412 u32 eeectl;
2413
2414 if (tp->link_config.active_speed == SPEED_1000)
2415 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2416 else
2417 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2418
2419 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2420
Nithin Sujir400dfba2013-05-18 06:26:53 +00002421 tg3_eee_pull_config(tp, NULL);
2422 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002423 tp->setlpicnt = 2;
2424 }
2425
2426 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002427 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002428 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002429 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002430 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002431 }
2432
Matt Carlson52b02d02010-10-14 10:37:41 +00002433 val = tr32(TG3_CPMU_EEE_MODE);
2434 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2435 }
2436}
2437
Matt Carlsonb0c59432011-05-19 12:12:48 +00002438static void tg3_phy_eee_enable(struct tg3 *tp)
2439{
2440 u32 val;
2441
2442 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002443 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2444 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002445 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002446 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002447 val = MII_TG3_DSP_TAP26_ALNOKO |
2448 MII_TG3_DSP_TAP26_RMRXSTO;
2449 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002450 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002451 }
2452
2453 val = tr32(TG3_CPMU_EEE_MODE);
2454 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static int tg3_wait_macro_done(struct tg3 *tp)
2458{
2459 int limit = 100;
2460
2461 while (limit--) {
2462 u32 tmp32;
2463
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002464 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if ((tmp32 & 0x1000) == 0)
2466 break;
2467 }
2468 }
Roel Kluind4675b52009-02-12 16:33:27 -08002469 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return -EBUSY;
2471
2472 return 0;
2473}
2474
2475static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2476{
2477 static const u32 test_pat[4][6] = {
2478 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2479 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2480 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2481 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2482 };
2483 int chan;
2484
2485 for (chan = 0; chan < 4; chan++) {
2486 int i;
2487
2488 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2489 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002490 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 for (i = 0; i < 6; i++)
2493 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2494 test_pat[chan][i]);
2495
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002496 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (tg3_wait_macro_done(tp)) {
2498 *resetp = 1;
2499 return -EBUSY;
2500 }
2501
2502 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2503 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002504 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (tg3_wait_macro_done(tp)) {
2506 *resetp = 1;
2507 return -EBUSY;
2508 }
2509
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002510 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (tg3_wait_macro_done(tp)) {
2512 *resetp = 1;
2513 return -EBUSY;
2514 }
2515
2516 for (i = 0; i < 6; i += 2) {
2517 u32 low, high;
2518
2519 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2520 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2521 tg3_wait_macro_done(tp)) {
2522 *resetp = 1;
2523 return -EBUSY;
2524 }
2525 low &= 0x7fff;
2526 high &= 0x000f;
2527 if (low != test_pat[chan][i] ||
2528 high != test_pat[chan][i+1]) {
2529 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2530 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2531 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2532
2533 return -EBUSY;
2534 }
2535 }
2536 }
2537
2538 return 0;
2539}
2540
2541static int tg3_phy_reset_chanpat(struct tg3 *tp)
2542{
2543 int chan;
2544
2545 for (chan = 0; chan < 4; chan++) {
2546 int i;
2547
2548 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2549 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002550 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 for (i = 0; i < 6; i++)
2552 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002553 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (tg3_wait_macro_done(tp))
2555 return -EBUSY;
2556 }
2557
2558 return 0;
2559}
2560
2561static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2562{
2563 u32 reg32, phy9_orig;
2564 int retries, do_phy_reset, err;
2565
2566 retries = 10;
2567 do_phy_reset = 1;
2568 do {
2569 if (do_phy_reset) {
2570 err = tg3_bmcr_reset(tp);
2571 if (err)
2572 return err;
2573 do_phy_reset = 0;
2574 }
2575
2576 /* Disable transmitter and interrupt. */
2577 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2578 continue;
2579
2580 reg32 |= 0x3000;
2581 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2582
2583 /* Set full-duplex, 1000 mbps. */
2584 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002585 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002588 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 continue;
2590
Matt Carlson221c5632011-06-13 13:39:01 +00002591 tg3_writephy(tp, MII_CTRL1000,
2592 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002594 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002595 if (err)
2596 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002599 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2602 if (!err)
2603 break;
2604 } while (--retries);
2605
2606 err = tg3_phy_reset_chanpat(tp);
2607 if (err)
2608 return err;
2609
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002610 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002613 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002615 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Matt Carlson221c5632011-06-13 13:39:01 +00002617 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002619 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2620 if (err)
2621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002623 reg32 &= ~0x3000;
2624 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2625
2626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002629static void tg3_carrier_off(struct tg3 *tp)
2630{
2631 netif_carrier_off(tp->dev);
2632 tp->link_up = false;
2633}
2634
Nithin Sujirce20f162013-04-09 08:48:04 +00002635static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2636{
2637 if (tg3_flag(tp, ENABLE_ASF))
2638 netdev_warn(tp->dev,
2639 "Management side-band traffic will be interrupted during phy settings change\n");
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/* This will reset the tigon3 PHY if there is no valid
2643 * link unless the FORCE argument is non-zero.
2644 */
2645static int tg3_phy_reset(struct tg3 *tp)
2646{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002647 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 int err;
2649
Joe Perches41535772013-02-16 11:20:04 +00002650 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002651 val = tr32(GRC_MISC_CFG);
2652 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2653 udelay(40);
2654 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002655 err = tg3_readphy(tp, MII_BMSR, &val);
2656 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (err != 0)
2658 return -EBUSY;
2659
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002660 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002661 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002662 tg3_link_report(tp);
2663 }
2664
Joe Perches41535772013-02-16 11:20:04 +00002665 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2666 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2667 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 err = tg3_phy_reset_5703_4_5(tp);
2669 if (err)
2670 return err;
2671 goto out;
2672 }
2673
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002674 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002675 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2676 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002677 cpmuctrl = tr32(TG3_CPMU_CTRL);
2678 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2679 tw32(TG3_CPMU_CTRL,
2680 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2681 }
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 err = tg3_bmcr_reset(tp);
2684 if (err)
2685 return err;
2686
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002687 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002688 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2689 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002690
2691 tw32(TG3_CPMU_CTRL, cpmuctrl);
2692 }
2693
Joe Perches41535772013-02-16 11:20:04 +00002694 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2695 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002696 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2697 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2698 CPMU_LSPD_1000MB_MACCLK_12_5) {
2699 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2700 udelay(40);
2701 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2702 }
2703 }
2704
Joe Perches63c3a662011-04-26 08:12:10 +00002705 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002706 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002707 return 0;
2708
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002709 tg3_phy_apply_otp(tp);
2710
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002711 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002712 tg3_phy_toggle_apd(tp, true);
2713 else
2714 tg3_phy_toggle_apd(tp, false);
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002717 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002718 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002719 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2720 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002721 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002723
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002724 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002725 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2726 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002728
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002729 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002730 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002731 tg3_phydsp_write(tp, 0x000a, 0x310b);
2732 tg3_phydsp_write(tp, 0x201f, 0x9506);
2733 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002734 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002735 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002736 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002737 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002738 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2739 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2740 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2741 tg3_writephy(tp, MII_TG3_TEST1,
2742 MII_TG3_TEST1_TRIM_EN | 0x4);
2743 } else
2744 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2745
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002746 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002747 }
Michael Chanc424cb22006-04-29 18:56:34 -07002748 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Set Extended packet length bit (bit 14) on all chips that */
2751 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002752 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002754 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002755 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002757 err = tg3_phy_auxctl_read(tp,
2758 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2759 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002760 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2761 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763
2764 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2765 * jumbo frames transmission.
2766 */
Joe Perches63c3a662011-04-26 08:12:10 +00002767 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002768 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002769 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002770 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772
Joe Perches41535772013-02-16 11:20:04 +00002773 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002774 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002775 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002776 }
2777
Joe Perches41535772013-02-16 11:20:04 +00002778 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002779 tg3_phydsp_write(tp, 0xffb, 0x4000);
2780
Joe Perches953c96e2013-04-09 10:18:14 +00002781 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 tg3_phy_set_wirespeed(tp);
2783 return 0;
2784}
2785
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002786#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2787#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2788#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2789 TG3_GPIO_MSG_NEED_VAUX)
2790#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2791 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2792 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2793 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2794 (TG3_GPIO_MSG_DRVR_PRES << 12))
2795
2796#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2797 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2798 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2799 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2800 (TG3_GPIO_MSG_NEED_VAUX << 12))
2801
2802static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2803{
2804 u32 status, 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 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2809 else
2810 status = tr32(TG3_CPMU_DRV_STATUS);
2811
2812 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2813 status &= ~(TG3_GPIO_MSG_MASK << shift);
2814 status |= (newstat << shift);
2815
Joe Perches41535772013-02-16 11:20:04 +00002816 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2817 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002818 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2819 else
2820 tw32(TG3_CPMU_DRV_STATUS, status);
2821
2822 return status >> TG3_APE_GPIO_MSG_SHIFT;
2823}
2824
Matt Carlson520b2752011-06-13 13:39:02 +00002825static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2826{
2827 if (!tg3_flag(tp, IS_NIC))
2828 return 0;
2829
Joe Perches41535772013-02-16 11:20:04 +00002830 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2831 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2832 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002833 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2834 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002835
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002836 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2837
2838 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2839 TG3_GRC_LCLCTL_PWRSW_DELAY);
2840
2841 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2842 } else {
2843 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2844 TG3_GRC_LCLCTL_PWRSW_DELAY);
2845 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002846
Matt Carlson520b2752011-06-13 13:39:02 +00002847 return 0;
2848}
2849
2850static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2851{
2852 u32 grc_local_ctrl;
2853
2854 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002855 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2856 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002857 return;
2858
2859 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2860
2861 tw32_wait_f(GRC_LOCAL_CTRL,
2862 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2863 TG3_GRC_LCLCTL_PWRSW_DELAY);
2864
2865 tw32_wait_f(GRC_LOCAL_CTRL,
2866 grc_local_ctrl,
2867 TG3_GRC_LCLCTL_PWRSW_DELAY);
2868
2869 tw32_wait_f(GRC_LOCAL_CTRL,
2870 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2871 TG3_GRC_LCLCTL_PWRSW_DELAY);
2872}
2873
2874static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2875{
2876 if (!tg3_flag(tp, IS_NIC))
2877 return;
2878
Joe Perches41535772013-02-16 11:20:04 +00002879 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2880 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002881 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2882 (GRC_LCLCTRL_GPIO_OE0 |
2883 GRC_LCLCTRL_GPIO_OE1 |
2884 GRC_LCLCTRL_GPIO_OE2 |
2885 GRC_LCLCTRL_GPIO_OUTPUT0 |
2886 GRC_LCLCTRL_GPIO_OUTPUT1),
2887 TG3_GRC_LCLCTL_PWRSW_DELAY);
2888 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2889 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2890 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2891 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2892 GRC_LCLCTRL_GPIO_OE1 |
2893 GRC_LCLCTRL_GPIO_OE2 |
2894 GRC_LCLCTRL_GPIO_OUTPUT0 |
2895 GRC_LCLCTRL_GPIO_OUTPUT1 |
2896 tp->grc_local_ctrl;
2897 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2898 TG3_GRC_LCLCTL_PWRSW_DELAY);
2899
2900 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2901 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2902 TG3_GRC_LCLCTL_PWRSW_DELAY);
2903
2904 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2905 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2906 TG3_GRC_LCLCTL_PWRSW_DELAY);
2907 } else {
2908 u32 no_gpio2;
2909 u32 grc_local_ctrl = 0;
2910
2911 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002912 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002913 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2914 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2915 grc_local_ctrl,
2916 TG3_GRC_LCLCTL_PWRSW_DELAY);
2917 }
2918
2919 /* On 5753 and variants, GPIO2 cannot be used. */
2920 no_gpio2 = tp->nic_sram_data_cfg &
2921 NIC_SRAM_DATA_CFG_NO_GPIO2;
2922
2923 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2924 GRC_LCLCTRL_GPIO_OE1 |
2925 GRC_LCLCTRL_GPIO_OE2 |
2926 GRC_LCLCTRL_GPIO_OUTPUT1 |
2927 GRC_LCLCTRL_GPIO_OUTPUT2;
2928 if (no_gpio2) {
2929 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2930 GRC_LCLCTRL_GPIO_OUTPUT2);
2931 }
2932 tw32_wait_f(GRC_LOCAL_CTRL,
2933 tp->grc_local_ctrl | grc_local_ctrl,
2934 TG3_GRC_LCLCTL_PWRSW_DELAY);
2935
2936 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2937
2938 tw32_wait_f(GRC_LOCAL_CTRL,
2939 tp->grc_local_ctrl | grc_local_ctrl,
2940 TG3_GRC_LCLCTL_PWRSW_DELAY);
2941
2942 if (!no_gpio2) {
2943 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2944 tw32_wait_f(GRC_LOCAL_CTRL,
2945 tp->grc_local_ctrl | grc_local_ctrl,
2946 TG3_GRC_LCLCTL_PWRSW_DELAY);
2947 }
2948 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002949}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002950
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002951static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002952{
2953 u32 msg = 0;
2954
2955 /* Serialize power state transitions */
2956 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2957 return;
2958
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002959 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002960 msg = TG3_GPIO_MSG_NEED_VAUX;
2961
2962 msg = tg3_set_function_status(tp, msg);
2963
2964 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2965 goto done;
2966
2967 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2968 tg3_pwrsrc_switch_to_vaux(tp);
2969 else
2970 tg3_pwrsrc_die_with_vmain(tp);
2971
2972done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002973 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002974}
2975
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002976static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Matt Carlson683644b2011-03-09 16:58:23 +00002978 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Matt Carlson334355a2010-01-20 16:58:10 +00002980 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002981 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return;
2983
Joe Perches41535772013-02-16 11:20:04 +00002984 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2985 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2986 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002987 tg3_frob_aux_power_5717(tp, include_wol ?
2988 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002989 return;
2990 }
2991
2992 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002993 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002995 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002996
Michael Chanbc1c7562006-03-20 17:48:03 -08002997 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002998 if (dev_peer) {
2999 struct tg3 *tp_peer = netdev_priv(dev_peer);
3000
Joe Perches63c3a662011-04-26 08:12:10 +00003001 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00003002 return;
3003
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003004 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003005 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003006 need_vaux = true;
3007 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08003008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003010 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3011 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003012 need_vaux = true;
3013
Matt Carlson520b2752011-06-13 13:39:02 +00003014 if (need_vaux)
3015 tg3_pwrsrc_switch_to_vaux(tp);
3016 else
3017 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003020static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3021{
3022 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3023 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003024 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003025 if (speed != SPEED_10)
3026 return 1;
3027 } else if (speed == SPEED_10)
3028 return 1;
3029
3030 return 0;
3031}
3032
Nithin Sujir44f3b502013-05-13 11:04:15 +00003033static bool tg3_phy_power_bug(struct tg3 *tp)
3034{
3035 switch (tg3_asic_rev(tp)) {
3036 case ASIC_REV_5700:
3037 case ASIC_REV_5704:
3038 return true;
3039 case ASIC_REV_5780:
3040 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3041 return true;
3042 return false;
3043 case ASIC_REV_5717:
3044 if (!tp->pci_fn)
3045 return true;
3046 return false;
3047 case ASIC_REV_5719:
3048 case ASIC_REV_5720:
3049 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3050 !tp->pci_fn)
3051 return true;
3052 return false;
3053 }
3054
3055 return false;
3056}
3057
Nithin Sujir989038e2013-08-30 17:01:36 -07003058static bool tg3_phy_led_bug(struct tg3 *tp)
3059{
3060 switch (tg3_asic_rev(tp)) {
3061 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003062 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003063 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3064 !tp->pci_fn)
3065 return true;
3066 return false;
3067 }
3068
3069 return false;
3070}
3071
Matt Carlson0a459aa2008-11-03 16:54:15 -08003072static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003073{
Matt Carlsonce057f02007-11-12 21:08:03 -08003074 u32 val;
3075
Nithin Sujir942d1af2013-04-09 08:48:07 +00003076 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3077 return;
3078
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003079 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003080 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003081 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3082 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3083
3084 sg_dig_ctrl |=
3085 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3086 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3087 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3088 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003089 return;
Michael Chan51297242007-02-13 12:17:57 -08003090 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003091
Joe Perches41535772013-02-16 11:20:04 +00003092 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003093 tg3_bmcr_reset(tp);
3094 val = tr32(GRC_MISC_CFG);
3095 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3096 udelay(40);
3097 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003098 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003099 u32 phytest;
3100 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3101 u32 phy;
3102
3103 tg3_writephy(tp, MII_ADVERTISE, 0);
3104 tg3_writephy(tp, MII_BMCR,
3105 BMCR_ANENABLE | BMCR_ANRESTART);
3106
3107 tg3_writephy(tp, MII_TG3_FET_TEST,
3108 phytest | MII_TG3_FET_SHADOW_EN);
3109 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3110 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3111 tg3_writephy(tp,
3112 MII_TG3_FET_SHDW_AUXMODE4,
3113 phy);
3114 }
3115 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3116 }
3117 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003118 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003119 if (!tg3_phy_led_bug(tp))
3120 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3121 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003122
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003123 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3124 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3125 MII_TG3_AUXCTL_PCTL_VREG_11V;
3126 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003127 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003128
Michael Chan15c3b692006-03-22 01:06:52 -08003129 /* The PHY should not be powered down on some chips because
3130 * of bugs.
3131 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003132 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003133 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003134
Joe Perches41535772013-02-16 11:20:04 +00003135 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3136 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003137 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3138 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3139 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3140 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3141 }
3142
Michael Chan15c3b692006-03-22 01:06:52 -08003143 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3144}
3145
Matt Carlson3f007892008-11-03 16:51:36 -08003146/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003147static int tg3_nvram_lock(struct tg3 *tp)
3148{
Joe Perches63c3a662011-04-26 08:12:10 +00003149 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003150 int i;
3151
3152 if (tp->nvram_lock_cnt == 0) {
3153 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3154 for (i = 0; i < 8000; i++) {
3155 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3156 break;
3157 udelay(20);
3158 }
3159 if (i == 8000) {
3160 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3161 return -ENODEV;
3162 }
3163 }
3164 tp->nvram_lock_cnt++;
3165 }
3166 return 0;
3167}
3168
3169/* tp->lock is held. */
3170static void tg3_nvram_unlock(struct tg3 *tp)
3171{
Joe Perches63c3a662011-04-26 08:12:10 +00003172 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003173 if (tp->nvram_lock_cnt > 0)
3174 tp->nvram_lock_cnt--;
3175 if (tp->nvram_lock_cnt == 0)
3176 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3177 }
3178}
3179
3180/* tp->lock is held. */
3181static void tg3_enable_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
3190/* tp->lock is held. */
3191static void tg3_disable_nvram_access(struct tg3 *tp)
3192{
Joe Perches63c3a662011-04-26 08:12:10 +00003193 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003194 u32 nvaccess = tr32(NVRAM_ACCESS);
3195
3196 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3197 }
3198}
3199
3200static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3201 u32 offset, u32 *val)
3202{
3203 u32 tmp;
3204 int i;
3205
3206 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3207 return -EINVAL;
3208
3209 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3210 EEPROM_ADDR_DEVID_MASK |
3211 EEPROM_ADDR_READ);
3212 tw32(GRC_EEPROM_ADDR,
3213 tmp |
3214 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3215 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3216 EEPROM_ADDR_ADDR_MASK) |
3217 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3218
3219 for (i = 0; i < 1000; i++) {
3220 tmp = tr32(GRC_EEPROM_ADDR);
3221
3222 if (tmp & EEPROM_ADDR_COMPLETE)
3223 break;
3224 msleep(1);
3225 }
3226 if (!(tmp & EEPROM_ADDR_COMPLETE))
3227 return -EBUSY;
3228
Matt Carlson62cedd12009-04-20 14:52:29 -07003229 tmp = tr32(GRC_EEPROM_DATA);
3230
3231 /*
3232 * The data will always be opposite the native endian
3233 * format. Perform a blind byteswap to compensate.
3234 */
3235 *val = swab32(tmp);
3236
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003237 return 0;
3238}
3239
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +05303240#define NVRAM_CMD_TIMEOUT 10000
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003241
3242static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3243{
3244 int i;
3245
3246 tw32(NVRAM_CMD, nvram_cmd);
3247 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
Prashant Sreedharan66c965f2014-06-20 23:28:15 -07003248 usleep_range(10, 40);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003249 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3250 udelay(10);
3251 break;
3252 }
3253 }
3254
3255 if (i == NVRAM_CMD_TIMEOUT)
3256 return -EBUSY;
3257
3258 return 0;
3259}
3260
3261static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3262{
Joe Perches63c3a662011-04-26 08:12:10 +00003263 if (tg3_flag(tp, NVRAM) &&
3264 tg3_flag(tp, NVRAM_BUFFERED) &&
3265 tg3_flag(tp, FLASH) &&
3266 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003267 (tp->nvram_jedecnum == JEDEC_ATMEL))
3268
3269 addr = ((addr / tp->nvram_pagesize) <<
3270 ATMEL_AT45DB0X1B_PAGE_POS) +
3271 (addr % tp->nvram_pagesize);
3272
3273 return addr;
3274}
3275
3276static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3277{
Joe Perches63c3a662011-04-26 08:12:10 +00003278 if (tg3_flag(tp, NVRAM) &&
3279 tg3_flag(tp, NVRAM_BUFFERED) &&
3280 tg3_flag(tp, FLASH) &&
3281 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003282 (tp->nvram_jedecnum == JEDEC_ATMEL))
3283
3284 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3285 tp->nvram_pagesize) +
3286 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3287
3288 return addr;
3289}
3290
Matt Carlsone4f34112009-02-25 14:25:00 +00003291/* NOTE: Data read in from NVRAM is byteswapped according to
3292 * the byteswapping settings for all other register accesses.
3293 * tg3 devices are BE devices, so on a BE machine, the data
3294 * returned will be exactly as it is seen in NVRAM. On a LE
3295 * machine, the 32-bit value will be byteswapped.
3296 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003297static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3298{
3299 int ret;
3300
Joe Perches63c3a662011-04-26 08:12:10 +00003301 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003302 return tg3_nvram_read_using_eeprom(tp, offset, val);
3303
3304 offset = tg3_nvram_phys_addr(tp, offset);
3305
3306 if (offset > NVRAM_ADDR_MSK)
3307 return -EINVAL;
3308
3309 ret = tg3_nvram_lock(tp);
3310 if (ret)
3311 return ret;
3312
3313 tg3_enable_nvram_access(tp);
3314
3315 tw32(NVRAM_ADDR, offset);
3316 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3317 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3318
3319 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003320 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003321
3322 tg3_disable_nvram_access(tp);
3323
3324 tg3_nvram_unlock(tp);
3325
3326 return ret;
3327}
3328
Matt Carlsona9dc5292009-02-25 14:25:30 +00003329/* Ensures NVRAM data is in bytestream format. */
3330static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003331{
3332 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003333 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003334 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003335 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003336 return res;
3337}
3338
Matt Carlsondbe9b922012-02-13 10:20:09 +00003339static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3340 u32 offset, u32 len, u8 *buf)
3341{
3342 int i, j, rc = 0;
3343 u32 val;
3344
3345 for (i = 0; i < len; i += 4) {
3346 u32 addr;
3347 __be32 data;
3348
3349 addr = offset + i;
3350
3351 memcpy(&data, buf + i, 4);
3352
3353 /*
3354 * The SEEPROM interface expects the data to always be opposite
3355 * the native endian format. We accomplish this by reversing
3356 * all the operations that would have been performed on the
3357 * data from a call to tg3_nvram_read_be32().
3358 */
3359 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3360
3361 val = tr32(GRC_EEPROM_ADDR);
3362 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3363
3364 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3365 EEPROM_ADDR_READ);
3366 tw32(GRC_EEPROM_ADDR, val |
3367 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3368 (addr & EEPROM_ADDR_ADDR_MASK) |
3369 EEPROM_ADDR_START |
3370 EEPROM_ADDR_WRITE);
3371
3372 for (j = 0; j < 1000; j++) {
3373 val = tr32(GRC_EEPROM_ADDR);
3374
3375 if (val & EEPROM_ADDR_COMPLETE)
3376 break;
3377 msleep(1);
3378 }
3379 if (!(val & EEPROM_ADDR_COMPLETE)) {
3380 rc = -EBUSY;
3381 break;
3382 }
3383 }
3384
3385 return rc;
3386}
3387
3388/* offset and length are dword aligned */
3389static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3390 u8 *buf)
3391{
3392 int ret = 0;
3393 u32 pagesize = tp->nvram_pagesize;
3394 u32 pagemask = pagesize - 1;
3395 u32 nvram_cmd;
3396 u8 *tmp;
3397
3398 tmp = kmalloc(pagesize, GFP_KERNEL);
3399 if (tmp == NULL)
3400 return -ENOMEM;
3401
3402 while (len) {
3403 int j;
3404 u32 phy_addr, page_off, size;
3405
3406 phy_addr = offset & ~pagemask;
3407
3408 for (j = 0; j < pagesize; j += 4) {
3409 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3410 (__be32 *) (tmp + j));
3411 if (ret)
3412 break;
3413 }
3414 if (ret)
3415 break;
3416
3417 page_off = offset & pagemask;
3418 size = pagesize;
3419 if (len < size)
3420 size = len;
3421
3422 len -= size;
3423
3424 memcpy(tmp + page_off, buf, size);
3425
3426 offset = offset + (pagesize - page_off);
3427
3428 tg3_enable_nvram_access(tp);
3429
3430 /*
3431 * Before we can erase the flash page, we need
3432 * to issue a special "write enable" command.
3433 */
3434 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3435
3436 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3437 break;
3438
3439 /* Erase the target page */
3440 tw32(NVRAM_ADDR, phy_addr);
3441
3442 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3443 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3444
3445 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3446 break;
3447
3448 /* Issue another write enable to start the write. */
3449 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3450
3451 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3452 break;
3453
3454 for (j = 0; j < pagesize; j += 4) {
3455 __be32 data;
3456
3457 data = *((__be32 *) (tmp + j));
3458
3459 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3460
3461 tw32(NVRAM_ADDR, phy_addr + j);
3462
3463 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3464 NVRAM_CMD_WR;
3465
3466 if (j == 0)
3467 nvram_cmd |= NVRAM_CMD_FIRST;
3468 else if (j == (pagesize - 4))
3469 nvram_cmd |= NVRAM_CMD_LAST;
3470
3471 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3472 if (ret)
3473 break;
3474 }
3475 if (ret)
3476 break;
3477 }
3478
3479 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3480 tg3_nvram_exec_cmd(tp, nvram_cmd);
3481
3482 kfree(tmp);
3483
3484 return ret;
3485}
3486
3487/* offset and length are dword aligned */
3488static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3489 u8 *buf)
3490{
3491 int i, ret = 0;
3492
3493 for (i = 0; i < len; i += 4, offset += 4) {
3494 u32 page_off, phy_addr, nvram_cmd;
3495 __be32 data;
3496
3497 memcpy(&data, buf + i, 4);
3498 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3499
3500 page_off = offset % tp->nvram_pagesize;
3501
3502 phy_addr = tg3_nvram_phys_addr(tp, offset);
3503
Matt Carlsondbe9b922012-02-13 10:20:09 +00003504 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3505
3506 if (page_off == 0 || i == 0)
3507 nvram_cmd |= NVRAM_CMD_FIRST;
3508 if (page_off == (tp->nvram_pagesize - 4))
3509 nvram_cmd |= NVRAM_CMD_LAST;
3510
3511 if (i == (len - 4))
3512 nvram_cmd |= NVRAM_CMD_LAST;
3513
Matt Carlson42278222012-02-13 15:20:11 +00003514 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3515 !tg3_flag(tp, FLASH) ||
3516 !tg3_flag(tp, 57765_PLUS))
3517 tw32(NVRAM_ADDR, phy_addr);
3518
Joe Perches41535772013-02-16 11:20:04 +00003519 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003520 !tg3_flag(tp, 5755_PLUS) &&
3521 (tp->nvram_jedecnum == JEDEC_ST) &&
3522 (nvram_cmd & NVRAM_CMD_FIRST)) {
3523 u32 cmd;
3524
3525 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3526 ret = tg3_nvram_exec_cmd(tp, cmd);
3527 if (ret)
3528 break;
3529 }
3530 if (!tg3_flag(tp, FLASH)) {
3531 /* We always do complete word writes to eeprom. */
3532 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3533 }
3534
3535 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3536 if (ret)
3537 break;
3538 }
3539 return ret;
3540}
3541
3542/* offset and length are dword aligned */
3543static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3544{
3545 int ret;
3546
3547 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3548 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3549 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3550 udelay(40);
3551 }
3552
3553 if (!tg3_flag(tp, NVRAM)) {
3554 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3555 } else {
3556 u32 grc_mode;
3557
3558 ret = tg3_nvram_lock(tp);
3559 if (ret)
3560 return ret;
3561
3562 tg3_enable_nvram_access(tp);
3563 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3564 tw32(NVRAM_WRITE1, 0x406);
3565
3566 grc_mode = tr32(GRC_MODE);
3567 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3568
3569 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3570 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3571 buf);
3572 } else {
3573 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3574 buf);
3575 }
3576
3577 grc_mode = tr32(GRC_MODE);
3578 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3579
3580 tg3_disable_nvram_access(tp);
3581 tg3_nvram_unlock(tp);
3582 }
3583
3584 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3585 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3586 udelay(40);
3587 }
3588
3589 return ret;
3590}
3591
Matt Carlson997b4f12011-08-31 11:44:53 +00003592#define RX_CPU_SCRATCH_BASE 0x30000
3593#define RX_CPU_SCRATCH_SIZE 0x04000
3594#define TX_CPU_SCRATCH_BASE 0x34000
3595#define TX_CPU_SCRATCH_SIZE 0x04000
3596
3597/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003598static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003599{
3600 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003601 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003602
Nithin Sujir837c45b2013-03-06 17:02:30 +00003603 for (i = 0; i < iters; i++) {
3604 tw32(cpu_base + CPU_STATE, 0xffffffff);
3605 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3606 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3607 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003608 if (pci_channel_offline(tp->pdev))
3609 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003610 }
3611
3612 return (i == iters) ? -EBUSY : 0;
3613}
3614
3615/* tp->lock is held. */
3616static int tg3_rxcpu_pause(struct tg3 *tp)
3617{
3618 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3619
3620 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3621 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3622 udelay(10);
3623
3624 return rc;
3625}
3626
3627/* tp->lock is held. */
3628static int tg3_txcpu_pause(struct tg3 *tp)
3629{
3630 return tg3_pause_cpu(tp, TX_CPU_BASE);
3631}
3632
3633/* tp->lock is held. */
3634static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3635{
3636 tw32(cpu_base + CPU_STATE, 0xffffffff);
3637 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3638}
3639
3640/* tp->lock is held. */
3641static void tg3_rxcpu_resume(struct tg3 *tp)
3642{
3643 tg3_resume_cpu(tp, RX_CPU_BASE);
3644}
3645
3646/* tp->lock is held. */
3647static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3648{
3649 int rc;
3650
3651 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003652
Joe Perches41535772013-02-16 11:20:04 +00003653 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003654 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3655
3656 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3657 return 0;
3658 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003659 if (cpu_base == RX_CPU_BASE) {
3660 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003661 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003662 /*
3663 * There is only an Rx CPU for the 5750 derivative in the
3664 * BCM4785.
3665 */
3666 if (tg3_flag(tp, IS_SSB_CORE))
3667 return 0;
3668
Nithin Sujir837c45b2013-03-06 17:02:30 +00003669 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003670 }
3671
Nithin Sujir837c45b2013-03-06 17:02:30 +00003672 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003673 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003674 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003675 return -ENODEV;
3676 }
3677
3678 /* Clear firmware's nvram arbitration. */
3679 if (tg3_flag(tp, NVRAM))
3680 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3681 return 0;
3682}
3683
Nithin Sujir31f11a92013-03-06 17:02:33 +00003684static int tg3_fw_data_len(struct tg3 *tp,
3685 const struct tg3_firmware_hdr *fw_hdr)
3686{
3687 int fw_len;
3688
3689 /* Non fragmented firmware have one firmware header followed by a
3690 * contiguous chunk of data to be written. The length field in that
3691 * header is not the length of data to be written but the complete
3692 * length of the bss. The data length is determined based on
3693 * tp->fw->size minus headers.
3694 *
3695 * Fragmented firmware have a main header followed by multiple
3696 * fragments. Each fragment is identical to non fragmented firmware
3697 * with a firmware header followed by a contiguous chunk of data. In
3698 * the main header, the length field is unused and set to 0xffffffff.
3699 * In each fragment header the length is the entire size of that
3700 * fragment i.e. fragment data + header length. Data length is
3701 * therefore length field in the header minus TG3_FW_HDR_LEN.
3702 */
3703 if (tp->fw_len == 0xffffffff)
3704 fw_len = be32_to_cpu(fw_hdr->len);
3705 else
3706 fw_len = tp->fw->size;
3707
3708 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3709}
3710
Matt Carlson997b4f12011-08-31 11:44:53 +00003711/* tp->lock is held. */
3712static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3713 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003714 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003715{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003716 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003717 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003718 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003719
3720 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3721 netdev_err(tp->dev,
3722 "%s: Trying to load TX cpu firmware which is 5705\n",
3723 __func__);
3724 return -EINVAL;
3725 }
3726
Nithin Sujirc4dab502013-03-06 17:02:34 +00003727 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003728 write_op = tg3_write_mem;
3729 else
3730 write_op = tg3_write_indirect_reg32;
3731
Nithin Sujirc4dab502013-03-06 17:02:34 +00003732 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3733 /* It is possible that bootcode is still loading at this point.
3734 * Get the nvram lock first before halting the cpu.
3735 */
3736 int lock_err = tg3_nvram_lock(tp);
3737 err = tg3_halt_cpu(tp, cpu_base);
3738 if (!lock_err)
3739 tg3_nvram_unlock(tp);
3740 if (err)
3741 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003742
Nithin Sujirc4dab502013-03-06 17:02:34 +00003743 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3744 write_op(tp, cpu_scratch_base + i, 0);
3745 tw32(cpu_base + CPU_STATE, 0xffffffff);
3746 tw32(cpu_base + CPU_MODE,
3747 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3748 } else {
3749 /* Subtract additional main header for fragmented firmware and
3750 * advance to the first fragment
3751 */
3752 total_len -= TG3_FW_HDR_LEN;
3753 fw_hdr++;
3754 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003755
Nithin Sujir31f11a92013-03-06 17:02:33 +00003756 do {
3757 u32 *fw_data = (u32 *)(fw_hdr + 1);
3758 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3759 write_op(tp, cpu_scratch_base +
3760 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3761 (i * sizeof(u32)),
3762 be32_to_cpu(fw_data[i]));
3763
3764 total_len -= be32_to_cpu(fw_hdr->len);
3765
3766 /* Advance to next fragment */
3767 fw_hdr = (struct tg3_firmware_hdr *)
3768 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3769 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003770
3771 err = 0;
3772
3773out:
3774 return err;
3775}
3776
3777/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003778static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3779{
3780 int i;
3781 const int iters = 5;
3782
3783 tw32(cpu_base + CPU_STATE, 0xffffffff);
3784 tw32_f(cpu_base + CPU_PC, pc);
3785
3786 for (i = 0; i < iters; i++) {
3787 if (tr32(cpu_base + CPU_PC) == pc)
3788 break;
3789 tw32(cpu_base + CPU_STATE, 0xffffffff);
3790 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3791 tw32_f(cpu_base + CPU_PC, pc);
3792 udelay(1000);
3793 }
3794
3795 return (i == iters) ? -EBUSY : 0;
3796}
3797
3798/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003799static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3800{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003801 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003802 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003803
Nithin Sujir77997ea2013-03-06 17:02:32 +00003804 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003805
3806 /* Firmware blob starts with version numbers, followed by
3807 start address and length. We are setting complete length.
3808 length = end_address_of_bss - start_address_of_text.
3809 Remainder is the blob to be loaded contiguously
3810 from start address. */
3811
Matt Carlson997b4f12011-08-31 11:44:53 +00003812 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3813 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003814 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003815 if (err)
3816 return err;
3817
3818 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3819 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003820 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003821 if (err)
3822 return err;
3823
3824 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003825 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3826 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003827 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003828 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3829 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003830 tr32(RX_CPU_BASE + CPU_PC),
3831 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003832 return -ENODEV;
3833 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003834
3835 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003836
3837 return 0;
3838}
3839
Nithin Sujirc4dab502013-03-06 17:02:34 +00003840static int tg3_validate_rxcpu_state(struct tg3 *tp)
3841{
3842 const int iters = 1000;
3843 int i;
3844 u32 val;
3845
3846 /* Wait for boot code to complete initialization and enter service
3847 * loop. It is then safe to download service patches
3848 */
3849 for (i = 0; i < iters; i++) {
3850 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3851 break;
3852
3853 udelay(10);
3854 }
3855
3856 if (i == iters) {
3857 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3858 return -EBUSY;
3859 }
3860
3861 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3862 if (val & 0xff) {
3863 netdev_warn(tp->dev,
3864 "Other patches exist. Not downloading EEE patch\n");
3865 return -EEXIST;
3866 }
3867
3868 return 0;
3869}
3870
3871/* tp->lock is held. */
3872static void tg3_load_57766_firmware(struct tg3 *tp)
3873{
3874 struct tg3_firmware_hdr *fw_hdr;
3875
3876 if (!tg3_flag(tp, NO_NVRAM))
3877 return;
3878
3879 if (tg3_validate_rxcpu_state(tp))
3880 return;
3881
3882 if (!tp->fw)
3883 return;
3884
3885 /* This firmware blob has a different format than older firmware
3886 * releases as given below. The main difference is we have fragmented
3887 * data to be written to non-contiguous locations.
3888 *
3889 * In the beginning we have a firmware header identical to other
3890 * firmware which consists of version, base addr and length. The length
3891 * here is unused and set to 0xffffffff.
3892 *
3893 * This is followed by a series of firmware fragments which are
3894 * individually identical to previous firmware. i.e. they have the
3895 * firmware header and followed by data for that fragment. The version
3896 * field of the individual fragment header is unused.
3897 */
3898
3899 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3900 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3901 return;
3902
3903 if (tg3_rxcpu_pause(tp))
3904 return;
3905
3906 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3907 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3908
3909 tg3_rxcpu_resume(tp);
3910}
3911
Matt Carlson997b4f12011-08-31 11:44:53 +00003912/* tp->lock is held. */
3913static int tg3_load_tso_firmware(struct tg3 *tp)
3914{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003915 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003916 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003917 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003918
Matt Carlson1caf13e2013-03-06 17:02:29 +00003919 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003920 return 0;
3921
Nithin Sujir77997ea2013-03-06 17:02:32 +00003922 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003923
3924 /* Firmware blob starts with version numbers, followed by
3925 start address and length. We are setting complete length.
3926 length = end_address_of_bss - start_address_of_text.
3927 Remainder is the blob to be loaded contiguously
3928 from start address. */
3929
Matt Carlson997b4f12011-08-31 11:44:53 +00003930 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003931
Joe Perches41535772013-02-16 11:20:04 +00003932 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003933 cpu_base = RX_CPU_BASE;
3934 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3935 } else {
3936 cpu_base = TX_CPU_BASE;
3937 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3938 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3939 }
3940
3941 err = tg3_load_firmware_cpu(tp, cpu_base,
3942 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003943 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003944 if (err)
3945 return err;
3946
3947 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003948 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3949 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003950 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003951 netdev_err(tp->dev,
3952 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003953 __func__, tr32(cpu_base + CPU_PC),
3954 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003955 return -ENODEV;
3956 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003957
3958 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003959 return 0;
3960}
3961
Michael Chanf022ae62014-01-03 10:09:11 -08003962/* tp->lock is held. */
3963static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3964{
3965 u32 addr_high, addr_low;
3966
3967 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3968 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3969 (mac_addr[4] << 8) | mac_addr[5]);
3970
3971 if (index < 4) {
3972 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3973 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3974 } else {
3975 index -= 4;
3976 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3977 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3978 }
3979}
Matt Carlson997b4f12011-08-31 11:44:53 +00003980
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003981/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003982static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003983{
Michael Chanf022ae62014-01-03 10:09:11 -08003984 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003985 int i;
3986
Matt Carlson3f007892008-11-03 16:51:36 -08003987 for (i = 0; i < 4; i++) {
3988 if (i == 1 && skip_mac_1)
3989 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003990 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003991 }
3992
Joe Perches41535772013-02-16 11:20:04 +00003993 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3994 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003995 for (i = 4; i < 16; i++)
3996 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003997 }
3998
3999 addr_high = (tp->dev->dev_addr[0] +
4000 tp->dev->dev_addr[1] +
4001 tp->dev->dev_addr[2] +
4002 tp->dev->dev_addr[3] +
4003 tp->dev->dev_addr[4] +
4004 tp->dev->dev_addr[5]) &
4005 TX_BACKOFF_SEED_MASK;
4006 tw32(MAC_TX_BACKOFF_SEED, addr_high);
4007}
4008
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004009static void tg3_enable_register_access(struct tg3 *tp)
4010{
4011 /*
4012 * Make sure register accesses (indirect or otherwise) will function
4013 * correctly.
4014 */
4015 pci_write_config_dword(tp->pdev,
4016 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4017}
4018
4019static int tg3_power_up(struct tg3 *tp)
4020{
Matt Carlsonbed98292011-07-13 09:27:29 +00004021 int err;
4022
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004023 tg3_enable_register_access(tp);
4024
Matt Carlsonbed98292011-07-13 09:27:29 +00004025 err = pci_set_power_state(tp->pdev, PCI_D0);
4026 if (!err) {
4027 /* Switch out of Vaux if it is a NIC */
4028 tg3_pwrsrc_switch_to_vmain(tp);
4029 } else {
4030 netdev_err(tp->dev, "Transition to D0 failed\n");
4031 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004032
Matt Carlsonbed98292011-07-13 09:27:29 +00004033 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004034}
4035
Joe Perches953c96e2013-04-09 10:18:14 +00004036static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004037
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004038static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
4040 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004041 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004043 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004044
4045 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004046 if (tg3_flag(tp, CLKREQ_BUG))
4047 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4048 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4051 tw32(TG3PCI_MISC_HOST_CTRL,
4052 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4053
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004054 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004055 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004056
Joe Perches63c3a662011-04-26 08:12:10 +00004057 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004058 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004059 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00004060 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004061 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004062 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004063
Andrew Lunn7f854422016-01-06 20:11:18 +01004064 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004065
Matt Carlson80096062010-08-02 11:26:06 +00004066 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004067
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004068 tp->link_config.speed = phydev->speed;
4069 tp->link_config.duplex = phydev->duplex;
4070 tp->link_config.autoneg = phydev->autoneg;
4071 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004072
4073 advertising = ADVERTISED_TP |
4074 ADVERTISED_Pause |
4075 ADVERTISED_Autoneg |
4076 ADVERTISED_10baseT_Half;
4077
Joe Perches63c3a662011-04-26 08:12:10 +00004078 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4079 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004080 advertising |=
4081 ADVERTISED_100baseT_Half |
4082 ADVERTISED_100baseT_Full |
4083 ADVERTISED_10baseT_Full;
4084 else
4085 advertising |= ADVERTISED_10baseT_Full;
4086 }
4087
4088 phydev->advertising = advertising;
4089
4090 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004091
4092 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004093 if (phyid != PHY_ID_BCMAC131) {
4094 phyid &= PHY_BCM_OUI_MASK;
4095 if (phyid == PHY_BCM_OUI_1 ||
4096 phyid == PHY_BCM_OUI_2 ||
4097 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004098 do_low_power = true;
4099 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004100 }
Matt Carlsondd477002008-05-25 23:45:58 -07004101 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004102 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004103
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004104 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00004105 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Matt Carlson2855b9f2012-02-13 15:20:14 +00004107 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004108 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 }
4110
Joe Perches41535772013-02-16 11:20:04 +00004111 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004112 u32 val;
4113
4114 val = tr32(GRC_VCPU_EXT_CTRL);
4115 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004116 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004117 int i;
4118 u32 val;
4119
4120 for (i = 0; i < 200; i++) {
4121 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4122 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4123 break;
4124 msleep(1);
4125 }
4126 }
Joe Perches63c3a662011-04-26 08:12:10 +00004127 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004128 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4129 WOL_DRV_STATE_SHUTDOWN |
4130 WOL_DRV_WOL |
4131 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004132
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004133 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 u32 mac_mode;
4135
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004136 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004137 if (do_low_power &&
4138 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4139 tg3_phy_auxctl_write(tp,
4140 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4141 MII_TG3_AUXCTL_PCTL_WOL_EN |
4142 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4143 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004144 udelay(40);
4145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004147 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004148 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004149 else if (tp->phy_flags &
4150 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4151 if (tp->link_config.active_speed == SPEED_1000)
4152 mac_mode = MAC_MODE_PORT_MODE_GMII;
4153 else
4154 mac_mode = MAC_MODE_PORT_MODE_MII;
4155 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004156 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004158 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004159 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004160 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004161 SPEED_100 : SPEED_10;
4162 if (tg3_5700_link_polarity(tp, speed))
4163 mac_mode |= MAC_MODE_LINK_POLARITY;
4164 else
4165 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 } else {
4168 mac_mode = MAC_MODE_PORT_MODE_TBI;
4169 }
4170
Joe Perches63c3a662011-04-26 08:12:10 +00004171 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 tw32(MAC_LED_CTRL, tp->led_ctrl);
4173
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004174 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004175 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4176 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004177 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Joe Perches63c3a662011-04-26 08:12:10 +00004179 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004180 mac_mode |= MAC_MODE_APE_TX_EN |
4181 MAC_MODE_APE_RX_EN |
4182 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 tw32_f(MAC_MODE, mac_mode);
4185 udelay(100);
4186
4187 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4188 udelay(10);
4189 }
4190
Joe Perches63c3a662011-04-26 08:12:10 +00004191 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004192 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4193 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 u32 base_val;
4195
4196 base_val = tp->pci_clock_ctrl;
4197 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4198 CLOCK_CTRL_TXCLK_DISABLE);
4199
Michael Chanb401e9e2005-12-19 16:27:04 -08004200 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4201 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004202 } else if (tg3_flag(tp, 5780_CLASS) ||
4203 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004204 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004205 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004206 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 u32 newbits1, newbits2;
4208
Joe Perches41535772013-02-16 11:20:04 +00004209 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4210 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4212 CLOCK_CTRL_TXCLK_DISABLE |
4213 CLOCK_CTRL_ALTCLK);
4214 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004215 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 newbits1 = CLOCK_CTRL_625_CORE;
4217 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4218 } else {
4219 newbits1 = CLOCK_CTRL_ALTCLK;
4220 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4221 }
4222
Michael Chanb401e9e2005-12-19 16:27:04 -08004223 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4224 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Michael Chanb401e9e2005-12-19 16:27:04 -08004226 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4227 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Joe Perches63c3a662011-04-26 08:12:10 +00004229 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 u32 newbits3;
4231
Joe Perches41535772013-02-16 11:20:04 +00004232 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4233 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4235 CLOCK_CTRL_TXCLK_DISABLE |
4236 CLOCK_CTRL_44MHZ_CORE);
4237 } else {
4238 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4239 }
4240
Michael Chanb401e9e2005-12-19 16:27:04 -08004241 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4242 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 }
4244 }
4245
Joe Perches63c3a662011-04-26 08:12:10 +00004246 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004247 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004248
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004249 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004252 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004253 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4254 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 u32 val = tr32(0x7d00);
4256
4257 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4258 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004259 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004260 int err;
4261
4262 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004264 if (!err)
4265 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 }
4268
Michael Chanbbadf502006-04-06 21:46:34 -07004269 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4270
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004271 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return 0;
4274}
4275
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004276static void tg3_power_down(struct tg3 *tp)
4277{
Joe Perches63c3a662011-04-26 08:12:10 +00004278 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004279 pci_set_power_state(tp->pdev, PCI_D3hot);
4280}
4281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4283{
4284 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4285 case MII_TG3_AUX_STAT_10HALF:
4286 *speed = SPEED_10;
4287 *duplex = DUPLEX_HALF;
4288 break;
4289
4290 case MII_TG3_AUX_STAT_10FULL:
4291 *speed = SPEED_10;
4292 *duplex = DUPLEX_FULL;
4293 break;
4294
4295 case MII_TG3_AUX_STAT_100HALF:
4296 *speed = SPEED_100;
4297 *duplex = DUPLEX_HALF;
4298 break;
4299
4300 case MII_TG3_AUX_STAT_100FULL:
4301 *speed = SPEED_100;
4302 *duplex = DUPLEX_FULL;
4303 break;
4304
4305 case MII_TG3_AUX_STAT_1000HALF:
4306 *speed = SPEED_1000;
4307 *duplex = DUPLEX_HALF;
4308 break;
4309
4310 case MII_TG3_AUX_STAT_1000FULL:
4311 *speed = SPEED_1000;
4312 *duplex = DUPLEX_FULL;
4313 break;
4314
4315 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004316 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004317 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4318 SPEED_10;
4319 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4320 DUPLEX_HALF;
4321 break;
4322 }
Matt Carlsone7405222012-02-13 15:20:16 +00004323 *speed = SPEED_UNKNOWN;
4324 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328
Matt Carlson42b64a42011-05-19 12:12:49 +00004329static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Matt Carlson42b64a42011-05-19 12:12:49 +00004331 int err = 0;
4332 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Matt Carlson42b64a42011-05-19 12:12:49 +00004334 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004335 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004336 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Matt Carlson42b64a42011-05-19 12:12:49 +00004338 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4339 if (err)
4340 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Matt Carlson4f272092011-12-14 11:09:57 +00004342 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4343 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004344
Joe Perches41535772013-02-16 11:20:04 +00004345 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4346 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004347 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004348
Matt Carlson4f272092011-12-14 11:09:57 +00004349 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4350 if (err)
4351 goto done;
4352 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004353
Matt Carlson42b64a42011-05-19 12:12:49 +00004354 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4355 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Matt Carlson42b64a42011-05-19 12:12:49 +00004357 tw32(TG3_CPMU_EEE_MODE,
4358 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004359
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004360 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004361 if (!err) {
4362 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004363
Matt Carlsona6b68da2010-12-06 08:28:52 +00004364 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004365 /* Advertise 100-BaseTX EEE ability */
4366 if (advertise & ADVERTISED_100baseT_Full)
4367 val |= MDIO_AN_EEE_ADV_100TX;
4368 /* Advertise 1000-BaseT EEE ability */
4369 if (advertise & ADVERTISED_1000baseT_Full)
4370 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004371
4372 if (!tp->eee.eee_enabled) {
4373 val = 0;
4374 tp->eee.advertised = 0;
4375 } else {
4376 tp->eee.advertised = advertise &
4377 (ADVERTISED_100baseT_Full |
4378 ADVERTISED_1000baseT_Full);
4379 }
4380
Matt Carlson42b64a42011-05-19 12:12:49 +00004381 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004382 if (err)
4383 val = 0;
4384
Joe Perches41535772013-02-16 11:20:04 +00004385 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004386 case ASIC_REV_5717:
4387 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004388 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004389 case ASIC_REV_5719:
4390 /* If we advertised any eee advertisements above... */
4391 if (val)
4392 val = MII_TG3_DSP_TAP26_ALNOKO |
4393 MII_TG3_DSP_TAP26_RMRXSTO |
4394 MII_TG3_DSP_TAP26_OPCSINPT;
4395 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4396 /* Fall through */
4397 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004398 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004399 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4400 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4401 MII_TG3_DSP_CH34TP2_HIBW01);
4402 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004403
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004404 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004405 if (!err)
4406 err = err2;
4407 }
4408
4409done:
4410 return err;
4411}
4412
4413static void tg3_phy_copper_begin(struct tg3 *tp)
4414{
Matt Carlsond13ba512012-02-22 12:35:19 +00004415 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4416 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4417 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004418
Nithin Sujir942d1af2013-04-09 08:48:07 +00004419 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4420 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004421 adv = ADVERTISED_10baseT_Half |
4422 ADVERTISED_10baseT_Full;
4423 if (tg3_flag(tp, WOL_SPEED_100MB))
4424 adv |= ADVERTISED_100baseT_Half |
4425 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004426 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4427 if (!(tp->phy_flags &
4428 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4429 adv |= ADVERTISED_1000baseT_Half;
4430 adv |= ADVERTISED_1000baseT_Full;
4431 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004432
Matt Carlsond13ba512012-02-22 12:35:19 +00004433 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004434 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004435 adv = tp->link_config.advertising;
4436 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4437 adv &= ~(ADVERTISED_1000baseT_Half |
4438 ADVERTISED_1000baseT_Full);
4439
4440 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004441 }
4442
Matt Carlsond13ba512012-02-22 12:35:19 +00004443 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004444
Nithin Sujir942d1af2013-04-09 08:48:07 +00004445 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4446 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4447 /* Normally during power down we want to autonegotiate
4448 * the lowest possible speed for WOL. However, to avoid
4449 * link flap, we leave it untouched.
4450 */
4451 return;
4452 }
4453
Matt Carlsond13ba512012-02-22 12:35:19 +00004454 tg3_writephy(tp, MII_BMCR,
4455 BMCR_ANENABLE | BMCR_ANRESTART);
4456 } else {
4457 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 u32 bmcr, orig_bmcr;
4459
4460 tp->link_config.active_speed = tp->link_config.speed;
4461 tp->link_config.active_duplex = tp->link_config.duplex;
4462
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004463 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4464 /* With autoneg disabled, 5715 only links up when the
4465 * advertisement register has the configured speed
4466 * enabled.
4467 */
4468 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4469 }
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 bmcr = 0;
4472 switch (tp->link_config.speed) {
4473 default:
4474 case SPEED_10:
4475 break;
4476
4477 case SPEED_100:
4478 bmcr |= BMCR_SPEED100;
4479 break;
4480
4481 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004482 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 if (tp->link_config.duplex == DUPLEX_FULL)
4487 bmcr |= BMCR_FULLDPLX;
4488
4489 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4490 (bmcr != orig_bmcr)) {
4491 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4492 for (i = 0; i < 1500; i++) {
4493 u32 tmp;
4494
4495 udelay(10);
4496 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4497 tg3_readphy(tp, MII_BMSR, &tmp))
4498 continue;
4499 if (!(tmp & BMSR_LSTATUS)) {
4500 udelay(40);
4501 break;
4502 }
4503 }
4504 tg3_writephy(tp, MII_BMCR, bmcr);
4505 udelay(40);
4506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 }
4508}
4509
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004510static int tg3_phy_pull_config(struct tg3 *tp)
4511{
4512 int err;
4513 u32 val;
4514
4515 err = tg3_readphy(tp, MII_BMCR, &val);
4516 if (err)
4517 goto done;
4518
4519 if (!(val & BMCR_ANENABLE)) {
4520 tp->link_config.autoneg = AUTONEG_DISABLE;
4521 tp->link_config.advertising = 0;
4522 tg3_flag_clear(tp, PAUSE_AUTONEG);
4523
4524 err = -EIO;
4525
4526 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4527 case 0:
4528 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4529 goto done;
4530
4531 tp->link_config.speed = SPEED_10;
4532 break;
4533 case BMCR_SPEED100:
4534 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4535 goto done;
4536
4537 tp->link_config.speed = SPEED_100;
4538 break;
4539 case BMCR_SPEED1000:
4540 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4541 tp->link_config.speed = SPEED_1000;
4542 break;
4543 }
4544 /* Fall through */
4545 default:
4546 goto done;
4547 }
4548
4549 if (val & BMCR_FULLDPLX)
4550 tp->link_config.duplex = DUPLEX_FULL;
4551 else
4552 tp->link_config.duplex = DUPLEX_HALF;
4553
4554 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4555
4556 err = 0;
4557 goto done;
4558 }
4559
4560 tp->link_config.autoneg = AUTONEG_ENABLE;
4561 tp->link_config.advertising = ADVERTISED_Autoneg;
4562 tg3_flag_set(tp, PAUSE_AUTONEG);
4563
4564 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4565 u32 adv;
4566
4567 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4568 if (err)
4569 goto done;
4570
4571 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4572 tp->link_config.advertising |= adv | ADVERTISED_TP;
4573
4574 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4575 } else {
4576 tp->link_config.advertising |= ADVERTISED_FIBRE;
4577 }
4578
4579 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4580 u32 adv;
4581
4582 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4583 err = tg3_readphy(tp, MII_CTRL1000, &val);
4584 if (err)
4585 goto done;
4586
4587 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4588 } else {
4589 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4590 if (err)
4591 goto done;
4592
4593 adv = tg3_decode_flowctrl_1000X(val);
4594 tp->link_config.flowctrl = adv;
4595
4596 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4597 adv = mii_adv_to_ethtool_adv_x(val);
4598 }
4599
4600 tp->link_config.advertising |= adv;
4601 }
4602
4603done:
4604 return err;
4605}
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607static int tg3_init_5401phy_dsp(struct tg3 *tp)
4608{
4609 int err;
4610
4611 /* Turn off tap power management. */
4612 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004613 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004615 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4616 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4617 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4618 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4619 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 udelay(40);
4622
4623 return err;
4624}
4625
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004626static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4627{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004628 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004629
4630 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4631 return true;
4632
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004633 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004634
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004635 if (tp->eee.eee_enabled) {
4636 if (tp->eee.advertised != eee.advertised ||
4637 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4638 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4639 return false;
4640 } else {
4641 /* EEE is disabled but we're advertising */
4642 if (eee.advertised)
4643 return false;
4644 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004645
4646 return true;
4647}
4648
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004651 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004652
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004653 advertising = tp->link_config.advertising;
4654 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004656 advmsk = ADVERTISE_ALL;
4657 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004658 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004659 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004662 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4663 return false;
4664
4665 if ((*lcladv & advmsk) != tgtadv)
4666 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004667
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004668 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 u32 tg3_ctrl;
4670
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004671 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004672
Matt Carlson221c5632011-06-13 13:39:01 +00004673 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004674 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Matt Carlson3198e072012-02-13 15:20:10 +00004676 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004677 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4678 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004679 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4680 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4681 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4682 } else {
4683 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4684 }
4685
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004686 if (tg3_ctrl != tgtadv)
4687 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004689
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004690 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004691}
4692
Matt Carlson859edb22011-12-08 14:40:16 +00004693static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4694{
4695 u32 lpeth = 0;
4696
4697 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4698 u32 val;
4699
4700 if (tg3_readphy(tp, MII_STAT1000, &val))
4701 return false;
4702
4703 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4704 }
4705
4706 if (tg3_readphy(tp, MII_LPA, rmtadv))
4707 return false;
4708
4709 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4710 tp->link_config.rmt_adv = lpeth;
4711
4712 return true;
4713}
4714
Joe Perches953c96e2013-04-09 10:18:14 +00004715static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004716{
4717 if (curr_link_up != tp->link_up) {
4718 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004719 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004720 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004721 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004722 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4723 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4724 }
4725
4726 tg3_link_report(tp);
4727 return true;
4728 }
4729
4730 return false;
4731}
4732
Michael Chan3310e242013-04-09 08:48:05 +00004733static void tg3_clear_mac_status(struct tg3 *tp)
4734{
4735 tw32(MAC_EVENT, 0);
4736
4737 tw32_f(MAC_STATUS,
4738 MAC_STATUS_SYNC_CHANGED |
4739 MAC_STATUS_CFG_CHANGED |
4740 MAC_STATUS_MI_COMPLETION |
4741 MAC_STATUS_LNKSTATE_CHANGED);
4742 udelay(40);
4743}
4744
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004745static void tg3_setup_eee(struct tg3 *tp)
4746{
4747 u32 val;
4748
4749 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4750 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4751 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4752 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4753
4754 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4755
4756 tw32_f(TG3_CPMU_EEE_CTRL,
4757 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4758
4759 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4760 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4761 TG3_CPMU_EEEMD_LPI_IN_RX |
4762 TG3_CPMU_EEEMD_EEE_ENABLE;
4763
4764 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4765 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4766
4767 if (tg3_flag(tp, ENABLE_APE))
4768 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4769
4770 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4771
4772 tw32_f(TG3_CPMU_EEE_DBTMR1,
4773 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4774 (tp->eee.tx_lpi_timer & 0xffff));
4775
4776 tw32_f(TG3_CPMU_EEE_DBTMR2,
4777 TG3_CPMU_DBTMR2_APE_TX_2047US |
4778 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4779}
4780
Joe Perches953c96e2013-04-09 10:18:14 +00004781static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Joe Perches953c96e2013-04-09 10:18:14 +00004783 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004784 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004785 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 u16 current_speed;
4787 u8 current_duplex;
4788 int i, err;
4789
Michael Chan3310e242013-04-09 08:48:05 +00004790 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
Matt Carlson8ef21422008-05-02 16:47:53 -07004792 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4793 tw32_f(MAC_MI_MODE,
4794 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4795 udelay(80);
4796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004798 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 /* Some third-party PHYs need to be reset on link going
4801 * down.
4802 */
Joe Perches41535772013-02-16 11:20:04 +00004803 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4804 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4805 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004806 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 tg3_readphy(tp, MII_BMSR, &bmsr);
4808 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4809 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004810 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
4812 if (force_reset)
4813 tg3_phy_reset(tp);
4814
Matt Carlson79eb6902010-02-17 15:17:03 +00004815 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 tg3_readphy(tp, MII_BMSR, &bmsr);
4817 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004818 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 bmsr = 0;
4820
4821 if (!(bmsr & BMSR_LSTATUS)) {
4822 err = tg3_init_5401phy_dsp(tp);
4823 if (err)
4824 return err;
4825
4826 tg3_readphy(tp, MII_BMSR, &bmsr);
4827 for (i = 0; i < 1000; i++) {
4828 udelay(10);
4829 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4830 (bmsr & BMSR_LSTATUS)) {
4831 udelay(40);
4832 break;
4833 }
4834 }
4835
Matt Carlson79eb6902010-02-17 15:17:03 +00004836 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4837 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 !(bmsr & BMSR_LSTATUS) &&
4839 tp->link_config.active_speed == SPEED_1000) {
4840 err = tg3_phy_reset(tp);
4841 if (!err)
4842 err = tg3_init_5401phy_dsp(tp);
4843 if (err)
4844 return err;
4845 }
4846 }
Joe Perches41535772013-02-16 11:20:04 +00004847 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4848 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 /* 5701 {A0,B0} CRC bug workaround */
4850 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004851 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4852 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4853 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855
4856 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004857 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4858 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004860 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004862 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4864
Joe Perches41535772013-02-16 11:20:04 +00004865 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4866 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4868 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4869 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4870 else
4871 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4872 }
4873
Joe Perches953c96e2013-04-09 10:18:14 +00004874 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004875 current_speed = SPEED_UNKNOWN;
4876 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004877 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004878 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004880 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004881 err = tg3_phy_auxctl_read(tp,
4882 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4883 &val);
4884 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004885 tg3_phy_auxctl_write(tp,
4886 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4887 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 goto relink;
4889 }
4890 }
4891
4892 bmsr = 0;
4893 for (i = 0; i < 100; i++) {
4894 tg3_readphy(tp, MII_BMSR, &bmsr);
4895 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4896 (bmsr & BMSR_LSTATUS))
4897 break;
4898 udelay(40);
4899 }
4900
4901 if (bmsr & BMSR_LSTATUS) {
4902 u32 aux_stat, bmcr;
4903
4904 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4905 for (i = 0; i < 2000; i++) {
4906 udelay(10);
4907 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4908 aux_stat)
4909 break;
4910 }
4911
4912 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4913 &current_speed,
4914 &current_duplex);
4915
4916 bmcr = 0;
4917 for (i = 0; i < 200; i++) {
4918 tg3_readphy(tp, MII_BMCR, &bmcr);
4919 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4920 continue;
4921 if (bmcr && bmcr != 0x7fff)
4922 break;
4923 udelay(10);
4924 }
4925
Matt Carlsonef167e22007-12-20 20:10:01 -08004926 lcl_adv = 0;
4927 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Matt Carlsonef167e22007-12-20 20:10:01 -08004929 tp->link_config.active_speed = current_speed;
4930 tp->link_config.active_duplex = current_duplex;
4931
4932 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004933 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4934
Matt Carlsonef167e22007-12-20 20:10:01 -08004935 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004936 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004937 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004938 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004939 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004940
4941 /* EEE settings changes take effect only after a phy
4942 * reset. If we have skipped a reset due to Link Flap
4943 * Avoidance being enabled, do it now.
4944 */
4945 if (!eee_config_ok &&
4946 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004947 !force_reset) {
4948 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004949 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 } else {
4952 if (!(bmcr & BMCR_ANENABLE) &&
4953 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004954 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004955 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957 }
4958
Joe Perches953c96e2013-04-09 10:18:14 +00004959 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004960 tp->link_config.active_duplex == DUPLEX_FULL) {
4961 u32 reg, bit;
4962
4963 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4964 reg = MII_TG3_FET_GEN_STAT;
4965 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4966 } else {
4967 reg = MII_TG3_EXT_STAT;
4968 bit = MII_TG3_EXT_STAT_MDIX;
4969 }
4970
4971 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4972 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4973
Matt Carlsonef167e22007-12-20 20:10:01 -08004974 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004979 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 tg3_phy_copper_begin(tp);
4981
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004982 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004983 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004984 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4985 current_speed = SPEED_1000;
4986 current_duplex = DUPLEX_FULL;
4987 tp->link_config.active_speed = current_speed;
4988 tp->link_config.active_duplex = current_duplex;
4989 }
4990
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004991 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004992 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4993 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004994 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 }
4996
4997 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004998 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (tp->link_config.active_speed == SPEED_100 ||
5000 tp->link_config.active_speed == SPEED_10)
5001 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5002 else
5003 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005004 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00005005 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5006 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5008
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00005009 /* In order for the 5750 core in BCM4785 chip to work properly
5010 * in RGMII mode, the Led Control Register must be set up.
5011 */
5012 if (tg3_flag(tp, RGMII_MODE)) {
5013 u32 led_ctrl = tr32(MAC_LED_CTRL);
5014 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5015
5016 if (tp->link_config.active_speed == SPEED_10)
5017 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5018 else if (tp->link_config.active_speed == SPEED_100)
5019 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5020 LED_CTRL_100MBPS_ON);
5021 else if (tp->link_config.active_speed == SPEED_1000)
5022 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5023 LED_CTRL_1000MBPS_ON);
5024
5025 tw32(MAC_LED_CTRL, led_ctrl);
5026 udelay(40);
5027 }
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5030 if (tp->link_config.active_duplex == DUPLEX_HALF)
5031 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5032
Joe Perches41535772013-02-16 11:20:04 +00005033 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005034 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005035 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005037 else
5038 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 }
5040
5041 /* ??? Without this setting Netgear GA302T PHY does not
5042 * ??? send/receive packets...
5043 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005044 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005045 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5047 tw32_f(MAC_MI_MODE, tp->mi_mode);
5048 udelay(80);
5049 }
5050
5051 tw32_f(MAC_MODE, tp->mac_mode);
5052 udelay(40);
5053
Matt Carlson52b02d02010-10-14 10:37:41 +00005054 tg3_phy_eee_adjust(tp, current_link_up);
5055
Joe Perches63c3a662011-04-26 08:12:10 +00005056 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 /* Polled via timer. */
5058 tw32_f(MAC_EVENT, 0);
5059 } else {
5060 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5061 }
5062 udelay(40);
5063
Joe Perches41535772013-02-16 11:20:04 +00005064 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005065 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005067 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 udelay(120);
5069 tw32_f(MAC_STATUS,
5070 (MAC_STATUS_SYNC_CHANGED |
5071 MAC_STATUS_CFG_CHANGED));
5072 udelay(40);
5073 tg3_write_mem(tp,
5074 NIC_SRAM_FIRMWARE_MBOX,
5075 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5076 }
5077
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005078 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005079 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005080 if (tp->link_config.active_speed == SPEED_100 ||
5081 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005082 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5083 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005084 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005085 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5086 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005087 }
5088
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005089 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 return 0;
5092}
5093
5094struct tg3_fiber_aneginfo {
5095 int state;
5096#define ANEG_STATE_UNKNOWN 0
5097#define ANEG_STATE_AN_ENABLE 1
5098#define ANEG_STATE_RESTART_INIT 2
5099#define ANEG_STATE_RESTART 3
5100#define ANEG_STATE_DISABLE_LINK_OK 4
5101#define ANEG_STATE_ABILITY_DETECT_INIT 5
5102#define ANEG_STATE_ABILITY_DETECT 6
5103#define ANEG_STATE_ACK_DETECT_INIT 7
5104#define ANEG_STATE_ACK_DETECT 8
5105#define ANEG_STATE_COMPLETE_ACK_INIT 9
5106#define ANEG_STATE_COMPLETE_ACK 10
5107#define ANEG_STATE_IDLE_DETECT_INIT 11
5108#define ANEG_STATE_IDLE_DETECT 12
5109#define ANEG_STATE_LINK_OK 13
5110#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5111#define ANEG_STATE_NEXT_PAGE_WAIT 15
5112
5113 u32 flags;
5114#define MR_AN_ENABLE 0x00000001
5115#define MR_RESTART_AN 0x00000002
5116#define MR_AN_COMPLETE 0x00000004
5117#define MR_PAGE_RX 0x00000008
5118#define MR_NP_LOADED 0x00000010
5119#define MR_TOGGLE_TX 0x00000020
5120#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5121#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5122#define MR_LP_ADV_SYM_PAUSE 0x00000100
5123#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5124#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5125#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5126#define MR_LP_ADV_NEXT_PAGE 0x00001000
5127#define MR_TOGGLE_RX 0x00002000
5128#define MR_NP_RX 0x00004000
5129
5130#define MR_LINK_OK 0x80000000
5131
5132 unsigned long link_time, cur_time;
5133
5134 u32 ability_match_cfg;
5135 int ability_match_count;
5136
5137 char ability_match, idle_match, ack_match;
5138
5139 u32 txconfig, rxconfig;
5140#define ANEG_CFG_NP 0x00000080
5141#define ANEG_CFG_ACK 0x00000040
5142#define ANEG_CFG_RF2 0x00000020
5143#define ANEG_CFG_RF1 0x00000010
5144#define ANEG_CFG_PS2 0x00000001
5145#define ANEG_CFG_PS1 0x00008000
5146#define ANEG_CFG_HD 0x00004000
5147#define ANEG_CFG_FD 0x00002000
5148#define ANEG_CFG_INVAL 0x00001f06
5149
5150};
5151#define ANEG_OK 0
5152#define ANEG_DONE 1
5153#define ANEG_TIMER_ENAB 2
5154#define ANEG_FAILED -1
5155
5156#define ANEG_STATE_SETTLE_TIME 10000
5157
5158static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5159 struct tg3_fiber_aneginfo *ap)
5160{
Matt Carlson5be73b42007-12-20 20:09:29 -08005161 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 unsigned long delta;
5163 u32 rx_cfg_reg;
5164 int ret;
5165
5166 if (ap->state == ANEG_STATE_UNKNOWN) {
5167 ap->rxconfig = 0;
5168 ap->link_time = 0;
5169 ap->cur_time = 0;
5170 ap->ability_match_cfg = 0;
5171 ap->ability_match_count = 0;
5172 ap->ability_match = 0;
5173 ap->idle_match = 0;
5174 ap->ack_match = 0;
5175 }
5176 ap->cur_time++;
5177
5178 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5179 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5180
5181 if (rx_cfg_reg != ap->ability_match_cfg) {
5182 ap->ability_match_cfg = rx_cfg_reg;
5183 ap->ability_match = 0;
5184 ap->ability_match_count = 0;
5185 } else {
5186 if (++ap->ability_match_count > 1) {
5187 ap->ability_match = 1;
5188 ap->ability_match_cfg = rx_cfg_reg;
5189 }
5190 }
5191 if (rx_cfg_reg & ANEG_CFG_ACK)
5192 ap->ack_match = 1;
5193 else
5194 ap->ack_match = 0;
5195
5196 ap->idle_match = 0;
5197 } else {
5198 ap->idle_match = 1;
5199 ap->ability_match_cfg = 0;
5200 ap->ability_match_count = 0;
5201 ap->ability_match = 0;
5202 ap->ack_match = 0;
5203
5204 rx_cfg_reg = 0;
5205 }
5206
5207 ap->rxconfig = rx_cfg_reg;
5208 ret = ANEG_OK;
5209
Matt Carlson33f401a2010-04-05 10:19:27 +00005210 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 case ANEG_STATE_UNKNOWN:
5212 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5213 ap->state = ANEG_STATE_AN_ENABLE;
5214
5215 /* fallthru */
5216 case ANEG_STATE_AN_ENABLE:
5217 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5218 if (ap->flags & MR_AN_ENABLE) {
5219 ap->link_time = 0;
5220 ap->cur_time = 0;
5221 ap->ability_match_cfg = 0;
5222 ap->ability_match_count = 0;
5223 ap->ability_match = 0;
5224 ap->idle_match = 0;
5225 ap->ack_match = 0;
5226
5227 ap->state = ANEG_STATE_RESTART_INIT;
5228 } else {
5229 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5230 }
5231 break;
5232
5233 case ANEG_STATE_RESTART_INIT:
5234 ap->link_time = ap->cur_time;
5235 ap->flags &= ~(MR_NP_LOADED);
5236 ap->txconfig = 0;
5237 tw32(MAC_TX_AUTO_NEG, 0);
5238 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5239 tw32_f(MAC_MODE, tp->mac_mode);
5240 udelay(40);
5241
5242 ret = ANEG_TIMER_ENAB;
5243 ap->state = ANEG_STATE_RESTART;
5244
5245 /* fallthru */
5246 case ANEG_STATE_RESTART:
5247 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005248 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005250 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 break;
5253
5254 case ANEG_STATE_DISABLE_LINK_OK:
5255 ret = ANEG_DONE;
5256 break;
5257
5258 case ANEG_STATE_ABILITY_DETECT_INIT:
5259 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005260 ap->txconfig = ANEG_CFG_FD;
5261 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5262 if (flowctrl & ADVERTISE_1000XPAUSE)
5263 ap->txconfig |= ANEG_CFG_PS1;
5264 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5265 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5267 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5268 tw32_f(MAC_MODE, tp->mac_mode);
5269 udelay(40);
5270
5271 ap->state = ANEG_STATE_ABILITY_DETECT;
5272 break;
5273
5274 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005275 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 break;
5278
5279 case ANEG_STATE_ACK_DETECT_INIT:
5280 ap->txconfig |= ANEG_CFG_ACK;
5281 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5282 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5283 tw32_f(MAC_MODE, tp->mac_mode);
5284 udelay(40);
5285
5286 ap->state = ANEG_STATE_ACK_DETECT;
5287
5288 /* fallthru */
5289 case ANEG_STATE_ACK_DETECT:
5290 if (ap->ack_match != 0) {
5291 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5292 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5293 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5294 } else {
5295 ap->state = ANEG_STATE_AN_ENABLE;
5296 }
5297 } else if (ap->ability_match != 0 &&
5298 ap->rxconfig == 0) {
5299 ap->state = ANEG_STATE_AN_ENABLE;
5300 }
5301 break;
5302
5303 case ANEG_STATE_COMPLETE_ACK_INIT:
5304 if (ap->rxconfig & ANEG_CFG_INVAL) {
5305 ret = ANEG_FAILED;
5306 break;
5307 }
5308 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5309 MR_LP_ADV_HALF_DUPLEX |
5310 MR_LP_ADV_SYM_PAUSE |
5311 MR_LP_ADV_ASYM_PAUSE |
5312 MR_LP_ADV_REMOTE_FAULT1 |
5313 MR_LP_ADV_REMOTE_FAULT2 |
5314 MR_LP_ADV_NEXT_PAGE |
5315 MR_TOGGLE_RX |
5316 MR_NP_RX);
5317 if (ap->rxconfig & ANEG_CFG_FD)
5318 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5319 if (ap->rxconfig & ANEG_CFG_HD)
5320 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5321 if (ap->rxconfig & ANEG_CFG_PS1)
5322 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5323 if (ap->rxconfig & ANEG_CFG_PS2)
5324 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5325 if (ap->rxconfig & ANEG_CFG_RF1)
5326 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5327 if (ap->rxconfig & ANEG_CFG_RF2)
5328 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5329 if (ap->rxconfig & ANEG_CFG_NP)
5330 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5331
5332 ap->link_time = ap->cur_time;
5333
5334 ap->flags ^= (MR_TOGGLE_TX);
5335 if (ap->rxconfig & 0x0008)
5336 ap->flags |= MR_TOGGLE_RX;
5337 if (ap->rxconfig & ANEG_CFG_NP)
5338 ap->flags |= MR_NP_RX;
5339 ap->flags |= MR_PAGE_RX;
5340
5341 ap->state = ANEG_STATE_COMPLETE_ACK;
5342 ret = ANEG_TIMER_ENAB;
5343 break;
5344
5345 case ANEG_STATE_COMPLETE_ACK:
5346 if (ap->ability_match != 0 &&
5347 ap->rxconfig == 0) {
5348 ap->state = ANEG_STATE_AN_ENABLE;
5349 break;
5350 }
5351 delta = ap->cur_time - ap->link_time;
5352 if (delta > ANEG_STATE_SETTLE_TIME) {
5353 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5354 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5355 } else {
5356 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5357 !(ap->flags & MR_NP_RX)) {
5358 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5359 } else {
5360 ret = ANEG_FAILED;
5361 }
5362 }
5363 }
5364 break;
5365
5366 case ANEG_STATE_IDLE_DETECT_INIT:
5367 ap->link_time = ap->cur_time;
5368 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5369 tw32_f(MAC_MODE, tp->mac_mode);
5370 udelay(40);
5371
5372 ap->state = ANEG_STATE_IDLE_DETECT;
5373 ret = ANEG_TIMER_ENAB;
5374 break;
5375
5376 case ANEG_STATE_IDLE_DETECT:
5377 if (ap->ability_match != 0 &&
5378 ap->rxconfig == 0) {
5379 ap->state = ANEG_STATE_AN_ENABLE;
5380 break;
5381 }
5382 delta = ap->cur_time - ap->link_time;
5383 if (delta > ANEG_STATE_SETTLE_TIME) {
5384 /* XXX another gem from the Broadcom driver :( */
5385 ap->state = ANEG_STATE_LINK_OK;
5386 }
5387 break;
5388
5389 case ANEG_STATE_LINK_OK:
5390 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5391 ret = ANEG_DONE;
5392 break;
5393
5394 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5395 /* ??? unimplemented */
5396 break;
5397
5398 case ANEG_STATE_NEXT_PAGE_WAIT:
5399 /* ??? unimplemented */
5400 break;
5401
5402 default:
5403 ret = ANEG_FAILED;
5404 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 return ret;
5408}
5409
Matt Carlson5be73b42007-12-20 20:09:29 -08005410static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
5412 int res = 0;
5413 struct tg3_fiber_aneginfo aninfo;
5414 int status = ANEG_FAILED;
5415 unsigned int tick;
5416 u32 tmp;
5417
5418 tw32_f(MAC_TX_AUTO_NEG, 0);
5419
5420 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5421 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5422 udelay(40);
5423
5424 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5425 udelay(40);
5426
5427 memset(&aninfo, 0, sizeof(aninfo));
5428 aninfo.flags |= MR_AN_ENABLE;
5429 aninfo.state = ANEG_STATE_UNKNOWN;
5430 aninfo.cur_time = 0;
5431 tick = 0;
5432 while (++tick < 195000) {
5433 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5434 if (status == ANEG_DONE || status == ANEG_FAILED)
5435 break;
5436
5437 udelay(1);
5438 }
5439
5440 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5441 tw32_f(MAC_MODE, tp->mac_mode);
5442 udelay(40);
5443
Matt Carlson5be73b42007-12-20 20:09:29 -08005444 *txflags = aninfo.txconfig;
5445 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (status == ANEG_DONE &&
5448 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5449 MR_LP_ADV_FULL_DUPLEX)))
5450 res = 1;
5451
5452 return res;
5453}
5454
5455static void tg3_init_bcm8002(struct tg3 *tp)
5456{
5457 u32 mac_status = tr32(MAC_STATUS);
5458 int i;
5459
5460 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005461 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 !(mac_status & MAC_STATUS_PCS_SYNCED))
5463 return;
5464
5465 /* Set PLL lock range. */
5466 tg3_writephy(tp, 0x16, 0x8007);
5467
5468 /* SW reset */
5469 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5470
5471 /* Wait for reset to complete. */
5472 /* XXX schedule_timeout() ... */
5473 for (i = 0; i < 500; i++)
5474 udelay(10);
5475
5476 /* Config mode; select PMA/Ch 1 regs. */
5477 tg3_writephy(tp, 0x10, 0x8411);
5478
5479 /* Enable auto-lock and comdet, select txclk for tx. */
5480 tg3_writephy(tp, 0x11, 0x0a10);
5481
5482 tg3_writephy(tp, 0x18, 0x00a0);
5483 tg3_writephy(tp, 0x16, 0x41ff);
5484
5485 /* Assert and deassert POR. */
5486 tg3_writephy(tp, 0x13, 0x0400);
5487 udelay(40);
5488 tg3_writephy(tp, 0x13, 0x0000);
5489
5490 tg3_writephy(tp, 0x11, 0x0a50);
5491 udelay(40);
5492 tg3_writephy(tp, 0x11, 0x0a10);
5493
5494 /* Wait for signal to stabilize */
5495 /* XXX schedule_timeout() ... */
5496 for (i = 0; i < 15000; i++)
5497 udelay(10);
5498
5499 /* Deselect the channel register so we can read the PHYID
5500 * later.
5501 */
5502 tg3_writephy(tp, 0x10, 0x8011);
5503}
5504
Joe Perches953c96e2013-04-09 10:18:14 +00005505static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005507 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005508 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 u32 sg_dig_ctrl, sg_dig_status;
5510 u32 serdes_cfg, expected_sg_dig_ctrl;
5511 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 serdes_cfg = 0;
5514 expected_sg_dig_ctrl = 0;
5515 workaround = 0;
5516 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005517 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Joe Perches41535772013-02-16 11:20:04 +00005519 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5520 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 workaround = 1;
5522 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5523 port_a = 0;
5524
5525 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5526 /* preserve bits 20-23 for voltage regulator */
5527 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5528 }
5529
5530 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5531
5532 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005533 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 if (workaround) {
5535 u32 val = serdes_cfg;
5536
5537 if (port_a)
5538 val |= 0xc010000;
5539 else
5540 val |= 0x4010000;
5541 tw32_f(MAC_SERDES_CFG, val);
5542 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005543
5544 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5547 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005548 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 }
5550 goto out;
5551 }
5552
5553 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005554 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Matt Carlson82cd3d12007-12-20 20:09:00 -08005556 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5557 if (flowctrl & ADVERTISE_1000XPAUSE)
5558 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5559 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5560 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
5562 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005563 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005564 tp->serdes_counter &&
5565 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5566 MAC_STATUS_RCVD_CFG)) ==
5567 MAC_STATUS_PCS_SYNCED)) {
5568 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005569 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005570 goto out;
5571 }
5572restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (workaround)
5574 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005575 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 udelay(5);
5577 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5578
Michael Chan3d3ebe72006-09-27 15:59:15 -07005579 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005580 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5582 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005583 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 mac_status = tr32(MAC_STATUS);
5585
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005586 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005588 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
Matt Carlson82cd3d12007-12-20 20:09:00 -08005590 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5591 local_adv |= ADVERTISE_1000XPAUSE;
5592 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5593 local_adv |= ADVERTISE_1000XPSE_ASYM;
5594
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005595 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005596 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005597 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005598 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Matt Carlson859edb22011-12-08 14:40:16 +00005600 tp->link_config.rmt_adv =
5601 mii_adv_to_ethtool_adv_x(remote_adv);
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005604 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005605 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005606 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005607 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005608 if (tp->serdes_counter)
5609 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 else {
5611 if (workaround) {
5612 u32 val = serdes_cfg;
5613
5614 if (port_a)
5615 val |= 0xc010000;
5616 else
5617 val |= 0x4010000;
5618
5619 tw32_f(MAC_SERDES_CFG, val);
5620 }
5621
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005622 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 udelay(40);
5624
5625 /* Link parallel detection - link is up */
5626 /* only if we have PCS_SYNC and not */
5627 /* receiving config code words */
5628 mac_status = tr32(MAC_STATUS);
5629 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5630 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5631 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005632 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005633 tp->phy_flags |=
5634 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005635 tp->serdes_counter =
5636 SERDES_PARALLEL_DET_TIMEOUT;
5637 } else
5638 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 }
5640 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005641 } else {
5642 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005643 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 }
5645
5646out:
5647 return current_link_up;
5648}
5649
Joe Perches953c96e2013-04-09 10:18:14 +00005650static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651{
Joe Perches953c96e2013-04-09 10:18:14 +00005652 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005654 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
5657 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005658 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005660
Matt Carlson5be73b42007-12-20 20:09:29 -08005661 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5662 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
Matt Carlson5be73b42007-12-20 20:09:29 -08005664 if (txflags & ANEG_CFG_PS1)
5665 local_adv |= ADVERTISE_1000XPAUSE;
5666 if (txflags & ANEG_CFG_PS2)
5667 local_adv |= ADVERTISE_1000XPSE_ASYM;
5668
5669 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5670 remote_adv |= LPA_1000XPAUSE;
5671 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5672 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Matt Carlson859edb22011-12-08 14:40:16 +00005674 tp->link_config.rmt_adv =
5675 mii_adv_to_ethtool_adv_x(remote_adv);
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 tg3_setup_flow_control(tp, local_adv, remote_adv);
5678
Joe Perches953c96e2013-04-09 10:18:14 +00005679 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681 for (i = 0; i < 30; i++) {
5682 udelay(20);
5683 tw32_f(MAC_STATUS,
5684 (MAC_STATUS_SYNC_CHANGED |
5685 MAC_STATUS_CFG_CHANGED));
5686 udelay(40);
5687 if ((tr32(MAC_STATUS) &
5688 (MAC_STATUS_SYNC_CHANGED |
5689 MAC_STATUS_CFG_CHANGED)) == 0)
5690 break;
5691 }
5692
5693 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005694 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5696 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005697 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005699 tg3_setup_flow_control(tp, 0, 0);
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005702 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703
5704 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5705 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005706
5707 tw32_f(MAC_MODE, tp->mac_mode);
5708 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
5710
5711out:
5712 return current_link_up;
5713}
5714
Joe Perches953c96e2013-04-09 10:18:14 +00005715static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716{
5717 u32 orig_pause_cfg;
5718 u16 orig_active_speed;
5719 u8 orig_active_duplex;
5720 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005721 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 int i;
5723
Matt Carlson8d018622007-12-20 20:05:44 -08005724 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 orig_active_speed = tp->link_config.active_speed;
5726 orig_active_duplex = tp->link_config.active_duplex;
5727
Joe Perches63c3a662011-04-26 08:12:10 +00005728 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005729 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005730 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 mac_status = tr32(MAC_STATUS);
5732 mac_status &= (MAC_STATUS_PCS_SYNCED |
5733 MAC_STATUS_SIGNAL_DET |
5734 MAC_STATUS_CFG_CHANGED |
5735 MAC_STATUS_RCVD_CFG);
5736 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5737 MAC_STATUS_SIGNAL_DET)) {
5738 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5739 MAC_STATUS_CFG_CHANGED));
5740 return 0;
5741 }
5742 }
5743
5744 tw32_f(MAC_TX_AUTO_NEG, 0);
5745
5746 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5747 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5748 tw32_f(MAC_MODE, tp->mac_mode);
5749 udelay(40);
5750
Matt Carlson79eb6902010-02-17 15:17:03 +00005751 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 tg3_init_bcm8002(tp);
5753
5754 /* Enable link change event even when serdes polling. */
5755 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5756 udelay(40);
5757
Joe Perches953c96e2013-04-09 10:18:14 +00005758 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005759 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 mac_status = tr32(MAC_STATUS);
5761
Joe Perches63c3a662011-04-26 08:12:10 +00005762 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5764 else
5765 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5766
Matt Carlson898a56f2009-08-28 14:02:40 +00005767 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005769 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
5771 for (i = 0; i < 100; i++) {
5772 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5773 MAC_STATUS_CFG_CHANGED));
5774 udelay(5);
5775 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005776 MAC_STATUS_CFG_CHANGED |
5777 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 break;
5779 }
5780
5781 mac_status = tr32(MAC_STATUS);
5782 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005783 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005784 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5785 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 tw32_f(MAC_MODE, (tp->mac_mode |
5787 MAC_MODE_SEND_CONFIGS));
5788 udelay(1);
5789 tw32_f(MAC_MODE, tp->mac_mode);
5790 }
5791 }
5792
Joe Perches953c96e2013-04-09 10:18:14 +00005793 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 tp->link_config.active_speed = SPEED_1000;
5795 tp->link_config.active_duplex = DUPLEX_FULL;
5796 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5797 LED_CTRL_LNKLED_OVERRIDE |
5798 LED_CTRL_1000MBPS_ON));
5799 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005800 tp->link_config.active_speed = SPEED_UNKNOWN;
5801 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5803 LED_CTRL_LNKLED_OVERRIDE |
5804 LED_CTRL_TRAFFIC_OVERRIDE));
5805 }
5806
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005807 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005808 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 if (orig_pause_cfg != now_pause_cfg ||
5810 orig_active_speed != tp->link_config.active_speed ||
5811 orig_active_duplex != tp->link_config.active_duplex)
5812 tg3_link_report(tp);
5813 }
5814
5815 return 0;
5816}
5817
Joe Perches953c96e2013-04-09 10:18:14 +00005818static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005819{
Joe Perches953c96e2013-04-09 10:18:14 +00005820 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005821 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005822 u16 current_speed = SPEED_UNKNOWN;
5823 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005824 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005825 u32 local_adv, remote_adv, sgsr;
5826
5827 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5828 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5829 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5830 (sgsr & SERDES_TG3_SGMII_MODE)) {
5831
5832 if (force_reset)
5833 tg3_phy_reset(tp);
5834
5835 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5836
5837 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5838 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5839 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005840 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005841 if (sgsr & SERDES_TG3_SPEED_1000) {
5842 current_speed = SPEED_1000;
5843 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5844 } else if (sgsr & SERDES_TG3_SPEED_100) {
5845 current_speed = SPEED_100;
5846 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5847 } else {
5848 current_speed = SPEED_10;
5849 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5850 }
5851
5852 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5853 current_duplex = DUPLEX_FULL;
5854 else
5855 current_duplex = DUPLEX_HALF;
5856 }
5857
5858 tw32_f(MAC_MODE, tp->mac_mode);
5859 udelay(40);
5860
5861 tg3_clear_mac_status(tp);
5862
5863 goto fiber_setup_done;
5864 }
Michael Chan747e8f82005-07-25 12:33:22 -07005865
5866 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5867 tw32_f(MAC_MODE, tp->mac_mode);
5868 udelay(40);
5869
Michael Chan3310e242013-04-09 08:48:05 +00005870 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005871
5872 if (force_reset)
5873 tg3_phy_reset(tp);
5874
Matt Carlson859edb22011-12-08 14:40:16 +00005875 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005876
5877 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5878 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005879 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005880 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5881 bmsr |= BMSR_LSTATUS;
5882 else
5883 bmsr &= ~BMSR_LSTATUS;
5884 }
Michael Chan747e8f82005-07-25 12:33:22 -07005885
5886 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5887
5888 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005889 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005890 /* do nothing, just check for link up at the end */
5891 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005892 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005893
5894 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005895 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5896 ADVERTISE_1000XPAUSE |
5897 ADVERTISE_1000XPSE_ASYM |
5898 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005899
Matt Carlson28011cf2011-11-16 18:36:59 -05005900 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005901 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005902
Matt Carlson28011cf2011-11-16 18:36:59 -05005903 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5904 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005905 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5906 tg3_writephy(tp, MII_BMCR, bmcr);
5907
5908 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005909 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005910 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005911
5912 return err;
5913 }
5914 } else {
5915 u32 new_bmcr;
5916
5917 bmcr &= ~BMCR_SPEED1000;
5918 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5919
5920 if (tp->link_config.duplex == DUPLEX_FULL)
5921 new_bmcr |= BMCR_FULLDPLX;
5922
5923 if (new_bmcr != bmcr) {
5924 /* BMCR_SPEED1000 is a reserved bit that needs
5925 * to be set on write.
5926 */
5927 new_bmcr |= BMCR_SPEED1000;
5928
5929 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005930 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005931 u32 adv;
5932
5933 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5934 adv &= ~(ADVERTISE_1000XFULL |
5935 ADVERTISE_1000XHALF |
5936 ADVERTISE_SLCT);
5937 tg3_writephy(tp, MII_ADVERTISE, adv);
5938 tg3_writephy(tp, MII_BMCR, bmcr |
5939 BMCR_ANRESTART |
5940 BMCR_ANENABLE);
5941 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005942 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005943 }
5944 tg3_writephy(tp, MII_BMCR, new_bmcr);
5945 bmcr = new_bmcr;
5946 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5947 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005948 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005949 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5950 bmsr |= BMSR_LSTATUS;
5951 else
5952 bmsr &= ~BMSR_LSTATUS;
5953 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005954 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005955 }
5956 }
5957
5958 if (bmsr & BMSR_LSTATUS) {
5959 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005960 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005961 if (bmcr & BMCR_FULLDPLX)
5962 current_duplex = DUPLEX_FULL;
5963 else
5964 current_duplex = DUPLEX_HALF;
5965
Matt Carlsonef167e22007-12-20 20:10:01 -08005966 local_adv = 0;
5967 remote_adv = 0;
5968
Michael Chan747e8f82005-07-25 12:33:22 -07005969 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005970 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005971
5972 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5973 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5974 common = local_adv & remote_adv;
5975 if (common & (ADVERTISE_1000XHALF |
5976 ADVERTISE_1000XFULL)) {
5977 if (common & ADVERTISE_1000XFULL)
5978 current_duplex = DUPLEX_FULL;
5979 else
5980 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005981
5982 tp->link_config.rmt_adv =
5983 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005984 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005985 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005986 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005987 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005988 }
Michael Chan747e8f82005-07-25 12:33:22 -07005989 }
5990 }
5991
Michael Chan85730a62013-04-09 08:48:06 +00005992fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005993 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005994 tg3_setup_flow_control(tp, local_adv, remote_adv);
5995
Michael Chan747e8f82005-07-25 12:33:22 -07005996 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5997 if (tp->link_config.active_duplex == DUPLEX_HALF)
5998 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5999
6000 tw32_f(MAC_MODE, tp->mac_mode);
6001 udelay(40);
6002
6003 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
6004
6005 tp->link_config.active_speed = current_speed;
6006 tp->link_config.active_duplex = current_duplex;
6007
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006008 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07006009 return err;
6010}
6011
6012static void tg3_serdes_parallel_detect(struct tg3 *tp)
6013{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006014 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006015 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006016 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006017 return;
6018 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006019
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006020 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006021 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6022 u32 bmcr;
6023
6024 tg3_readphy(tp, MII_BMCR, &bmcr);
6025 if (bmcr & BMCR_ANENABLE) {
6026 u32 phy1, phy2;
6027
6028 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006029 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6030 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006031
6032 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006033 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6034 MII_TG3_DSP_EXP1_INT_STAT);
6035 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6036 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006037
6038 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6039 /* We have signal detect and not receiving
6040 * config code words, link is up by parallel
6041 * detection.
6042 */
6043
6044 bmcr &= ~BMCR_ANENABLE;
6045 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6046 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006047 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006048 }
6049 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006050 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006051 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006052 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006053 u32 phy2;
6054
6055 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006056 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6057 MII_TG3_DSP_EXP1_INT_STAT);
6058 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006059 if (phy2 & 0x20) {
6060 u32 bmcr;
6061
6062 /* Config code words received, turn on autoneg. */
6063 tg3_readphy(tp, MII_BMCR, &bmcr);
6064 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6065
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006066 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006067
6068 }
6069 }
6070}
6071
Joe Perches953c96e2013-04-09 10:18:14 +00006072static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006074 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 int err;
6076
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006077 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006079 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006080 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006081 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Joe Perches41535772013-02-16 11:20:04 +00006084 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006085 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006086
6087 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6088 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6089 scale = 65;
6090 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6091 scale = 6;
6092 else
6093 scale = 12;
6094
6095 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6096 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6097 tw32(GRC_MISC_CFG, val);
6098 }
6099
Matt Carlsonf2096f92011-04-05 14:22:48 +00006100 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6101 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006102 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6103 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006104 val |= tr32(MAC_TX_LENGTHS) &
6105 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6106 TX_LENGTHS_CNT_DWN_VAL_MSK);
6107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 if (tp->link_config.active_speed == SPEED_1000 &&
6109 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006110 tw32(MAC_TX_LENGTHS, val |
6111 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006113 tw32(MAC_TX_LENGTHS, val |
6114 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
Joe Perches63c3a662011-04-26 08:12:10 +00006116 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006117 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006119 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 } else {
6121 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6122 }
6123 }
6124
Joe Perches63c3a662011-04-26 08:12:10 +00006125 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006126 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006127 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006128 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6129 tp->pwrmgmt_thresh;
6130 else
6131 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6132 tw32(PCIE_PWR_MGMT_THRESH, val);
6133 }
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 return err;
6136}
6137
Matt Carlsonbe947302012-12-03 19:36:57 +00006138/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006139static u64 tg3_refclk_read(struct tg3 *tp)
6140{
6141 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6142 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6143}
6144
6145/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006146static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6147{
Nithin Sujir92e64572013-07-29 13:58:38 -07006148 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6149
6150 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006151 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6152 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006153 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006154}
6155
Matt Carlson7d41e492012-12-03 19:36:58 +00006156static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6157static inline void tg3_full_unlock(struct tg3 *tp);
6158static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6159{
6160 struct tg3 *tp = netdev_priv(dev);
6161
6162 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6163 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006164 SOF_TIMESTAMPING_SOFTWARE;
6165
6166 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006167 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006168 SOF_TIMESTAMPING_RX_HARDWARE |
6169 SOF_TIMESTAMPING_RAW_HARDWARE;
6170 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006171
6172 if (tp->ptp_clock)
6173 info->phc_index = ptp_clock_index(tp->ptp_clock);
6174 else
6175 info->phc_index = -1;
6176
6177 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6178
6179 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6180 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6181 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6182 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6183 return 0;
6184}
6185
6186static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6187{
6188 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6189 bool neg_adj = false;
6190 u32 correction = 0;
6191
6192 if (ppb < 0) {
6193 neg_adj = true;
6194 ppb = -ppb;
6195 }
6196
6197 /* Frequency adjustment is performed using hardware with a 24 bit
6198 * accumulator and a programmable correction value. On each clk, the
6199 * correction value gets added to the accumulator and when it
6200 * overflows, the time counter is incremented/decremented.
6201 *
6202 * So conversion from ppb to correction value is
6203 * ppb * (1 << 24) / 1000000000
6204 */
6205 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6206 TG3_EAV_REF_CLK_CORRECT_MASK;
6207
6208 tg3_full_lock(tp, 0);
6209
6210 if (correction)
6211 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6212 TG3_EAV_REF_CLK_CORRECT_EN |
6213 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6214 else
6215 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6216
6217 tg3_full_unlock(tp);
6218
6219 return 0;
6220}
6221
6222static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6223{
6224 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6225
6226 tg3_full_lock(tp, 0);
6227 tp->ptp_adjust += delta;
6228 tg3_full_unlock(tp);
6229
6230 return 0;
6231}
6232
Richard Cochranf578b412015-03-29 23:11:57 +02006233static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006234{
6235 u64 ns;
Matt Carlson7d41e492012-12-03 19:36:58 +00006236 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6237
6238 tg3_full_lock(tp, 0);
6239 ns = tg3_refclk_read(tp);
6240 ns += tp->ptp_adjust;
6241 tg3_full_unlock(tp);
6242
Richard Cochran7a20efb2015-03-31 23:08:08 +02006243 *ts = ns_to_timespec64(ns);
Matt Carlson7d41e492012-12-03 19:36:58 +00006244
6245 return 0;
6246}
6247
6248static int tg3_ptp_settime(struct ptp_clock_info *ptp,
Richard Cochranf578b412015-03-29 23:11:57 +02006249 const struct timespec64 *ts)
Matt Carlson7d41e492012-12-03 19:36:58 +00006250{
6251 u64 ns;
6252 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6253
Richard Cochranf578b412015-03-29 23:11:57 +02006254 ns = timespec64_to_ns(ts);
Matt Carlson7d41e492012-12-03 19:36:58 +00006255
6256 tg3_full_lock(tp, 0);
6257 tg3_refclk_write(tp, ns);
6258 tp->ptp_adjust = 0;
6259 tg3_full_unlock(tp);
6260
6261 return 0;
6262}
6263
6264static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6265 struct ptp_clock_request *rq, int on)
6266{
Nithin Sujir92e64572013-07-29 13:58:38 -07006267 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6268 u32 clock_ctl;
6269 int rval = 0;
6270
6271 switch (rq->type) {
6272 case PTP_CLK_REQ_PEROUT:
6273 if (rq->perout.index != 0)
6274 return -EINVAL;
6275
6276 tg3_full_lock(tp, 0);
6277 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6278 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6279
6280 if (on) {
6281 u64 nsec;
6282
6283 nsec = rq->perout.start.sec * 1000000000ULL +
6284 rq->perout.start.nsec;
6285
6286 if (rq->perout.period.sec || rq->perout.period.nsec) {
6287 netdev_warn(tp->dev,
6288 "Device supports only a one-shot timesync output, period must be 0\n");
6289 rval = -EINVAL;
6290 goto err_out;
6291 }
6292
6293 if (nsec & (1ULL << 63)) {
6294 netdev_warn(tp->dev,
6295 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6296 rval = -EINVAL;
6297 goto err_out;
6298 }
6299
6300 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6301 tw32(TG3_EAV_WATCHDOG0_MSB,
6302 TG3_EAV_WATCHDOG0_EN |
6303 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6304
6305 tw32(TG3_EAV_REF_CLCK_CTL,
6306 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6307 } else {
6308 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6309 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6310 }
6311
6312err_out:
6313 tg3_full_unlock(tp);
6314 return rval;
6315
6316 default:
6317 break;
6318 }
6319
Matt Carlson7d41e492012-12-03 19:36:58 +00006320 return -EOPNOTSUPP;
6321}
6322
6323static const struct ptp_clock_info tg3_ptp_caps = {
6324 .owner = THIS_MODULE,
6325 .name = "tg3 clock",
6326 .max_adj = 250000000,
6327 .n_alarm = 0,
6328 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006329 .n_per_out = 1,
Richard Cochran4986b4f02014-03-20 22:21:55 +01006330 .n_pins = 0,
Matt Carlson7d41e492012-12-03 19:36:58 +00006331 .pps = 0,
6332 .adjfreq = tg3_ptp_adjfreq,
6333 .adjtime = tg3_ptp_adjtime,
Richard Cochranf578b412015-03-29 23:11:57 +02006334 .gettime64 = tg3_ptp_gettime,
6335 .settime64 = tg3_ptp_settime,
Matt Carlson7d41e492012-12-03 19:36:58 +00006336 .enable = tg3_ptp_enable,
6337};
6338
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006339static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6340 struct skb_shared_hwtstamps *timestamp)
6341{
6342 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6343 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6344 tp->ptp_adjust);
6345}
6346
Matt Carlsonbe947302012-12-03 19:36:57 +00006347/* tp->lock must be held */
6348static void tg3_ptp_init(struct tg3 *tp)
6349{
6350 if (!tg3_flag(tp, PTP_CAPABLE))
6351 return;
6352
6353 /* Initialize the hardware clock to the system time. */
6354 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6355 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006356 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006357}
6358
6359/* tp->lock must be held */
6360static void tg3_ptp_resume(struct tg3 *tp)
6361{
6362 if (!tg3_flag(tp, PTP_CAPABLE))
6363 return;
6364
6365 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6366 tp->ptp_adjust = 0;
6367}
6368
6369static void tg3_ptp_fini(struct tg3 *tp)
6370{
6371 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6372 return;
6373
Matt Carlson7d41e492012-12-03 19:36:58 +00006374 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006375 tp->ptp_clock = NULL;
6376 tp->ptp_adjust = 0;
6377}
6378
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006379static inline int tg3_irq_sync(struct tg3 *tp)
6380{
6381 return tp->irq_sync;
6382}
6383
Matt Carlson97bd8e42011-04-13 11:05:04 +00006384static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6385{
6386 int i;
6387
6388 dst = (u32 *)((u8 *)dst + off);
6389 for (i = 0; i < len; i += sizeof(u32))
6390 *dst++ = tr32(off + i);
6391}
6392
6393static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6394{
6395 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6396 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6397 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6398 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6399 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6400 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6401 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6402 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6403 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6404 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6405 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6406 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6407 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6408 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6409 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6410 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6411 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6412 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6413 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6414
Joe Perches63c3a662011-04-26 08:12:10 +00006415 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006416 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6417
6418 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6419 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6420 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6421 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6422 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6423 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6424 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6425 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6426
Joe Perches63c3a662011-04-26 08:12:10 +00006427 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006428 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6429 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6430 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6431 }
6432
6433 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6434 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6435 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6436 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6437 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6438
Joe Perches63c3a662011-04-26 08:12:10 +00006439 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006440 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6441}
6442
6443static void tg3_dump_state(struct tg3 *tp)
6444{
6445 int i;
6446 u32 *regs;
6447
6448 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006449 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006450 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006451
Joe Perches63c3a662011-04-26 08:12:10 +00006452 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006453 /* Read up to but not including private PCI registers */
6454 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6455 regs[i / sizeof(u32)] = tr32(i);
6456 } else
6457 tg3_dump_legacy_regs(tp, regs);
6458
6459 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6460 if (!regs[i + 0] && !regs[i + 1] &&
6461 !regs[i + 2] && !regs[i + 3])
6462 continue;
6463
6464 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6465 i * 4,
6466 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6467 }
6468
6469 kfree(regs);
6470
6471 for (i = 0; i < tp->irq_cnt; i++) {
6472 struct tg3_napi *tnapi = &tp->napi[i];
6473
6474 /* SW status block */
6475 netdev_err(tp->dev,
6476 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6477 i,
6478 tnapi->hw_status->status,
6479 tnapi->hw_status->status_tag,
6480 tnapi->hw_status->rx_jumbo_consumer,
6481 tnapi->hw_status->rx_consumer,
6482 tnapi->hw_status->rx_mini_consumer,
6483 tnapi->hw_status->idx[0].rx_producer,
6484 tnapi->hw_status->idx[0].tx_consumer);
6485
6486 netdev_err(tp->dev,
6487 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6488 i,
6489 tnapi->last_tag, tnapi->last_irq_tag,
6490 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6491 tnapi->rx_rcb_ptr,
6492 tnapi->prodring.rx_std_prod_idx,
6493 tnapi->prodring.rx_std_cons_idx,
6494 tnapi->prodring.rx_jmb_prod_idx,
6495 tnapi->prodring.rx_jmb_cons_idx);
6496 }
6497}
6498
Michael Chandf3e6542006-05-26 17:48:07 -07006499/* This is called whenever we suspect that the system chipset is re-
6500 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6501 * is bogus tx completions. We try to recover by setting the
6502 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6503 * in the workqueue.
6504 */
6505static void tg3_tx_recover(struct tg3 *tp)
6506{
Joe Perches63c3a662011-04-26 08:12:10 +00006507 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006508 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6509
Matt Carlson5129c3a2010-04-05 10:19:23 +00006510 netdev_warn(tp->dev,
6511 "The system may be re-ordering memory-mapped I/O "
6512 "cycles to the network device, attempting to recover. "
6513 "Please report the problem to the driver maintainer "
6514 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006515
Joe Perches63c3a662011-04-26 08:12:10 +00006516 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006517}
6518
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006519static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006520{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006521 /* Tell compiler to fetch tx indices from memory. */
6522 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006523 return tnapi->tx_pending -
6524 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006525}
6526
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527/* Tigon3 never reports partial packet sends. So we do not
6528 * need special logic to handle SKBs that have not had all
6529 * of their frags sent yet, like SunGEM does.
6530 */
Matt Carlson17375d22009-08-28 14:02:18 +00006531static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532{
Matt Carlson17375d22009-08-28 14:02:18 +00006533 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006534 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006535 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006536 struct netdev_queue *txq;
6537 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006538 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006539
Joe Perches63c3a662011-04-26 08:12:10 +00006540 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006541 index--;
6542
6543 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
6545 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006546 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006548 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Michael Chandf3e6542006-05-26 17:48:07 -07006550 if (unlikely(skb == NULL)) {
6551 tg3_tx_recover(tp);
6552 return;
6553 }
6554
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006555 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6556 struct skb_shared_hwtstamps timestamp;
6557 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6558 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6559
6560 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6561
6562 skb_tstamp_tx(skb, &timestamp);
6563 }
6564
Alexander Duyckf4188d82009-12-02 16:48:38 +00006565 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006566 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006567 skb_headlen(skb),
6568 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569
6570 ri->skb = NULL;
6571
Matt Carlsone01ee142011-07-27 14:20:50 +00006572 while (ri->fragmented) {
6573 ri->fragmented = false;
6574 sw_idx = NEXT_TX(sw_idx);
6575 ri = &tnapi->tx_buffers[sw_idx];
6576 }
6577
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 sw_idx = NEXT_TX(sw_idx);
6579
6580 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006581 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006582 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6583 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006584
6585 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006586 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006587 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006588 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006589
6590 while (ri->fragmented) {
6591 ri->fragmented = false;
6592 sw_idx = NEXT_TX(sw_idx);
6593 ri = &tnapi->tx_buffers[sw_idx];
6594 }
6595
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 sw_idx = NEXT_TX(sw_idx);
6597 }
6598
Tom Herbert298376d2011-11-28 16:33:30 +00006599 pkts_compl++;
6600 bytes_compl += skb->len;
6601
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07006602 dev_consume_skb_any(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006603
6604 if (unlikely(tx_bug)) {
6605 tg3_tx_recover(tp);
6606 return;
6607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 }
6609
Tom Herbert5cb917b2012-03-05 19:53:50 +00006610 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006611
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006612 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
Michael Chan1b2a7202006-08-07 21:46:02 -07006614 /* Need to make the tx_cons update visible to tg3_start_xmit()
6615 * before checking for netif_queue_stopped(). Without the
6616 * memory barrier, there is a small possibility that tg3_start_xmit()
6617 * will miss it and cause the queue to be stopped forever.
6618 */
6619 smp_mb();
6620
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006621 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006622 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006623 __netif_tx_lock(txq, smp_processor_id());
6624 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006625 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006626 netif_tx_wake_queue(txq);
6627 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629}
6630
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006631static void tg3_frag_free(bool is_frag, void *data)
6632{
6633 if (is_frag)
Alexander Duycke51423d2015-05-06 21:12:31 -07006634 skb_free_frag(data);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006635 else
6636 kfree(data);
6637}
6638
Eric Dumazet9205fd92011-11-18 06:47:01 +00006639static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006640{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006641 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6642 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6643
Eric Dumazet9205fd92011-11-18 06:47:01 +00006644 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006645 return;
6646
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006647 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006648 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006649 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006650 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006651}
6652
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006653
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654/* Returns size of skb allocated or < 0 on error.
6655 *
6656 * We only need to fill in the address because the other members
6657 * of the RX descriptor are invariant, see tg3_init_rings.
6658 *
6659 * Note the purposeful assymetry of cpu vs. chip accesses. For
6660 * posting buffers we only dirty the first cache line of the RX
6661 * descriptor (containing the address). Whereas for the RX status
6662 * buffers the cpu only reads the last cacheline of the RX descriptor
6663 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6664 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006665static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006666 u32 opaque_key, u32 dest_idx_unmasked,
6667 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
6669 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006670 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006671 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006673 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 switch (opaque_key) {
6676 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006677 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006678 desc = &tpr->rx_std[dest_idx];
6679 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006680 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 break;
6682
6683 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006684 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006685 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006686 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006687 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 break;
6689
6690 default:
6691 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
6694 /* Do not overwrite any of the map or rp information
6695 * until we are sure we can commit to a new buffer.
6696 *
6697 * Callers depend upon this behavior and assume that
6698 * we leave everything unchanged if we fail.
6699 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006700 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6701 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006702 if (skb_size <= PAGE_SIZE) {
6703 data = netdev_alloc_frag(skb_size);
6704 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006705 } else {
6706 data = kmalloc(skb_size, GFP_ATOMIC);
6707 *frag_size = 0;
6708 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006709 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 return -ENOMEM;
6711
Eric Dumazet9205fd92011-11-18 06:47:01 +00006712 mapping = pci_map_single(tp->pdev,
6713 data + TG3_RX_OFFSET(tp),
6714 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006716 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006717 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006718 return -EIO;
6719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Eric Dumazet9205fd92011-11-18 06:47:01 +00006721 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006722 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 desc->addr_hi = ((u64)mapping >> 32);
6725 desc->addr_lo = ((u64)mapping & 0xffffffff);
6726
Eric Dumazet9205fd92011-11-18 06:47:01 +00006727 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728}
6729
6730/* We only need to move over in the address because the other
6731 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006732 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 */
Matt Carlsona3896162009-11-13 13:03:44 +00006734static void tg3_recycle_rx(struct tg3_napi *tnapi,
6735 struct tg3_rx_prodring_set *dpr,
6736 u32 opaque_key, int src_idx,
6737 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738{
Matt Carlson17375d22009-08-28 14:02:18 +00006739 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6741 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006742 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006743 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
6745 switch (opaque_key) {
6746 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006747 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006748 dest_desc = &dpr->rx_std[dest_idx];
6749 dest_map = &dpr->rx_std_buffers[dest_idx];
6750 src_desc = &spr->rx_std[src_idx];
6751 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 break;
6753
6754 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006755 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006756 dest_desc = &dpr->rx_jmb[dest_idx].std;
6757 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6758 src_desc = &spr->rx_jmb[src_idx].std;
6759 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 break;
6761
6762 default:
6763 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
Eric Dumazet9205fd92011-11-18 06:47:01 +00006766 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006767 dma_unmap_addr_set(dest_map, mapping,
6768 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 dest_desc->addr_hi = src_desc->addr_hi;
6770 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006771
6772 /* Ensure that the update to the skb happens after the physical
6773 * addresses have been transferred to the new BD location.
6774 */
6775 smp_wmb();
6776
Eric Dumazet9205fd92011-11-18 06:47:01 +00006777 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780/* The RX ring scheme is composed of multiple rings which post fresh
6781 * buffers to the chip, and one special ring the chip uses to report
6782 * status back to the host.
6783 *
6784 * The special ring reports the status of received packets to the
6785 * host. The chip does not write into the original descriptor the
6786 * RX buffer was obtained from. The chip simply takes the original
6787 * descriptor as provided by the host, updates the status and length
6788 * field, then writes this into the next status ring entry.
6789 *
6790 * Each ring the host uses to post buffers to the chip is described
6791 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6792 * it is first placed into the on-chip ram. When the packet's length
6793 * is known, it walks down the TG3_BDINFO entries to select the ring.
6794 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6795 * which is within the range of the new packet's length is chosen.
6796 *
6797 * The "separate ring for rx status" scheme may sound queer, but it makes
6798 * sense from a cache coherency perspective. If only the host writes
6799 * to the buffer post rings, and only the chip writes to the rx status
6800 * rings, then cache lines never move beyond shared-modified state.
6801 * If both the host and chip were to write into the same ring, cache line
6802 * eviction could occur since both entities want it in an exclusive state.
6803 */
Matt Carlson17375d22009-08-28 14:02:18 +00006804static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805{
Matt Carlson17375d22009-08-28 14:02:18 +00006806 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006807 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006808 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006809 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006810 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006812 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006814 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 /*
6816 * We need to order the read of hw_idx and the read of
6817 * the opaque cookie.
6818 */
6819 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 work_mask = 0;
6821 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006822 std_prod_idx = tpr->rx_std_prod_idx;
6823 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006825 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006826 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 unsigned int len;
6828 struct sk_buff *skb;
6829 dma_addr_t dma_addr;
6830 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006831 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006832 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
6834 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6835 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6836 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006837 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006838 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006839 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006840 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006841 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006843 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006844 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006845 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006846 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006847 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
6850 work_mask |= opaque_key;
6851
Michael Chand7b95312014-02-28 15:05:10 -08006852 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006854 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 desc_idx, *post_ptr);
6856 drop_it_no_recycle:
6857 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006858 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 goto next_pkt;
6860 }
6861
Eric Dumazet9205fd92011-11-18 06:47:01 +00006862 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006863 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6864 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006866 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6867 RXD_FLAG_PTPSTAT_PTPV1 ||
6868 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6869 RXD_FLAG_PTPSTAT_PTPV2) {
6870 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6871 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6872 }
6873
Matt Carlsond2757fc2010-04-12 06:58:27 +00006874 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006876 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Eric Dumazet9205fd92011-11-18 06:47:01 +00006878 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006879 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 if (skb_size < 0)
6881 goto drop_it;
6882
Matt Carlson287be122009-08-28 13:58:46 +00006883 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 PCI_DMA_FROMDEVICE);
6885
Eric Dumazet9205fd92011-11-18 06:47:01 +00006886 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006887 * after the usage of the old DMA mapping.
6888 */
6889 smp_wmb();
6890
Eric Dumazet9205fd92011-11-18 06:47:01 +00006891 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006892
Ivan Vecera85aec732013-11-06 14:02:36 +01006893 skb = build_skb(data, frag_size);
6894 if (!skb) {
6895 tg3_frag_free(frag_size != 0, data);
6896 goto drop_it_no_recycle;
6897 }
6898 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006900 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 desc_idx, *post_ptr);
6902
Eric Dumazet9205fd92011-11-18 06:47:01 +00006903 skb = netdev_alloc_skb(tp->dev,
6904 len + TG3_RAW_IP_ALIGN);
6905 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 goto drop_it_no_recycle;
6907
Eric Dumazet9205fd92011-11-18 06:47:01 +00006908 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006910 memcpy(skb->data,
6911 data + TG3_RX_OFFSET(tp),
6912 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 }
6915
Eric Dumazet9205fd92011-11-18 06:47:01 +00006916 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006917 if (tstamp)
6918 tg3_hwclock_to_timestamp(tp, tstamp,
6919 skb_hwtstamps(skb));
6920
Michał Mirosławdc668912011-04-07 03:35:07 +00006921 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6923 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6924 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6925 skb->ip_summed = CHECKSUM_UNNECESSARY;
6926 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006927 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
6929 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006930
6931 if (len > (tp->dev->mtu + ETH_HLEN) &&
Vlad Yasevich7d3083e2014-09-30 19:39:36 -04006932 skb->protocol != htons(ETH_P_8021Q) &&
6933 skb->protocol != htons(ETH_P_8021AD)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07006934 dev_kfree_skb_any(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006935 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006936 }
6937
Matt Carlson9dc7a112010-04-12 06:58:28 +00006938 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006939 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006940 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006941 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006942
Matt Carlsonbf933c82011-01-25 15:58:49 +00006943 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 received++;
6946 budget--;
6947
6948next_pkt:
6949 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006950
6951 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006952 tpr->rx_std_prod_idx = std_prod_idx &
6953 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006954 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6955 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006956 work_mask &= ~RXD_OPAQUE_RING_STD;
6957 rx_std_posted = 0;
6958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006960 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006961 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006962
6963 /* Refresh hw_idx to see if there is new work */
6964 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006965 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006966 rmb();
6967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 }
6969
6970 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006971 tnapi->rx_rcb_ptr = sw_idx;
6972 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
6974 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006975 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006976 /* Sync BD data before updating mailbox */
6977 wmb();
6978
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006979 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006980 tpr->rx_std_prod_idx = std_prod_idx &
6981 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006982 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6983 tpr->rx_std_prod_idx);
6984 }
6985 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006986 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6987 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006988 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6989 tpr->rx_jmb_prod_idx);
6990 }
6991 mmiowb();
6992 } else if (work_mask) {
6993 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6994 * updated before the producer indices can be updated.
6995 */
6996 smp_wmb();
6997
Matt Carlson2c49a442010-09-30 10:34:35 +00006998 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6999 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007000
Michael Chan7ae52892012-03-21 15:38:33 +00007001 if (tnapi != &tp->napi[1]) {
7002 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007003 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00007004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
7007 return received;
7008}
7009
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007010static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007013 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007014 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7015
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 if (sblk->status & SD_STATUS_LINK_CHG) {
7017 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007018 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007019 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007020 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007021 tw32_f(MAC_STATUS,
7022 (MAC_STATUS_SYNC_CHANGED |
7023 MAC_STATUS_CFG_CHANGED |
7024 MAC_STATUS_MI_COMPLETION |
7025 MAC_STATUS_LNKSTATE_CHANGED));
7026 udelay(40);
7027 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007028 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007029 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 }
7031 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007032}
7033
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007034static int tg3_rx_prodring_xfer(struct tg3 *tp,
7035 struct tg3_rx_prodring_set *dpr,
7036 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007037{
7038 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007039 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007040
7041 while (1) {
7042 src_prod_idx = spr->rx_std_prod_idx;
7043
7044 /* Make sure updates to the rx_std_buffers[] entries and the
7045 * standard producer index are seen in the correct order.
7046 */
7047 smp_rmb();
7048
7049 if (spr->rx_std_cons_idx == src_prod_idx)
7050 break;
7051
7052 if (spr->rx_std_cons_idx < src_prod_idx)
7053 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7054 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007055 cpycnt = tp->rx_std_ring_mask + 1 -
7056 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007057
Matt Carlson2c49a442010-09-30 10:34:35 +00007058 cpycnt = min(cpycnt,
7059 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007060
7061 si = spr->rx_std_cons_idx;
7062 di = dpr->rx_std_prod_idx;
7063
Matt Carlsone92967b2010-02-12 14:47:06 +00007064 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007065 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007066 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007067 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007068 break;
7069 }
7070 }
7071
7072 if (!cpycnt)
7073 break;
7074
7075 /* Ensure that updates to the rx_std_buffers ring and the
7076 * shadowed hardware producer ring from tg3_recycle_skb() are
7077 * ordered correctly WRT the skb check above.
7078 */
7079 smp_rmb();
7080
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007081 memcpy(&dpr->rx_std_buffers[di],
7082 &spr->rx_std_buffers[si],
7083 cpycnt * sizeof(struct ring_info));
7084
7085 for (i = 0; i < cpycnt; i++, di++, si++) {
7086 struct tg3_rx_buffer_desc *sbd, *dbd;
7087 sbd = &spr->rx_std[si];
7088 dbd = &dpr->rx_std[di];
7089 dbd->addr_hi = sbd->addr_hi;
7090 dbd->addr_lo = sbd->addr_lo;
7091 }
7092
Matt Carlson2c49a442010-09-30 10:34:35 +00007093 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7094 tp->rx_std_ring_mask;
7095 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7096 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007097 }
7098
7099 while (1) {
7100 src_prod_idx = spr->rx_jmb_prod_idx;
7101
7102 /* Make sure updates to the rx_jmb_buffers[] entries and
7103 * the jumbo producer index are seen in the correct order.
7104 */
7105 smp_rmb();
7106
7107 if (spr->rx_jmb_cons_idx == src_prod_idx)
7108 break;
7109
7110 if (spr->rx_jmb_cons_idx < src_prod_idx)
7111 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7112 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007113 cpycnt = tp->rx_jmb_ring_mask + 1 -
7114 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007115
7116 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007117 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007118
7119 si = spr->rx_jmb_cons_idx;
7120 di = dpr->rx_jmb_prod_idx;
7121
Matt Carlsone92967b2010-02-12 14:47:06 +00007122 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007123 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007124 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007125 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007126 break;
7127 }
7128 }
7129
7130 if (!cpycnt)
7131 break;
7132
7133 /* Ensure that updates to the rx_jmb_buffers ring and the
7134 * shadowed hardware producer ring from tg3_recycle_skb() are
7135 * ordered correctly WRT the skb check above.
7136 */
7137 smp_rmb();
7138
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007139 memcpy(&dpr->rx_jmb_buffers[di],
7140 &spr->rx_jmb_buffers[si],
7141 cpycnt * sizeof(struct ring_info));
7142
7143 for (i = 0; i < cpycnt; i++, di++, si++) {
7144 struct tg3_rx_buffer_desc *sbd, *dbd;
7145 sbd = &spr->rx_jmb[si].std;
7146 dbd = &dpr->rx_jmb[di].std;
7147 dbd->addr_hi = sbd->addr_hi;
7148 dbd->addr_lo = sbd->addr_lo;
7149 }
7150
Matt Carlson2c49a442010-09-30 10:34:35 +00007151 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7152 tp->rx_jmb_ring_mask;
7153 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7154 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007155 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007156
7157 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007158}
7159
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007160static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7161{
7162 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
7164 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007165 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007166 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007167 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007168 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 }
7170
Matt Carlsonf891ea12012-04-24 13:37:01 +00007171 if (!tnapi->rx_rcb_prod_idx)
7172 return work_done;
7173
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 /* run RX thread, within the bounds set by NAPI.
7175 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007176 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007178 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007179 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Joe Perches63c3a662011-04-26 08:12:10 +00007181 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007182 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007183 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007184 u32 std_prod_idx = dpr->rx_std_prod_idx;
7185 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007186
Michael Chan7ae52892012-03-21 15:38:33 +00007187 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007188 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007189 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007190 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007191
7192 wmb();
7193
Matt Carlsone4af1af2010-02-12 14:47:05 +00007194 if (std_prod_idx != dpr->rx_std_prod_idx)
7195 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7196 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007197
Matt Carlsone4af1af2010-02-12 14:47:05 +00007198 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7199 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7200 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007201
7202 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007203
7204 if (err)
7205 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007206 }
7207
David S. Miller6f535762007-10-11 18:08:29 -07007208 return work_done;
7209}
David S. Millerf7383c222005-05-18 22:50:53 -07007210
Matt Carlsondb219972011-11-04 09:15:03 +00007211static inline void tg3_reset_task_schedule(struct tg3 *tp)
7212{
7213 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7214 schedule_work(&tp->reset_task);
7215}
7216
7217static inline void tg3_reset_task_cancel(struct tg3 *tp)
7218{
7219 cancel_work_sync(&tp->reset_task);
7220 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007221 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007222}
7223
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007224static int tg3_poll_msix(struct napi_struct *napi, int budget)
7225{
7226 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7227 struct tg3 *tp = tnapi->tp;
7228 int work_done = 0;
7229 struct tg3_hw_status *sblk = tnapi->hw_status;
7230
7231 while (1) {
7232 work_done = tg3_poll_work(tnapi, work_done, budget);
7233
Joe Perches63c3a662011-04-26 08:12:10 +00007234 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007235 goto tx_recovery;
7236
7237 if (unlikely(work_done >= budget))
7238 break;
7239
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007240 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007241 * to tell the hw how much work has been processed,
7242 * so we must read it before checking for more work.
7243 */
7244 tnapi->last_tag = sblk->status_tag;
7245 tnapi->last_irq_tag = tnapi->last_tag;
7246 rmb();
7247
7248 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007249 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7250 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007251
7252 /* This test here is not race free, but will reduce
7253 * the number of interrupts by looping again.
7254 */
7255 if (tnapi == &tp->napi[1] && tp->rx_refill)
7256 continue;
7257
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007258 napi_complete_done(napi, work_done);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007259 /* Reenable interrupts. */
7260 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007261
7262 /* This test here is synchronized by napi_schedule()
7263 * and napi_complete() to close the race condition.
7264 */
7265 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7266 tw32(HOSTCC_MODE, tp->coalesce_mode |
7267 HOSTCC_MODE_ENABLE |
7268 tnapi->coal_now);
7269 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007270 mmiowb();
7271 break;
7272 }
7273 }
7274
Prashant Sreedharan506b0a32018-02-19 12:27:04 +05307275 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007276 return work_done;
7277
7278tx_recovery:
7279 /* work_done is guaranteed to be less than budget. */
7280 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007281 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007282 return work_done;
7283}
7284
Matt Carlsone64de4e2011-04-13 11:05:05 +00007285static void tg3_process_error(struct tg3 *tp)
7286{
7287 u32 val;
7288 bool real_error = false;
7289
Joe Perches63c3a662011-04-26 08:12:10 +00007290 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007291 return;
7292
7293 /* Check Flow Attention register */
7294 val = tr32(HOSTCC_FLOW_ATTN);
7295 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7296 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7297 real_error = true;
7298 }
7299
7300 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7301 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7302 real_error = true;
7303 }
7304
7305 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7306 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7307 real_error = true;
7308 }
7309
7310 if (!real_error)
7311 return;
7312
7313 tg3_dump_state(tp);
7314
Joe Perches63c3a662011-04-26 08:12:10 +00007315 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007316 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007317}
7318
David S. Miller6f535762007-10-11 18:08:29 -07007319static int tg3_poll(struct napi_struct *napi, int budget)
7320{
Matt Carlson8ef04422009-08-28 14:01:37 +00007321 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7322 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007323 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007324 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007325
7326 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007327 if (sblk->status & SD_STATUS_ERROR)
7328 tg3_process_error(tp);
7329
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007330 tg3_poll_link(tp);
7331
Matt Carlson17375d22009-08-28 14:02:18 +00007332 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007333
Joe Perches63c3a662011-04-26 08:12:10 +00007334 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007335 goto tx_recovery;
7336
7337 if (unlikely(work_done >= budget))
7338 break;
7339
Joe Perches63c3a662011-04-26 08:12:10 +00007340 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007341 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007342 * to tell the hw how much work has been processed,
7343 * so we must read it before checking for more work.
7344 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007345 tnapi->last_tag = sblk->status_tag;
7346 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007347 rmb();
7348 } else
7349 sblk->status &= ~SD_STATUS_UPDATED;
7350
Matt Carlson17375d22009-08-28 14:02:18 +00007351 if (likely(!tg3_has_work(tnapi))) {
Eric Dumazet24d2e4a2015-03-05 10:41:34 -08007352 napi_complete_done(napi, work_done);
Matt Carlson17375d22009-08-28 14:02:18 +00007353 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007354 break;
7355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
7357
Prashant Sreedharan506b0a32018-02-19 12:27:04 +05307358 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL << 1);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007359 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007360
7361tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007362 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007363 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007364 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007365 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366}
7367
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007368static void tg3_napi_disable(struct tg3 *tp)
7369{
7370 int i;
7371
7372 for (i = tp->irq_cnt - 1; i >= 0; i--)
7373 napi_disable(&tp->napi[i].napi);
7374}
7375
7376static void tg3_napi_enable(struct tg3 *tp)
7377{
7378 int i;
7379
7380 for (i = 0; i < tp->irq_cnt; i++)
7381 napi_enable(&tp->napi[i].napi);
7382}
7383
7384static void tg3_napi_init(struct tg3 *tp)
7385{
7386 int i;
7387
7388 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7389 for (i = 1; i < tp->irq_cnt; i++)
7390 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7391}
7392
7393static void tg3_napi_fini(struct tg3 *tp)
7394{
7395 int i;
7396
7397 for (i = 0; i < tp->irq_cnt; i++)
7398 netif_napi_del(&tp->napi[i].napi);
7399}
7400
7401static inline void tg3_netif_stop(struct tg3 *tp)
7402{
Florian Westphal860e9532016-05-03 16:33:13 +02007403 netif_trans_update(tp->dev); /* prevent tx timeout */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007404 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007405 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007406 netif_tx_disable(tp->dev);
7407}
7408
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007409/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007410static inline void tg3_netif_start(struct tg3 *tp)
7411{
Matt Carlsonbe947302012-12-03 19:36:57 +00007412 tg3_ptp_resume(tp);
7413
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007414 /* NOTE: unconditional netif_tx_wake_all_queues is only
7415 * appropriate so long as all callers are assured to
7416 * have free tx slots (such as after tg3_init_hw)
7417 */
7418 netif_tx_wake_all_queues(tp->dev);
7419
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007420 if (tp->link_up)
7421 netif_carrier_on(tp->dev);
7422
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007423 tg3_napi_enable(tp);
7424 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7425 tg3_enable_ints(tp);
7426}
7427
David S. Millerf47c11e2005-06-24 20:18:35 -07007428static void tg3_irq_quiesce(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007429 __releases(tp->lock)
7430 __acquires(tp->lock)
David S. Millerf47c11e2005-06-24 20:18:35 -07007431{
Matt Carlson4f125f42009-09-01 12:55:02 +00007432 int i;
7433
David S. Millerf47c11e2005-06-24 20:18:35 -07007434 BUG_ON(tp->irq_sync);
7435
7436 tp->irq_sync = 1;
7437 smp_mb();
7438
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007439 spin_unlock_bh(&tp->lock);
7440
Matt Carlson4f125f42009-09-01 12:55:02 +00007441 for (i = 0; i < tp->irq_cnt; i++)
7442 synchronize_irq(tp->napi[i].irq_vec);
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08007443
7444 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007445}
7446
David S. Millerf47c11e2005-06-24 20:18:35 -07007447/* Fully shutdown all tg3 driver activity elsewhere in the system.
7448 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7449 * with as well. Most of the time, this is not necessary except when
7450 * shutting down the device.
7451 */
7452static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7453{
Michael Chan46966542007-07-11 19:47:19 -07007454 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007455 if (irq_sync)
7456 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007457}
7458
7459static inline void tg3_full_unlock(struct tg3 *tp)
7460{
David S. Millerf47c11e2005-06-24 20:18:35 -07007461 spin_unlock_bh(&tp->lock);
7462}
7463
Michael Chanfcfa0a32006-03-20 22:28:41 -08007464/* One-shot MSI handler - Chip automatically disables interrupt
7465 * after sending MSI so driver doesn't have to do it.
7466 */
David Howells7d12e782006-10-05 14:55:46 +01007467static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007468{
Matt Carlson09943a12009-08-28 14:01:57 +00007469 struct tg3_napi *tnapi = dev_id;
7470 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007471
Matt Carlson898a56f2009-08-28 14:02:40 +00007472 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007473 if (tnapi->rx_rcb)
7474 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007475
7476 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007477 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007478
7479 return IRQ_HANDLED;
7480}
7481
Michael Chan88b06bc22005-04-21 17:13:25 -07007482/* MSI ISR - No need to check for interrupt sharing and no need to
7483 * flush status block and interrupt mailbox. PCI ordering rules
7484 * guarantee that MSI will arrive after the status block.
7485 */
David Howells7d12e782006-10-05 14:55:46 +01007486static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007487{
Matt Carlson09943a12009-08-28 14:01:57 +00007488 struct tg3_napi *tnapi = dev_id;
7489 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007490
Matt Carlson898a56f2009-08-28 14:02:40 +00007491 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007492 if (tnapi->rx_rcb)
7493 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007494 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007495 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007496 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007497 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007498 * NIC to stop sending us irqs, engaging "in-intr-handler"
7499 * event coalescing.
7500 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007501 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007502 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007503 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007504
Michael Chan88b06bc22005-04-21 17:13:25 -07007505 return IRQ_RETVAL(1);
7506}
7507
David Howells7d12e782006-10-05 14:55:46 +01007508static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509{
Matt Carlson09943a12009-08-28 14:01:57 +00007510 struct tg3_napi *tnapi = dev_id;
7511 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007512 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 unsigned int handled = 1;
7514
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 /* In INTx mode, it is possible for the interrupt to arrive at
7516 * the CPU before the status block posted prior to the interrupt.
7517 * Reading the PCI State register will confirm whether the
7518 * interrupt is ours and will flush the status block.
7519 */
Michael Chand18edcb2007-03-24 20:57:11 -07007520 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007521 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007522 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7523 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007524 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007525 }
Michael Chand18edcb2007-03-24 20:57:11 -07007526 }
7527
7528 /*
7529 * Writing any value to intr-mbox-0 clears PCI INTA# and
7530 * chip-internal interrupt pending events.
7531 * Writing non-zero to intr-mbox-0 additional tells the
7532 * NIC to stop sending us irqs, engaging "in-intr-handler"
7533 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007534 *
7535 * Flush the mailbox to de-assert the IRQ immediately to prevent
7536 * spurious interrupts. The flush impacts performance but
7537 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007538 */
Michael Chanc04cb342007-05-07 00:26:15 -07007539 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007540 if (tg3_irq_sync(tp))
7541 goto out;
7542 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007543 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007544 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007545 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007546 } else {
7547 /* No work, shared interrupt perhaps? re-enable
7548 * interrupts, and flush that PCI write
7549 */
7550 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7551 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007552 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007553out:
David S. Millerfac9b832005-05-18 22:46:34 -07007554 return IRQ_RETVAL(handled);
7555}
7556
David Howells7d12e782006-10-05 14:55:46 +01007557static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007558{
Matt Carlson09943a12009-08-28 14:01:57 +00007559 struct tg3_napi *tnapi = dev_id;
7560 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007561 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007562 unsigned int handled = 1;
7563
David S. Millerfac9b832005-05-18 22:46:34 -07007564 /* In INTx mode, it is possible for the interrupt to arrive at
7565 * the CPU before the status block posted prior to the interrupt.
7566 * Reading the PCI State register will confirm whether the
7567 * interrupt is ours and will flush the status block.
7568 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007569 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007570 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007571 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7572 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 }
Michael Chand18edcb2007-03-24 20:57:11 -07007575 }
7576
7577 /*
7578 * writing any value to intr-mbox-0 clears PCI INTA# and
7579 * chip-internal interrupt pending events.
7580 * writing non-zero to intr-mbox-0 additional tells the
7581 * NIC to stop sending us irqs, engaging "in-intr-handler"
7582 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007583 *
7584 * Flush the mailbox to de-assert the IRQ immediately to prevent
7585 * spurious interrupts. The flush impacts performance but
7586 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007587 */
Michael Chanc04cb342007-05-07 00:26:15 -07007588 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007589
7590 /*
7591 * In a shared interrupt configuration, sometimes other devices'
7592 * interrupts will scream. We record the current status tag here
7593 * so that the above check can report that the screaming interrupts
7594 * are unhandled. Eventually they will be silenced.
7595 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007596 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007597
Michael Chand18edcb2007-03-24 20:57:11 -07007598 if (tg3_irq_sync(tp))
7599 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007600
Matt Carlson72334482009-08-28 14:03:01 +00007601 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007602
Matt Carlson09943a12009-08-28 14:01:57 +00007603 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007604
David S. Millerf47c11e2005-06-24 20:18:35 -07007605out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 return IRQ_RETVAL(handled);
7607}
7608
Michael Chan79381092005-04-21 17:13:59 -07007609/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007610static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007611{
Matt Carlson09943a12009-08-28 14:01:57 +00007612 struct tg3_napi *tnapi = dev_id;
7613 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007614 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007615
Michael Chanf9804dd2005-09-27 12:13:10 -07007616 if ((sblk->status & SD_STATUS_UPDATED) ||
7617 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007618 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007619 return IRQ_RETVAL(1);
7620 }
7621 return IRQ_RETVAL(0);
7622}
7623
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624#ifdef CONFIG_NET_POLL_CONTROLLER
7625static void tg3_poll_controller(struct net_device *dev)
7626{
Matt Carlson4f125f42009-09-01 12:55:02 +00007627 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007628 struct tg3 *tp = netdev_priv(dev);
7629
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007630 if (tg3_irq_sync(tp))
7631 return;
7632
Matt Carlson4f125f42009-09-01 12:55:02 +00007633 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007634 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635}
7636#endif
7637
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638static void tg3_tx_timeout(struct net_device *dev)
7639{
7640 struct tg3 *tp = netdev_priv(dev);
7641
Michael Chanb0408752007-02-13 12:18:30 -08007642 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007643 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007644 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646
Matt Carlsondb219972011-11-04 09:15:03 +00007647 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648}
7649
Michael Chanc58ec932005-09-17 00:46:27 -07007650/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7651static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7652{
7653 u32 base = (u32) mapping & 0xffffffff;
7654
Nithin Sujir37567912013-12-19 17:44:11 -08007655 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007656}
7657
Michael Chan0f0d1512013-05-13 11:04:16 +00007658/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7659 * of any 4GB boundaries: 4G, 8G, etc
7660 */
7661static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7662 u32 len, u32 mss)
7663{
7664 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7665 u32 base = (u32) mapping & 0xffffffff;
7666
7667 return ((base + len + (mss & 0x3fff)) < base);
7668 }
7669 return 0;
7670}
7671
Michael Chan72f2afb2006-03-06 19:28:35 -08007672/* Test for DMA addresses > 40-bit */
7673static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7674 int len)
7675{
7676#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007677 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007678 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007679 return 0;
7680#else
7681 return 0;
7682#endif
7683}
7684
Matt Carlsond1a3b732011-07-27 14:20:51 +00007685static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007686 dma_addr_t mapping, u32 len, u32 flags,
7687 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007688{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007689 txbd->addr_hi = ((u64) mapping >> 32);
7690 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7691 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7692 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
Matt Carlson84b67b22011-07-27 14:20:52 +00007695static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007696 dma_addr_t map, u32 len, u32 flags,
7697 u32 mss, u32 vlan)
7698{
7699 struct tg3 *tp = tnapi->tp;
7700 bool hwbug = false;
7701
7702 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007703 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007704
7705 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007706 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007707
Michael Chan0f0d1512013-05-13 11:04:16 +00007708 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7709 hwbug = true;
7710
Matt Carlsond1a3b732011-07-27 14:20:51 +00007711 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007712 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007713
Matt Carlsona4cb4282011-12-14 11:09:58 +00007714 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007715 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007716 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007717 while (len > tp->dma_limit && *budget) {
7718 u32 frag_len = tp->dma_limit;
7719 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007720
Matt Carlsonb9e45482011-11-04 09:14:59 +00007721 /* Avoid the 8byte DMA problem */
7722 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007723 len += tp->dma_limit / 2;
7724 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007725 }
7726
Matt Carlsonb9e45482011-11-04 09:14:59 +00007727 tnapi->tx_buffers[*entry].fragmented = true;
7728
7729 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7730 frag_len, tmp_flag, mss, vlan);
7731 *budget -= 1;
7732 prvidx = *entry;
7733 *entry = NEXT_TX(*entry);
7734
Matt Carlsone31aa982011-07-27 14:20:53 +00007735 map += frag_len;
7736 }
7737
7738 if (len) {
7739 if (*budget) {
7740 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7741 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007742 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007743 *entry = NEXT_TX(*entry);
7744 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007745 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007746 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007747 }
7748 }
7749 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007750 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7751 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007752 *entry = NEXT_TX(*entry);
7753 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007754
7755 return hwbug;
7756}
7757
Matt Carlson0d681b22011-07-27 14:20:49 +00007758static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007759{
7760 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007761 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007762 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007763
Matt Carlson0d681b22011-07-27 14:20:49 +00007764 skb = txb->skb;
7765 txb->skb = NULL;
7766
Matt Carlson432aa7e2011-05-19 12:12:45 +00007767 pci_unmap_single(tnapi->tp->pdev,
7768 dma_unmap_addr(txb, mapping),
7769 skb_headlen(skb),
7770 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007771
7772 while (txb->fragmented) {
7773 txb->fragmented = false;
7774 entry = NEXT_TX(entry);
7775 txb = &tnapi->tx_buffers[entry];
7776 }
7777
Matt Carlsonba1142e2011-11-04 09:15:00 +00007778 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007779 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007780
7781 entry = NEXT_TX(entry);
7782 txb = &tnapi->tx_buffers[entry];
7783
7784 pci_unmap_page(tnapi->tp->pdev,
7785 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007786 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007787
7788 while (txb->fragmented) {
7789 txb->fragmented = false;
7790 entry = NEXT_TX(entry);
7791 txb = &tnapi->tx_buffers[entry];
7792 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007793 }
7794}
7795
Michael Chan72f2afb2006-03-06 19:28:35 -08007796/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007797static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007798 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007799 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007800 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007802 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007803 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007804 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007805 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806
Joe Perches41535772013-02-16 11:20:04 +00007807 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba12008-04-19 18:12:33 -07007808 new_skb = skb_copy(skb, GFP_ATOMIC);
7809 else {
7810 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7811
7812 new_skb = skb_copy_expand(skb,
7813 skb_headroom(skb) + more_headroom,
7814 skb_tailroom(skb), GFP_ATOMIC);
7815 }
7816
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007818 ret = -1;
7819 } else {
7820 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007821 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7822 PCI_DMA_TODEVICE);
7823 /* Make sure the mapping succeeded */
7824 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007825 dev_kfree_skb_any(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007826 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007827 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007828 u32 save_entry = *entry;
7829
Matt Carlson92cd3a12011-07-27 14:20:47 +00007830 base_flags |= TXD_FLAG_END;
7831
Matt Carlson84b67b22011-07-27 14:20:52 +00007832 tnapi->tx_buffers[*entry].skb = new_skb;
7833 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007834 mapping, new_addr);
7835
Matt Carlson84b67b22011-07-27 14:20:52 +00007836 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007837 new_skb->len, base_flags,
7838 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007839 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Eric W. Biederman497a27b2014-03-11 14:18:14 -07007840 dev_kfree_skb_any(new_skb);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007841 ret = -1;
7842 }
Michael Chanc58ec932005-09-17 00:46:27 -07007843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 }
7845
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007846 dev_consume_skb_any(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007847 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849}
7850
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307851static bool tg3_tso_bug_gso_check(struct tg3_napi *tnapi, struct sk_buff *skb)
7852{
7853 /* Check if we will never have enough descriptors,
7854 * as gso_segs can be more than current ring size
7855 */
7856 return skb_shinfo(skb)->gso_segs < tnapi->tx_pending / 3;
7857}
7858
Matt Carlson2ffcc982011-05-19 12:12:44 +00007859static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007860
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007861/* Use GSO to workaround all TSO packets that meet HW bug conditions
7862 * indicated in tg3_tx_frag_set()
Michael Chan52c0fd82006-06-29 20:15:54 -07007863 */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007864static int tg3_tso_bug(struct tg3 *tp, struct tg3_napi *tnapi,
7865 struct netdev_queue *txq, struct sk_buff *skb)
Michael Chan52c0fd82006-06-29 20:15:54 -07007866{
7867 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007868 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007869
7870 /* Estimate the number of fragments in the worst case */
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007871 if (unlikely(tg3_tx_avail(tnapi) <= frag_cnt_est)) {
7872 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007873
7874 /* netif_tx_stop_queue() must be done before checking
7875 * checking tx index in tg3_tx_avail() below, because in
7876 * tg3_tx(), we update tx index before checking for
7877 * netif_tx_queue_stopped().
7878 */
7879 smp_mb();
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007880 if (tg3_tx_avail(tnapi) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007881 return NETDEV_TX_BUSY;
7882
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007883 netif_tx_wake_queue(txq);
Michael Chan52c0fd82006-06-29 20:15:54 -07007884 }
7885
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07007886 segs = skb_gso_segment(skb, tp->dev->features &
7887 ~(NETIF_F_TSO | NETIF_F_TSO6));
Prashant Sreedharan40c1dea2014-06-18 18:38:13 -07007888 if (IS_ERR(segs) || !segs)
Michael Chan52c0fd82006-06-29 20:15:54 -07007889 goto tg3_tso_bug_end;
7890
7891 do {
7892 nskb = segs;
7893 segs = segs->next;
7894 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007895 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007896 } while (segs);
7897
7898tg3_tso_bug_end:
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07007899 dev_consume_skb_any(skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007900
7901 return NETDEV_TX_OK;
7902}
Michael Chan52c0fd82006-06-29 20:15:54 -07007903
Michael Chand71c0dc2014-05-11 20:22:53 -07007904/* hard_start_xmit for all devices */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007905static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007906{
7907 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007908 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007909 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007910 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007911 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007912 struct tg3_napi *tnapi;
7913 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007914 unsigned int last;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007915 struct iphdr *iph = NULL;
7916 struct tcphdr *tcph = NULL;
7917 __sum16 tcp_csum = 0, ip_csum = 0;
7918 __be16 ip_tot_len = 0;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007919
Matt Carlson24f4efd2009-11-13 13:03:35 +00007920 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7921 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007922 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007923 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
Matt Carlson84b67b22011-07-27 14:20:52 +00007925 budget = tg3_tx_avail(tnapi);
7926
Michael Chan00b70502006-06-17 21:58:45 -07007927 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007928 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007929 * interrupt. Furthermore, IRQ processing runs lockless so we have
7930 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007932 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007933 if (!netif_tx_queue_stopped(txq)) {
7934 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007935
7936 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007937 netdev_err(dev,
7938 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 return NETDEV_TX_BUSY;
7941 }
7942
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007943 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 base_flags = 0;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007945
Matt Carlsonbe98da62010-07-11 09:31:46 +00007946 mss = skb_shinfo(skb)->gso_size;
7947 if (mss) {
Matt Carlson34195c32010-07-11 09:31:42 +00007948 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949
françois romieu105dcb52014-03-29 12:26:29 +01007950 if (skb_cow_head(skb, 0))
Eric Dumazet48855432011-10-24 07:53:03 +00007951 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952
Matt Carlson34195c32010-07-11 09:31:42 +00007953 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007954 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955
Eric Dumazeta5a11952012-01-23 01:22:09 +00007956 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007957
Vlad Yasevich476c1882014-09-18 10:31:17 -04007958 /* HW/FW can not correctly segment packets that have been
7959 * vlan encapsulated.
7960 */
7961 if (skb->protocol == htons(ETH_P_8021Q) ||
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307962 skb->protocol == htons(ETH_P_8021AD)) {
7963 if (tg3_tso_bug_gso_check(tnapi, skb))
7964 return tg3_tso_bug(tp, tnapi, txq, skb);
7965 goto drop;
7966 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04007967
Eric Dumazeta5a11952012-01-23 01:22:09 +00007968 if (!skb_is_gso_v6(skb)) {
Michael Chand71c0dc2014-05-11 20:22:53 -07007969 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05307970 tg3_flag(tp, TSO_BUG)) {
7971 if (tg3_tso_bug_gso_check(tnapi, skb))
7972 return tg3_tso_bug(tp, tnapi, txq, skb);
7973 goto drop;
7974 }
Michael Chand3f6f3a2014-05-11 20:22:54 -07007975 ip_csum = iph->check;
7976 ip_tot_len = iph->tot_len;
Matt Carlson34195c32010-07-11 09:31:42 +00007977 iph->check = 0;
7978 iph->tot_len = htons(mss + hdr_len);
7979 }
7980
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7982 TXD_FLAG_CPU_POST_DMA);
7983
Michael Chand3f6f3a2014-05-11 20:22:54 -07007984 tcph = tcp_hdr(skb);
7985 tcp_csum = tcph->check;
7986
Joe Perches63c3a662011-04-26 08:12:10 +00007987 if (tg3_flag(tp, HW_TSO_1) ||
7988 tg3_flag(tp, HW_TSO_2) ||
7989 tg3_flag(tp, HW_TSO_3)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07007990 tcph->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Michael Chand3f6f3a2014-05-11 20:22:54 -07007992 } else {
7993 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
7994 0, IPPROTO_TCP, 0);
7995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007996
Joe Perches63c3a662011-04-26 08:12:10 +00007997 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007998 mss |= (hdr_len & 0xc) << 12;
7999 if (hdr_len & 0x10)
8000 base_flags |= 0x00000010;
8001 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00008002 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008003 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00008004 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00008005 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008006 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 int tsflags;
8008
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008009 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 mss |= (tsflags << 11);
8011 }
8012 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008013 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008014 int tsflags;
8015
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07008016 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017 base_flags |= tsflags << 12;
8018 }
8019 }
Vlad Yasevich476c1882014-09-18 10:31:17 -04008020 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
8021 /* HW/FW can not correctly checksum packets that have been
8022 * vlan encapsulated.
8023 */
8024 if (skb->protocol == htons(ETH_P_8021Q) ||
8025 skb->protocol == htons(ETH_P_8021AD)) {
8026 if (skb_checksum_help(skb))
8027 goto drop;
8028 } else {
8029 base_flags |= TXD_FLAG_TCPUDP_CSUM;
8030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00008032
Matt Carlson93a700a2011-08-31 11:44:54 +00008033 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
8034 !mss && skb->len > VLAN_ETH_FRAME_LEN)
8035 base_flags |= TXD_FLAG_JMB_PKT;
8036
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008037 if (skb_vlan_tag_present(skb)) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008038 base_flags |= TXD_FLAG_VLAN;
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01008039 vlan = skb_vlan_tag_get(skb);
Matt Carlson92cd3a12011-07-27 14:20:47 +00008040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00008042 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
8043 tg3_flag(tp, TX_TSTAMP_EN)) {
8044 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
8045 base_flags |= TXD_FLAG_HWTSTAMP;
8046 }
8047
Alexander Duyckf4188d82009-12-02 16:48:38 +00008048 len = skb_headlen(skb);
8049
8050 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008051 if (pci_dma_mapping_error(tp->pdev, mapping))
8052 goto drop;
8053
David S. Miller90079ce2008-09-11 04:52:51 -07008054
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008055 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008056 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057
8058 would_hit_hwbug = 0;
8059
Joe Perches63c3a662011-04-26 08:12:10 +00008060 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008061 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
Matt Carlson84b67b22011-07-27 14:20:52 +00008063 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008064 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008065 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008066 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008067 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008068 u32 tmp_mss = mss;
8069
8070 if (!tg3_flag(tp, HW_TSO_1) &&
8071 !tg3_flag(tp, HW_TSO_2) &&
8072 !tg3_flag(tp, HW_TSO_3))
8073 tmp_mss = 0;
8074
Matt Carlsonc5665a52012-02-13 10:20:12 +00008075 /* Now loop through additional data
8076 * fragments, and queue them.
8077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 last = skb_shinfo(skb)->nr_frags - 1;
8079 for (i = 0; i <= last; i++) {
8080 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8081
Eric Dumazet9e903e02011-10-18 21:00:24 +00008082 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008083 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008084 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008086 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008087 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008088 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008089 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008090 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091
Matt Carlsonb9e45482011-11-04 09:14:59 +00008092 if (!budget ||
8093 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008094 len, base_flags |
8095 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008096 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008097 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008098 break;
8099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 }
8101 }
8102
8103 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008104 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
Siva Reddy Kallamb7d98722016-02-03 14:09:38 +05308106 if (mss && tg3_tso_bug_gso_check(tnapi, skb)) {
Michael Chand3f6f3a2014-05-11 20:22:54 -07008107 /* If it's a TSO packet, do GSO instead of
8108 * allocating and copying to a large linear SKB
8109 */
8110 if (ip_tot_len) {
8111 iph->check = ip_csum;
8112 iph->tot_len = ip_tot_len;
8113 }
8114 tcph->check = tcp_csum;
Prashant Sreedharan4d8fdc92014-08-05 16:02:02 -07008115 return tg3_tso_bug(tp, tnapi, txq, skb);
Michael Chand3f6f3a2014-05-11 20:22:54 -07008116 }
8117
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 /* If the workaround fails due to memory/mapping
8119 * failure, silently drop this packet.
8120 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008121 entry = tnapi->tx_prod;
8122 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008123 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008124 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008125 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008126 }
8127
Richard Cochrand515b452011-06-19 03:31:41 +00008128 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008129 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008130
Michael Chan6541b802012-03-04 14:48:14 +00008131 /* Sync BD data before updating mailbox */
8132 wmb();
8133
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008134 tnapi->tx_prod = entry;
8135 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008136 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008137
8138 /* netif_tx_stop_queue() must be done before checking
8139 * checking tx index in tg3_tx_avail() below, because in
8140 * tg3_tx(), we update tx index before checking for
8141 * netif_tx_queue_stopped().
8142 */
8143 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008144 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008145 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147
Prashant Sreedharan2c7c9ea2014-10-13 09:21:42 -07008148 if (!skb->xmit_more || netif_xmit_stopped(txq)) {
8149 /* Packets are ready, update Tx producer idx on card. */
8150 tw32_tx_mbox(tnapi->prodmbox, entry);
8151 mmiowb();
8152 }
8153
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008155
8156dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008157 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008158 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008159drop:
Eric W. Biederman497a27b2014-03-11 14:18:14 -07008160 dev_kfree_skb_any(skb);
Eric Dumazet48855432011-10-24 07:53:03 +00008161drop_nofree:
8162 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008163 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164}
8165
Matt Carlson6e01b202011-08-19 13:58:20 +00008166static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8167{
8168 if (enable) {
8169 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8170 MAC_MODE_PORT_MODE_MASK);
8171
8172 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8173
8174 if (!tg3_flag(tp, 5705_PLUS))
8175 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8176
8177 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8178 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8179 else
8180 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8181 } else {
8182 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8183
8184 if (tg3_flag(tp, 5705_PLUS) ||
8185 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008186 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008187 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8188 }
8189
8190 tw32(MAC_MODE, tp->mac_mode);
8191 udelay(40);
8192}
8193
Matt Carlson941ec902011-08-19 13:58:23 +00008194static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008195{
Matt Carlson941ec902011-08-19 13:58:23 +00008196 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008197
8198 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008199 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008200
Matt Carlson941ec902011-08-19 13:58:23 +00008201 if (extlpbk && tg3_phy_set_extloopbk(tp))
8202 return -EIO;
8203
8204 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008205 switch (speed) {
8206 case SPEED_10:
8207 break;
8208 case SPEED_100:
8209 bmcr |= BMCR_SPEED100;
8210 break;
8211 case SPEED_1000:
8212 default:
8213 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8214 speed = SPEED_100;
8215 bmcr |= BMCR_SPEED100;
8216 } else {
8217 speed = SPEED_1000;
8218 bmcr |= BMCR_SPEED1000;
8219 }
8220 }
8221
Matt Carlson941ec902011-08-19 13:58:23 +00008222 if (extlpbk) {
8223 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8224 tg3_readphy(tp, MII_CTRL1000, &val);
8225 val |= CTL1000_AS_MASTER |
8226 CTL1000_ENABLE_MASTER;
8227 tg3_writephy(tp, MII_CTRL1000, val);
8228 } else {
8229 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8230 MII_TG3_FET_PTEST_TRIM_2;
8231 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8232 }
8233 } else
8234 bmcr |= BMCR_LOOPBACK;
8235
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008236 tg3_writephy(tp, MII_BMCR, bmcr);
8237
8238 /* The write needs to be flushed for the FETs */
8239 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8240 tg3_readphy(tp, MII_BMCR, &bmcr);
8241
8242 udelay(40);
8243
8244 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008245 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008246 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008247 MII_TG3_FET_PTEST_FRC_TX_LINK |
8248 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8249
8250 /* The write needs to be flushed for the AC131 */
8251 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8252 }
8253
8254 /* Reset to prevent losing 1st rx packet intermittently */
8255 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8256 tg3_flag(tp, 5780_CLASS)) {
8257 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8258 udelay(10);
8259 tw32_f(MAC_RX_MODE, tp->rx_mode);
8260 }
8261
8262 mac_mode = tp->mac_mode &
8263 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8264 if (speed == SPEED_1000)
8265 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8266 else
8267 mac_mode |= MAC_MODE_PORT_MODE_MII;
8268
Joe Perches41535772013-02-16 11:20:04 +00008269 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008270 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8271
8272 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8273 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8274 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8275 mac_mode |= MAC_MODE_LINK_POLARITY;
8276
8277 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8278 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8279 }
8280
8281 tw32(MAC_MODE, mac_mode);
8282 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008283
8284 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008285}
8286
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008287static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008288{
8289 struct tg3 *tp = netdev_priv(dev);
8290
8291 if (features & NETIF_F_LOOPBACK) {
8292 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8293 return;
8294
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008295 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008296 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008297 netif_carrier_on(tp->dev);
8298 spin_unlock_bh(&tp->lock);
8299 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8300 } else {
8301 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8302 return;
8303
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008304 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008305 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008306 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008307 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008308 spin_unlock_bh(&tp->lock);
8309 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8310 }
8311}
8312
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008313static netdev_features_t tg3_fix_features(struct net_device *dev,
8314 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008315{
8316 struct tg3 *tp = netdev_priv(dev);
8317
Joe Perches63c3a662011-04-26 08:12:10 +00008318 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008319 features &= ~NETIF_F_ALL_TSO;
8320
8321 return features;
8322}
8323
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008324static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008325{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008326 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008327
8328 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8329 tg3_set_loopback(dev, features);
8330
8331 return 0;
8332}
8333
Matt Carlson21f581a2009-08-28 14:00:25 +00008334static void tg3_rx_prodring_free(struct tg3 *tp,
8335 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337 int i;
8338
Matt Carlson8fea32b2010-09-15 08:59:58 +00008339 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008340 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008341 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008342 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008343 tp->rx_pkt_map_sz);
8344
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008346 for (i = tpr->rx_jmb_cons_idx;
8347 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008348 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008349 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008350 TG3_RX_JMB_MAP_SZ);
8351 }
8352 }
8353
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008354 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
Matt Carlson2c49a442010-09-30 10:34:35 +00008357 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008358 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008359 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008360
Joe Perches63c3a662011-04-26 08:12:10 +00008361 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008362 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008363 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008364 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 }
8366}
8367
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008368/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 *
8370 * The chip has been shut down and the driver detached from
8371 * the networking, so no interrupts or new tx packets will
8372 * end up in the driver. tp->{tx,}lock are held and thus
8373 * we may not sleep.
8374 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008375static int tg3_rx_prodring_alloc(struct tg3 *tp,
8376 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377{
Matt Carlson287be122009-08-28 13:58:46 +00008378 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008380 tpr->rx_std_cons_idx = 0;
8381 tpr->rx_std_prod_idx = 0;
8382 tpr->rx_jmb_cons_idx = 0;
8383 tpr->rx_jmb_prod_idx = 0;
8384
Matt Carlson8fea32b2010-09-15 08:59:58 +00008385 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008386 memset(&tpr->rx_std_buffers[0], 0,
8387 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008388 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008389 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008390 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008391 goto done;
8392 }
8393
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008395 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396
Matt Carlson287be122009-08-28 13:58:46 +00008397 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008398 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008399 tp->dev->mtu > ETH_DATA_LEN)
8400 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8401 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07008402
Linus Torvalds1da177e2005-04-16 15:20:36 -07008403 /* Initialize invariants of the rings, we only set this
8404 * stuff once. This works because the card does not
8405 * write into the rx buffer posting rings.
8406 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008407 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 struct tg3_rx_buffer_desc *rxd;
8409
Matt Carlson21f581a2009-08-28 14:00:25 +00008410 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008411 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8413 rxd->opaque = (RXD_OPAQUE_RING_STD |
8414 (i << RXD_OPAQUE_INDEX_SHIFT));
8415 }
8416
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008417 /* Now allocate fresh SKBs for each rx ring. */
8418 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008419 unsigned int frag_size;
8420
8421 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8422 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008423 netdev_warn(tp->dev,
8424 "Using a smaller RX standard ring. Only "
8425 "%d out of %d buffers were allocated "
8426 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008427 if (i == 0)
8428 goto initfail;
8429 tp->rx_pending = i;
8430 break;
8431 }
8432 }
8433
Joe Perches63c3a662011-04-26 08:12:10 +00008434 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008435 goto done;
8436
Matt Carlson2c49a442010-09-30 10:34:35 +00008437 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008438
Joe Perches63c3a662011-04-26 08:12:10 +00008439 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008440 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441
Matt Carlson2c49a442010-09-30 10:34:35 +00008442 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008443 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444
Matt Carlson0d86df82010-02-17 15:17:00 +00008445 rxd = &tpr->rx_jmb[i].std;
8446 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8447 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8448 RXD_FLAG_JUMBO;
8449 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8450 (i << RXD_OPAQUE_INDEX_SHIFT));
8451 }
8452
8453 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008454 unsigned int frag_size;
8455
8456 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8457 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008458 netdev_warn(tp->dev,
8459 "Using a smaller RX jumbo ring. Only %d "
8460 "out of %d buffers were allocated "
8461 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008462 if (i == 0)
8463 goto initfail;
8464 tp->rx_jumbo_pending = i;
8465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 }
8467 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008468
8469done:
Michael Chan32d8c572006-07-25 16:38:29 -07008470 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008471
8472initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008473 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475}
8476
Matt Carlson21f581a2009-08-28 14:00:25 +00008477static void tg3_rx_prodring_fini(struct tg3 *tp,
8478 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479{
Matt Carlson21f581a2009-08-28 14:00:25 +00008480 kfree(tpr->rx_std_buffers);
8481 tpr->rx_std_buffers = NULL;
8482 kfree(tpr->rx_jmb_buffers);
8483 tpr->rx_jmb_buffers = NULL;
8484 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008485 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8486 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008487 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008489 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008490 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8491 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008492 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008494}
8495
Matt Carlson21f581a2009-08-28 14:00:25 +00008496static int tg3_rx_prodring_init(struct tg3 *tp,
8497 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008498{
Matt Carlson2c49a442010-09-30 10:34:35 +00008499 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8500 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008501 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008502 return -ENOMEM;
8503
Matt Carlson4bae65c2010-11-24 08:31:52 +00008504 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8505 TG3_RX_STD_RING_BYTES(tp),
8506 &tpr->rx_std_mapping,
8507 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008508 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008509 goto err_out;
8510
Joe Perches63c3a662011-04-26 08:12:10 +00008511 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008512 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008513 GFP_KERNEL);
8514 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008515 goto err_out;
8516
Matt Carlson4bae65c2010-11-24 08:31:52 +00008517 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8518 TG3_RX_JMB_RING_BYTES(tp),
8519 &tpr->rx_jmb_mapping,
8520 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008521 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008522 goto err_out;
8523 }
8524
8525 return 0;
8526
8527err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008528 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008529 return -ENOMEM;
8530}
8531
8532/* Free up pending packets in all rx/tx rings.
8533 *
8534 * The chip has been shut down and the driver detached from
8535 * the networking, so no interrupts or new tx packets will
8536 * end up in the driver. tp->{tx,}lock is not held and we are not
8537 * in an interrupt context and thus may sleep.
8538 */
8539static void tg3_free_rings(struct tg3 *tp)
8540{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008541 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008542
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008543 for (j = 0; j < tp->irq_cnt; j++) {
8544 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008545
Matt Carlson8fea32b2010-09-15 08:59:58 +00008546 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008547
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008548 if (!tnapi->tx_buffers)
8549 continue;
8550
Matt Carlson0d681b22011-07-27 14:20:49 +00008551 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8552 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008553
Matt Carlson0d681b22011-07-27 14:20:49 +00008554 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008555 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008556
Matt Carlsonba1142e2011-11-04 09:15:00 +00008557 tg3_tx_skb_unmap(tnapi, i,
8558 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008559
Florian Fainelli1e9d8e72017-08-24 17:47:11 -07008560 dev_consume_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008561 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008562 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008563 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008564}
8565
8566/* Initialize tx/rx rings for packet processing.
8567 *
8568 * The chip has been shut down and the driver detached from
8569 * the networking, so no interrupts or new tx packets will
8570 * end up in the driver. tp->{tx,}lock are held and thus
8571 * we may not sleep.
8572 */
8573static int tg3_init_rings(struct tg3 *tp)
8574{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008575 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008576
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008577 /* Free up all the SKBs. */
8578 tg3_free_rings(tp);
8579
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008580 for (i = 0; i < tp->irq_cnt; i++) {
8581 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008582
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008583 tnapi->last_tag = 0;
8584 tnapi->last_irq_tag = 0;
8585 tnapi->hw_status->status = 0;
8586 tnapi->hw_status->status_tag = 0;
8587 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8588
8589 tnapi->tx_prod = 0;
8590 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008591 if (tnapi->tx_ring)
8592 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008593
8594 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008595 if (tnapi->rx_rcb)
8596 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008597
Thadeu Lima de Souza Cascardoa620a6b2014-11-25 14:21:11 -02008598 if (tnapi->prodring.rx_std &&
8599 tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008600 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008601 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008602 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008603 }
Matt Carlson72334482009-08-28 14:03:01 +00008604
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008605 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008606}
8607
Michael Chan49a359e2012-09-28 07:12:37 +00008608static void tg3_mem_tx_release(struct tg3 *tp)
8609{
8610 int i;
8611
8612 for (i = 0; i < tp->irq_max; i++) {
8613 struct tg3_napi *tnapi = &tp->napi[i];
8614
8615 if (tnapi->tx_ring) {
8616 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8617 tnapi->tx_ring, tnapi->tx_desc_mapping);
8618 tnapi->tx_ring = NULL;
8619 }
8620
8621 kfree(tnapi->tx_buffers);
8622 tnapi->tx_buffers = NULL;
8623 }
8624}
8625
8626static int tg3_mem_tx_acquire(struct tg3 *tp)
8627{
8628 int i;
8629 struct tg3_napi *tnapi = &tp->napi[0];
8630
8631 /* If multivector TSS is enabled, vector 0 does not handle
8632 * tx interrupts. Don't allocate any resources for it.
8633 */
8634 if (tg3_flag(tp, ENABLE_TSS))
8635 tnapi++;
8636
8637 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
Kees Cook6396bb22018-06-12 14:03:40 -07008638 tnapi->tx_buffers = kcalloc(TG3_TX_RING_SIZE,
8639 sizeof(struct tg3_tx_ring_info),
8640 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008641 if (!tnapi->tx_buffers)
8642 goto err_out;
8643
8644 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8645 TG3_TX_RING_BYTES,
8646 &tnapi->tx_desc_mapping,
8647 GFP_KERNEL);
8648 if (!tnapi->tx_ring)
8649 goto err_out;
8650 }
8651
8652 return 0;
8653
8654err_out:
8655 tg3_mem_tx_release(tp);
8656 return -ENOMEM;
8657}
8658
8659static void tg3_mem_rx_release(struct tg3 *tp)
8660{
8661 int i;
8662
8663 for (i = 0; i < tp->irq_max; i++) {
8664 struct tg3_napi *tnapi = &tp->napi[i];
8665
8666 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8667
8668 if (!tnapi->rx_rcb)
8669 continue;
8670
8671 dma_free_coherent(&tp->pdev->dev,
8672 TG3_RX_RCB_RING_BYTES(tp),
8673 tnapi->rx_rcb,
8674 tnapi->rx_rcb_mapping);
8675 tnapi->rx_rcb = NULL;
8676 }
8677}
8678
8679static int tg3_mem_rx_acquire(struct tg3 *tp)
8680{
8681 unsigned int i, limit;
8682
8683 limit = tp->rxq_cnt;
8684
8685 /* If RSS is enabled, we need a (dummy) producer ring
8686 * set on vector zero. This is the true hw prodring.
8687 */
8688 if (tg3_flag(tp, ENABLE_RSS))
8689 limit++;
8690
8691 for (i = 0; i < limit; i++) {
8692 struct tg3_napi *tnapi = &tp->napi[i];
8693
8694 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8695 goto err_out;
8696
8697 /* If multivector RSS is enabled, vector 0
8698 * does not handle rx or tx interrupts.
8699 * Don't allocate any resources for it.
8700 */
8701 if (!i && tg3_flag(tp, ENABLE_RSS))
8702 continue;
8703
Joe Perchesede23fa82013-08-26 22:45:23 -07008704 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8705 TG3_RX_RCB_RING_BYTES(tp),
8706 &tnapi->rx_rcb_mapping,
8707 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008708 if (!tnapi->rx_rcb)
8709 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008710 }
8711
8712 return 0;
8713
8714err_out:
8715 tg3_mem_rx_release(tp);
8716 return -ENOMEM;
8717}
8718
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008719/*
8720 * Must not be invoked with interrupt sources disabled and
8721 * the hardware shutdown down.
8722 */
8723static void tg3_free_consistent(struct tg3 *tp)
8724{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008725 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008726
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008727 for (i = 0; i < tp->irq_cnt; i++) {
8728 struct tg3_napi *tnapi = &tp->napi[i];
8729
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008730 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008731 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8732 tnapi->hw_status,
8733 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008734 tnapi->hw_status = NULL;
8735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008737
Michael Chan49a359e2012-09-28 07:12:37 +00008738 tg3_mem_rx_release(tp);
8739 tg3_mem_tx_release(tp);
8740
Michael Chand89a2ad2018-05-03 20:04:27 -04008741 /* tp->hw_stats can be referenced safely:
8742 * 1. under rtnl_lock
8743 * 2. or under tp->lock if TG3_FLAG_INIT_COMPLETE is set.
8744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008745 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008746 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8747 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 tp->hw_stats = NULL;
8749 }
8750}
8751
8752/*
8753 * Must not be invoked with interrupt sources disabled and
8754 * the hardware shutdown down. Can sleep.
8755 */
8756static int tg3_alloc_consistent(struct tg3 *tp)
8757{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008758 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008759
Joe Perchesede23fa82013-08-26 22:45:23 -07008760 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8761 sizeof(struct tg3_hw_stats),
8762 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763 if (!tp->hw_stats)
8764 goto err_out;
8765
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008766 for (i = 0; i < tp->irq_cnt; i++) {
8767 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008768 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008769
Joe Perchesede23fa82013-08-26 22:45:23 -07008770 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8771 TG3_HW_STATUS_SIZE,
8772 &tnapi->status_mapping,
8773 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008774 if (!tnapi->hw_status)
8775 goto err_out;
8776
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008777 sblk = tnapi->hw_status;
8778
Michael Chan49a359e2012-09-28 07:12:37 +00008779 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008780 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008781
Michael Chan49a359e2012-09-28 07:12:37 +00008782 /*
8783 * When RSS is enabled, the status block format changes
8784 * slightly. The "rx_jumbo_consumer", "reserved",
8785 * and "rx_mini_consumer" members get mapped to the
8786 * other three rx return ring producer indexes.
8787 */
8788 switch (i) {
8789 case 1:
8790 prodptr = &sblk->idx[0].rx_producer;
8791 break;
8792 case 2:
8793 prodptr = &sblk->rx_jumbo_consumer;
8794 break;
8795 case 3:
8796 prodptr = &sblk->reserved;
8797 break;
8798 case 4:
8799 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008800 break;
8801 }
Michael Chan49a359e2012-09-28 07:12:37 +00008802 tnapi->rx_rcb_prod_idx = prodptr;
8803 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008804 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008805 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008806 }
8807
Michael Chan49a359e2012-09-28 07:12:37 +00008808 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8809 goto err_out;
8810
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 return 0;
8812
8813err_out:
8814 tg3_free_consistent(tp);
8815 return -ENOMEM;
8816}
8817
8818#define MAX_WAIT_CNT 1000
8819
8820/* To stop a block, clear the enable bit and poll till it
8821 * clears. tp->lock is held.
8822 */
Joe Perches953c96e2013-04-09 10:18:14 +00008823static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824{
8825 unsigned int i;
8826 u32 val;
8827
Joe Perches63c3a662011-04-26 08:12:10 +00008828 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008829 switch (ofs) {
8830 case RCVLSC_MODE:
8831 case DMAC_MODE:
8832 case MBFREE_MODE:
8833 case BUFMGR_MODE:
8834 case MEMARB_MODE:
8835 /* We can't enable/disable these bits of the
8836 * 5705/5750, just say success.
8837 */
8838 return 0;
8839
8840 default:
8841 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 }
8844
8845 val = tr32(ofs);
8846 val &= ~enable_bit;
8847 tw32_f(ofs, val);
8848
8849 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008850 if (pci_channel_offline(tp->pdev)) {
8851 dev_err(&tp->pdev->dev,
8852 "tg3_stop_block device offline, "
8853 "ofs=%lx enable_bit=%x\n",
8854 ofs, enable_bit);
8855 return -ENODEV;
8856 }
8857
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 udelay(100);
8859 val = tr32(ofs);
8860 if ((val & enable_bit) == 0)
8861 break;
8862 }
8863
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008864 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008865 dev_err(&tp->pdev->dev,
8866 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8867 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 return -ENODEV;
8869 }
8870
8871 return 0;
8872}
8873
8874/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008875static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876{
8877 int i, err;
8878
8879 tg3_disable_ints(tp);
8880
Gavin Shan6d446ec2013-06-25 15:24:32 +08008881 if (pci_channel_offline(tp->pdev)) {
8882 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8883 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8884 err = -ENODEV;
8885 goto err_no_dev;
8886 }
8887
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 tp->rx_mode &= ~RX_MODE_ENABLE;
8889 tw32_f(MAC_RX_MODE, tp->rx_mode);
8890 udelay(10);
8891
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008892 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8893 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8894 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8895 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8896 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8897 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008898
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008899 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8900 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8901 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8902 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8903 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8904 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8905 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906
8907 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8908 tw32_f(MAC_MODE, tp->mac_mode);
8909 udelay(40);
8910
8911 tp->tx_mode &= ~TX_MODE_ENABLE;
8912 tw32_f(MAC_TX_MODE, tp->tx_mode);
8913
8914 for (i = 0; i < MAX_WAIT_CNT; i++) {
8915 udelay(100);
8916 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8917 break;
8918 }
8919 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008920 dev_err(&tp->pdev->dev,
8921 "%s timed out, TX_MODE_ENABLE will not clear "
8922 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008923 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 }
8925
Michael Chane6de8ad2005-05-05 14:42:41 -07008926 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008927 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8928 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929
8930 tw32(FTQ_RESET, 0xffffffff);
8931 tw32(FTQ_RESET, 0x00000000);
8932
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008933 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8934 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935
Gavin Shan6d446ec2013-06-25 15:24:32 +08008936err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008937 for (i = 0; i < tp->irq_cnt; i++) {
8938 struct tg3_napi *tnapi = &tp->napi[i];
8939 if (tnapi->hw_status)
8940 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008942
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 return err;
8944}
8945
Michael Chanee6a99b2007-07-18 21:49:10 -07008946/* Save PCI command register before chip reset */
8947static void tg3_save_pci_state(struct tg3 *tp)
8948{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008949 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008950}
8951
8952/* Restore PCI state after chip reset */
8953static void tg3_restore_pci_state(struct tg3 *tp)
8954{
8955 u32 val;
8956
8957 /* Re-enable indirect register accesses. */
8958 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8959 tp->misc_host_ctrl);
8960
8961 /* Set MAX PCI retry to zero. */
8962 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008963 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008964 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008965 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008966 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008967 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008968 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008969 PCISTATE_ALLOW_APE_SHMEM_WR |
8970 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008971 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8972
Matt Carlson8a6eac92007-10-21 16:17:55 -07008973 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008974
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008975 if (!tg3_flag(tp, PCI_EXPRESS)) {
8976 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8977 tp->pci_cacheline_sz);
8978 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8979 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008980 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008981
Michael Chanee6a99b2007-07-18 21:49:10 -07008982 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008983 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008984 u16 pcix_cmd;
8985
8986 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8987 &pcix_cmd);
8988 pcix_cmd &= ~PCI_X_CMD_ERO;
8989 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8990 pcix_cmd);
8991 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008992
Joe Perches63c3a662011-04-26 08:12:10 +00008993 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008994
8995 /* Chip reset on 5780 will reset MSI enable bit,
8996 * so need to restore it.
8997 */
Joe Perches63c3a662011-04-26 08:12:10 +00008998 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008999 u16 ctrl;
9000
9001 pci_read_config_word(tp->pdev,
9002 tp->msi_cap + PCI_MSI_FLAGS,
9003 &ctrl);
9004 pci_write_config_word(tp->pdev,
9005 tp->msi_cap + PCI_MSI_FLAGS,
9006 ctrl | PCI_MSI_FLAGS_ENABLE);
9007 val = tr32(MSGINT_MODE);
9008 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
9009 }
9010 }
9011}
9012
Nithin Sujirf82995b2014-01-03 10:09:13 -08009013static void tg3_override_clk(struct tg3 *tp)
9014{
9015 u32 val;
9016
9017 switch (tg3_asic_rev(tp)) {
9018 case ASIC_REV_5717:
9019 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9020 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
9021 TG3_CPMU_MAC_ORIDE_ENABLE);
9022 break;
9023
9024 case ASIC_REV_5719:
9025 case ASIC_REV_5720:
9026 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9027 break;
9028
9029 default:
9030 return;
9031 }
9032}
9033
9034static void tg3_restore_clk(struct tg3 *tp)
9035{
9036 u32 val;
9037
9038 switch (tg3_asic_rev(tp)) {
9039 case ASIC_REV_5717:
9040 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9041 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
9042 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
9043 break;
9044
9045 case ASIC_REV_5719:
9046 case ASIC_REV_5720:
9047 val = tr32(TG3_CPMU_CLCK_ORIDE);
9048 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
9049 break;
9050
9051 default:
9052 return;
9053 }
9054}
9055
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056/* tp->lock is held. */
9057static int tg3_chip_reset(struct tg3 *tp)
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009058 __releases(tp->lock)
9059 __acquires(tp->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060{
9061 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07009062 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00009063 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009064
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01009065 if (!pci_device_is_present(tp->pdev))
9066 return -ENODEV;
9067
David S. Millerf49639e2006-06-09 11:58:36 -07009068 tg3_nvram_lock(tp);
9069
Matt Carlson77b483f2008-08-15 14:07:24 -07009070 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
9071
David S. Millerf49639e2006-06-09 11:58:36 -07009072 /* No matching tg3_nvram_unlock() after this because
9073 * chip reset below will undo the nvram lock.
9074 */
9075 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076
Michael Chanee6a99b2007-07-18 21:49:10 -07009077 /* GRC_MISC_CFG core clock reset will clear the memory
9078 * enable bit in PCI register 4 and the MSI enable bit
9079 * on some chips, so we save relevant registers here.
9080 */
9081 tg3_save_pci_state(tp);
9082
Joe Perches41535772013-02-16 11:20:04 +00009083 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009084 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08009085 tw32(GRC_FASTBOOT_PC, 0);
9086
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087 /*
9088 * We must avoid the readl() that normally takes place.
9089 * It locks machines, causes machine checks, and other
9090 * fun things. So, temporarily disable the 5701
9091 * hardware workaround, while we do the reset.
9092 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009093 write_op = tp->write32;
9094 if (write_op == tg3_write_flush_reg32)
9095 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009096
Michael Chand18edcb2007-03-24 20:57:11 -07009097 /* Prevent the irq handler from reading or writing PCI registers
9098 * during chip reset when the memory enable bit in the PCI command
9099 * register may be cleared. The chip does not generate interrupt
9100 * at this time, but the irq handler may still be called due to irq
9101 * sharing or irqpoll.
9102 */
Joe Perches63c3a662011-04-26 08:12:10 +00009103 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009104 for (i = 0; i < tp->irq_cnt; i++) {
9105 struct tg3_napi *tnapi = &tp->napi[i];
9106 if (tnapi->hw_status) {
9107 tnapi->hw_status->status = 0;
9108 tnapi->hw_status->status_tag = 0;
9109 }
9110 tnapi->last_tag = 0;
9111 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009112 }
Michael Chand18edcb2007-03-24 20:57:11 -07009113 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009114
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009115 tg3_full_unlock(tp);
9116
Matt Carlson4f125f42009-09-01 12:55:02 +00009117 for (i = 0; i < tp->irq_cnt; i++)
9118 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009119
Prashant Sreedharan932f19d2015-01-14 11:34:44 -08009120 tg3_full_lock(tp, 0);
9121
Joe Perches41535772013-02-16 11:20:04 +00009122 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009123 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9124 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9125 }
9126
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 /* do the reset */
9128 val = GRC_MISC_CFG_CORECLK_RESET;
9129
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009131 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009132 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009133 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009134 tr32(TG3_PCIE_PHY_TSTCTL) ==
9135 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9136 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9137
Joe Perches41535772013-02-16 11:20:04 +00009138 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009139 tw32(GRC_MISC_CFG, (1 << 29));
9140 val |= (1 << 29);
9141 }
9142 }
9143
Joe Perches41535772013-02-16 11:20:04 +00009144 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009145 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9146 tw32(GRC_VCPU_EXT_CTRL,
9147 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9148 }
9149
Nithin Sujirf82995b2014-01-03 10:09:13 -08009150 /* Set the clock to the highest frequency to avoid timeouts. With link
9151 * aware mode, the clock speed could be slow and bootcode does not
9152 * complete within the expected time. Override the clock to allow the
9153 * bootcode to finish sooner and then restore it.
9154 */
9155 tg3_override_clk(tp);
9156
Matt Carlsonf37500d2010-08-02 11:25:59 +00009157 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009158 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009160
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161 tw32(GRC_MISC_CFG, val);
9162
Michael Chan1ee582d2005-08-09 20:16:46 -07009163 /* restore 5701 hardware bug workaround write method */
9164 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165
9166 /* Unfortunately, we have to delay before the PCI read back.
9167 * Some 575X chips even will not respond to a PCI cfg access
9168 * when the reset command is given to the chip.
9169 *
9170 * How do these hardware designers expect things to work
9171 * properly if the PCI write is posted for a long period
9172 * of time? It is always necessary to have some method by
9173 * which a register read back can occur to push the write
9174 * out which does the reset.
9175 *
9176 * For most tg3 variants the trick below was working.
9177 * Ho hum...
9178 */
9179 udelay(120);
9180
9181 /* Flush PCI posted writes. The normal MMIO registers
9182 * are inaccessible at this time so this is the only
9183 * way to make this reliably (actually, this is no longer
9184 * the case, see above). I tried to use indirect
9185 * register read/write but this upset some 5701 variants.
9186 */
9187 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9188
9189 udelay(120);
9190
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009191 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009192 u16 val16;
9193
Joe Perches41535772013-02-16 11:20:04 +00009194 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009195 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196 u32 cfg_val;
9197
9198 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009199 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 udelay(100);
9201
9202 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9203 pci_write_config_dword(tp->pdev, 0xc4,
9204 cfg_val | (1 << 15));
9205 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009206
Matt Carlsone7126992009-08-25 10:08:16 +00009207 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009208 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009209 /*
9210 * Older PCIe devices only support the 128 byte
9211 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009212 */
Joe Perches63c3a662011-04-26 08:12:10 +00009213 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009214 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9215 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009216
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009217 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009218 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009219 PCI_EXP_DEVSTA_CED |
9220 PCI_EXP_DEVSTA_NFED |
9221 PCI_EXP_DEVSTA_FED |
9222 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 }
9224
Michael Chanee6a99b2007-07-18 21:49:10 -07009225 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226
Joe Perches63c3a662011-04-26 08:12:10 +00009227 tg3_flag_clear(tp, CHIP_RESETTING);
9228 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009229
Michael Chanee6a99b2007-07-18 21:49:10 -07009230 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009231 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009232 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009233 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234
Joe Perches41535772013-02-16 11:20:04 +00009235 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236 tg3_stop_fw(tp);
9237 tw32(0x5000, 0x400);
9238 }
9239
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009240 if (tg3_flag(tp, IS_SSB_CORE)) {
9241 /*
9242 * BCM4785: In order to avoid repercussions from using
9243 * potentially defective internal ROM, stop the Rx RISC CPU,
9244 * which is not required.
9245 */
9246 tg3_stop_fw(tp);
9247 tg3_halt_cpu(tp, RX_CPU_BASE);
9248 }
9249
Nithin Sujirfb03a432013-05-21 12:57:32 +00009250 err = tg3_poll_fw(tp);
9251 if (err)
9252 return err;
9253
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 tw32(GRC_MODE, tp->grc_mode);
9255
Joe Perches41535772013-02-16 11:20:04 +00009256 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009257 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258
9259 tw32(0xc4, val | (1 << 15));
9260 }
9261
9262 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009263 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009265 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9267 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9268 }
9269
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009270 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009271 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009272 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009273 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009274 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009275 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009276 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009277 val = 0;
9278
9279 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280 udelay(40);
9281
Matt Carlson77b483f2008-08-15 14:07:24 -07009282 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9283
Matt Carlson0a9140c2009-08-28 12:27:50 +00009284 tg3_mdio_start(tp);
9285
Joe Perches63c3a662011-04-26 08:12:10 +00009286 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009287 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9288 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009289 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009290 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291
9292 tw32(0x7c00, val | (1 << 25));
9293 }
9294
Nithin Sujirf82995b2014-01-03 10:09:13 -08009295 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009296
Sanjeev Bansal3a498602018-07-16 11:13:32 +05309297 /* Increase the core clock speed to fix tx timeout issue for 5762
9298 * with 100Mbps link speed.
9299 */
9300 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
9301 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
9302 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
9303 TG3_CPMU_MAC_ORIDE_ENABLE);
9304 }
9305
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009307 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009308 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9309 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9310
Joe Perches63c3a662011-04-26 08:12:10 +00009311 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9313 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9314 u32 nic_cfg;
9315
9316 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9317 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009318 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009319 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009320 if (tg3_flag(tp, 5750_PLUS))
9321 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009322
9323 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9324 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9325 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9326 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9327 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 }
9329 }
9330
9331 return 0;
9332}
9333
Matt Carlson65ec6982012-02-28 23:33:37 +00009334static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9335static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009336static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009337
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009339static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009340{
9341 int err;
9342
9343 tg3_stop_fw(tp);
9344
Michael Chan944d9802005-05-29 14:57:48 -07009345 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009347 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 err = tg3_chip_reset(tp);
9349
Joe Perches953c96e2013-04-09 10:18:14 +00009350 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009351
Michael Chan944d9802005-05-29 14:57:48 -07009352 tg3_write_sig_legacy(tp, kind);
9353 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354
Matt Carlson92feeab2011-12-08 14:40:14 +00009355 if (tp->hw_stats) {
9356 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009357 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009358 tg3_get_estats(tp, &tp->estats_prev);
9359
9360 /* And make sure the next sample is new data */
9361 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9362 }
9363
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365}
9366
Linus Torvalds1da177e2005-04-16 15:20:36 -07009367static int tg3_set_mac_addr(struct net_device *dev, void *p)
9368{
9369 struct tg3 *tp = netdev_priv(dev);
9370 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009371 int err = 0;
9372 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373
Michael Chanf9804dd2005-09-27 12:13:10 -07009374 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009375 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009376
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9378
Michael Chane75f7c92006-03-20 21:33:26 -08009379 if (!netif_running(dev))
9380 return 0;
9381
Joe Perches63c3a662011-04-26 08:12:10 +00009382 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009383 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009384
Michael Chan986e0ae2007-05-05 12:10:20 -07009385 addr0_high = tr32(MAC_ADDR_0_HIGH);
9386 addr0_low = tr32(MAC_ADDR_0_LOW);
9387 addr1_high = tr32(MAC_ADDR_1_HIGH);
9388 addr1_low = tr32(MAC_ADDR_1_LOW);
9389
9390 /* Skip MAC addr 1 if ASF is using it. */
9391 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9392 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009393 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009394 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009395 spin_lock_bh(&tp->lock);
9396 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009397 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009398 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399
Michael Chanb9ec6c12006-07-25 16:37:27 -07009400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401}
9402
9403/* tp->lock is held. */
9404static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9405 dma_addr_t mapping, u32 maxlen_flags,
9406 u32 nic_addr)
9407{
9408 tg3_write_mem(tp,
9409 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9410 ((u64) mapping >> 32));
9411 tg3_write_mem(tp,
9412 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9413 ((u64) mapping & 0xffffffff));
9414 tg3_write_mem(tp,
9415 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9416 maxlen_flags);
9417
Joe Perches63c3a662011-04-26 08:12:10 +00009418 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 tg3_write_mem(tp,
9420 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9421 nic_addr);
9422}
9423
Michael Chana489b6d2012-09-28 07:12:39 +00009424
9425static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009426{
Michael Chana489b6d2012-09-28 07:12:39 +00009427 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009428
Joe Perches63c3a662011-04-26 08:12:10 +00009429 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009430 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9431 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9432 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009433 } else {
9434 tw32(HOSTCC_TXCOL_TICKS, 0);
9435 tw32(HOSTCC_TXMAX_FRAMES, 0);
9436 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009437
9438 for (; i < tp->txq_cnt; i++) {
9439 u32 reg;
9440
9441 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9442 tw32(reg, ec->tx_coalesce_usecs);
9443 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9444 tw32(reg, ec->tx_max_coalesced_frames);
9445 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9446 tw32(reg, ec->tx_max_coalesced_frames_irq);
9447 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009448 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009449
Michael Chana489b6d2012-09-28 07:12:39 +00009450 for (; i < tp->irq_max - 1; i++) {
9451 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9452 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9453 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9454 }
9455}
9456
9457static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9458{
9459 int i = 0;
9460 u32 limit = tp->rxq_cnt;
9461
Joe Perches63c3a662011-04-26 08:12:10 +00009462 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009463 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9464 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9465 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009466 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009467 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009468 tw32(HOSTCC_RXCOL_TICKS, 0);
9469 tw32(HOSTCC_RXMAX_FRAMES, 0);
9470 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009471 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009472
Michael Chana489b6d2012-09-28 07:12:39 +00009473 for (; i < limit; i++) {
9474 u32 reg;
9475
9476 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9477 tw32(reg, ec->rx_coalesce_usecs);
9478 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9479 tw32(reg, ec->rx_max_coalesced_frames);
9480 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9481 tw32(reg, ec->rx_max_coalesced_frames_irq);
9482 }
9483
9484 for (; i < tp->irq_max - 1; i++) {
9485 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9486 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9487 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9488 }
9489}
9490
9491static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9492{
9493 tg3_coal_tx_init(tp, ec);
9494 tg3_coal_rx_init(tp, ec);
9495
Joe Perches63c3a662011-04-26 08:12:10 +00009496 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009497 u32 val = ec->stats_block_coalesce_usecs;
9498
Matt Carlsonb6080e12009-09-01 13:12:00 +00009499 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9500 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9501
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009502 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009503 val = 0;
9504
9505 tw32(HOSTCC_STAT_COAL_TICKS, val);
9506 }
9507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508
9509/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009510static void tg3_tx_rcbs_disable(struct tg3 *tp)
9511{
9512 u32 txrcb, limit;
9513
9514 /* Disable all transmit rings but the first. */
9515 if (!tg3_flag(tp, 5705_PLUS))
9516 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9517 else if (tg3_flag(tp, 5717_PLUS))
9518 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9519 else if (tg3_flag(tp, 57765_CLASS) ||
9520 tg3_asic_rev(tp) == ASIC_REV_5762)
9521 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9522 else
9523 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9524
9525 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9526 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9527 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9528 BDINFO_FLAGS_DISABLED);
9529}
9530
9531/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009532static void tg3_tx_rcbs_init(struct tg3 *tp)
9533{
9534 int i = 0;
9535 u32 txrcb = NIC_SRAM_SEND_RCB;
9536
9537 if (tg3_flag(tp, ENABLE_TSS))
9538 i++;
9539
9540 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9541 struct tg3_napi *tnapi = &tp->napi[i];
9542
9543 if (!tnapi->tx_ring)
9544 continue;
9545
9546 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9547 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9548 NIC_SRAM_TX_BUFFER_DESC);
9549 }
9550}
9551
9552/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009553static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9554{
9555 u32 rxrcb, limit;
9556
9557 /* Disable all receive return rings but the first. */
9558 if (tg3_flag(tp, 5717_PLUS))
9559 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9560 else if (!tg3_flag(tp, 5705_PLUS))
9561 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9562 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9563 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9564 tg3_flag(tp, 57765_CLASS))
9565 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9566 else
9567 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9568
9569 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9570 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9571 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9572 BDINFO_FLAGS_DISABLED);
9573}
9574
9575/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009576static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9577{
9578 int i = 0;
9579 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9580
9581 if (tg3_flag(tp, ENABLE_RSS))
9582 i++;
9583
9584 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9585 struct tg3_napi *tnapi = &tp->napi[i];
9586
9587 if (!tnapi->rx_rcb)
9588 continue;
9589
9590 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9591 (tp->rx_ret_ring_mask + 1) <<
9592 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9593 }
9594}
9595
9596/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009597static void tg3_rings_reset(struct tg3 *tp)
9598{
9599 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009600 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009601 struct tg3_napi *tnapi = &tp->napi[0];
9602
Nithin Sujir328947f2013-05-23 11:11:24 +00009603 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009604
Nithin Sujir328947f2013-05-23 11:11:24 +00009605 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009606
9607 /* Disable interrupts */
9608 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009609 tp->napi[0].chk_msi_cnt = 0;
9610 tp->napi[0].last_rx_cons = 0;
9611 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009612
9613 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009614 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009615 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009616 tp->napi[i].tx_prod = 0;
9617 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009618 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009619 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009620 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9621 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009622 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009623 tp->napi[i].last_rx_cons = 0;
9624 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009625 }
Joe Perches63c3a662011-04-26 08:12:10 +00009626 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009627 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009628 } else {
9629 tp->napi[0].tx_prod = 0;
9630 tp->napi[0].tx_cons = 0;
9631 tw32_mailbox(tp->napi[0].prodmbox, 0);
9632 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9633 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009634
9635 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009636 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009637 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9638 for (i = 0; i < 16; i++)
9639 tw32_tx_mbox(mbox + i * 8, 0);
9640 }
9641
Matt Carlson2d31eca2009-09-01 12:53:31 +00009642 /* Clear status block in ram. */
9643 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9644
9645 /* Set status block DMA address */
9646 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9647 ((u64) tnapi->status_mapping >> 32));
9648 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9649 ((u64) tnapi->status_mapping & 0xffffffff));
9650
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009651 stblk = HOSTCC_STATBLCK_RING1;
9652
9653 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9654 u64 mapping = (u64)tnapi->status_mapping;
9655 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9656 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009657 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009658
9659 /* Clear status block in ram. */
9660 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009661 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009662
9663 tg3_tx_rcbs_init(tp);
9664 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009665}
9666
Matt Carlsoneb07a942011-04-20 07:57:36 +00009667static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9668{
9669 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9670
Joe Perches63c3a662011-04-26 08:12:10 +00009671 if (!tg3_flag(tp, 5750_PLUS) ||
9672 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009673 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9674 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009675 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009676 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009677 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9678 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009679 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9680 else
9681 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9682
9683 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9684 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9685
9686 val = min(nic_rep_thresh, host_rep_thresh);
9687 tw32(RCVBDI_STD_THRESH, val);
9688
Joe Perches63c3a662011-04-26 08:12:10 +00009689 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009690 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9691
Joe Perches63c3a662011-04-26 08:12:10 +00009692 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009693 return;
9694
Matt Carlson513aa6e2011-11-21 15:01:18 +00009695 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009696
9697 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9698
9699 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9700 tw32(RCVBDI_JUMBO_THRESH, val);
9701
Joe Perches63c3a662011-04-26 08:12:10 +00009702 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009703 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9704}
9705
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009706static inline u32 calc_crc(unsigned char *buf, int len)
9707{
9708 u32 reg;
9709 u32 tmp;
9710 int j, k;
9711
9712 reg = 0xffffffff;
9713
9714 for (j = 0; j < len; j++) {
9715 reg ^= buf[j];
9716
9717 for (k = 0; k < 8; k++) {
9718 tmp = reg & 0x01;
9719
9720 reg >>= 1;
9721
9722 if (tmp)
9723 reg ^= 0xedb88320;
9724 }
9725 }
9726
9727 return ~reg;
9728}
9729
9730static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9731{
9732 /* accept or reject all multicast frames */
9733 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9734 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9735 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9736 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9737}
9738
9739static void __tg3_set_rx_mode(struct net_device *dev)
9740{
9741 struct tg3 *tp = netdev_priv(dev);
9742 u32 rx_mode;
9743
9744 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9745 RX_MODE_KEEP_VLAN_TAG);
9746
9747#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9748 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9749 * flag clear.
9750 */
9751 if (!tg3_flag(tp, ENABLE_ASF))
9752 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9753#endif
9754
9755 if (dev->flags & IFF_PROMISC) {
9756 /* Promiscuous mode. */
9757 rx_mode |= RX_MODE_PROMISC;
9758 } else if (dev->flags & IFF_ALLMULTI) {
9759 /* Accept all multicast. */
9760 tg3_set_multi(tp, 1);
9761 } else if (netdev_mc_empty(dev)) {
9762 /* Reject all multicast. */
9763 tg3_set_multi(tp, 0);
9764 } else {
9765 /* Accept one or more multicast(s). */
9766 struct netdev_hw_addr *ha;
9767 u32 mc_filter[4] = { 0, };
9768 u32 regidx;
9769 u32 bit;
9770 u32 crc;
9771
9772 netdev_for_each_mc_addr(ha, dev) {
9773 crc = calc_crc(ha->addr, ETH_ALEN);
9774 bit = ~crc & 0x7f;
9775 regidx = (bit & 0x60) >> 5;
9776 bit &= 0x1f;
9777 mc_filter[regidx] |= (1 << bit);
9778 }
9779
9780 tw32(MAC_HASH_REG_0, mc_filter[0]);
9781 tw32(MAC_HASH_REG_1, mc_filter[1]);
9782 tw32(MAC_HASH_REG_2, mc_filter[2]);
9783 tw32(MAC_HASH_REG_3, mc_filter[3]);
9784 }
9785
Michael Chane565eec2014-01-03 10:09:12 -08009786 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9787 rx_mode |= RX_MODE_PROMISC;
9788 } else if (!(dev->flags & IFF_PROMISC)) {
9789 /* Add all entries into to the mac addr filter list */
9790 int i = 0;
9791 struct netdev_hw_addr *ha;
9792
9793 netdev_for_each_uc_addr(ha, dev) {
9794 __tg3_set_one_mac_addr(tp, ha->addr,
9795 i + TG3_UCAST_ADDR_IDX(tp));
9796 i++;
9797 }
9798 }
9799
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009800 if (rx_mode != tp->rx_mode) {
9801 tp->rx_mode = rx_mode;
9802 tw32_f(MAC_RX_MODE, rx_mode);
9803 udelay(10);
9804 }
9805}
9806
Michael Chan91024262012-09-28 07:12:38 +00009807static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009808{
9809 int i;
9810
9811 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009812 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009813}
9814
9815static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009816{
9817 int i;
9818
9819 if (!tg3_flag(tp, SUPPORT_MSIX))
9820 return;
9821
Michael Chan0b3ba052012-11-14 14:44:29 +00009822 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009823 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009824 return;
9825 }
9826
9827 /* Validate table against current IRQ count */
9828 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009829 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009830 break;
9831 }
9832
9833 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009834 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009835}
9836
Matt Carlson90415472011-12-16 13:33:23 +00009837static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009838{
9839 int i = 0;
9840 u32 reg = MAC_RSS_INDIR_TBL_0;
9841
9842 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9843 u32 val = tp->rss_ind_tbl[i];
9844 i++;
9845 for (; i % 8; i++) {
9846 val <<= 4;
9847 val |= tp->rss_ind_tbl[i];
9848 }
9849 tw32(reg, val);
9850 reg += 4;
9851 }
9852}
9853
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009854static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9855{
9856 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9857 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9858 else
9859 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9860}
9861
Matt Carlson2d31eca2009-09-01 12:53:31 +00009862/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009863static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864{
9865 u32 val, rdmac_mode;
9866 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009867 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868
9869 tg3_disable_ints(tp);
9870
9871 tg3_stop_fw(tp);
9872
9873 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9874
Joe Perches63c3a662011-04-26 08:12:10 +00009875 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009876 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009878 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9879 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9880 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009881 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009882 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9883 }
9884
Nithin Sujir400dfba2013-05-18 06:26:53 +00009885 /* Enable MAC control of LPI */
9886 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9887 tg3_setup_eee(tp);
9888
Matt Carlson603f1172010-02-12 14:47:10 +00009889 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009890 tg3_phy_reset(tp);
9891
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 err = tg3_chip_reset(tp);
9893 if (err)
9894 return err;
9895
9896 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9897
Joe Perches41535772013-02-16 11:20:04 +00009898 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009899 val = tr32(TG3_CPMU_CTRL);
9900 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9901 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009902
9903 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9904 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9905 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9906 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9907
9908 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9909 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9910 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9911 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9912
9913 val = tr32(TG3_CPMU_HST_ACC);
9914 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9915 val |= CPMU_HST_ACC_MACCLK_6_25;
9916 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009917 }
9918
Joe Perches41535772013-02-16 11:20:04 +00009919 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d932009-04-20 06:57:41 +00009920 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9921 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9922 PCIE_PWR_MGMT_L1_THRESH_4MS;
9923 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009924
9925 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9926 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9927
9928 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009929
Matt Carlsonf40386c2009-11-02 14:24:02 +00009930 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9931 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009932 }
9933
Joe Perches63c3a662011-04-26 08:12:10 +00009934 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009935 u32 grc_mode = tr32(GRC_MODE);
9936
9937 /* Access the lower 1K of PL PCIE block registers. */
9938 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9939 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9940
9941 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9942 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9943 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9944
9945 tw32(GRC_MODE, grc_mode);
9946 }
9947
Matt Carlson55086ad2011-12-14 11:09:59 +00009948 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009949 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009950 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009951
Matt Carlson5093eed2010-11-24 08:31:45 +00009952 /* Access the lower 1K of PL PCIE block registers. */
9953 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9954 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009955
Matt Carlson5093eed2010-11-24 08:31:45 +00009956 val = tr32(TG3_PCIE_TLDLPL_PORT +
9957 TG3_PCIE_PL_LO_PHYCTL5);
9958 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9959 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009960
Matt Carlson5093eed2010-11-24 08:31:45 +00009961 tw32(GRC_MODE, grc_mode);
9962 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009963
Joe Perches41535772013-02-16 11:20:04 +00009964 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009965 u32 grc_mode;
9966
9967 /* Fix transmit hangs */
9968 val = tr32(TG3_CPMU_PADRNG_CTL);
9969 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9970 tw32(TG3_CPMU_PADRNG_CTL, val);
9971
9972 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009973
9974 /* Access the lower 1K of DL PCIE block registers. */
9975 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9976 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9977
9978 val = tr32(TG3_PCIE_TLDLPL_PORT +
9979 TG3_PCIE_DL_LO_FTSMAX);
9980 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9981 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9982 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9983
9984 tw32(GRC_MODE, grc_mode);
9985 }
9986
Matt Carlsona977dbe2010-04-12 06:58:26 +00009987 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9988 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9989 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9990 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009991 }
9992
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993 /* This works around an issue with Athlon chipsets on
9994 * B3 tigon3 silicon. This bit has no effect on any
9995 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009996 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 */
Joe Perches63c3a662011-04-26 08:12:10 +00009998 if (!tg3_flag(tp, CPMU_PRESENT)) {
9999 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -070010000 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
10001 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
10002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003
Joe Perches41535772013-02-16 11:20:04 +000010004 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000010005 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 val = tr32(TG3PCI_PCISTATE);
10007 val |= PCISTATE_RETRY_SAME_DMA;
10008 tw32(TG3PCI_PCISTATE, val);
10009 }
10010
Joe Perches63c3a662011-04-26 08:12:10 +000010011 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070010012 /* Allow reads and writes to the
10013 * APE register and memory space.
10014 */
10015 val = tr32(TG3PCI_PCISTATE);
10016 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000010017 PCISTATE_ALLOW_APE_SHMEM_WR |
10018 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070010019 tw32(TG3PCI_PCISTATE, val);
10020 }
10021
Joe Perches41535772013-02-16 11:20:04 +000010022 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010023 /* Enable some hw fixes. */
10024 val = tr32(TG3PCI_MSI_DATA);
10025 val |= (1 << 26) | (1 << 28) | (1 << 29);
10026 tw32(TG3PCI_MSI_DATA, val);
10027 }
10028
10029 /* Descriptor ring init may make accesses to the
10030 * NIC SRAM area to setup the TX descriptors, so we
10031 * can only do this after the hardware has been
10032 * successfully reset.
10033 */
Michael Chan32d8c572006-07-25 16:38:29 -070010034 err = tg3_init_rings(tp);
10035 if (err)
10036 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037
Joe Perches63c3a662011-04-26 08:12:10 +000010038 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010039 val = tr32(TG3PCI_DMA_RW_CTRL) &
10040 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +000010041 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +000010042 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +000010043 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +000010044 tg3_asic_rev(tp) != ASIC_REV_5717 &&
10045 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +000010046 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000010047 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +000010048 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
10049 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -070010050 /* This value is determined during the probe time DMA
10051 * engine test, tg3_test_dma.
10052 */
10053 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
10054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055
10056 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
10057 GRC_MODE_4X_NIC_SEND_RINGS |
10058 GRC_MODE_NO_TX_PHDR_CSUM |
10059 GRC_MODE_NO_RX_PHDR_CSUM);
10060 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -070010061
10062 /* Pseudo-header checksum is done by hardware logic and not
10063 * the offload processers, so make the chip do the pseudo-
10064 * header checksums on receive. For transmit it is more
10065 * convenient to do the pseudo-header checksum in software
10066 * as Linux does that on transmit for us in all cases.
10067 */
10068 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000010070 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
10071 if (tp->rxptpctl)
10072 tw32(TG3_RX_PTP_CTL,
10073 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
10074
10075 if (tg3_flag(tp, PTP_CAPABLE))
10076 val |= GRC_MODE_TIME_SYNC_ENABLE;
10077
10078 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079
Siva Reddy Kallam4419bb12017-12-22 16:05:28 +053010080 /* On one of the AMD platform, MRRS is restricted to 4000 because of
10081 * south bridge limitation. As a workaround, Driver is setting MRRS
10082 * to 2048 instead of default 4096.
10083 */
10084 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
10085 tp->pdev->subsystem_device == TG3PCI_SUBDEVICE_ID_DELL_5762) {
10086 val = tr32(TG3PCI_DEV_STATUS_CTRL) & ~MAX_READ_REQ_MASK;
10087 tw32(TG3PCI_DEV_STATUS_CTRL, val | MAX_READ_REQ_SIZE_2048);
10088 }
10089
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090 /* Setup the timer prescalar register. Clock is always 66Mhz. */
10091 val = tr32(GRC_MISC_CFG);
10092 val &= ~0xff;
10093 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
10094 tw32(GRC_MISC_CFG, val);
10095
10096 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010097 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010099 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010101 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10103 else
10104 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10105 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10106 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010107 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108 int fw_len;
10109
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010110 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10112 tw32(BUFMGR_MB_POOL_ADDR,
10113 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10114 tw32(BUFMGR_MB_POOL_SIZE,
10115 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117
Michael Chan0f893dc2005-07-25 12:30:38 -070010118 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10120 tp->bufmgr_config.mbuf_read_dma_low_water);
10121 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10122 tp->bufmgr_config.mbuf_mac_rx_low_water);
10123 tw32(BUFMGR_MB_HIGH_WATER,
10124 tp->bufmgr_config.mbuf_high_water);
10125 } else {
10126 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10127 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10128 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10129 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10130 tw32(BUFMGR_MB_HIGH_WATER,
10131 tp->bufmgr_config.mbuf_high_water_jumbo);
10132 }
10133 tw32(BUFMGR_DMA_LOW_WATER,
10134 tp->bufmgr_config.dma_low_water);
10135 tw32(BUFMGR_DMA_HIGH_WATER,
10136 tp->bufmgr_config.dma_high_water);
10137
Matt Carlsond309a462010-09-30 10:34:31 +000010138 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010139 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010140 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010141 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010142 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010143 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10144 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010145 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010146 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147 for (i = 0; i < 2000; i++) {
10148 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10149 break;
10150 udelay(10);
10151 }
10152 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010153 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 return -ENODEV;
10155 }
10156
Joe Perches41535772013-02-16 11:20:04 +000010157 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010158 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010159
Matt Carlsoneb07a942011-04-20 07:57:36 +000010160 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161
10162 /* Initialize TG3_BDINFO's at:
10163 * RCVDBDI_STD_BD: standard eth size rx ring
10164 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10165 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10166 *
10167 * like so:
10168 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10169 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10170 * ring attribute flags
10171 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10172 *
10173 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10174 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10175 *
10176 * The size of each ring is fixed in the firmware, but the location is
10177 * configurable.
10178 */
10179 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010180 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010182 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010183 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010184 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10185 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010187 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010188 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10190 BDINFO_FLAGS_DISABLED);
10191
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010192 /* Program the jumbo buffer descriptor ring control
10193 * blocks on those devices that have them.
10194 */
Joe Perches41535772013-02-16 11:20:04 +000010195 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010196 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197
Joe Perches63c3a662011-04-26 08:12:10 +000010198 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010199 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010200 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010202 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010203 val = TG3_RX_JMB_RING_SIZE(tp) <<
10204 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010206 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010207 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010208 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010209 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010210 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10211 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212 } else {
10213 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10214 BDINFO_FLAGS_DISABLED);
10215 }
10216
Joe Perches63c3a662011-04-26 08:12:10 +000010217 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010218 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010219 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10220 val |= (TG3_RX_STD_DMA_SZ << 2);
10221 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010222 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010223 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010224 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010225
10226 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227
Matt Carlson411da642009-11-13 13:03:46 +000010228 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +000010229 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230
Joe Perches63c3a662011-04-26 08:12:10 +000010231 tpr->rx_jmb_prod_idx =
10232 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +000010233 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234
Matt Carlson2d31eca2009-09-01 12:53:31 +000010235 tg3_rings_reset(tp);
10236
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010238 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239
10240 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010241 tw32(MAC_RX_MTU_SIZE,
10242 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243
10244 /* The slot time is changed by tg3_setup_phy if we
10245 * run at gigabit with half duplex.
10246 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010247 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10248 (6 << TX_LENGTHS_IPG_SHIFT) |
10249 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10250
Joe Perches41535772013-02-16 11:20:04 +000010251 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10252 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010253 val |= tr32(MAC_TX_LENGTHS) &
10254 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10255 TX_LENGTHS_CNT_DWN_VAL_MSK);
10256
10257 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010258
10259 /* Receive rules. */
10260 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10261 tw32(RCVLPC_CONFIG, 0x0181);
10262
10263 /* Calculate RDMAC_MODE setting early, we need it to determine
10264 * the RCVLPC_STATE_ENABLE mask.
10265 */
10266 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10267 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10268 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10269 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10270 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010271
Joe Perches41535772013-02-16 11:20:04 +000010272 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010273 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10274
Joe Perches41535772013-02-16 11:20:04 +000010275 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10276 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10277 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010278 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10279 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10280 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10281
Joe Perches41535772013-02-16 11:20:04 +000010282 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10283 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010284 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010285 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10287 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010288 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10290 }
10291 }
10292
Joe Perches63c3a662011-04-26 08:12:10 +000010293 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010294 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10295
Joe Perches41535772013-02-16 11:20:04 +000010296 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010297 tp->dma_limit = 0;
10298 if (tp->dev->mtu <= ETH_DATA_LEN) {
10299 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10300 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10301 }
10302 }
10303
Joe Perches63c3a662011-04-26 08:12:10 +000010304 if (tg3_flag(tp, HW_TSO_1) ||
10305 tg3_flag(tp, HW_TSO_2) ||
10306 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010307 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10308
Matt Carlson108a6c12011-05-19 12:12:47 +000010309 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010310 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10311 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010312 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313
Joe Perches41535772013-02-16 11:20:04 +000010314 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10315 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010316 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10317
Joe Perches41535772013-02-16 11:20:04 +000010318 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10319 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10320 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10321 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010322 tg3_flag(tp, 57765_PLUS)) {
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_RDMA_RSRVCTRL_REG2;
10327 else
10328 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10329
10330 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010331 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10332 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010333 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10334 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10335 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10336 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10337 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10338 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010339 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010340 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010341 }
10342
Joe Perches41535772013-02-16 11:20:04 +000010343 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10344 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10345 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010346 u32 tgtreg;
10347
Joe Perches41535772013-02-16 11:20:04 +000010348 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010349 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10350 else
10351 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10352
10353 val = tr32(tgtreg);
10354 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010355 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10356 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10357 }
10358
Linus Torvalds1da177e2005-04-16 15:20:36 -070010359 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010360 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010361 val = tr32(RCVLPC_STATS_ENABLE);
10362 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10363 tw32(RCVLPC_STATS_ENABLE, val);
10364 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010365 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 val = tr32(RCVLPC_STATS_ENABLE);
10367 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10368 tw32(RCVLPC_STATS_ENABLE, val);
10369 } else {
10370 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10371 }
10372 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10373 tw32(SNDDATAI_STATSENAB, 0xffffff);
10374 tw32(SNDDATAI_STATSCTRL,
10375 (SNDDATAI_SCTRL_ENABLE |
10376 SNDDATAI_SCTRL_FASTUPD));
10377
10378 /* Setup host coalescing engine. */
10379 tw32(HOSTCC_MODE, 0);
10380 for (i = 0; i < 2000; i++) {
10381 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10382 break;
10383 udelay(10);
10384 }
10385
Michael Chand244c892005-07-05 14:42:33 -070010386 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387
Joe Perches63c3a662011-04-26 08:12:10 +000010388 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 /* Status/statistics block address. See tg3_timer,
10390 * the tg3_periodic_fetch_stats call there, and
10391 * tg3_get_stats to see how this works for 5705/5750 chips.
10392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10394 ((u64) tp->stats_mapping >> 32));
10395 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10396 ((u64) tp->stats_mapping & 0xffffffff));
10397 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010398
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010400
10401 /* Clear statistics and status block memory areas */
10402 for (i = NIC_SRAM_STATS_BLK;
10403 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10404 i += sizeof(u32)) {
10405 tg3_write_mem(tp, i, 0);
10406 udelay(40);
10407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010408 }
10409
10410 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10411
10412 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10413 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010414 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10416
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010417 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10418 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010419 /* reset to prevent losing 1st rx packet intermittently */
10420 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10421 udelay(10);
10422 }
10423
Matt Carlson3bda1252008-08-15 14:08:22 -070010424 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010425 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10426 MAC_MODE_FHDE_ENABLE;
10427 if (tg3_flag(tp, ENABLE_APE))
10428 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010429 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010430 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010431 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010432 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10434 udelay(40);
10435
Michael Chan314fba32005-04-21 17:07:04 -070010436 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010437 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010438 * register to preserve the GPIO settings for LOMs. The GPIOs,
10439 * whether used as inputs or outputs, are set by boot code after
10440 * reset.
10441 */
Joe Perches63c3a662011-04-26 08:12:10 +000010442 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010443 u32 gpio_mask;
10444
Michael Chan9d26e212006-12-07 00:21:14 -080010445 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10446 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10447 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010448
Joe Perches41535772013-02-16 11:20:04 +000010449 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010450 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10451 GRC_LCLCTRL_GPIO_OUTPUT3;
10452
Joe Perches41535772013-02-16 11:20:04 +000010453 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010454 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10455
Gary Zambranoaaf84462007-05-05 11:51:45 -070010456 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010457 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10458
10459 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010460 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010461 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10462 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10465 udelay(100);
10466
Matt Carlsonc3b50032012-01-17 15:27:23 +000010467 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010468 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010469 val |= MSGINT_MODE_ENABLE;
10470 if (tp->irq_cnt > 1)
10471 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010472 if (!tg3_flag(tp, 1SHOT_MSI))
10473 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010474 tw32(MSGINT_MODE, val);
10475 }
10476
Joe Perches63c3a662011-04-26 08:12:10 +000010477 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010478 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10479 udelay(40);
10480 }
10481
10482 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10483 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10484 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10485 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10486 WDMAC_MODE_LNGREAD_ENAB);
10487
Joe Perches41535772013-02-16 11:20:04 +000010488 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10489 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010490 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010491 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10492 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493 /* nothing */
10494 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010495 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010496 val |= WDMAC_MODE_RX_ACCEL;
10497 }
10498 }
10499
Michael Chand9ab5ad12006-03-20 22:27:35 -080010500 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010501 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010502 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010503
Joe Perches41535772013-02-16 11:20:04 +000010504 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010505 val |= WDMAC_MODE_BURST_ALL_DATA;
10506
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 tw32_f(WDMAC_MODE, val);
10508 udelay(40);
10509
Joe Perches63c3a662011-04-26 08:12:10 +000010510 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010511 u16 pcix_cmd;
10512
10513 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10514 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010515 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010516 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10517 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010518 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010519 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10520 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521 }
Matt Carlson9974a352007-10-07 23:27:28 -070010522 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10523 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524 }
10525
10526 tw32_f(RDMAC_MODE, rdmac_mode);
10527 udelay(40);
10528
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010529 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10530 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010531 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10532 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10533 break;
10534 }
10535 if (i < TG3_NUM_RDMA_CHANNELS) {
10536 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010537 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010538 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010539 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010540 }
10541 }
10542
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010544 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010546
Joe Perches41535772013-02-16 11:20:04 +000010547 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010548 tw32(SNDDATAC_MODE,
10549 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10550 else
10551 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10552
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10554 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010555 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010556 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010557 val |= RCVDBDI_MODE_LRG_RING_SZ;
10558 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010559 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010560 if (tg3_flag(tp, HW_TSO_1) ||
10561 tg3_flag(tp, HW_TSO_2) ||
10562 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010563 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010564 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010565 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010566 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10567 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010568 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10569
Joe Perches41535772013-02-16 11:20:04 +000010570 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010571 err = tg3_load_5701_a0_firmware_fix(tp);
10572 if (err)
10573 return err;
10574 }
10575
Nithin Sujirc4dab502013-03-06 17:02:34 +000010576 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10577 /* Ignore any errors for the firmware download. If download
10578 * fails, the device will operate with EEE disabled
10579 */
10580 tg3_load_57766_firmware(tp);
10581 }
10582
Joe Perches63c3a662011-04-26 08:12:10 +000010583 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584 err = tg3_load_tso_firmware(tp);
10585 if (err)
10586 return err;
10587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010588
10589 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010590
Joe Perches63c3a662011-04-26 08:12:10 +000010591 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010592 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010593 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010594
Joe Perches41535772013-02-16 11:20:04 +000010595 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10596 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010597 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10598 tp->tx_mode &= ~val;
10599 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10600 }
10601
Linus Torvalds1da177e2005-04-16 15:20:36 -070010602 tw32_f(MAC_TX_MODE, tp->tx_mode);
10603 udelay(100);
10604
Joe Perches63c3a662011-04-26 08:12:10 +000010605 if (tg3_flag(tp, ENABLE_RSS)) {
Eric Dumazet39648352014-11-16 06:23:08 -080010606 u32 rss_key[10];
10607
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010608 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010609
Eric Dumazet39648352014-11-16 06:23:08 -080010610 netdev_rss_key_fill(rss_key, 10 * sizeof(u32));
10611
10612 for (i = 0; i < 10 ; i++)
10613 tw32(MAC_RSS_HASH_KEY_0 + i*4, rss_key[i]);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010614 }
10615
Linus Torvalds1da177e2005-04-16 15:20:36 -070010616 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010617 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010618 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10619
Nithin Sujir378b72c2013-07-29 13:58:39 -070010620 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10621 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10622
Joe Perches63c3a662011-04-26 08:12:10 +000010623 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010624 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10625 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10626 RX_MODE_RSS_IPV6_HASH_EN |
10627 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10628 RX_MODE_RSS_IPV4_HASH_EN |
10629 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10630
Linus Torvalds1da177e2005-04-16 15:20:36 -070010631 tw32_f(MAC_RX_MODE, tp->rx_mode);
10632 udelay(10);
10633
Linus Torvalds1da177e2005-04-16 15:20:36 -070010634 tw32(MAC_LED_CTRL, tp->led_ctrl);
10635
10636 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010637 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010638 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10639 udelay(10);
10640 }
10641 tw32_f(MAC_RX_MODE, tp->rx_mode);
10642 udelay(10);
10643
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010644 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010645 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10646 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647 /* Set drive transmission level to 1.2V */
10648 /* only if the signal pre-emphasis bit is not set */
10649 val = tr32(MAC_SERDES_CFG);
10650 val &= 0xfffff000;
10651 val |= 0x880;
10652 tw32(MAC_SERDES_CFG, val);
10653 }
Joe Perches41535772013-02-16 11:20:04 +000010654 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010655 tw32(MAC_SERDES_CFG, 0x616000);
10656 }
10657
10658 /* Prevent chip from dropping frames when flow control
10659 * is enabled.
10660 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010661 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010662 val = 1;
10663 else
10664 val = 2;
10665 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666
Joe Perches41535772013-02-16 11:20:04 +000010667 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010668 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010670 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010671 }
10672
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010673 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010674 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010675 u32 tmp;
10676
10677 tmp = tr32(SERDES_RX_CTRL);
10678 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10679 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10680 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10681 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10682 }
10683
Joe Perches63c3a662011-04-26 08:12:10 +000010684 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010685 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010686 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687
Joe Perches953c96e2013-04-09 10:18:14 +000010688 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010689 if (err)
10690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010692 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10693 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010694 u32 tmp;
10695
10696 /* Clear CRC stats. */
10697 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10698 tg3_writephy(tp, MII_TG3_TEST1,
10699 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010700 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702 }
10703 }
10704
10705 __tg3_set_rx_mode(tp->dev);
10706
10707 /* Initialize receive rules. */
10708 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10709 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10710 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10711 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10712
Joe Perches63c3a662011-04-26 08:12:10 +000010713 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714 limit = 8;
10715 else
10716 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010717 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718 limit -= 4;
10719 switch (limit) {
10720 case 16:
10721 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10722 case 15:
10723 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10724 case 14:
10725 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10726 case 13:
10727 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10728 case 12:
10729 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10730 case 11:
10731 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10732 case 10:
10733 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10734 case 9:
10735 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10736 case 8:
10737 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10738 case 7:
10739 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10740 case 6:
10741 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10742 case 5:
10743 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10744 case 4:
10745 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10746 case 3:
10747 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10748 case 2:
10749 case 1:
10750
10751 default:
10752 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010754
Joe Perches63c3a662011-04-26 08:12:10 +000010755 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010756 /* Write our heartbeat update interval to APE. */
10757 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053010758 APE_HOST_HEARTBEAT_INT_5SEC);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010759
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10761
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 return 0;
10763}
10764
10765/* Called at device open time to get the chip ready for
10766 * packet processing. Invoked with tp->lock held.
10767 */
Joe Perches953c96e2013-04-09 10:18:14 +000010768static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010769{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010770 /* Chip may have been just powered on. If so, the boot code may still
10771 * be running initialization. Wait for it to finish to avoid races in
10772 * accessing the hardware.
10773 */
10774 tg3_enable_register_access(tp);
10775 tg3_poll_fw(tp);
10776
Linus Torvalds1da177e2005-04-16 15:20:36 -070010777 tg3_switch_clocks(tp);
10778
10779 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10780
Matt Carlson2f751b62008-08-04 23:17:34 -070010781 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010782}
10783
Florian Fainelli038e8932017-03-06 12:56:02 -080010784#ifdef CONFIG_TIGON3_HWMON
Michael Chanaed93e02012-07-16 16:24:02 +000010785static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10786{
10787 int i;
10788
10789 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10790 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10791
10792 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10793 off += len;
10794
10795 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10796 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10797 memset(ocir, 0, TG3_OCIR_LEN);
10798 }
10799}
10800
10801/* sysfs attributes for hwmon */
10802static ssize_t tg3_show_temp(struct device *dev,
10803 struct device_attribute *devattr, char *buf)
10804{
Michael Chanaed93e02012-07-16 16:24:02 +000010805 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010806 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010807 u32 temperature;
10808
10809 spin_lock_bh(&tp->lock);
10810 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10811 sizeof(temperature));
10812 spin_unlock_bh(&tp->lock);
Jean Delvared3d11fe2015-09-01 18:07:41 +020010813 return sprintf(buf, "%u\n", temperature * 1000);
Michael Chanaed93e02012-07-16 16:24:02 +000010814}
10815
10816
Joe Perchesd3757ba2018-03-23 16:34:44 -070010817static SENSOR_DEVICE_ATTR(temp1_input, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010818 TG3_TEMP_SENSOR_OFFSET);
Joe Perchesd3757ba2018-03-23 16:34:44 -070010819static SENSOR_DEVICE_ATTR(temp1_crit, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010820 TG3_TEMP_CAUTION_OFFSET);
Joe Perchesd3757ba2018-03-23 16:34:44 -070010821static SENSOR_DEVICE_ATTR(temp1_max, 0444, tg3_show_temp, NULL,
Michael Chanaed93e02012-07-16 16:24:02 +000010822 TG3_TEMP_MAX_OFFSET);
10823
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010824static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010825 &sensor_dev_attr_temp1_input.dev_attr.attr,
10826 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10827 &sensor_dev_attr_temp1_max.dev_attr.attr,
10828 NULL
10829};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010830ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010831
Michael Chanaed93e02012-07-16 16:24:02 +000010832static void tg3_hwmon_close(struct tg3 *tp)
10833{
Michael Chanaed93e02012-07-16 16:24:02 +000010834 if (tp->hwmon_dev) {
10835 hwmon_device_unregister(tp->hwmon_dev);
10836 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010837 }
Michael Chanaed93e02012-07-16 16:24:02 +000010838}
10839
10840static void tg3_hwmon_open(struct tg3 *tp)
10841{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010842 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010843 u32 size = 0;
10844 struct pci_dev *pdev = tp->pdev;
10845 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10846
10847 tg3_sd_scan_scratchpad(tp, ocirs);
10848
10849 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10850 if (!ocirs[i].src_data_length)
10851 continue;
10852
10853 size += ocirs[i].src_hdr_length;
10854 size += ocirs[i].src_data_length;
10855 }
10856
10857 if (!size)
10858 return;
10859
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010860 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10861 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010862 if (IS_ERR(tp->hwmon_dev)) {
10863 tp->hwmon_dev = NULL;
10864 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010865 }
Michael Chanaed93e02012-07-16 16:24:02 +000010866}
Florian Fainelli038e8932017-03-06 12:56:02 -080010867#else
10868static inline void tg3_hwmon_close(struct tg3 *tp) { }
10869static inline void tg3_hwmon_open(struct tg3 *tp) { }
10870#endif /* CONFIG_TIGON3_HWMON */
Michael Chanaed93e02012-07-16 16:24:02 +000010871
10872
Linus Torvalds1da177e2005-04-16 15:20:36 -070010873#define TG3_STAT_ADD32(PSTAT, REG) \
10874do { u32 __val = tr32(REG); \
10875 (PSTAT)->low += __val; \
10876 if ((PSTAT)->low < __val) \
10877 (PSTAT)->high += 1; \
10878} while (0)
10879
10880static void tg3_periodic_fetch_stats(struct tg3 *tp)
10881{
10882 struct tg3_hw_stats *sp = tp->hw_stats;
10883
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010884 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885 return;
10886
10887 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10888 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10889 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10890 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10891 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10892 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10893 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10894 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10895 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10896 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10897 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10898 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10899 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010900 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010901 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10902 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10903 u32 val;
10904
10905 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010906 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010907 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010908 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910
10911 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10912 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10913 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10914 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10915 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10916 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10917 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10918 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10919 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10920 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10921 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10922 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10923 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10924 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010925
10926 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010927 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010928 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010929 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10930 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010931 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10932 } else {
10933 u32 val = tr32(HOSTCC_FLOW_ATTN);
10934 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10935 if (val) {
10936 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10937 sp->rx_discards.low += val;
10938 if (sp->rx_discards.low < val)
10939 sp->rx_discards.high += 1;
10940 }
10941 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10942 }
Michael Chan463d3052006-05-22 16:36:27 -070010943 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010944}
10945
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010946static void tg3_chk_missed_msi(struct tg3 *tp)
10947{
10948 u32 i;
10949
10950 for (i = 0; i < tp->irq_cnt; i++) {
10951 struct tg3_napi *tnapi = &tp->napi[i];
10952
10953 if (tg3_has_work(tnapi)) {
10954 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10955 tnapi->last_tx_cons == tnapi->tx_cons) {
10956 if (tnapi->chk_msi_cnt < 1) {
10957 tnapi->chk_msi_cnt++;
10958 return;
10959 }
Matt Carlson7f230732011-08-31 11:44:48 +000010960 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010961 }
10962 }
10963 tnapi->chk_msi_cnt = 0;
10964 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10965 tnapi->last_tx_cons = tnapi->tx_cons;
10966 }
10967}
10968
Kees Cooke99e88a2017-10-16 14:43:17 -070010969static void tg3_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970{
Kees Cooke99e88a2017-10-16 14:43:17 -070010971 struct tg3 *tp = from_timer(tp, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010972
David S. Millerf47c11e2005-06-24 20:18:35 -070010973 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010974
Prashant Sreedharan4fd190a2015-01-14 11:33:49 -080010975 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING)) {
10976 spin_unlock(&tp->lock);
10977 goto restart_timer;
10978 }
10979
Joe Perches41535772013-02-16 11:20:04 +000010980 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010981 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010982 tg3_chk_missed_msi(tp);
10983
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010984 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10985 /* BCM4785: Flush posted writes from GbE to host memory. */
10986 tr32(HOSTCC_MODE);
10987 }
10988
Joe Perches63c3a662011-04-26 08:12:10 +000010989 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010990 /* All of this garbage is because when using non-tagged
10991 * IRQ status the mailbox/status_block protocol the chip
10992 * uses with the cpu is race prone.
10993 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010994 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010995 tw32(GRC_LOCAL_CTRL,
10996 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10997 } else {
10998 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010999 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070011000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011001
David S. Millerfac9b832005-05-18 22:46:34 -070011002 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011003 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000011004 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000011005 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070011006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011007 }
11008
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 /* This part only runs once per second. */
11010 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011011 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070011012 tg3_periodic_fetch_stats(tp);
11013
Matt Carlsonb0c59432011-05-19 12:12:48 +000011014 if (tp->setlpicnt && !--tp->setlpicnt)
11015 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000011016
Joe Perches63c3a662011-04-26 08:12:10 +000011017 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011018 u32 mac_stat;
11019 int phy_event;
11020
11021 mac_stat = tr32(MAC_STATUS);
11022
11023 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011024 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011025 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
11026 phy_event = 1;
11027 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
11028 phy_event = 1;
11029
11030 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000011031 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000011032 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011033 u32 mac_stat = tr32(MAC_STATUS);
11034 int need_setup = 0;
11035
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011036 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011037 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
11038 need_setup = 1;
11039 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011040 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011041 (mac_stat & (MAC_STATUS_PCS_SYNCED |
11042 MAC_STATUS_SIGNAL_DET))) {
11043 need_setup = 1;
11044 }
11045 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070011046 if (!tp->serdes_counter) {
11047 tw32_f(MAC_MODE,
11048 (tp->mac_mode &
11049 ~MAC_MODE_PORT_MODE_MASK));
11050 udelay(40);
11051 tw32_f(MAC_MODE, tp->mac_mode);
11052 udelay(40);
11053 }
Joe Perches953c96e2013-04-09 10:18:14 +000011054 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011055 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011056 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011057 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070011058 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080011059 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
11060 u32 cpmu = tr32(TG3_CPMU_STATUS);
11061 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
11062 TG3_CPMU_STATUS_LINK_MASK);
11063
11064 if (link_up != tp->link_up)
11065 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000011066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067
11068 tp->timer_counter = tp->timer_multiplier;
11069 }
11070
Michael Chan130b8e42006-09-27 16:00:40 -070011071 /* Heartbeat is only sent once every 2 seconds.
11072 *
11073 * The heartbeat is to tell the ASF firmware that the host
11074 * driver is still alive. In the event that the OS crashes,
11075 * ASF needs to reset the hardware to free up the FIFO space
11076 * that may be filled with rx packets destined for the host.
11077 * If the FIFO is full, ASF will no longer function properly.
11078 *
11079 * Unintended resets have been reported on real time kernels
11080 * where the timer doesn't run on time. Netpoll will also have
11081 * same problem.
11082 *
11083 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
11084 * to check the ring condition when the heartbeat is expiring
11085 * before doing the reset. This will prevent most unintended
11086 * resets.
11087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000011089 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070011090 tg3_wait_for_event_ack(tp);
11091
Michael Chanbbadf502006-04-06 21:46:34 -070011092 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070011093 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070011094 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011095 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
11096 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011097
11098 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011099 }
11100 tp->asf_counter = tp->asf_multiplier;
11101 }
11102
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053011103 /* Update the APE heartbeat every 5 seconds.*/
11104 tg3_send_ape_heartbeat(tp, TG3_APE_HB_INTERVAL);
11105
David S. Millerf47c11e2005-06-24 20:18:35 -070011106 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107
Michael Chanf475f162006-03-27 23:20:14 -080011108restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 tp->timer.expires = jiffies + tp->timer_offset;
11110 add_timer(&tp->timer);
11111}
11112
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011113static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011114{
11115 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011116 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011117 !tg3_flag(tp, 57765_CLASS))
11118 tp->timer_offset = HZ;
11119 else
11120 tp->timer_offset = HZ / 10;
11121
11122 BUG_ON(tp->timer_offset > HZ);
11123
11124 tp->timer_multiplier = (HZ / tp->timer_offset);
11125 tp->asf_multiplier = (HZ / tp->timer_offset) *
11126 TG3_FW_UPDATE_FREQ_SEC;
11127
Kees Cooke99e88a2017-10-16 14:43:17 -070011128 timer_setup(&tp->timer, tg3_timer, 0);
Matt Carlson21f76382012-02-22 12:35:21 +000011129}
11130
11131static void tg3_timer_start(struct tg3 *tp)
11132{
11133 tp->asf_counter = tp->asf_multiplier;
11134 tp->timer_counter = tp->timer_multiplier;
11135
11136 tp->timer.expires = jiffies + tp->timer_offset;
11137 add_timer(&tp->timer);
11138}
11139
11140static void tg3_timer_stop(struct tg3 *tp)
11141{
11142 del_timer_sync(&tp->timer);
11143}
11144
11145/* Restart hardware after configuration changes, self-test, etc.
11146 * Invoked with tp->lock held.
11147 */
Joe Perches953c96e2013-04-09 10:18:14 +000011148static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011149 __releases(tp->lock)
11150 __acquires(tp->lock)
11151{
11152 int err;
11153
11154 err = tg3_init_hw(tp, reset_phy);
11155 if (err) {
11156 netdev_err(tp->dev,
11157 "Failed to re-initialize device, aborting\n");
11158 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11159 tg3_full_unlock(tp);
11160 tg3_timer_stop(tp);
11161 tp->irq_sync = 0;
11162 tg3_napi_enable(tp);
11163 dev_close(tp->dev);
11164 tg3_full_lock(tp, 0);
11165 }
11166 return err;
11167}
11168
11169static void tg3_reset_task(struct work_struct *work)
11170{
11171 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11172 int err;
11173
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011174 rtnl_lock();
Matt Carlson21f76382012-02-22 12:35:21 +000011175 tg3_full_lock(tp, 0);
11176
11177 if (!netif_running(tp->dev)) {
11178 tg3_flag_clear(tp, RESET_TASK_PENDING);
11179 tg3_full_unlock(tp);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011180 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011181 return;
11182 }
11183
11184 tg3_full_unlock(tp);
11185
11186 tg3_phy_stop(tp);
11187
11188 tg3_netif_stop(tp);
11189
11190 tg3_full_lock(tp, 1);
11191
11192 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11193 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11194 tp->write32_rx_mbox = tg3_write_flush_reg32;
11195 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11196 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11197 }
11198
11199 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011200 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011201 if (err)
11202 goto out;
11203
11204 tg3_netif_start(tp);
11205
11206out:
11207 tg3_full_unlock(tp);
11208
11209 if (!err)
11210 tg3_phy_start(tp);
11211
11212 tg3_flag_clear(tp, RESET_TASK_PENDING);
Prashant Sreedharandb84bf42015-01-14 11:34:17 -080011213 rtnl_unlock();
Matt Carlson21f76382012-02-22 12:35:21 +000011214}
11215
Matt Carlson4f125f42009-09-01 12:55:02 +000011216static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011217{
David Howells7d12e782006-10-05 14:55:46 +010011218 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011219 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011220 char *name;
11221 struct tg3_napi *tnapi = &tp->napi[irq_num];
11222
11223 if (tp->irq_cnt == 1)
11224 name = tp->dev->name;
11225 else {
11226 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011227 if (tnapi->tx_buffers && tnapi->rx_rcb)
11228 snprintf(name, IFNAMSIZ,
11229 "%s-txrx-%d", tp->dev->name, irq_num);
11230 else if (tnapi->tx_buffers)
11231 snprintf(name, IFNAMSIZ,
11232 "%s-tx-%d", tp->dev->name, irq_num);
11233 else if (tnapi->rx_rcb)
11234 snprintf(name, IFNAMSIZ,
11235 "%s-rx-%d", tp->dev->name, irq_num);
11236 else
11237 snprintf(name, IFNAMSIZ,
11238 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011239 name[IFNAMSIZ-1] = 0;
11240 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011241
Joe Perches63c3a662011-04-26 08:12:10 +000011242 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011243 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011244 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011245 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011246 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011247 } else {
11248 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011249 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011250 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011251 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011252 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011253
11254 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011255}
11256
Michael Chan79381092005-04-21 17:13:59 -070011257static int tg3_test_interrupt(struct tg3 *tp)
11258{
Matt Carlson09943a12009-08-28 14:01:57 +000011259 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011260 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011261 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011262 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011263
Michael Chand4bc3922005-05-29 14:59:20 -070011264 if (!netif_running(dev))
11265 return -ENODEV;
11266
Michael Chan79381092005-04-21 17:13:59 -070011267 tg3_disable_ints(tp);
11268
Matt Carlson4f125f42009-09-01 12:55:02 +000011269 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011270
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011271 /*
11272 * Turn off MSI one shot mode. Otherwise this test has no
11273 * observable way to know whether the interrupt was delivered.
11274 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011275 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011276 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11277 tw32(MSGINT_MODE, val);
11278 }
11279
Matt Carlson4f125f42009-09-01 12:55:02 +000011280 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011281 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011282 if (err)
11283 return err;
11284
Matt Carlson898a56f2009-08-28 14:02:40 +000011285 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011286 tg3_enable_ints(tp);
11287
11288 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011289 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011290
11291 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011292 u32 int_mbox, misc_host_ctrl;
11293
Matt Carlson898a56f2009-08-28 14:02:40 +000011294 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011295 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11296
11297 if ((int_mbox != 0) ||
11298 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11299 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011300 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011301 }
11302
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011303 if (tg3_flag(tp, 57765_PLUS) &&
11304 tnapi->hw_status->status_tag != tnapi->last_tag)
11305 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11306
Michael Chan79381092005-04-21 17:13:59 -070011307 msleep(10);
11308 }
11309
11310 tg3_disable_ints(tp);
11311
Matt Carlson4f125f42009-09-01 12:55:02 +000011312 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011313
Matt Carlson4f125f42009-09-01 12:55:02 +000011314 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011315
11316 if (err)
11317 return err;
11318
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011319 if (intr_ok) {
11320 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011321 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011322 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11323 tw32(MSGINT_MODE, val);
11324 }
Michael Chan79381092005-04-21 17:13:59 -070011325 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011326 }
Michael Chan79381092005-04-21 17:13:59 -070011327
11328 return -EIO;
11329}
11330
11331/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11332 * successfully restored
11333 */
11334static int tg3_test_msi(struct tg3 *tp)
11335{
Michael Chan79381092005-04-21 17:13:59 -070011336 int err;
11337 u16 pci_cmd;
11338
Joe Perches63c3a662011-04-26 08:12:10 +000011339 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011340 return 0;
11341
11342 /* Turn off SERR reporting in case MSI terminates with Master
11343 * Abort.
11344 */
11345 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11346 pci_write_config_word(tp->pdev, PCI_COMMAND,
11347 pci_cmd & ~PCI_COMMAND_SERR);
11348
11349 err = tg3_test_interrupt(tp);
11350
11351 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11352
11353 if (!err)
11354 return 0;
11355
11356 /* other failures */
11357 if (err != -EIO)
11358 return err;
11359
11360 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011361 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11362 "to INTx mode. Please report this failure to the PCI "
11363 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011364
Matt Carlson4f125f42009-09-01 12:55:02 +000011365 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011366
Michael Chan79381092005-04-21 17:13:59 -070011367 pci_disable_msi(tp->pdev);
11368
Joe Perches63c3a662011-04-26 08:12:10 +000011369 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011370 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011371
Matt Carlson4f125f42009-09-01 12:55:02 +000011372 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011373 if (err)
11374 return err;
11375
11376 /* Need to reset the chip because the MSI cycle may have terminated
11377 * with Master Abort.
11378 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011379 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011380
Michael Chan944d9802005-05-29 14:57:48 -070011381 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011382 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011383
David S. Millerf47c11e2005-06-24 20:18:35 -070011384 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011385
11386 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011387 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011388
11389 return err;
11390}
11391
Matt Carlson9e9fd122009-01-19 16:57:45 -080011392static int tg3_request_firmware(struct tg3 *tp)
11393{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011394 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011395
11396 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011397 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11398 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011399 return -ENOENT;
11400 }
11401
Nithin Sujir77997ea2013-03-06 17:02:32 +000011402 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011403
11404 /* Firmware blob starts with version numbers, followed by
11405 * start address and _full_ length including BSS sections
11406 * (which must be longer than the actual data, of course
11407 */
11408
Nithin Sujir77997ea2013-03-06 17:02:32 +000011409 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11410 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011411 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11412 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011413 release_firmware(tp->fw);
11414 tp->fw = NULL;
11415 return -EINVAL;
11416 }
11417
11418 /* We no longer need firmware; we have it. */
11419 tp->fw_needed = NULL;
11420 return 0;
11421}
11422
Michael Chan91024262012-09-28 07:12:38 +000011423static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011424{
Michael Chan91024262012-09-28 07:12:38 +000011425 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011426
Michael Chan91024262012-09-28 07:12:38 +000011427 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011428 /* We want as many rx rings enabled as there are cpus.
11429 * In multiqueue MSI-X mode, the first MSI-X vector
11430 * only deals with link interrupts, etc, so we add
11431 * one to the number of vectors we are requesting.
11432 */
Michael Chan91024262012-09-28 07:12:38 +000011433 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011434 }
Matt Carlson679563f2009-09-01 12:55:46 +000011435
Michael Chan91024262012-09-28 07:12:38 +000011436 return irq_cnt;
11437}
11438
11439static bool tg3_enable_msix(struct tg3 *tp)
11440{
11441 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011442 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011443
Michael Chan09681692012-09-28 07:12:42 +000011444 tp->txq_cnt = tp->txq_req;
11445 tp->rxq_cnt = tp->rxq_req;
11446 if (!tp->rxq_cnt)
11447 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011448 if (tp->rxq_cnt > tp->rxq_max)
11449 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011450
11451 /* Disable multiple TX rings by default. Simple round-robin hardware
11452 * scheduling of the TX rings can cause starvation of rings with
11453 * small packets when other rings have TSO or jumbo packets.
11454 */
11455 if (!tp->txq_req)
11456 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011457
11458 tp->irq_cnt = tg3_irq_count(tp);
11459
Matt Carlson679563f2009-09-01 12:55:46 +000011460 for (i = 0; i < tp->irq_max; i++) {
11461 msix_ent[i].entry = i;
11462 msix_ent[i].vector = 0;
11463 }
11464
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011465 rc = pci_enable_msix_range(tp->pdev, msix_ent, 1, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011466 if (rc < 0) {
11467 return false;
Alexander Gordeev6f1f4112014-02-18 11:07:55 +010011468 } else if (rc < tp->irq_cnt) {
Joe Perches05dbe002010-02-17 19:44:19 +000011469 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11470 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011471 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011472 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011473 if (tp->txq_cnt)
11474 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011475 }
11476
11477 for (i = 0; i < tp->irq_max; i++)
11478 tp->napi[i].irq_vec = msix_ent[i].vector;
11479
Michael Chan49a359e2012-09-28 07:12:37 +000011480 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011481 pci_disable_msix(tp->pdev);
11482 return false;
11483 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011484
Michael Chan91024262012-09-28 07:12:38 +000011485 if (tp->irq_cnt == 1)
11486 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011487
Michael Chan91024262012-09-28 07:12:38 +000011488 tg3_flag_set(tp, ENABLE_RSS);
11489
11490 if (tp->txq_cnt > 1)
11491 tg3_flag_set(tp, ENABLE_TSS);
11492
11493 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011494
Matt Carlson679563f2009-09-01 12:55:46 +000011495 return true;
11496}
11497
Matt Carlson07b01732009-08-28 14:01:15 +000011498static void tg3_ints_init(struct tg3 *tp)
11499{
Joe Perches63c3a662011-04-26 08:12:10 +000011500 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11501 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011502 /* All MSI supporting chips should support tagged
11503 * status. Assert that this is the case.
11504 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011505 netdev_warn(tp->dev,
11506 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011507 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011508 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011509
Joe Perches63c3a662011-04-26 08:12:10 +000011510 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11511 tg3_flag_set(tp, USING_MSIX);
11512 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11513 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011514
Joe Perches63c3a662011-04-26 08:12:10 +000011515 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011516 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011517 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011518 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011519 if (!tg3_flag(tp, 1SHOT_MSI))
11520 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011521 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11522 }
11523defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011524 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011525 tp->irq_cnt = 1;
11526 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011527 }
11528
11529 if (tp->irq_cnt == 1) {
11530 tp->txq_cnt = 1;
11531 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011532 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011533 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011534 }
Matt Carlson07b01732009-08-28 14:01:15 +000011535}
11536
11537static void tg3_ints_fini(struct tg3 *tp)
11538{
Joe Perches63c3a662011-04-26 08:12:10 +000011539 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011540 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011541 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011542 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011543 tg3_flag_clear(tp, USING_MSI);
11544 tg3_flag_clear(tp, USING_MSIX);
11545 tg3_flag_clear(tp, ENABLE_RSS);
11546 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011547}
11548
Matt Carlsonbe947302012-12-03 19:36:57 +000011549static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11550 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551{
Michael Chand8f4cd32012-09-28 07:12:40 +000011552 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011553 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554
Matt Carlson679563f2009-09-01 12:55:46 +000011555 /*
11556 * Setup interrupts first so we know how
11557 * many NAPI resources to allocate
11558 */
11559 tg3_ints_init(tp);
11560
Matt Carlson90415472011-12-16 13:33:23 +000011561 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011562
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563 /* The placement of this call is tied
11564 * to the setup and use of Host TX descriptors.
11565 */
11566 err = tg3_alloc_consistent(tp);
11567 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011568 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011570 tg3_napi_init(tp);
11571
Matt Carlsonfed97812009-09-01 13:10:19 +000011572 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011573
Matt Carlson4f125f42009-09-01 12:55:02 +000011574 for (i = 0; i < tp->irq_cnt; i++) {
Matt Carlson4f125f42009-09-01 12:55:02 +000011575 err = tg3_request_irq(tp, i);
11576 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011577 for (i--; i >= 0; i--) {
Colin Ian King23f48222017-09-14 17:01:25 +010011578 struct tg3_napi *tnapi = &tp->napi[i];
11579
Matt Carlson4f125f42009-09-01 12:55:02 +000011580 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011581 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011582 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011583 }
11584 }
Matt Carlson07b01732009-08-28 14:01:15 +000011585
David S. Millerf47c11e2005-06-24 20:18:35 -070011586 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011587
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011588 if (init)
11589 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11590
Michael Chand8f4cd32012-09-28 07:12:40 +000011591 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011593 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011595 }
11596
David S. Millerf47c11e2005-06-24 20:18:35 -070011597 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598
Matt Carlson07b01732009-08-28 14:01:15 +000011599 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011600 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601
Michael Chand8f4cd32012-09-28 07:12:40 +000011602 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011603 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011604
Michael Chan79381092005-04-21 17:13:59 -070011605 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011606 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011607 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011608 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011609 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011610
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011611 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011612 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011613
Joe Perches63c3a662011-04-26 08:12:10 +000011614 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011615 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011616
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011617 tw32(PCIE_TRANSACTION_CFG,
11618 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011619 }
Michael Chan79381092005-04-21 17:13:59 -070011620 }
11621
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011622 tg3_phy_start(tp);
11623
Michael Chanaed93e02012-07-16 16:24:02 +000011624 tg3_hwmon_open(tp);
11625
David S. Millerf47c11e2005-06-24 20:18:35 -070011626 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011627
Matt Carlson21f76382012-02-22 12:35:21 +000011628 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011629 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011630 tg3_enable_ints(tp);
11631
Ivan Vecera20d14a52015-01-08 16:13:07 +010011632 tg3_ptp_resume(tp);
Matt Carlsonbe947302012-12-03 19:36:57 +000011633
David S. Millerf47c11e2005-06-24 20:18:35 -070011634 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011636 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011637
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011638 /*
11639 * Reset loopback feature if it was turned on while the device was down
11640 * make sure that it's installed properly now.
11641 */
11642 if (dev->features & NETIF_F_LOOPBACK)
11643 tg3_set_loopback(dev, dev->features);
11644
Linus Torvalds1da177e2005-04-16 15:20:36 -070011645 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011646
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011647out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011648 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11649 struct tg3_napi *tnapi = &tp->napi[i];
11650 free_irq(tnapi->irq_vec, tnapi);
11651 }
Matt Carlson07b01732009-08-28 14:01:15 +000011652
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011653out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011654 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011655 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011656 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011657
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011658out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011659 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011660
Matt Carlson07b01732009-08-28 14:01:15 +000011661 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011662}
11663
Michael Chan65138592012-09-28 07:12:41 +000011664static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011665{
Matt Carlson4f125f42009-09-01 12:55:02 +000011666 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667
Matt Carlsondb219972011-11-04 09:15:03 +000011668 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011669 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011670
Matt Carlson21f76382012-02-22 12:35:21 +000011671 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672
Michael Chanaed93e02012-07-16 16:24:02 +000011673 tg3_hwmon_close(tp);
11674
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011675 tg3_phy_stop(tp);
11676
David S. Millerf47c11e2005-06-24 20:18:35 -070011677 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011678
11679 tg3_disable_ints(tp);
11680
Michael Chan944d9802005-05-29 14:57:48 -070011681 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011683 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684
David S. Millerf47c11e2005-06-24 20:18:35 -070011685 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011686
Matt Carlson4f125f42009-09-01 12:55:02 +000011687 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11688 struct tg3_napi *tnapi = &tp->napi[i];
11689 free_irq(tnapi->irq_vec, tnapi);
11690 }
Matt Carlson07b01732009-08-28 14:01:15 +000011691
11692 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011693
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011694 tg3_napi_fini(tp);
11695
Linus Torvalds1da177e2005-04-16 15:20:36 -070011696 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011697}
11698
Michael Chand8f4cd32012-09-28 07:12:40 +000011699static int tg3_open(struct net_device *dev)
11700{
11701 struct tg3 *tp = netdev_priv(dev);
11702 int err;
11703
Ivan Vecera0486a062014-09-01 14:21:57 +020011704 if (tp->pcierr_recovery) {
11705 netdev_err(dev, "Failed to open device. PCI error recovery "
11706 "in progress\n");
11707 return -EAGAIN;
11708 }
11709
Michael Chand8f4cd32012-09-28 07:12:40 +000011710 if (tp->fw_needed) {
11711 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011712 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11713 if (err) {
11714 netdev_warn(tp->dev, "EEE capability disabled\n");
11715 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11716 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11717 netdev_warn(tp->dev, "EEE capability restored\n");
11718 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11719 }
11720 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011721 if (err)
11722 return err;
11723 } else if (err) {
11724 netdev_warn(tp->dev, "TSO capability disabled\n");
11725 tg3_flag_clear(tp, TSO_CAPABLE);
11726 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11727 netdev_notice(tp->dev, "TSO capability restored\n");
11728 tg3_flag_set(tp, TSO_CAPABLE);
11729 }
11730 }
11731
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011732 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011733
11734 err = tg3_power_up(tp);
11735 if (err)
11736 return err;
11737
11738 tg3_full_lock(tp, 0);
11739
11740 tg3_disable_ints(tp);
11741 tg3_flag_clear(tp, INIT_COMPLETE);
11742
11743 tg3_full_unlock(tp);
11744
Nithin Sujir942d1af2013-04-09 08:48:07 +000011745 err = tg3_start(tp,
11746 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11747 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011748 if (err) {
11749 tg3_frob_aux_power(tp, false);
11750 pci_set_power_state(tp->pdev, PCI_D3hot);
11751 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011752
Linus Torvalds1da177e2005-04-16 15:20:36 -070011753 return err;
11754}
11755
11756static int tg3_close(struct net_device *dev)
11757{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011758 struct tg3 *tp = netdev_priv(dev);
11759
Ivan Vecera0486a062014-09-01 14:21:57 +020011760 if (tp->pcierr_recovery) {
11761 netdev_err(dev, "Failed to close device. PCI error recovery "
11762 "in progress\n");
11763 return -EAGAIN;
11764 }
11765
Michael Chan65138592012-09-28 07:12:41 +000011766 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011767
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011768 if (pci_device_is_present(tp->pdev)) {
11769 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011770
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011771 tg3_carrier_off(tp);
11772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011773 return 0;
11774}
11775
11776static inline u64 get_stat64(tg3_stat64_t *val)
11777{
11778 return ((u64)val->high << 32) | ((u64)val->low);
11779}
11780
11781static u64 tg3_calc_crc_errors(struct tg3 *tp)
11782{
11783 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11784
11785 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011786 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11787 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011788 u32 val;
11789
11790 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11791 tg3_writephy(tp, MII_TG3_TEST1,
11792 val | MII_TG3_TEST1_CRC_EN);
11793 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11794 } else
11795 val = 0;
11796
11797 tp->phy_crc_errors += val;
11798
11799 return tp->phy_crc_errors;
11800 }
11801
11802 return get_stat64(&hw_stats->rx_fcs_errors);
11803}
11804
11805#define ESTAT_ADD(member) \
11806 estats->member = old_estats->member + \
11807 get_stat64(&hw_stats->member)
11808
11809static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11810{
11811 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11812 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11813
11814 ESTAT_ADD(rx_octets);
11815 ESTAT_ADD(rx_fragments);
11816 ESTAT_ADD(rx_ucast_packets);
11817 ESTAT_ADD(rx_mcast_packets);
11818 ESTAT_ADD(rx_bcast_packets);
11819 ESTAT_ADD(rx_fcs_errors);
11820 ESTAT_ADD(rx_align_errors);
11821 ESTAT_ADD(rx_xon_pause_rcvd);
11822 ESTAT_ADD(rx_xoff_pause_rcvd);
11823 ESTAT_ADD(rx_mac_ctrl_rcvd);
11824 ESTAT_ADD(rx_xoff_entered);
11825 ESTAT_ADD(rx_frame_too_long_errors);
11826 ESTAT_ADD(rx_jabbers);
11827 ESTAT_ADD(rx_undersize_packets);
11828 ESTAT_ADD(rx_in_length_errors);
11829 ESTAT_ADD(rx_out_length_errors);
11830 ESTAT_ADD(rx_64_or_less_octet_packets);
11831 ESTAT_ADD(rx_65_to_127_octet_packets);
11832 ESTAT_ADD(rx_128_to_255_octet_packets);
11833 ESTAT_ADD(rx_256_to_511_octet_packets);
11834 ESTAT_ADD(rx_512_to_1023_octet_packets);
11835 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11836 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11837 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11838 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11839 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11840
11841 ESTAT_ADD(tx_octets);
11842 ESTAT_ADD(tx_collisions);
11843 ESTAT_ADD(tx_xon_sent);
11844 ESTAT_ADD(tx_xoff_sent);
11845 ESTAT_ADD(tx_flow_control);
11846 ESTAT_ADD(tx_mac_errors);
11847 ESTAT_ADD(tx_single_collisions);
11848 ESTAT_ADD(tx_mult_collisions);
11849 ESTAT_ADD(tx_deferred);
11850 ESTAT_ADD(tx_excessive_collisions);
11851 ESTAT_ADD(tx_late_collisions);
11852 ESTAT_ADD(tx_collide_2times);
11853 ESTAT_ADD(tx_collide_3times);
11854 ESTAT_ADD(tx_collide_4times);
11855 ESTAT_ADD(tx_collide_5times);
11856 ESTAT_ADD(tx_collide_6times);
11857 ESTAT_ADD(tx_collide_7times);
11858 ESTAT_ADD(tx_collide_8times);
11859 ESTAT_ADD(tx_collide_9times);
11860 ESTAT_ADD(tx_collide_10times);
11861 ESTAT_ADD(tx_collide_11times);
11862 ESTAT_ADD(tx_collide_12times);
11863 ESTAT_ADD(tx_collide_13times);
11864 ESTAT_ADD(tx_collide_14times);
11865 ESTAT_ADD(tx_collide_15times);
11866 ESTAT_ADD(tx_ucast_packets);
11867 ESTAT_ADD(tx_mcast_packets);
11868 ESTAT_ADD(tx_bcast_packets);
11869 ESTAT_ADD(tx_carrier_sense_errors);
11870 ESTAT_ADD(tx_discards);
11871 ESTAT_ADD(tx_errors);
11872
11873 ESTAT_ADD(dma_writeq_full);
11874 ESTAT_ADD(dma_write_prioq_full);
11875 ESTAT_ADD(rxbds_empty);
11876 ESTAT_ADD(rx_discards);
11877 ESTAT_ADD(rx_errors);
11878 ESTAT_ADD(rx_threshold_hit);
11879
11880 ESTAT_ADD(dma_readq_full);
11881 ESTAT_ADD(dma_read_prioq_full);
11882 ESTAT_ADD(tx_comp_queue_full);
11883
11884 ESTAT_ADD(ring_set_send_prod_index);
11885 ESTAT_ADD(ring_status_update);
11886 ESTAT_ADD(nic_irqs);
11887 ESTAT_ADD(nic_avoided_irqs);
11888 ESTAT_ADD(nic_tx_threshold_hit);
11889
Matt Carlson4452d092011-05-19 12:12:51 +000011890 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891}
11892
Matt Carlson65ec6982012-02-28 23:33:37 +000011893static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011894{
Eric Dumazet511d2222010-07-07 20:44:24 +000011895 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11897
Linus Torvalds1da177e2005-04-16 15:20:36 -070011898 stats->rx_packets = old_stats->rx_packets +
11899 get_stat64(&hw_stats->rx_ucast_packets) +
11900 get_stat64(&hw_stats->rx_mcast_packets) +
11901 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011902
Linus Torvalds1da177e2005-04-16 15:20:36 -070011903 stats->tx_packets = old_stats->tx_packets +
11904 get_stat64(&hw_stats->tx_ucast_packets) +
11905 get_stat64(&hw_stats->tx_mcast_packets) +
11906 get_stat64(&hw_stats->tx_bcast_packets);
11907
11908 stats->rx_bytes = old_stats->rx_bytes +
11909 get_stat64(&hw_stats->rx_octets);
11910 stats->tx_bytes = old_stats->tx_bytes +
11911 get_stat64(&hw_stats->tx_octets);
11912
11913 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011914 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 stats->tx_errors = old_stats->tx_errors +
11916 get_stat64(&hw_stats->tx_errors) +
11917 get_stat64(&hw_stats->tx_mac_errors) +
11918 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11919 get_stat64(&hw_stats->tx_discards);
11920
11921 stats->multicast = old_stats->multicast +
11922 get_stat64(&hw_stats->rx_mcast_packets);
11923 stats->collisions = old_stats->collisions +
11924 get_stat64(&hw_stats->tx_collisions);
11925
11926 stats->rx_length_errors = old_stats->rx_length_errors +
11927 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11928 get_stat64(&hw_stats->rx_undersize_packets);
11929
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930 stats->rx_frame_errors = old_stats->rx_frame_errors +
11931 get_stat64(&hw_stats->rx_align_errors);
11932 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11933 get_stat64(&hw_stats->tx_discards);
11934 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11935 get_stat64(&hw_stats->tx_carrier_sense_errors);
11936
11937 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011938 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939
John W. Linville4f63b872005-09-12 14:43:18 -070011940 stats->rx_missed_errors = old_stats->rx_missed_errors +
11941 get_stat64(&hw_stats->rx_discards);
11942
Eric Dumazetb0057c52010-10-10 19:55:52 +000011943 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011944 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945}
11946
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947static int tg3_get_regs_len(struct net_device *dev)
11948{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011949 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011950}
11951
11952static void tg3_get_regs(struct net_device *dev,
11953 struct ethtool_regs *regs, void *_p)
11954{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956
11957 regs->version = 0;
11958
Matt Carlson97bd8e42011-04-13 11:05:04 +000011959 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011960
Matt Carlson80096062010-08-02 11:26:06 +000011961 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011962 return;
11963
David S. Millerf47c11e2005-06-24 20:18:35 -070011964 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965
Matt Carlson97bd8e42011-04-13 11:05:04 +000011966 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011967
David S. Millerf47c11e2005-06-24 20:18:35 -070011968 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969}
11970
11971static int tg3_get_eeprom_len(struct net_device *dev)
11972{
11973 struct tg3 *tp = netdev_priv(dev);
11974
11975 return tp->nvram_size;
11976}
11977
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11979{
11980 struct tg3 *tp = netdev_priv(dev);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011981 int ret, cpmu_restore = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011982 u8 *pd;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011983 u32 i, offset, len, b_offset, b_count, cpmu_val = 0;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011984 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011985
Joe Perches63c3a662011-04-26 08:12:10 +000011986 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011987 return -EINVAL;
11988
Linus Torvalds1da177e2005-04-16 15:20:36 -070011989 offset = eeprom->offset;
11990 len = eeprom->len;
11991 eeprom->len = 0;
11992
11993 eeprom->magic = TG3_EEPROM_MAGIC;
11994
Prashant Sreedharan506724c2014-05-24 01:32:09 -070011995 /* Override clock, link aware and link idle modes */
11996 if (tg3_flag(tp, CPMU_PRESENT)) {
11997 cpmu_val = tr32(TG3_CPMU_CTRL);
11998 if (cpmu_val & (CPMU_CTRL_LINK_AWARE_MODE |
11999 CPMU_CTRL_LINK_IDLE_MODE)) {
12000 tw32(TG3_CPMU_CTRL, cpmu_val &
12001 ~(CPMU_CTRL_LINK_AWARE_MODE |
12002 CPMU_CTRL_LINK_IDLE_MODE));
12003 cpmu_restore = 1;
12004 }
12005 }
12006 tg3_override_clk(tp);
12007
Linus Torvalds1da177e2005-04-16 15:20:36 -070012008 if (offset & 3) {
12009 /* adjustments to start on required 4 byte boundary */
12010 b_offset = offset & 3;
12011 b_count = 4 - b_offset;
12012 if (b_count > len) {
12013 /* i.e. offset=1 len=2 */
12014 b_count = len;
12015 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000012016 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012018 goto eeprom_done;
Matt Carlsonbe98da62010-07-11 09:31:46 +000012019 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012020 len -= b_count;
12021 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012022 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012023 }
12024
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012025 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012026 pd = &data[eeprom->len];
12027 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012028 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012029 if (ret) {
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012030 if (i)
12031 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012032 eeprom->len += i;
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012033 goto eeprom_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012035 memcpy(pd + i, &val, 4);
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012036 if (need_resched()) {
12037 if (signal_pending(current)) {
12038 eeprom->len += i;
12039 ret = -EINTR;
12040 goto eeprom_done;
12041 }
12042 cond_resched();
12043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044 }
12045 eeprom->len += i;
12046
12047 if (len & 3) {
12048 /* read last bytes not ending on 4 byte boundary */
12049 pd = &data[eeprom->len];
12050 b_count = len & 3;
12051 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012052 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012053 if (ret)
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012054 goto eeprom_done;
Al Virob9fc7dc2007-12-17 22:59:57 -080012055 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012056 eeprom->len += b_count;
12057 }
Prashant Sreedharan506724c2014-05-24 01:32:09 -070012058 ret = 0;
12059
12060eeprom_done:
12061 /* Restore clock, link aware and link idle modes */
12062 tg3_restore_clk(tp);
12063 if (cpmu_restore)
12064 tw32(TG3_CPMU_CTRL, cpmu_val);
12065
12066 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012067}
12068
Linus Torvalds1da177e2005-04-16 15:20:36 -070012069static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
12070{
12071 struct tg3 *tp = netdev_priv(dev);
12072 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080012073 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074 u8 *buf;
Arnd Bergmanne434e042016-01-29 12:39:15 +010012075 __be32 start = 0, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012076
Joe Perches63c3a662011-04-26 08:12:10 +000012077 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000012078 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079 return -EINVAL;
12080
12081 offset = eeprom->offset;
12082 len = eeprom->len;
12083
12084 if ((b_offset = (offset & 3))) {
12085 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012086 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087 if (ret)
12088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089 len += b_offset;
12090 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070012091 if (len < 4)
12092 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012093 }
12094
12095 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070012096 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 /* adjustments to end on required 4 byte boundary */
12098 odd_len = 1;
12099 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012100 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012101 if (ret)
12102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 }
12104
12105 buf = data;
12106 if (b_offset || odd_len) {
12107 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012108 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012109 return -ENOMEM;
12110 if (b_offset)
12111 memcpy(buf, &start, 4);
12112 if (odd_len)
12113 memcpy(buf+len-4, &end, 4);
12114 memcpy(buf + b_offset, data, eeprom->len);
12115 }
12116
12117 ret = tg3_nvram_write_block(tp, offset, len, buf);
12118
12119 if (buf != data)
12120 kfree(buf);
12121
12122 return ret;
12123}
12124
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012125static int tg3_get_link_ksettings(struct net_device *dev,
12126 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012127{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012128 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012129 u32 supported, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012130
Joe Perches63c3a662011-04-26 08:12:10 +000012131 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012132 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012133 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012134 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012135 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +030012136 phy_ethtool_ksettings_get(phydev, cmd);
12137
12138 return 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012139 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012140
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012141 supported = (SUPPORTED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012143 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012144 supported |= (SUPPORTED_1000baseT_Half |
12145 SUPPORTED_1000baseT_Full);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012146
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012147 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012148 supported |= (SUPPORTED_100baseT_Half |
12149 SUPPORTED_100baseT_Full |
12150 SUPPORTED_10baseT_Half |
12151 SUPPORTED_10baseT_Full |
12152 SUPPORTED_TP);
12153 cmd->base.port = PORT_TP;
Karsten Keilef348142006-05-12 12:49:08 -070012154 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012155 supported |= SUPPORTED_FIBRE;
12156 cmd->base.port = PORT_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012157 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012158 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
12159 supported);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012160
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012161 advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012162 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12163 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12164 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012165 advertising |= ADVERTISED_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012166 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012167 advertising |= ADVERTISED_Pause |
12168 ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012169 }
12170 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012171 advertising |= ADVERTISED_Asym_Pause;
Matt Carlson5bb09772011-06-13 13:39:00 +000012172 }
12173 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012174 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
12175 advertising);
12176
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012177 if (netif_running(dev) && tp->link_up) {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012178 cmd->base.speed = tp->link_config.active_speed;
12179 cmd->base.duplex = tp->link_config.active_duplex;
12180 ethtool_convert_legacy_u32_to_link_mode(
12181 cmd->link_modes.lp_advertising,
12182 tp->link_config.rmt_adv);
12183
Matt Carlsone348c5e2011-11-21 15:01:20 +000012184 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12185 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012186 cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012187 else
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012188 cmd->base.eth_tp_mdix = ETH_TP_MDI;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012189 }
Matt Carlson64c22182010-10-14 10:37:44 +000012190 } else {
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012191 cmd->base.speed = SPEED_UNKNOWN;
12192 cmd->base.duplex = DUPLEX_UNKNOWN;
12193 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 }
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012195 cmd->base.phy_address = tp->phy_addr;
12196 cmd->base.autoneg = tp->link_config.autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012197 return 0;
12198}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012199
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012200static int tg3_set_link_ksettings(struct net_device *dev,
12201 const struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012202{
12203 struct tg3 *tp = netdev_priv(dev);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012204 u32 speed = cmd->base.speed;
12205 u32 advertising;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012206
Joe Perches63c3a662011-04-26 08:12:10 +000012207 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012208 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012209 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012210 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012211 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012212 return phy_ethtool_ksettings_set(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012213 }
12214
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012215 if (cmd->base.autoneg != AUTONEG_ENABLE &&
12216 cmd->base.autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012217 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012218
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012219 if (cmd->base.autoneg == AUTONEG_DISABLE &&
12220 cmd->base.duplex != DUPLEX_FULL &&
12221 cmd->base.duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012223
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012224 ethtool_convert_link_mode_to_legacy_u32(&advertising,
12225 cmd->link_modes.advertising);
12226
12227 if (cmd->base.autoneg == AUTONEG_ENABLE) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000012228 u32 mask = ADVERTISED_Autoneg |
12229 ADVERTISED_Pause |
12230 ADVERTISED_Asym_Pause;
12231
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012232 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012233 mask |= ADVERTISED_1000baseT_Half |
12234 ADVERTISED_1000baseT_Full;
12235
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012236 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012237 mask |= ADVERTISED_100baseT_Half |
12238 ADVERTISED_100baseT_Full |
12239 ADVERTISED_10baseT_Half |
12240 ADVERTISED_10baseT_Full |
12241 ADVERTISED_TP;
12242 else
12243 mask |= ADVERTISED_FIBRE;
12244
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012245 if (advertising & ~mask)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012246 return -EINVAL;
12247
12248 mask &= (ADVERTISED_1000baseT_Half |
12249 ADVERTISED_1000baseT_Full |
12250 ADVERTISED_100baseT_Half |
12251 ADVERTISED_100baseT_Full |
12252 ADVERTISED_10baseT_Half |
12253 ADVERTISED_10baseT_Full);
12254
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012255 advertising &= mask;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012256 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012257 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012258 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012259 return -EINVAL;
12260
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012261 if (cmd->base.duplex != DUPLEX_FULL)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012262 return -EINVAL;
12263 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012264 if (speed != SPEED_100 &&
12265 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012266 return -EINVAL;
12267 }
12268 }
12269
David S. Millerf47c11e2005-06-24 20:18:35 -070012270 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012271
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012272 tp->link_config.autoneg = cmd->base.autoneg;
12273 if (cmd->base.autoneg == AUTONEG_ENABLE) {
12274 tp->link_config.advertising = (advertising |
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012275 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012276 tp->link_config.speed = SPEED_UNKNOWN;
12277 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012278 } else {
12279 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012280 tp->link_config.speed = speed;
Philippe Reynesb6f5be22016-09-25 23:31:24 +020012281 tp->link_config.duplex = cmd->base.duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012282 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012283
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012284 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12285
Nithin Sujirce20f162013-04-09 08:48:04 +000012286 tg3_warn_mgmt_link_flap(tp);
12287
Linus Torvalds1da177e2005-04-16 15:20:36 -070012288 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012289 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290
David S. Millerf47c11e2005-06-24 20:18:35 -070012291 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012292
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 return 0;
12294}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012295
Linus Torvalds1da177e2005-04-16 15:20:36 -070012296static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12297{
12298 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012299
Rick Jones68aad782011-11-07 13:29:27 +000012300 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12301 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12302 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12303 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012305
Linus Torvalds1da177e2005-04-16 15:20:36 -070012306static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12307{
12308 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012309
Joe Perches63c3a662011-04-26 08:12:10 +000012310 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012311 wol->supported = WAKE_MAGIC;
12312 else
12313 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012314 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012315 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316 wol->wolopts = WAKE_MAGIC;
12317 memset(&wol->sopass, 0, sizeof(wol->sopass));
12318}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012319
Linus Torvalds1da177e2005-04-16 15:20:36 -070012320static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12321{
12322 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012323 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012324
Linus Torvalds1da177e2005-04-16 15:20:36 -070012325 if (wol->wolopts & ~WAKE_MAGIC)
12326 return -EINVAL;
12327 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012328 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012329 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012330
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012331 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12332
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012333 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012334 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012335 else
Joe Perches63c3a662011-04-26 08:12:10 +000012336 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012337
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338 return 0;
12339}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012340
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341static u32 tg3_get_msglevel(struct net_device *dev)
12342{
12343 struct tg3 *tp = netdev_priv(dev);
12344 return tp->msg_enable;
12345}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012346
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347static void tg3_set_msglevel(struct net_device *dev, u32 value)
12348{
12349 struct tg3 *tp = netdev_priv(dev);
12350 tp->msg_enable = value;
12351}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012352
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353static int tg3_nway_reset(struct net_device *dev)
12354{
12355 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012357
Linus Torvalds1da177e2005-04-16 15:20:36 -070012358 if (!netif_running(dev))
12359 return -EAGAIN;
12360
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012361 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012362 return -EINVAL;
12363
Nithin Sujirce20f162013-04-09 08:48:04 +000012364 tg3_warn_mgmt_link_flap(tp);
12365
Joe Perches63c3a662011-04-26 08:12:10 +000012366 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012367 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012368 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010012369 r = phy_start_aneg(mdiobus_get_phy(tp->mdio_bus, tp->phy_addr));
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012370 } else {
12371 u32 bmcr;
12372
12373 spin_lock_bh(&tp->lock);
12374 r = -EINVAL;
12375 tg3_readphy(tp, MII_BMCR, &bmcr);
12376 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12377 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012378 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012379 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12380 BMCR_ANENABLE);
12381 r = 0;
12382 }
12383 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012384 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012385
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386 return r;
12387}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012388
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12390{
12391 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012392
Matt Carlson2c49a442010-09-30 10:34:35 +000012393 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012394 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012395 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012396 else
12397 ering->rx_jumbo_max_pending = 0;
12398
12399 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400
12401 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012402 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012403 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12404 else
12405 ering->rx_jumbo_pending = 0;
12406
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012407 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012409
Linus Torvalds1da177e2005-04-16 15:20:36 -070012410static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12411{
12412 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012413 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012414
Matt Carlson2c49a442010-09-30 10:34:35 +000012415 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12416 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012417 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12418 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012419 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012420 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012421 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012422
Michael Chanbbe832c2005-06-24 20:20:04 -070012423 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012424 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012426 irq_sync = 1;
12427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012428
Michael Chanbbe832c2005-06-24 20:20:04 -070012429 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012430
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431 tp->rx_pending = ering->rx_pending;
12432
Joe Perches63c3a662011-04-26 08:12:10 +000012433 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434 tp->rx_pending > 63)
12435 tp->rx_pending = 63;
Ivan Veceraba67b512014-04-17 14:51:08 +020012436
12437 if (tg3_flag(tp, JUMBO_RING_ENABLE))
12438 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012439
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012440 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012441 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442
12443 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012444 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012445 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012446 if (!err)
12447 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448 }
12449
David S. Millerf47c11e2005-06-24 20:18:35 -070012450 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012451
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012452 if (irq_sync && !err)
12453 tg3_phy_start(tp);
12454
Michael Chanb9ec6c12006-07-25 16:37:27 -070012455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012456}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012457
Linus Torvalds1da177e2005-04-16 15:20:36 -070012458static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12459{
12460 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012461
Joe Perches63c3a662011-04-26 08:12:10 +000012462 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012463
Matt Carlson4a2db502011-12-08 14:40:17 +000012464 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012465 epause->rx_pause = 1;
12466 else
12467 epause->rx_pause = 0;
12468
Matt Carlson4a2db502011-12-08 14:40:17 +000012469 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012470 epause->tx_pause = 1;
12471 else
12472 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012473}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012474
Linus Torvalds1da177e2005-04-16 15:20:36 -070012475static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12476{
12477 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012478 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012479
Nithin Sujirce20f162013-04-09 08:48:04 +000012480 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12481 tg3_warn_mgmt_link_flap(tp);
12482
Joe Perches63c3a662011-04-26 08:12:10 +000012483 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012484 u32 newadv;
12485 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012486
Andrew Lunn7f854422016-01-06 20:11:18 +010012487 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012488
Matt Carlson27121682010-02-17 15:16:57 +000012489 if (!(phydev->supported & SUPPORTED_Pause) ||
12490 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012491 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012492 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012493
Matt Carlson27121682010-02-17 15:16:57 +000012494 tp->link_config.flowctrl = 0;
12495 if (epause->rx_pause) {
12496 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012497
Matt Carlson27121682010-02-17 15:16:57 +000012498 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012499 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012500 newadv = ADVERTISED_Pause;
12501 } else
12502 newadv = ADVERTISED_Pause |
12503 ADVERTISED_Asym_Pause;
12504 } else if (epause->tx_pause) {
12505 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12506 newadv = ADVERTISED_Asym_Pause;
12507 } else
12508 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012509
Matt Carlson27121682010-02-17 15:16:57 +000012510 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012511 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012512 else
Joe Perches63c3a662011-04-26 08:12:10 +000012513 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012514
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012515 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012516 u32 oldadv = phydev->advertising &
12517 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12518 if (oldadv != newadv) {
12519 phydev->advertising &=
12520 ~(ADVERTISED_Pause |
12521 ADVERTISED_Asym_Pause);
12522 phydev->advertising |= newadv;
12523 if (phydev->autoneg) {
12524 /*
12525 * Always renegotiate the link to
12526 * inform our link partner of our
12527 * flow control settings, even if the
12528 * flow control is forced. Let
12529 * tg3_adjust_link() do the final
12530 * flow control setup.
12531 */
12532 return phy_start_aneg(phydev);
12533 }
12534 }
12535
12536 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012537 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012538 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012539 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012540 ~(ADVERTISED_Pause |
12541 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012542 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012543 }
12544 } else {
12545 int irq_sync = 0;
12546
12547 if (netif_running(dev)) {
12548 tg3_netif_stop(tp);
12549 irq_sync = 1;
12550 }
12551
12552 tg3_full_lock(tp, irq_sync);
12553
12554 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012555 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012556 else
Joe Perches63c3a662011-04-26 08:12:10 +000012557 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012558 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012559 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012560 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012561 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012562 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012563 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012564 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012565 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012566
12567 if (netif_running(dev)) {
12568 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012569 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012570 if (!err)
12571 tg3_netif_start(tp);
12572 }
12573
12574 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012577 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12578
Michael Chanb9ec6c12006-07-25 16:37:27 -070012579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012580}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012581
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012582static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012583{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012584 switch (sset) {
12585 case ETH_SS_TEST:
12586 return TG3_NUM_TEST;
12587 case ETH_SS_STATS:
12588 return TG3_NUM_STATS;
12589 default:
12590 return -EOPNOTSUPP;
12591 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012592}
12593
Matt Carlson90415472011-12-16 13:33:23 +000012594static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12595 u32 *rules __always_unused)
12596{
12597 struct tg3 *tp = netdev_priv(dev);
12598
12599 if (!tg3_flag(tp, SUPPORT_MSIX))
12600 return -EOPNOTSUPP;
12601
12602 switch (info->cmd) {
12603 case ETHTOOL_GRXRINGS:
12604 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012605 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012606 else {
12607 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012608 if (info->data > TG3_RSS_MAX_NUM_QS)
12609 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012610 }
12611
Matt Carlson90415472011-12-16 13:33:23 +000012612 return 0;
12613
12614 default:
12615 return -EOPNOTSUPP;
12616 }
12617}
12618
12619static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12620{
12621 u32 size = 0;
12622 struct tg3 *tp = netdev_priv(dev);
12623
12624 if (tg3_flag(tp, SUPPORT_MSIX))
12625 size = TG3_RSS_INDIR_TBL_SIZE;
12626
12627 return size;
12628}
12629
Eyal Perry892311f2014-12-02 18:12:10 +020012630static int tg3_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012631{
12632 struct tg3 *tp = netdev_priv(dev);
12633 int i;
12634
Eyal Perry892311f2014-12-02 18:12:10 +020012635 if (hfunc)
12636 *hfunc = ETH_RSS_HASH_TOP;
12637 if (!indir)
12638 return 0;
12639
Matt Carlson90415472011-12-16 13:33:23 +000012640 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12641 indir[i] = tp->rss_ind_tbl[i];
12642
12643 return 0;
12644}
12645
Eyal Perry892311f2014-12-02 18:12:10 +020012646static int tg3_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key,
12647 const u8 hfunc)
Matt Carlson90415472011-12-16 13:33:23 +000012648{
12649 struct tg3 *tp = netdev_priv(dev);
12650 size_t i;
12651
Eyal Perry892311f2014-12-02 18:12:10 +020012652 /* We require at least one supported parameter to be changed and no
12653 * change in any of the unsupported parameters
12654 */
12655 if (key ||
12656 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
12657 return -EOPNOTSUPP;
12658
12659 if (!indir)
12660 return 0;
12661
Matt Carlson90415472011-12-16 13:33:23 +000012662 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12663 tp->rss_ind_tbl[i] = indir[i];
12664
12665 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12666 return 0;
12667
12668 /* It is legal to write the indirection
12669 * table while the device is running.
12670 */
12671 tg3_full_lock(tp, 0);
12672 tg3_rss_write_indir_tbl(tp);
12673 tg3_full_unlock(tp);
12674
12675 return 0;
12676}
12677
Michael Chan09681692012-09-28 07:12:42 +000012678static void tg3_get_channels(struct net_device *dev,
12679 struct ethtool_channels *channel)
12680{
12681 struct tg3 *tp = netdev_priv(dev);
12682 u32 deflt_qs = netif_get_num_default_rss_queues();
12683
12684 channel->max_rx = tp->rxq_max;
12685 channel->max_tx = tp->txq_max;
12686
12687 if (netif_running(dev)) {
12688 channel->rx_count = tp->rxq_cnt;
12689 channel->tx_count = tp->txq_cnt;
12690 } else {
12691 if (tp->rxq_req)
12692 channel->rx_count = tp->rxq_req;
12693 else
12694 channel->rx_count = min(deflt_qs, tp->rxq_max);
12695
12696 if (tp->txq_req)
12697 channel->tx_count = tp->txq_req;
12698 else
12699 channel->tx_count = min(deflt_qs, tp->txq_max);
12700 }
12701}
12702
12703static int tg3_set_channels(struct net_device *dev,
12704 struct ethtool_channels *channel)
12705{
12706 struct tg3 *tp = netdev_priv(dev);
12707
12708 if (!tg3_flag(tp, SUPPORT_MSIX))
12709 return -EOPNOTSUPP;
12710
12711 if (channel->rx_count > tp->rxq_max ||
12712 channel->tx_count > tp->txq_max)
12713 return -EINVAL;
12714
12715 tp->rxq_req = channel->rx_count;
12716 tp->txq_req = channel->tx_count;
12717
12718 if (!netif_running(dev))
12719 return 0;
12720
12721 tg3_stop(tp);
12722
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012723 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012724
Matt Carlsonbe947302012-12-03 19:36:57 +000012725 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012726
12727 return 0;
12728}
12729
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012730static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012731{
12732 switch (stringset) {
12733 case ETH_SS_STATS:
12734 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12735 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012736 case ETH_SS_TEST:
12737 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739 default:
12740 WARN_ON(1); /* we need a WARN() */
12741 break;
12742 }
12743}
12744
stephen hemminger81b87092011-04-04 08:43:50 +000012745static int tg3_set_phys_id(struct net_device *dev,
12746 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012747{
12748 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012749
12750 if (!netif_running(tp->dev))
12751 return -EAGAIN;
12752
stephen hemminger81b87092011-04-04 08:43:50 +000012753 switch (state) {
12754 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012755 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012756
stephen hemminger81b87092011-04-04 08:43:50 +000012757 case ETHTOOL_ID_ON:
12758 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12759 LED_CTRL_1000MBPS_ON |
12760 LED_CTRL_100MBPS_ON |
12761 LED_CTRL_10MBPS_ON |
12762 LED_CTRL_TRAFFIC_OVERRIDE |
12763 LED_CTRL_TRAFFIC_BLINK |
12764 LED_CTRL_TRAFFIC_LED);
12765 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012766
stephen hemminger81b87092011-04-04 08:43:50 +000012767 case ETHTOOL_ID_OFF:
12768 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12769 LED_CTRL_TRAFFIC_OVERRIDE);
12770 break;
Michael Chan4009a932005-09-05 17:52:54 -070012771
stephen hemminger81b87092011-04-04 08:43:50 +000012772 case ETHTOOL_ID_INACTIVE:
12773 tw32(MAC_LED_CTRL, tp->led_ctrl);
12774 break;
Michael Chan4009a932005-09-05 17:52:54 -070012775 }
stephen hemminger81b87092011-04-04 08:43:50 +000012776
Michael Chan4009a932005-09-05 17:52:54 -070012777 return 0;
12778}
12779
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012780static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012781 struct ethtool_stats *estats, u64 *tmp_stats)
12782{
12783 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012784
Matt Carlsonb546e462012-02-13 15:20:09 +000012785 if (tp->hw_stats)
12786 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12787 else
12788 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789}
12790
Matt Carlson535a4902011-07-20 10:20:56 +000012791static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012792{
12793 int i;
12794 __be32 *buf;
12795 u32 offset = 0, len = 0;
12796 u32 magic, val;
12797
Joe Perches63c3a662011-04-26 08:12:10 +000012798 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012799 return NULL;
12800
12801 if (magic == TG3_EEPROM_MAGIC) {
12802 for (offset = TG3_NVM_DIR_START;
12803 offset < TG3_NVM_DIR_END;
12804 offset += TG3_NVM_DIRENT_SIZE) {
12805 if (tg3_nvram_read(tp, offset, &val))
12806 return NULL;
12807
12808 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12809 TG3_NVM_DIRTYPE_EXTVPD)
12810 break;
12811 }
12812
12813 if (offset != TG3_NVM_DIR_END) {
12814 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12815 if (tg3_nvram_read(tp, offset + 4, &offset))
12816 return NULL;
12817
12818 offset = tg3_nvram_logical_addr(tp, offset);
12819 }
12820 }
12821
12822 if (!offset || !len) {
12823 offset = TG3_NVM_VPD_OFF;
12824 len = TG3_NVM_VPD_LEN;
12825 }
12826
12827 buf = kmalloc(len, GFP_KERNEL);
12828 if (buf == NULL)
12829 return NULL;
12830
12831 if (magic == TG3_EEPROM_MAGIC) {
12832 for (i = 0; i < len; i += 4) {
12833 /* The data is in little-endian format in NVRAM.
12834 * Use the big-endian read routines to preserve
12835 * the byte order as it exists in NVRAM.
12836 */
12837 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12838 goto error;
12839 }
12840 } else {
12841 u8 *ptr;
12842 ssize_t cnt;
12843 unsigned int pos = 0;
12844
12845 ptr = (u8 *)&buf[0];
12846 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12847 cnt = pci_read_vpd(tp->pdev, pos,
12848 len - pos, ptr);
12849 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12850 cnt = 0;
12851 else if (cnt < 0)
12852 goto error;
12853 }
12854 if (pos != len)
12855 goto error;
12856 }
12857
Matt Carlson535a4902011-07-20 10:20:56 +000012858 *vpdlen = len;
12859
Matt Carlsonc3e94502011-04-13 11:05:08 +000012860 return buf;
12861
12862error:
12863 kfree(buf);
12864 return NULL;
12865}
12866
Michael Chan566f86a2005-05-29 14:56:58 -070012867#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012868#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12869#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12870#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012871#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12872#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012873#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012874#define NVRAM_SELFBOOT_HW_SIZE 0x20
12875#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012876
12877static int tg3_test_nvram(struct tg3 *tp)
12878{
Matt Carlson535a4902011-07-20 10:20:56 +000012879 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012880 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012881 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012882
Joe Perches63c3a662011-04-26 08:12:10 +000012883 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012884 return 0;
12885
Matt Carlsone4f34112009-02-25 14:25:00 +000012886 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012887 return -EIO;
12888
Michael Chan1b277772006-03-20 22:27:48 -080012889 if (magic == TG3_EEPROM_MAGIC)
12890 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012891 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012892 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12893 TG3_EEPROM_SB_FORMAT_1) {
12894 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12895 case TG3_EEPROM_SB_REVISION_0:
12896 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12897 break;
12898 case TG3_EEPROM_SB_REVISION_2:
12899 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12900 break;
12901 case TG3_EEPROM_SB_REVISION_3:
12902 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12903 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012904 case TG3_EEPROM_SB_REVISION_4:
12905 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12906 break;
12907 case TG3_EEPROM_SB_REVISION_5:
12908 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12909 break;
12910 case TG3_EEPROM_SB_REVISION_6:
12911 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12912 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012913 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012914 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012915 }
12916 } else
Michael Chan1b277772006-03-20 22:27:48 -080012917 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012918 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12919 size = NVRAM_SELFBOOT_HW_SIZE;
12920 else
Michael Chan1b277772006-03-20 22:27:48 -080012921 return -EIO;
12922
12923 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012924 if (buf == NULL)
12925 return -ENOMEM;
12926
Michael Chan1b277772006-03-20 22:27:48 -080012927 err = -EIO;
12928 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012929 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12930 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012931 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012932 }
Michael Chan1b277772006-03-20 22:27:48 -080012933 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012934 goto out;
12935
Michael Chan1b277772006-03-20 22:27:48 -080012936 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012937 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012938 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012939 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012940 u8 *buf8 = (u8 *) buf, csum8 = 0;
12941
Al Virob9fc7dc2007-12-17 22:59:57 -080012942 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012943 TG3_EEPROM_SB_REVISION_2) {
12944 /* For rev 2, the csum doesn't include the MBA. */
12945 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12946 csum8 += buf8[i];
12947 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12948 csum8 += buf8[i];
12949 } else {
12950 for (i = 0; i < size; i++)
12951 csum8 += buf8[i];
12952 }
Michael Chan1b277772006-03-20 22:27:48 -080012953
Adrian Bunkad96b482006-04-05 22:21:04 -070012954 if (csum8 == 0) {
12955 err = 0;
12956 goto out;
12957 }
12958
12959 err = -EIO;
12960 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012961 }
Michael Chan566f86a2005-05-29 14:56:58 -070012962
Al Virob9fc7dc2007-12-17 22:59:57 -080012963 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012964 TG3_EEPROM_MAGIC_HW) {
12965 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012966 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012967 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012968
12969 /* Separate the parity bits and the data bytes. */
12970 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12971 if ((i == 0) || (i == 8)) {
12972 int l;
12973 u8 msk;
12974
12975 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12976 parity[k++] = buf8[i] & msk;
12977 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012978 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012979 int l;
12980 u8 msk;
12981
12982 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12983 parity[k++] = buf8[i] & msk;
12984 i++;
12985
12986 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12987 parity[k++] = buf8[i] & msk;
12988 i++;
12989 }
12990 data[j++] = buf8[i];
12991 }
12992
12993 err = -EIO;
12994 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12995 u8 hw8 = hweight8(data[i]);
12996
12997 if ((hw8 & 0x1) && parity[i])
12998 goto out;
12999 else if (!(hw8 & 0x1) && !parity[i])
13000 goto out;
13001 }
13002 err = 0;
13003 goto out;
13004 }
13005
Matt Carlson01c3a392011-03-09 16:58:20 +000013006 err = -EIO;
13007
Michael Chan566f86a2005-05-29 14:56:58 -070013008 /* Bootstrap checksum at offset 0x10 */
13009 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000013010 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070013011 goto out;
13012
13013 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
13014 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000013015 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000013016 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070013017
Matt Carlsonc3e94502011-04-13 11:05:08 +000013018 kfree(buf);
13019
Matt Carlson535a4902011-07-20 10:20:56 +000013020 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000013021 if (!buf)
13022 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000013023
Matt Carlson535a4902011-07-20 10:20:56 +000013024 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000013025 if (i > 0) {
13026 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
13027 if (j < 0)
13028 goto out;
13029
Matt Carlson535a4902011-07-20 10:20:56 +000013030 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000013031 goto out;
13032
13033 i += PCI_VPD_LRDT_TAG_SIZE;
13034 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
13035 PCI_VPD_RO_KEYWORD_CHKSUM);
13036 if (j > 0) {
13037 u8 csum8 = 0;
13038
13039 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13040
13041 for (i = 0; i <= j; i++)
13042 csum8 += ((u8 *)buf)[i];
13043
13044 if (csum8)
13045 goto out;
13046 }
13047 }
13048
Michael Chan566f86a2005-05-29 14:56:58 -070013049 err = 0;
13050
13051out:
13052 kfree(buf);
13053 return err;
13054}
13055
Michael Chanca430072005-05-29 14:57:23 -070013056#define TG3_SERDES_TIMEOUT_SEC 2
13057#define TG3_COPPER_TIMEOUT_SEC 6
13058
13059static int tg3_test_link(struct tg3 *tp)
13060{
13061 int i, max;
13062
13063 if (!netif_running(tp->dev))
13064 return -ENODEV;
13065
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013066 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070013067 max = TG3_SERDES_TIMEOUT_SEC;
13068 else
13069 max = TG3_COPPER_TIMEOUT_SEC;
13070
13071 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000013072 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070013073 return 0;
13074
13075 if (msleep_interruptible(1000))
13076 break;
13077 }
13078
13079 return -EIO;
13080}
13081
Michael Chana71116d2005-05-29 14:58:11 -070013082/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080013083static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070013084{
Michael Chanb16250e2006-09-27 16:10:14 -070013085 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070013086 u32 offset, read_mask, write_mask, val, save_val, read_val;
13087 static struct {
13088 u16 offset;
13089 u16 flags;
13090#define TG3_FL_5705 0x1
13091#define TG3_FL_NOT_5705 0x2
13092#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070013093#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070013094 u32 read_mask;
13095 u32 write_mask;
13096 } reg_tbl[] = {
13097 /* MAC Control Registers */
13098 { MAC_MODE, TG3_FL_NOT_5705,
13099 0x00000000, 0x00ef6f8c },
13100 { MAC_MODE, TG3_FL_5705,
13101 0x00000000, 0x01ef6b8c },
13102 { MAC_STATUS, TG3_FL_NOT_5705,
13103 0x03800107, 0x00000000 },
13104 { MAC_STATUS, TG3_FL_5705,
13105 0x03800100, 0x00000000 },
13106 { MAC_ADDR_0_HIGH, 0x0000,
13107 0x00000000, 0x0000ffff },
13108 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013109 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070013110 { MAC_RX_MTU_SIZE, 0x0000,
13111 0x00000000, 0x0000ffff },
13112 { MAC_TX_MODE, 0x0000,
13113 0x00000000, 0x00000070 },
13114 { MAC_TX_LENGTHS, 0x0000,
13115 0x00000000, 0x00003fff },
13116 { MAC_RX_MODE, TG3_FL_NOT_5705,
13117 0x00000000, 0x000007fc },
13118 { MAC_RX_MODE, TG3_FL_5705,
13119 0x00000000, 0x000007dc },
13120 { MAC_HASH_REG_0, 0x0000,
13121 0x00000000, 0xffffffff },
13122 { MAC_HASH_REG_1, 0x0000,
13123 0x00000000, 0xffffffff },
13124 { MAC_HASH_REG_2, 0x0000,
13125 0x00000000, 0xffffffff },
13126 { MAC_HASH_REG_3, 0x0000,
13127 0x00000000, 0xffffffff },
13128
13129 /* Receive Data and Receive BD Initiator Control Registers. */
13130 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
13131 0x00000000, 0xffffffff },
13132 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
13133 0x00000000, 0xffffffff },
13134 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
13135 0x00000000, 0x00000003 },
13136 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
13137 0x00000000, 0xffffffff },
13138 { RCVDBDI_STD_BD+0, 0x0000,
13139 0x00000000, 0xffffffff },
13140 { RCVDBDI_STD_BD+4, 0x0000,
13141 0x00000000, 0xffffffff },
13142 { RCVDBDI_STD_BD+8, 0x0000,
13143 0x00000000, 0xffff0002 },
13144 { RCVDBDI_STD_BD+0xc, 0x0000,
13145 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013146
Michael Chana71116d2005-05-29 14:58:11 -070013147 /* Receive BD Initiator Control Registers. */
13148 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
13149 0x00000000, 0xffffffff },
13150 { RCVBDI_STD_THRESH, TG3_FL_5705,
13151 0x00000000, 0x000003ff },
13152 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13153 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013154
Michael Chana71116d2005-05-29 14:58:11 -070013155 /* Host Coalescing Control Registers. */
13156 { HOSTCC_MODE, TG3_FL_NOT_5705,
13157 0x00000000, 0x00000004 },
13158 { HOSTCC_MODE, TG3_FL_5705,
13159 0x00000000, 0x000000f6 },
13160 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13161 0x00000000, 0xffffffff },
13162 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13163 0x00000000, 0x000003ff },
13164 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13165 0x00000000, 0xffffffff },
13166 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13167 0x00000000, 0x000003ff },
13168 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13169 0x00000000, 0xffffffff },
13170 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13171 0x00000000, 0x000000ff },
13172 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13173 0x00000000, 0xffffffff },
13174 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13175 0x00000000, 0x000000ff },
13176 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13177 0x00000000, 0xffffffff },
13178 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13179 0x00000000, 0xffffffff },
13180 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13181 0x00000000, 0xffffffff },
13182 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13183 0x00000000, 0x000000ff },
13184 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13185 0x00000000, 0xffffffff },
13186 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13187 0x00000000, 0x000000ff },
13188 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13189 0x00000000, 0xffffffff },
13190 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13191 0x00000000, 0xffffffff },
13192 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13193 0x00000000, 0xffffffff },
13194 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13195 0x00000000, 0xffffffff },
13196 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13197 0x00000000, 0xffffffff },
13198 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13199 0xffffffff, 0x00000000 },
13200 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13201 0xffffffff, 0x00000000 },
13202
13203 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013204 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013205 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013206 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013207 0x00000000, 0x007fffff },
13208 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13209 0x00000000, 0x0000003f },
13210 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13211 0x00000000, 0x000001ff },
13212 { BUFMGR_MB_HIGH_WATER, 0x0000,
13213 0x00000000, 0x000001ff },
13214 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13215 0xffffffff, 0x00000000 },
13216 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13217 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013218
Michael Chana71116d2005-05-29 14:58:11 -070013219 /* Mailbox Registers */
13220 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13221 0x00000000, 0x000001ff },
13222 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13223 0x00000000, 0x000001ff },
13224 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13225 0x00000000, 0x000007ff },
13226 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13227 0x00000000, 0x000001ff },
13228
13229 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13230 };
13231
Michael Chanb16250e2006-09-27 16:10:14 -070013232 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013233 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013234 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013235 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013236 is_5750 = 1;
13237 }
Michael Chana71116d2005-05-29 14:58:11 -070013238
13239 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13240 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13241 continue;
13242
13243 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13244 continue;
13245
Joe Perches63c3a662011-04-26 08:12:10 +000013246 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013247 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13248 continue;
13249
Michael Chanb16250e2006-09-27 16:10:14 -070013250 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13251 continue;
13252
Michael Chana71116d2005-05-29 14:58:11 -070013253 offset = (u32) reg_tbl[i].offset;
13254 read_mask = reg_tbl[i].read_mask;
13255 write_mask = reg_tbl[i].write_mask;
13256
13257 /* Save the original register content */
13258 save_val = tr32(offset);
13259
13260 /* Determine the read-only value. */
13261 read_val = save_val & read_mask;
13262
13263 /* Write zero to the register, then make sure the read-only bits
13264 * are not changed and the read/write bits are all zeros.
13265 */
13266 tw32(offset, 0);
13267
13268 val = tr32(offset);
13269
13270 /* Test the read-only and read/write bits. */
13271 if (((val & read_mask) != read_val) || (val & write_mask))
13272 goto out;
13273
13274 /* Write ones to all the bits defined by RdMask and WrMask, then
13275 * make sure the read-only bits are not changed and the
13276 * read/write bits are all ones.
13277 */
13278 tw32(offset, read_mask | write_mask);
13279
13280 val = tr32(offset);
13281
13282 /* Test the read-only bits. */
13283 if ((val & read_mask) != read_val)
13284 goto out;
13285
13286 /* Test the read/write bits. */
13287 if ((val & write_mask) != write_mask)
13288 goto out;
13289
13290 tw32(offset, save_val);
13291 }
13292
13293 return 0;
13294
13295out:
Michael Chan9f88f292006-12-07 00:22:54 -080013296 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013297 netdev_err(tp->dev,
13298 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013299 tw32(offset, save_val);
13300 return -EIO;
13301}
13302
Michael Chan7942e1d2005-05-29 14:58:36 -070013303static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13304{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013305 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013306 int i;
13307 u32 j;
13308
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013309 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013310 for (j = 0; j < len; j += 4) {
13311 u32 val;
13312
13313 tg3_write_mem(tp, offset + j, test_pattern[i]);
13314 tg3_read_mem(tp, offset + j, &val);
13315 if (val != test_pattern[i])
13316 return -EIO;
13317 }
13318 }
13319 return 0;
13320}
13321
13322static int tg3_test_memory(struct tg3 *tp)
13323{
13324 static struct mem_entry {
13325 u32 offset;
13326 u32 len;
13327 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013328 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013329 { 0x00002000, 0x1c000},
13330 { 0xffffffff, 0x00000}
13331 }, mem_tbl_5705[] = {
13332 { 0x00000100, 0x0000c},
13333 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013334 { 0x00004000, 0x00800},
13335 { 0x00006000, 0x01000},
13336 { 0x00008000, 0x02000},
13337 { 0x00010000, 0x0e000},
13338 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013339 }, mem_tbl_5755[] = {
13340 { 0x00000200, 0x00008},
13341 { 0x00004000, 0x00800},
13342 { 0x00006000, 0x00800},
13343 { 0x00008000, 0x02000},
13344 { 0x00010000, 0x0c000},
13345 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013346 }, mem_tbl_5906[] = {
13347 { 0x00000200, 0x00008},
13348 { 0x00004000, 0x00400},
13349 { 0x00006000, 0x00400},
13350 { 0x00008000, 0x01000},
13351 { 0x00010000, 0x01000},
13352 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013353 }, mem_tbl_5717[] = {
13354 { 0x00000200, 0x00008},
13355 { 0x00010000, 0x0a000},
13356 { 0x00020000, 0x13c00},
13357 { 0xffffffff, 0x00000}
13358 }, mem_tbl_57765[] = {
13359 { 0x00000200, 0x00008},
13360 { 0x00004000, 0x00800},
13361 { 0x00006000, 0x09800},
13362 { 0x00010000, 0x0a000},
13363 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013364 };
13365 struct mem_entry *mem_tbl;
13366 int err = 0;
13367 int i;
13368
Joe Perches63c3a662011-04-26 08:12:10 +000013369 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013370 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013371 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013372 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013373 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013374 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013375 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013376 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013377 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013378 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013379 mem_tbl = mem_tbl_5705;
13380 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013381 mem_tbl = mem_tbl_570x;
13382
13383 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013384 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13385 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013386 break;
13387 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013388
Michael Chan7942e1d2005-05-29 14:58:36 -070013389 return err;
13390}
13391
Matt Carlsonbb158d62011-04-25 12:42:47 +000013392#define TG3_TSO_MSS 500
13393
13394#define TG3_TSO_IP_HDR_LEN 20
13395#define TG3_TSO_TCP_HDR_LEN 20
13396#define TG3_TSO_TCP_OPT_LEN 12
13397
13398static const u8 tg3_tso_header[] = {
133990x08, 0x00,
134000x45, 0x00, 0x00, 0x00,
134010x00, 0x00, 0x40, 0x00,
134020x40, 0x06, 0x00, 0x00,
134030x0a, 0x00, 0x00, 0x01,
134040x0a, 0x00, 0x00, 0x02,
134050x0d, 0x00, 0xe0, 0x00,
134060x00, 0x00, 0x01, 0x00,
134070x00, 0x00, 0x02, 0x00,
134080x80, 0x10, 0x10, 0x00,
134090x14, 0x09, 0x00, 0x00,
134100x01, 0x01, 0x08, 0x0a,
134110x11, 0x11, 0x11, 0x11,
134120x11, 0x11, 0x11, 0x11,
13413};
Michael Chan9f40dea2005-09-05 17:53:06 -070013414
Matt Carlson28a45952011-08-19 13:58:22 +000013415static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013416{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013417 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013418 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013419 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013420 struct sk_buff *skb;
13421 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013422 dma_addr_t map;
13423 int num_pkts, tx_len, rx_len, i, err;
13424 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013425 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013426 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013427
Matt Carlsonc8873402010-02-12 14:47:11 +000013428 tnapi = &tp->napi[0];
13429 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013430 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013431 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013432 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013433 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013434 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013435 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013436 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013437
Michael Chanc76949a2005-05-29 14:58:59 -070013438 err = -EIO;
13439
Matt Carlson4852a862011-04-13 11:05:07 +000013440 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013441 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013442 if (!skb)
13443 return -ENOMEM;
13444
Michael Chanc76949a2005-05-29 14:58:59 -070013445 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013446 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13447 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013448
Matt Carlson4852a862011-04-13 11:05:07 +000013449 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013450
Matt Carlson28a45952011-08-19 13:58:22 +000013451 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013452 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13453
13454 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13455 TG3_TSO_TCP_OPT_LEN;
13456
13457 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13458 sizeof(tg3_tso_header));
13459 mss = TG3_TSO_MSS;
13460
13461 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13462 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13463
13464 /* Set the total length field in the IP header */
13465 iph->tot_len = htons((u16)(mss + hdr_len));
13466
13467 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13468 TXD_FLAG_CPU_POST_DMA);
13469
Joe Perches63c3a662011-04-26 08:12:10 +000013470 if (tg3_flag(tp, HW_TSO_1) ||
13471 tg3_flag(tp, HW_TSO_2) ||
13472 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013473 struct tcphdr *th;
13474 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13475 th = (struct tcphdr *)&tx_data[val];
13476 th->check = 0;
13477 } else
13478 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13479
Joe Perches63c3a662011-04-26 08:12:10 +000013480 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013481 mss |= (hdr_len & 0xc) << 12;
13482 if (hdr_len & 0x10)
13483 base_flags |= 0x00000010;
13484 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013485 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013486 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013487 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013488 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013489 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13490 } else {
13491 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13492 }
13493
13494 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13495 } else {
13496 num_pkts = 1;
13497 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013498
13499 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13500 tx_len > VLAN_ETH_FRAME_LEN)
13501 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013502 }
13503
13504 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013505 tx_data[i] = (u8) (i & 0xff);
13506
Alexander Duyckf4188d82009-12-02 16:48:38 +000013507 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13508 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013509 dev_kfree_skb(skb);
13510 return -EIO;
13511 }
Michael Chanc76949a2005-05-29 14:58:59 -070013512
Matt Carlson0d681b22011-07-27 14:20:49 +000013513 val = tnapi->tx_prod;
13514 tnapi->tx_buffers[val].skb = skb;
13515 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13516
Michael Chanc76949a2005-05-29 14:58:59 -070013517 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013518 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013519
13520 udelay(10);
13521
Matt Carlson898a56f2009-08-28 14:02:40 +000013522 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013523
Matt Carlson84b67b22011-07-27 14:20:52 +000013524 budget = tg3_tx_avail(tnapi);
13525 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013526 base_flags | TXD_FLAG_END, mss, 0)) {
13527 tnapi->tx_buffers[val].skb = NULL;
13528 dev_kfree_skb(skb);
13529 return -EIO;
13530 }
Michael Chanc76949a2005-05-29 14:58:59 -070013531
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013532 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013533
Michael Chan6541b802012-03-04 14:48:14 +000013534 /* Sync BD data before updating mailbox */
13535 wmb();
13536
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013537 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13538 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013539
13540 udelay(10);
13541
Matt Carlson303fc922009-11-02 14:27:34 +000013542 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13543 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013544 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013545 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013546
13547 udelay(10);
13548
Matt Carlson898a56f2009-08-28 14:02:40 +000013549 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13550 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013551 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013552 (rx_idx == (rx_start_idx + num_pkts)))
13553 break;
13554 }
13555
Matt Carlsonba1142e2011-11-04 09:15:00 +000013556 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013557 dev_kfree_skb(skb);
13558
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013559 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013560 goto out;
13561
13562 if (rx_idx != rx_start_idx + num_pkts)
13563 goto out;
13564
Matt Carlsonbb158d62011-04-25 12:42:47 +000013565 val = data_off;
13566 while (rx_idx != rx_start_idx) {
13567 desc = &rnapi->rx_rcb[rx_start_idx++];
13568 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13569 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013570
Matt Carlsonbb158d62011-04-25 12:42:47 +000013571 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13572 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013573 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013574
Matt Carlsonbb158d62011-04-25 12:42:47 +000013575 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13576 - ETH_FCS_LEN;
13577
Matt Carlson28a45952011-08-19 13:58:22 +000013578 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013579 if (rx_len != tx_len)
13580 goto out;
13581
13582 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13583 if (opaque_key != RXD_OPAQUE_RING_STD)
13584 goto out;
13585 } else {
13586 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13587 goto out;
13588 }
13589 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13590 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013591 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013592 goto out;
13593 }
13594
13595 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013596 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013597 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13598 mapping);
13599 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013600 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013601 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13602 mapping);
13603 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013604 goto out;
13605
Matt Carlsonbb158d62011-04-25 12:42:47 +000013606 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13607 PCI_DMA_FROMDEVICE);
13608
Eric Dumazet9205fd92011-11-18 06:47:01 +000013609 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013610 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013611 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013612 goto out;
13613 }
Matt Carlson4852a862011-04-13 11:05:07 +000013614 }
13615
Michael Chanc76949a2005-05-29 14:58:59 -070013616 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013617
Eric Dumazet9205fd92011-11-18 06:47:01 +000013618 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013619out:
13620 return err;
13621}
13622
Matt Carlson00c266b2011-04-25 12:42:46 +000013623#define TG3_STD_LOOPBACK_FAILED 1
13624#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013625#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013626#define TG3_LOOPBACK_FAILED \
13627 (TG3_STD_LOOPBACK_FAILED | \
13628 TG3_JMB_LOOPBACK_FAILED | \
13629 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013630
Matt Carlson941ec902011-08-19 13:58:23 +000013631static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013632{
Matt Carlson28a45952011-08-19 13:58:22 +000013633 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013634 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013635 u32 jmb_pkt_sz = 9000;
13636
13637 if (tp->dma_limit)
13638 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013639
Matt Carlsonab789042011-01-25 15:58:54 +000013640 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13641 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13642
Matt Carlson28a45952011-08-19 13:58:22 +000013643 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013644 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13645 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013646 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013647 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013648 goto done;
13649 }
13650
Joe Perches953c96e2013-04-09 10:18:14 +000013651 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013652 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013653 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13654 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013655 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013656 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013657 goto done;
13658 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013659
Joe Perches63c3a662011-04-26 08:12:10 +000013660 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013661 int i;
13662
13663 /* Reroute all rx packets to the 1st queue */
13664 for (i = MAC_RSS_INDIR_TBL_0;
13665 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13666 tw32(i, 0x0);
13667 }
13668
Matt Carlson6e01b202011-08-19 13:58:20 +000013669 /* HW errata - mac loopback fails in some cases on 5780.
13670 * Normal traffic and PHY loopback are not affected by
13671 * errata. Also, the MAC loopback test is deprecated for
13672 * all newer ASIC revisions.
13673 */
Joe Perches41535772013-02-16 11:20:04 +000013674 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013675 !tg3_flag(tp, CPMU_PRESENT)) {
13676 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013677
Matt Carlson28a45952011-08-19 13:58:22 +000013678 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013679 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013680
13681 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013682 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013683 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013684
13685 tg3_mac_loopback(tp, false);
13686 }
Matt Carlson4852a862011-04-13 11:05:07 +000013687
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013688 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013689 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013690 int i;
13691
Matt Carlson941ec902011-08-19 13:58:23 +000013692 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013693
13694 /* Wait for link */
13695 for (i = 0; i < 100; i++) {
13696 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13697 break;
13698 mdelay(1);
13699 }
13700
Matt Carlson28a45952011-08-19 13:58:22 +000013701 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013702 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013703 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013704 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013705 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013706 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013707 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013708 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013709
Matt Carlson941ec902011-08-19 13:58:23 +000013710 if (do_extlpbk) {
13711 tg3_phy_lpbk_set(tp, 0, true);
13712
13713 /* All link indications report up, but the hardware
13714 * isn't really ready for about 20 msec. Double it
13715 * to be sure.
13716 */
13717 mdelay(40);
13718
13719 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013720 data[TG3_EXT_LOOPB_TEST] |=
13721 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013722 if (tg3_flag(tp, TSO_CAPABLE) &&
13723 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013724 data[TG3_EXT_LOOPB_TEST] |=
13725 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013726 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013727 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013728 data[TG3_EXT_LOOPB_TEST] |=
13729 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013730 }
13731
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013732 /* Re-enable gphy autopowerdown. */
13733 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13734 tg3_phy_toggle_apd(tp, true);
13735 }
Matt Carlson6833c042008-11-21 17:18:59 -080013736
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013737 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13738 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013739
Matt Carlsonab789042011-01-25 15:58:54 +000013740done:
13741 tp->phy_flags |= eee_cap;
13742
Michael Chan9f40dea2005-09-05 17:53:06 -070013743 return err;
13744}
13745
Michael Chan4cafd3f2005-05-29 14:56:34 -070013746static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13747 u64 *data)
13748{
Michael Chan566f86a2005-05-29 14:56:58 -070013749 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013750 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013751
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013752 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13753 if (tg3_power_up(tp)) {
13754 etest->flags |= ETH_TEST_FL_FAILED;
13755 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13756 return;
13757 }
13758 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013759 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013760
Michael Chan566f86a2005-05-29 14:56:58 -070013761 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13762
13763 if (tg3_test_nvram(tp) != 0) {
13764 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013765 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013766 }
Matt Carlson941ec902011-08-19 13:58:23 +000013767 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013768 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013769 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013770 }
Michael Chana71116d2005-05-29 14:58:11 -070013771 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013772 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013773
Michael Chanbbe832c2005-06-24 20:20:04 -070013774 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013775 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013776 tg3_netif_stop(tp);
13777 irq_sync = 1;
13778 }
13779
13780 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013781 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013782 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013783 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013784 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013785 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013786 if (!err)
13787 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013788
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013789 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013790 tg3_phy_reset(tp);
13791
Michael Chana71116d2005-05-29 14:58:11 -070013792 if (tg3_test_registers(tp) != 0) {
13793 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013794 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013795 }
Matt Carlson28a45952011-08-19 13:58:22 +000013796
Michael Chan7942e1d2005-05-29 14:58:36 -070013797 if (tg3_test_memory(tp) != 0) {
13798 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013799 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013800 }
Matt Carlson28a45952011-08-19 13:58:22 +000013801
Matt Carlson941ec902011-08-19 13:58:23 +000013802 if (doextlpbk)
13803 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13804
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013805 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013806 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013807
David S. Millerf47c11e2005-06-24 20:18:35 -070013808 tg3_full_unlock(tp);
13809
Michael Chand4bc3922005-05-29 14:59:20 -070013810 if (tg3_test_interrupt(tp) != 0) {
13811 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013812 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013813 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013814
13815 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013816
Michael Chana71116d2005-05-29 14:58:11 -070013817 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13818 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013819 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013820 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013821 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013822 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013823 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013824
13825 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013826
13827 if (irq_sync && !err2)
13828 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013829 }
Matt Carlson80096062010-08-02 11:26:06 +000013830 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013831 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013832
Michael Chan4cafd3f2005-05-29 14:56:34 -070013833}
13834
Ben Hutchings72608992013-11-18 22:59:43 +000013835static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013836{
13837 struct tg3 *tp = netdev_priv(dev);
13838 struct hwtstamp_config stmpconf;
13839
13840 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013841 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013842
13843 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13844 return -EFAULT;
13845
13846 if (stmpconf.flags)
13847 return -EINVAL;
13848
Ben Hutchings58b187c2013-11-14 00:40:56 +000013849 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13850 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013851 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013852
13853 switch (stmpconf.rx_filter) {
13854 case HWTSTAMP_FILTER_NONE:
13855 tp->rxptpctl = 0;
13856 break;
13857 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13858 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13859 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13860 break;
13861 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13862 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13863 TG3_RX_PTP_CTL_SYNC_EVNT;
13864 break;
13865 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13866 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13867 TG3_RX_PTP_CTL_DELAY_REQ;
13868 break;
13869 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13870 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13871 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13872 break;
13873 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13874 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13875 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13876 break;
13877 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13878 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13879 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13880 break;
13881 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13882 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13883 TG3_RX_PTP_CTL_SYNC_EVNT;
13884 break;
13885 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13886 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13887 TG3_RX_PTP_CTL_SYNC_EVNT;
13888 break;
13889 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13890 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13891 TG3_RX_PTP_CTL_SYNC_EVNT;
13892 break;
13893 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13894 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13895 TG3_RX_PTP_CTL_DELAY_REQ;
13896 break;
13897 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13898 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13899 TG3_RX_PTP_CTL_DELAY_REQ;
13900 break;
13901 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13902 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13903 TG3_RX_PTP_CTL_DELAY_REQ;
13904 break;
13905 default:
13906 return -ERANGE;
13907 }
13908
13909 if (netif_running(dev) && tp->rxptpctl)
13910 tw32(TG3_RX_PTP_CTL,
13911 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13912
Ben Hutchings58b187c2013-11-14 00:40:56 +000013913 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13914 tg3_flag_set(tp, TX_TSTAMP_EN);
13915 else
13916 tg3_flag_clear(tp, TX_TSTAMP_EN);
13917
Matt Carlson0a633ac2012-12-03 19:36:59 +000013918 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13919 -EFAULT : 0;
13920}
13921
Ben Hutchings72608992013-11-18 22:59:43 +000013922static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13923{
13924 struct tg3 *tp = netdev_priv(dev);
13925 struct hwtstamp_config stmpconf;
13926
13927 if (!tg3_flag(tp, PTP_CAPABLE))
13928 return -EOPNOTSUPP;
13929
13930 stmpconf.flags = 0;
13931 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13932 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13933
13934 switch (tp->rxptpctl) {
13935 case 0:
13936 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13937 break;
13938 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13939 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13940 break;
13941 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13942 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13943 break;
13944 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13945 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13946 break;
13947 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13948 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13949 break;
13950 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13951 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13952 break;
13953 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13954 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13955 break;
13956 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13957 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13958 break;
13959 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13960 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13961 break;
13962 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13963 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13964 break;
13965 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13966 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13967 break;
13968 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13969 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13970 break;
13971 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13972 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13973 break;
13974 default:
13975 WARN_ON_ONCE(1);
13976 return -ERANGE;
13977 }
13978
13979 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13980 -EFAULT : 0;
13981}
13982
Linus Torvalds1da177e2005-04-16 15:20:36 -070013983static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13984{
13985 struct mii_ioctl_data *data = if_mii(ifr);
13986 struct tg3 *tp = netdev_priv(dev);
13987 int err;
13988
Joe Perches63c3a662011-04-26 08:12:10 +000013989 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013990 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013991 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013992 return -EAGAIN;
Andrew Lunn7f854422016-01-06 20:11:18 +010013993 phydev = mdiobus_get_phy(tp->mdio_bus, tp->phy_addr);
Richard Cochran28b04112010-07-17 08:48:55 +000013994 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013995 }
13996
Matt Carlson33f401a2010-04-05 10:19:27 +000013997 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013998 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013999 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014000
14001 /* fallthru */
14002 case SIOCGMIIREG: {
14003 u32 mii_regval;
14004
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014005 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014006 break; /* We have no PHY */
14007
Matt Carlson34eea5a2011-04-20 07:57:38 +000014008 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080014009 return -EAGAIN;
14010
David S. Millerf47c11e2005-06-24 20:18:35 -070014011 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000014012 err = __tg3_readphy(tp, data->phy_id & 0x1f,
14013 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070014014 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014015
14016 data->val_out = mii_regval;
14017
14018 return err;
14019 }
14020
14021 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014022 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023 break; /* We have no PHY */
14024
Matt Carlson34eea5a2011-04-20 07:57:38 +000014025 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080014026 return -EAGAIN;
14027
David S. Millerf47c11e2005-06-24 20:18:35 -070014028 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000014029 err = __tg3_writephy(tp, data->phy_id & 0x1f,
14030 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070014031 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014032
14033 return err;
14034
Matt Carlson0a633ac2012-12-03 19:36:59 +000014035 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000014036 return tg3_hwtstamp_set(dev, ifr);
14037
14038 case SIOCGHWTSTAMP:
14039 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000014040
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 default:
14042 /* do nothing */
14043 break;
14044 }
14045 return -EOPNOTSUPP;
14046}
14047
David S. Miller15f98502005-05-18 22:49:26 -070014048static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14049{
14050 struct tg3 *tp = netdev_priv(dev);
14051
14052 memcpy(ec, &tp->coal, sizeof(*ec));
14053 return 0;
14054}
14055
Michael Chand244c892005-07-05 14:42:33 -070014056static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
14057{
14058 struct tg3 *tp = netdev_priv(dev);
14059 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
14060 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
14061
Joe Perches63c3a662011-04-26 08:12:10 +000014062 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014063 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
14064 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
14065 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
14066 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
14067 }
14068
14069 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
Satish Baddipadige087d7a8c2016-08-03 09:43:59 +053014070 (!ec->rx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014071 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
Ivan Veceraaabdd092016-09-01 11:28:59 +020014072 (!ec->tx_coalesce_usecs) ||
Michael Chand244c892005-07-05 14:42:33 -070014073 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
14074 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
14075 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
14076 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
14077 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
14078 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
14079 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
14080 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
14081 return -EINVAL;
14082
Michael Chand244c892005-07-05 14:42:33 -070014083 /* Only copy relevant parameters, ignore all others. */
14084 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
14085 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
14086 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
14087 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
14088 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
14089 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
14090 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
14091 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
14092 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
14093
14094 if (netif_running(dev)) {
14095 tg3_full_lock(tp, 0);
14096 __tg3_set_coalesce(tp, &tp->coal);
14097 tg3_full_unlock(tp);
14098 }
14099 return 0;
14100}
14101
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014102static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
14103{
14104 struct tg3 *tp = netdev_priv(dev);
14105
14106 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14107 netdev_warn(tp->dev, "Board does not support EEE!\n");
14108 return -EOPNOTSUPP;
14109 }
14110
14111 if (edata->advertised != tp->eee.advertised) {
14112 netdev_warn(tp->dev,
14113 "Direct manipulation of EEE advertisement is not supported\n");
14114 return -EINVAL;
14115 }
14116
14117 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
14118 netdev_warn(tp->dev,
14119 "Maximal Tx Lpi timer supported is %#x(u)\n",
14120 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
14121 return -EINVAL;
14122 }
14123
14124 tp->eee = *edata;
14125
14126 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
14127 tg3_warn_mgmt_link_flap(tp);
14128
14129 if (netif_running(tp->dev)) {
14130 tg3_full_lock(tp, 0);
14131 tg3_setup_eee(tp);
14132 tg3_phy_reset(tp);
14133 tg3_full_unlock(tp);
14134 }
14135
14136 return 0;
14137}
14138
14139static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
14140{
14141 struct tg3 *tp = netdev_priv(dev);
14142
14143 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
14144 netdev_warn(tp->dev,
14145 "Board does not support EEE!\n");
14146 return -EOPNOTSUPP;
14147 }
14148
14149 *edata = tp->eee;
14150 return 0;
14151}
14152
Jeff Garzik7282d492006-09-13 14:30:00 -040014153static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154 .get_drvinfo = tg3_get_drvinfo,
14155 .get_regs_len = tg3_get_regs_len,
14156 .get_regs = tg3_get_regs,
14157 .get_wol = tg3_get_wol,
14158 .set_wol = tg3_set_wol,
14159 .get_msglevel = tg3_get_msglevel,
14160 .set_msglevel = tg3_set_msglevel,
14161 .nway_reset = tg3_nway_reset,
14162 .get_link = ethtool_op_get_link,
14163 .get_eeprom_len = tg3_get_eeprom_len,
14164 .get_eeprom = tg3_get_eeprom,
14165 .set_eeprom = tg3_set_eeprom,
14166 .get_ringparam = tg3_get_ringparam,
14167 .set_ringparam = tg3_set_ringparam,
14168 .get_pauseparam = tg3_get_pauseparam,
14169 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014170 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014172 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014174 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014175 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014176 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014177 .get_rxnfc = tg3_get_rxnfc,
14178 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
Ben Hutchingsfe62d002014-05-15 01:25:27 +010014179 .get_rxfh = tg3_get_rxfh,
14180 .set_rxfh = tg3_set_rxfh,
Michael Chan09681692012-09-28 07:12:42 +000014181 .get_channels = tg3_get_channels,
14182 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014183 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014184 .get_eee = tg3_get_eee,
14185 .set_eee = tg3_set_eee,
Philippe Reynesb6f5be22016-09-25 23:31:24 +020014186 .get_link_ksettings = tg3_get_link_ksettings,
14187 .set_link_ksettings = tg3_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014188};
14189
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014190static void tg3_get_stats64(struct net_device *dev,
14191 struct rtnl_link_stats64 *stats)
David S. Millerb4017c52012-03-01 17:57:40 -050014192{
14193 struct tg3 *tp = netdev_priv(dev);
14194
David S. Millerb4017c52012-03-01 17:57:40 -050014195 spin_lock_bh(&tp->lock);
Michael Chand89a2ad2018-05-03 20:04:27 -040014196 if (!tp->hw_stats || !tg3_flag(tp, INIT_COMPLETE)) {
Govindarajulu Varadarajan7b31b4d2014-08-13 13:04:56 +053014197 *stats = tp->net_stats_prev;
Michael Chan0f566b22012-07-29 19:15:44 +000014198 spin_unlock_bh(&tp->lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -080014199 return;
Michael Chan0f566b22012-07-29 19:15:44 +000014200 }
14201
David S. Millerb4017c52012-03-01 17:57:40 -050014202 tg3_get_nstats(tp, stats);
14203 spin_unlock_bh(&tp->lock);
David S. Millerb4017c52012-03-01 17:57:40 -050014204}
14205
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014206static void tg3_set_rx_mode(struct net_device *dev)
14207{
14208 struct tg3 *tp = netdev_priv(dev);
14209
14210 if (!netif_running(dev))
14211 return;
14212
14213 tg3_full_lock(tp, 0);
14214 __tg3_set_rx_mode(dev);
14215 tg3_full_unlock(tp);
14216}
14217
Matt Carlsonfaf16272012-02-13 10:20:07 +000014218static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14219 int new_mtu)
14220{
14221 dev->mtu = new_mtu;
14222
14223 if (new_mtu > ETH_DATA_LEN) {
14224 if (tg3_flag(tp, 5780_CLASS)) {
14225 netdev_update_features(dev);
14226 tg3_flag_clear(tp, TSO_CAPABLE);
14227 } else {
14228 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14229 }
14230 } else {
14231 if (tg3_flag(tp, 5780_CLASS)) {
14232 tg3_flag_set(tp, TSO_CAPABLE);
14233 netdev_update_features(dev);
14234 }
14235 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14236 }
14237}
14238
14239static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14240{
14241 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014242 int err;
14243 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014244
Matt Carlsonfaf16272012-02-13 10:20:07 +000014245 if (!netif_running(dev)) {
14246 /* We'll just catch it later when the
14247 * device is up'd.
14248 */
14249 tg3_set_mtu(dev, tp, new_mtu);
14250 return 0;
14251 }
14252
14253 tg3_phy_stop(tp);
14254
14255 tg3_netif_stop(tp);
14256
Nithin Sujirc6993df2014-02-06 14:13:05 -080014257 tg3_set_mtu(dev, tp, new_mtu);
14258
Matt Carlsonfaf16272012-02-13 10:20:07 +000014259 tg3_full_lock(tp, 1);
14260
14261 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14262
Michael Chan2fae5e32012-03-04 14:48:15 +000014263 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14264 * breaks all requests to 256 bytes.
14265 */
Brian King748a2402017-12-15 15:21:50 -060014266 if (tg3_asic_rev(tp) == ASIC_REV_57766 ||
14267 tg3_asic_rev(tp) == ASIC_REV_5717 ||
Siva Reddy Kallame60ee412017-12-22 16:05:29 +053014268 tg3_asic_rev(tp) == ASIC_REV_5719 ||
14269 tg3_asic_rev(tp) == ASIC_REV_5720)
Joe Perches953c96e2013-04-09 10:18:14 +000014270 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014271
14272 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014273
14274 if (!err)
14275 tg3_netif_start(tp);
14276
14277 tg3_full_unlock(tp);
14278
14279 if (!err)
14280 tg3_phy_start(tp);
14281
14282 return err;
14283}
14284
14285static const struct net_device_ops tg3_netdev_ops = {
14286 .ndo_open = tg3_open,
14287 .ndo_stop = tg3_close,
14288 .ndo_start_xmit = tg3_start_xmit,
14289 .ndo_get_stats64 = tg3_get_stats64,
14290 .ndo_validate_addr = eth_validate_addr,
14291 .ndo_set_rx_mode = tg3_set_rx_mode,
14292 .ndo_set_mac_address = tg3_set_mac_addr,
14293 .ndo_do_ioctl = tg3_ioctl,
14294 .ndo_tx_timeout = tg3_tx_timeout,
14295 .ndo_change_mtu = tg3_change_mtu,
14296 .ndo_fix_features = tg3_fix_features,
14297 .ndo_set_features = tg3_set_features,
14298#ifdef CONFIG_NET_POLL_CONTROLLER
14299 .ndo_poll_controller = tg3_poll_controller,
14300#endif
14301};
14302
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014303static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304{
Michael Chan1b277772006-03-20 22:27:48 -080014305 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014306
14307 tp->nvram_size = EEPROM_CHIP_SIZE;
14308
Matt Carlsone4f34112009-02-25 14:25:00 +000014309 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 return;
14311
Michael Chanb16250e2006-09-27 16:10:14 -070014312 if ((magic != TG3_EEPROM_MAGIC) &&
14313 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14314 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315 return;
14316
14317 /*
14318 * Size the chip by reading offsets at increasing powers of two.
14319 * When we encounter our validation signature, we know the addressing
14320 * has wrapped around, and thus have our chip size.
14321 */
Michael Chan1b277772006-03-20 22:27:48 -080014322 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323
14324 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014325 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 return;
14327
Michael Chan18201802006-03-20 22:29:15 -080014328 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329 break;
14330
14331 cursize <<= 1;
14332 }
14333
14334 tp->nvram_size = cursize;
14335}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014336
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014337static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338{
14339 u32 val;
14340
Joe Perches63c3a662011-04-26 08:12:10 +000014341 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014342 return;
14343
14344 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014345 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014346 tg3_get_eeprom_size(tp);
14347 return;
14348 }
14349
Matt Carlson6d348f22009-02-25 14:25:52 +000014350 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014351 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014352 /* This is confusing. We want to operate on the
14353 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14354 * call will read from NVRAM and byteswap the data
14355 * according to the byteswapping settings for all
14356 * other register accesses. This ensures the data we
14357 * want will always reside in the lower 16-bits.
14358 * However, the data in NVRAM is in LE format, which
14359 * means the data from the NVRAM read will always be
14360 * opposite the endianness of the CPU. The 16-bit
14361 * byteswap then brings the data to CPU endianness.
14362 */
14363 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 return;
14365 }
14366 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014367 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368}
14369
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014370static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371{
14372 u32 nvcfg1;
14373
14374 nvcfg1 = tr32(NVRAM_CFG1);
14375 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014376 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014377 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014378 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14379 tw32(NVRAM_CFG1, nvcfg1);
14380 }
14381
Joe Perches41535772013-02-16 11:20:04 +000014382 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014383 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014384 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014385 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14386 tp->nvram_jedecnum = JEDEC_ATMEL;
14387 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014388 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014389 break;
14390 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14391 tp->nvram_jedecnum = JEDEC_ATMEL;
14392 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14393 break;
14394 case FLASH_VENDOR_ATMEL_EEPROM:
14395 tp->nvram_jedecnum = JEDEC_ATMEL;
14396 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014397 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014398 break;
14399 case FLASH_VENDOR_ST:
14400 tp->nvram_jedecnum = JEDEC_ST;
14401 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014402 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014403 break;
14404 case FLASH_VENDOR_SAIFUN:
14405 tp->nvram_jedecnum = JEDEC_SAIFUN;
14406 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14407 break;
14408 case FLASH_VENDOR_SST_SMALL:
14409 case FLASH_VENDOR_SST_LARGE:
14410 tp->nvram_jedecnum = JEDEC_SST;
14411 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014413 }
Matt Carlson8590a602009-08-28 12:29:16 +000014414 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014415 tp->nvram_jedecnum = JEDEC_ATMEL;
14416 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014417 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418 }
14419}
14420
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014421static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014422{
14423 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14424 case FLASH_5752PAGE_SIZE_256:
14425 tp->nvram_pagesize = 256;
14426 break;
14427 case FLASH_5752PAGE_SIZE_512:
14428 tp->nvram_pagesize = 512;
14429 break;
14430 case FLASH_5752PAGE_SIZE_1K:
14431 tp->nvram_pagesize = 1024;
14432 break;
14433 case FLASH_5752PAGE_SIZE_2K:
14434 tp->nvram_pagesize = 2048;
14435 break;
14436 case FLASH_5752PAGE_SIZE_4K:
14437 tp->nvram_pagesize = 4096;
14438 break;
14439 case FLASH_5752PAGE_SIZE_264:
14440 tp->nvram_pagesize = 264;
14441 break;
14442 case FLASH_5752PAGE_SIZE_528:
14443 tp->nvram_pagesize = 528;
14444 break;
14445 }
14446}
14447
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014448static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014449{
14450 u32 nvcfg1;
14451
14452 nvcfg1 = tr32(NVRAM_CFG1);
14453
Michael Chane6af3012005-04-21 17:12:05 -070014454 /* NVRAM protection for TPM */
14455 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014456 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014457
Michael Chan361b4ac2005-04-21 17:11:21 -070014458 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014459 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14460 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14461 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014462 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014463 break;
14464 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14465 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014466 tg3_flag_set(tp, NVRAM_BUFFERED);
14467 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014468 break;
14469 case FLASH_5752VENDOR_ST_M45PE10:
14470 case FLASH_5752VENDOR_ST_M45PE20:
14471 case FLASH_5752VENDOR_ST_M45PE40:
14472 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014473 tg3_flag_set(tp, NVRAM_BUFFERED);
14474 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014475 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014476 }
14477
Joe Perches63c3a662011-04-26 08:12:10 +000014478 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014479 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014480 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014481 /* For eeprom, set pagesize to maximum eeprom size */
14482 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14483
14484 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14485 tw32(NVRAM_CFG1, nvcfg1);
14486 }
14487}
14488
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014489static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014490{
Matt Carlson989a9d22007-05-05 11:51:05 -070014491 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014492
14493 nvcfg1 = tr32(NVRAM_CFG1);
14494
14495 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014496 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014497 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014498 protect = 1;
14499 }
Michael Chand3c7b882006-03-23 01:28:25 -080014500
Matt Carlson989a9d22007-05-05 11:51:05 -070014501 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14502 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014503 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14504 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14505 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14506 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14507 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014508 tg3_flag_set(tp, NVRAM_BUFFERED);
14509 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014510 tp->nvram_pagesize = 264;
14511 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14512 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14513 tp->nvram_size = (protect ? 0x3e200 :
14514 TG3_NVRAM_SIZE_512KB);
14515 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14516 tp->nvram_size = (protect ? 0x1f200 :
14517 TG3_NVRAM_SIZE_256KB);
14518 else
14519 tp->nvram_size = (protect ? 0x1f200 :
14520 TG3_NVRAM_SIZE_128KB);
14521 break;
14522 case FLASH_5752VENDOR_ST_M45PE10:
14523 case FLASH_5752VENDOR_ST_M45PE20:
14524 case FLASH_5752VENDOR_ST_M45PE40:
14525 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014526 tg3_flag_set(tp, NVRAM_BUFFERED);
14527 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014528 tp->nvram_pagesize = 256;
14529 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14530 tp->nvram_size = (protect ?
14531 TG3_NVRAM_SIZE_64KB :
14532 TG3_NVRAM_SIZE_128KB);
14533 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14534 tp->nvram_size = (protect ?
14535 TG3_NVRAM_SIZE_64KB :
14536 TG3_NVRAM_SIZE_256KB);
14537 else
14538 tp->nvram_size = (protect ?
14539 TG3_NVRAM_SIZE_128KB :
14540 TG3_NVRAM_SIZE_512KB);
14541 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014542 }
14543}
14544
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014545static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014546{
14547 u32 nvcfg1;
14548
14549 nvcfg1 = tr32(NVRAM_CFG1);
14550
14551 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014552 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14553 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14554 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14555 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14556 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014557 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014558 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014559
Matt Carlson8590a602009-08-28 12:29:16 +000014560 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14561 tw32(NVRAM_CFG1, nvcfg1);
14562 break;
14563 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14564 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14565 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14566 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14567 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014568 tg3_flag_set(tp, NVRAM_BUFFERED);
14569 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014570 tp->nvram_pagesize = 264;
14571 break;
14572 case FLASH_5752VENDOR_ST_M45PE10:
14573 case FLASH_5752VENDOR_ST_M45PE20:
14574 case FLASH_5752VENDOR_ST_M45PE40:
14575 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014576 tg3_flag_set(tp, NVRAM_BUFFERED);
14577 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014578 tp->nvram_pagesize = 256;
14579 break;
Michael Chan1b277772006-03-20 22:27:48 -080014580 }
14581}
14582
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014583static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014584{
14585 u32 nvcfg1, protect = 0;
14586
14587 nvcfg1 = tr32(NVRAM_CFG1);
14588
14589 /* NVRAM protection for TPM */
14590 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014591 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014592 protect = 1;
14593 }
14594
14595 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14596 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014597 case FLASH_5761VENDOR_ATMEL_ADB021D:
14598 case FLASH_5761VENDOR_ATMEL_ADB041D:
14599 case FLASH_5761VENDOR_ATMEL_ADB081D:
14600 case FLASH_5761VENDOR_ATMEL_ADB161D:
14601 case FLASH_5761VENDOR_ATMEL_MDB021D:
14602 case FLASH_5761VENDOR_ATMEL_MDB041D:
14603 case FLASH_5761VENDOR_ATMEL_MDB081D:
14604 case FLASH_5761VENDOR_ATMEL_MDB161D:
14605 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014606 tg3_flag_set(tp, NVRAM_BUFFERED);
14607 tg3_flag_set(tp, FLASH);
14608 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014609 tp->nvram_pagesize = 256;
14610 break;
14611 case FLASH_5761VENDOR_ST_A_M45PE20:
14612 case FLASH_5761VENDOR_ST_A_M45PE40:
14613 case FLASH_5761VENDOR_ST_A_M45PE80:
14614 case FLASH_5761VENDOR_ST_A_M45PE16:
14615 case FLASH_5761VENDOR_ST_M_M45PE20:
14616 case FLASH_5761VENDOR_ST_M_M45PE40:
14617 case FLASH_5761VENDOR_ST_M_M45PE80:
14618 case FLASH_5761VENDOR_ST_M_M45PE16:
14619 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014620 tg3_flag_set(tp, NVRAM_BUFFERED);
14621 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014622 tp->nvram_pagesize = 256;
14623 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014624 }
14625
14626 if (protect) {
14627 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14628 } else {
14629 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014630 case FLASH_5761VENDOR_ATMEL_ADB161D:
14631 case FLASH_5761VENDOR_ATMEL_MDB161D:
14632 case FLASH_5761VENDOR_ST_A_M45PE16:
14633 case FLASH_5761VENDOR_ST_M_M45PE16:
14634 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14635 break;
14636 case FLASH_5761VENDOR_ATMEL_ADB081D:
14637 case FLASH_5761VENDOR_ATMEL_MDB081D:
14638 case FLASH_5761VENDOR_ST_A_M45PE80:
14639 case FLASH_5761VENDOR_ST_M_M45PE80:
14640 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14641 break;
14642 case FLASH_5761VENDOR_ATMEL_ADB041D:
14643 case FLASH_5761VENDOR_ATMEL_MDB041D:
14644 case FLASH_5761VENDOR_ST_A_M45PE40:
14645 case FLASH_5761VENDOR_ST_M_M45PE40:
14646 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14647 break;
14648 case FLASH_5761VENDOR_ATMEL_ADB021D:
14649 case FLASH_5761VENDOR_ATMEL_MDB021D:
14650 case FLASH_5761VENDOR_ST_A_M45PE20:
14651 case FLASH_5761VENDOR_ST_M_M45PE20:
14652 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14653 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014654 }
14655 }
14656}
14657
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014658static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014659{
14660 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014661 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014662 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14663}
14664
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014665static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014666{
14667 u32 nvcfg1;
14668
14669 nvcfg1 = tr32(NVRAM_CFG1);
14670
14671 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14672 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14673 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14674 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014675 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014676 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14677
14678 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14679 tw32(NVRAM_CFG1, nvcfg1);
14680 return;
14681 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14682 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14683 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14684 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14685 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14686 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14687 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14688 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014689 tg3_flag_set(tp, NVRAM_BUFFERED);
14690 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014691
14692 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14693 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14694 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14695 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14696 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14697 break;
14698 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14699 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14700 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14701 break;
14702 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14703 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14704 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14705 break;
14706 }
14707 break;
14708 case FLASH_5752VENDOR_ST_M45PE10:
14709 case FLASH_5752VENDOR_ST_M45PE20:
14710 case FLASH_5752VENDOR_ST_M45PE40:
14711 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014712 tg3_flag_set(tp, NVRAM_BUFFERED);
14713 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014714
14715 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14716 case FLASH_5752VENDOR_ST_M45PE10:
14717 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14718 break;
14719 case FLASH_5752VENDOR_ST_M45PE20:
14720 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14721 break;
14722 case FLASH_5752VENDOR_ST_M45PE40:
14723 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14724 break;
14725 }
14726 break;
14727 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014728 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014729 return;
14730 }
14731
Matt Carlsona1b950d2009-09-01 13:20:17 +000014732 tg3_nvram_get_pagesize(tp, nvcfg1);
14733 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014734 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014735}
14736
14737
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014738static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014739{
14740 u32 nvcfg1;
14741
14742 nvcfg1 = tr32(NVRAM_CFG1);
14743
14744 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14745 case FLASH_5717VENDOR_ATMEL_EEPROM:
14746 case FLASH_5717VENDOR_MICRO_EEPROM:
14747 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014748 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014749 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14750
14751 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14752 tw32(NVRAM_CFG1, nvcfg1);
14753 return;
14754 case FLASH_5717VENDOR_ATMEL_MDB011D:
14755 case FLASH_5717VENDOR_ATMEL_ADB011B:
14756 case FLASH_5717VENDOR_ATMEL_ADB011D:
14757 case FLASH_5717VENDOR_ATMEL_MDB021D:
14758 case FLASH_5717VENDOR_ATMEL_ADB021B:
14759 case FLASH_5717VENDOR_ATMEL_ADB021D:
14760 case FLASH_5717VENDOR_ATMEL_45USPT:
14761 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014762 tg3_flag_set(tp, NVRAM_BUFFERED);
14763 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014764
14765 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14766 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014767 /* Detect size with tg3_nvram_get_size() */
14768 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014769 case FLASH_5717VENDOR_ATMEL_ADB021B:
14770 case FLASH_5717VENDOR_ATMEL_ADB021D:
14771 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14772 break;
14773 default:
14774 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14775 break;
14776 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014777 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014778 case FLASH_5717VENDOR_ST_M_M25PE10:
14779 case FLASH_5717VENDOR_ST_A_M25PE10:
14780 case FLASH_5717VENDOR_ST_M_M45PE10:
14781 case FLASH_5717VENDOR_ST_A_M45PE10:
14782 case FLASH_5717VENDOR_ST_M_M25PE20:
14783 case FLASH_5717VENDOR_ST_A_M25PE20:
14784 case FLASH_5717VENDOR_ST_M_M45PE20:
14785 case FLASH_5717VENDOR_ST_A_M45PE20:
14786 case FLASH_5717VENDOR_ST_25USPT:
14787 case FLASH_5717VENDOR_ST_45USPT:
14788 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014789 tg3_flag_set(tp, NVRAM_BUFFERED);
14790 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014791
14792 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14793 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014794 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014795 /* Detect size with tg3_nvram_get_size() */
14796 break;
14797 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014798 case FLASH_5717VENDOR_ST_A_M45PE20:
14799 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14800 break;
14801 default:
14802 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14803 break;
14804 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014805 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014806 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014807 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014808 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014809 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014810
14811 tg3_nvram_get_pagesize(tp, nvcfg1);
14812 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014813 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014814}
14815
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014816static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014817{
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014818 u32 nvcfg1, nvmpinstrp, nv_status;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014819
14820 nvcfg1 = tr32(NVRAM_CFG1);
14821 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14822
Joe Perches41535772013-02-16 11:20:04 +000014823 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014824 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14825 tg3_flag_set(tp, NO_NVRAM);
14826 return;
14827 }
14828
14829 switch (nvmpinstrp) {
Prashant Sreedharan8a4816c2018-01-03 10:45:29 +053014830 case FLASH_5762_MX25L_100:
14831 case FLASH_5762_MX25L_200:
14832 case FLASH_5762_MX25L_400:
14833 case FLASH_5762_MX25L_800:
14834 case FLASH_5762_MX25L_160_320:
14835 tp->nvram_pagesize = 4096;
14836 tp->nvram_jedecnum = JEDEC_MACRONIX;
14837 tg3_flag_set(tp, NVRAM_BUFFERED);
14838 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
14839 tg3_flag_set(tp, FLASH);
14840 nv_status = tr32(NVRAM_AUTOSENSE_STATUS);
14841 tp->nvram_size =
14842 (1 << (nv_status >> AUTOSENSE_DEVID &
14843 AUTOSENSE_DEVID_MASK)
14844 << AUTOSENSE_SIZE_IN_MB);
14845 return;
14846
Michael Chanc86a8562013-01-06 12:51:08 +000014847 case FLASH_5762_EEPROM_HD:
14848 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014849 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014850 case FLASH_5762_EEPROM_LD:
14851 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014852 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014853 case FLASH_5720VENDOR_M_ST_M45PE20:
14854 /* This pinstrap supports multiple sizes, so force it
14855 * to read the actual size from location 0xf0.
14856 */
14857 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14858 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014859 }
14860 }
14861
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014862 switch (nvmpinstrp) {
14863 case FLASH_5720_EEPROM_HD:
14864 case FLASH_5720_EEPROM_LD:
14865 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014866 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014867
14868 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14869 tw32(NVRAM_CFG1, nvcfg1);
14870 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14871 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14872 else
14873 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14874 return;
14875 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14876 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14877 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14878 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14879 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14880 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14881 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14882 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14883 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14884 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14885 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14886 case FLASH_5720VENDOR_ATMEL_45USPT:
14887 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014888 tg3_flag_set(tp, NVRAM_BUFFERED);
14889 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014890
14891 switch (nvmpinstrp) {
14892 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14893 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14894 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14895 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14896 break;
14897 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14898 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14899 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14900 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14901 break;
14902 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14903 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14904 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14905 break;
14906 default:
Joe Perches41535772013-02-16 11:20:04 +000014907 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014908 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014909 break;
14910 }
14911 break;
14912 case FLASH_5720VENDOR_M_ST_M25PE10:
14913 case FLASH_5720VENDOR_M_ST_M45PE10:
14914 case FLASH_5720VENDOR_A_ST_M25PE10:
14915 case FLASH_5720VENDOR_A_ST_M45PE10:
14916 case FLASH_5720VENDOR_M_ST_M25PE20:
14917 case FLASH_5720VENDOR_M_ST_M45PE20:
14918 case FLASH_5720VENDOR_A_ST_M25PE20:
14919 case FLASH_5720VENDOR_A_ST_M45PE20:
14920 case FLASH_5720VENDOR_M_ST_M25PE40:
14921 case FLASH_5720VENDOR_M_ST_M45PE40:
14922 case FLASH_5720VENDOR_A_ST_M25PE40:
14923 case FLASH_5720VENDOR_A_ST_M45PE40:
14924 case FLASH_5720VENDOR_M_ST_M25PE80:
14925 case FLASH_5720VENDOR_M_ST_M45PE80:
14926 case FLASH_5720VENDOR_A_ST_M25PE80:
14927 case FLASH_5720VENDOR_A_ST_M45PE80:
14928 case FLASH_5720VENDOR_ST_25USPT:
14929 case FLASH_5720VENDOR_ST_45USPT:
14930 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014931 tg3_flag_set(tp, NVRAM_BUFFERED);
14932 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014933
14934 switch (nvmpinstrp) {
14935 case FLASH_5720VENDOR_M_ST_M25PE20:
14936 case FLASH_5720VENDOR_M_ST_M45PE20:
14937 case FLASH_5720VENDOR_A_ST_M25PE20:
14938 case FLASH_5720VENDOR_A_ST_M45PE20:
14939 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14940 break;
14941 case FLASH_5720VENDOR_M_ST_M25PE40:
14942 case FLASH_5720VENDOR_M_ST_M45PE40:
14943 case FLASH_5720VENDOR_A_ST_M25PE40:
14944 case FLASH_5720VENDOR_A_ST_M45PE40:
14945 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14946 break;
14947 case FLASH_5720VENDOR_M_ST_M25PE80:
14948 case FLASH_5720VENDOR_M_ST_M45PE80:
14949 case FLASH_5720VENDOR_A_ST_M25PE80:
14950 case FLASH_5720VENDOR_A_ST_M45PE80:
14951 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14952 break;
14953 default:
Joe Perches41535772013-02-16 11:20:04 +000014954 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014955 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014956 break;
14957 }
14958 break;
14959 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014960 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014961 return;
14962 }
14963
14964 tg3_nvram_get_pagesize(tp, nvcfg1);
14965 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014966 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014967
Joe Perches41535772013-02-16 11:20:04 +000014968 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014969 u32 val;
14970
14971 if (tg3_nvram_read(tp, 0, &val))
14972 return;
14973
14974 if (val != TG3_EEPROM_MAGIC &&
14975 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14976 tg3_flag_set(tp, NO_NVRAM);
14977 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014978}
14979
Linus Torvalds1da177e2005-04-16 15:20:36 -070014980/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014981static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014983 if (tg3_flag(tp, IS_SSB_CORE)) {
14984 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14985 tg3_flag_clear(tp, NVRAM);
14986 tg3_flag_clear(tp, NVRAM_BUFFERED);
14987 tg3_flag_set(tp, NO_NVRAM);
14988 return;
14989 }
14990
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991 tw32_f(GRC_EEPROM_ADDR,
14992 (EEPROM_ADDR_FSM_RESET |
14993 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14994 EEPROM_ADDR_CLKPERD_SHIFT)));
14995
Michael Chan9d57f012006-12-07 00:23:25 -080014996 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997
14998 /* Enable seeprom accesses. */
14999 tw32_f(GRC_LOCAL_CTRL,
15000 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
15001 udelay(100);
15002
Joe Perches41535772013-02-16 11:20:04 +000015003 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15004 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000015005 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006
Michael Chanec41c7d2006-01-17 02:40:55 -080015007 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015008 netdev_warn(tp->dev,
15009 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015010 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080015011 return;
15012 }
Michael Chane6af3012005-04-21 17:12:05 -070015013 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014
Matt Carlson989a9d22007-05-05 11:51:05 -070015015 tp->nvram_size = 0;
15016
Joe Perches41535772013-02-16 11:20:04 +000015017 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070015018 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015019 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080015020 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015021 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
15022 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15023 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080015024 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015025 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070015026 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015027 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015028 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015029 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015030 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080015031 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015032 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15033 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015034 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000015035 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15036 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000015037 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070015038 else
15039 tg3_get_nvram_info(tp);
15040
Matt Carlson989a9d22007-05-05 11:51:05 -070015041 if (tp->nvram_size == 0)
15042 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015043
Michael Chane6af3012005-04-21 17:12:05 -070015044 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080015045 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046
15047 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015048 tg3_flag_clear(tp, NVRAM);
15049 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050
15051 tg3_get_eeprom_size(tp);
15052 }
15053}
15054
Linus Torvalds1da177e2005-04-16 15:20:36 -070015055struct subsys_tbl_ent {
15056 u16 subsys_vendor, subsys_devid;
15057 u32 phy_id;
15058};
15059
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015060static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015061 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015062 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015063 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015064 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015065 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015066 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015067 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015068 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15069 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
15070 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015071 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015072 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015073 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015074 { TG3PCI_SUBVENDOR_ID_BROADCOM,
15075 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
15076 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015077 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015078 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015079 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015080 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015081 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015082 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015083 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015084
15085 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015086 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015087 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015088 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015089 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015090 { TG3PCI_SUBVENDOR_ID_3COM,
15091 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
15092 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015093 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015094 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000015095 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096
15097 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015098 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015099 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015100 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015101 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015102 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015103 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015104 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000015105 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015106
15107 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015108 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015109 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015110 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015111 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015112 { TG3PCI_SUBVENDOR_ID_COMPAQ,
15113 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
15114 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015115 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000015116 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000015117 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070015118
15119 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015120 { TG3PCI_SUBVENDOR_ID_IBM,
15121 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122};
15123
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015124static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015125{
15126 int i;
15127
15128 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
15129 if ((subsys_id_to_phy_id[i].subsys_vendor ==
15130 tp->pdev->subsystem_vendor) &&
15131 (subsys_id_to_phy_id[i].subsys_devid ==
15132 tp->pdev->subsystem_device))
15133 return &subsys_id_to_phy_id[i];
15134 }
15135 return NULL;
15136}
15137
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015138static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070015141
Matt Carlson79eb6902010-02-17 15:17:03 +000015142 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015143 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15144
Gary Zambranoa85feb82007-05-05 11:52:19 -070015145 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000015146 tg3_flag_set(tp, EEPROM_WRITE_PROT);
15147 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080015148
Joe Perches41535772013-02-16 11:20:04 +000015149 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080015150 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015151 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15152 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015153 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015154 val = tr32(VCPU_CFGSHDW);
15155 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000015156 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070015157 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015158 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015159 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015160 device_set_wakeup_enable(&tp->pdev->dev, true);
15161 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015162 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015163 }
15164
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15166 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15167 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015168 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15169 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015170 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171
15172 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15173 tp->nic_sram_data_cfg = nic_cfg;
15174
15175 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15176 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015177 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15178 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15179 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015180 (ver > 0) && (ver < 0x100))
15181 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15182
Joe Perches41535772013-02-16 11:20:04 +000015183 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015184 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15185
Nithin Sujir7c786062013-12-06 09:53:17 -080015186 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15187 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15188 tg3_asic_rev(tp) == ASIC_REV_5720)
15189 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15190
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15192 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15193 eeprom_phy_serdes = 1;
15194
15195 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15196 if (nic_phy_id != 0) {
15197 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15198 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15199
15200 eeprom_phy_id = (id1 >> 16) << 10;
15201 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15202 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15203 } else
15204 eeprom_phy_id = 0;
15205
Michael Chan7d0c41e2005-04-21 17:06:20 -070015206 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015207 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015208 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015209 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015210 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015211 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015212 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015213
Joe Perches63c3a662011-04-26 08:12:10 +000015214 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015215 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15216 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015218 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15219
15220 switch (led_cfg) {
15221 default:
15222 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15223 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15224 break;
15225
15226 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15227 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15228 break;
15229
15230 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15231 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015232
15233 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15234 * read on some older 5700/5701 bootcode.
15235 */
Joe Perches41535772013-02-16 11:20:04 +000015236 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15237 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015238 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15239
Linus Torvalds1da177e2005-04-16 15:20:36 -070015240 break;
15241
15242 case SHASTA_EXT_LED_SHARED:
15243 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015244 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15245 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015246 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15247 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015248
15249 if (tg3_flag(tp, 5717_PLUS) ||
15250 tg3_asic_rev(tp) == ASIC_REV_5762)
15251 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15252 LED_CTRL_BLINK_RATE_MASK;
15253
Linus Torvalds1da177e2005-04-16 15:20:36 -070015254 break;
15255
15256 case SHASTA_EXT_LED_MAC:
15257 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15258 break;
15259
15260 case SHASTA_EXT_LED_COMBO:
15261 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015262 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15264 LED_CTRL_MODE_PHY_2);
15265 break;
15266
Stephen Hemminger855e1112008-04-16 16:37:28 -070015267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015268
Joe Perches41535772013-02-16 11:20:04 +000015269 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15270 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015271 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15272 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15273
Joe Perches41535772013-02-16 11:20:04 +000015274 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015275 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015276
Michael Chan9d26e212006-12-07 00:21:14 -080015277 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015278 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015279 if ((tp->pdev->subsystem_vendor ==
15280 PCI_VENDOR_ID_ARIMA) &&
15281 (tp->pdev->subsystem_device == 0x205a ||
15282 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015283 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015284 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015285 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15286 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
15289 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015290 tg3_flag_set(tp, ENABLE_ASF);
15291 if (tg3_flag(tp, 5750_PLUS))
15292 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015293 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015294
15295 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015296 tg3_flag(tp, 5750_PLUS))
15297 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015298
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015299 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015300 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015301 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302
Joe Perches63c3a662011-04-26 08:12:10 +000015303 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015304 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015305 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015306 device_set_wakeup_enable(&tp->pdev->dev, true);
15307 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015308
Linus Torvalds1da177e2005-04-16 15:20:36 -070015309 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015310 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311
15312 /* serdes signal pre-emphasis in register 0x590 set by */
15313 /* bootcode if bit 18 is set */
15314 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015315 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015316
Joe Perches63c3a662011-04-26 08:12:10 +000015317 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015318 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15319 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015320 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015321 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015322
Nithin Sujir942d1af2013-04-09 08:48:07 +000015323 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015324 u32 cfg3;
15325
15326 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015327 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15328 !tg3_flag(tp, 57765_PLUS) &&
15329 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015330 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015331 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15332 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15333 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15334 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015335 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015336
Matt Carlson14417062010-02-17 15:16:59 +000015337 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015338 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015339 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015340 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015341 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015342 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015343
15344 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15345 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015346 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015347done:
Joe Perches63c3a662011-04-26 08:12:10 +000015348 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015349 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015350 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015351 else
15352 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015353}
15354
Michael Chanc86a8562013-01-06 12:51:08 +000015355static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15356{
15357 int i, err;
15358 u32 val2, off = offset * 8;
15359
15360 err = tg3_nvram_lock(tp);
15361 if (err)
15362 return err;
15363
15364 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15365 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15366 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15367 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15368 udelay(10);
15369
15370 for (i = 0; i < 100; i++) {
15371 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15372 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15373 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15374 break;
15375 }
15376 udelay(10);
15377 }
15378
15379 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15380
15381 tg3_nvram_unlock(tp);
15382 if (val2 & APE_OTP_STATUS_CMD_DONE)
15383 return 0;
15384
15385 return -EBUSY;
15386}
15387
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015388static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015389{
15390 int i;
15391 u32 val;
15392
15393 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15394 tw32(OTP_CTRL, cmd);
15395
15396 /* Wait for up to 1 ms for command to execute. */
15397 for (i = 0; i < 100; i++) {
15398 val = tr32(OTP_STATUS);
15399 if (val & OTP_STATUS_CMD_DONE)
15400 break;
15401 udelay(10);
15402 }
15403
15404 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15405}
15406
15407/* Read the gphy configuration from the OTP region of the chip. The gphy
15408 * configuration is a 32-bit value that straddles the alignment boundary.
15409 * We do two 32-bit reads and then shift and merge the results.
15410 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015411static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015412{
15413 u32 bhalf_otp, thalf_otp;
15414
15415 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15416
15417 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15418 return 0;
15419
15420 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15421
15422 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15423 return 0;
15424
15425 thalf_otp = tr32(OTP_READ_DATA);
15426
15427 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15428
15429 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15430 return 0;
15431
15432 bhalf_otp = tr32(OTP_READ_DATA);
15433
15434 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15435}
15436
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015437static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015438{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015439 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015440
Nithin Sujir7c786062013-12-06 09:53:17 -080015441 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15442 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15443 adv |= ADVERTISED_1000baseT_Half;
15444 adv |= ADVERTISED_1000baseT_Full;
15445 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015446
15447 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15448 adv |= ADVERTISED_100baseT_Half |
15449 ADVERTISED_100baseT_Full |
15450 ADVERTISED_10baseT_Half |
15451 ADVERTISED_10baseT_Full |
15452 ADVERTISED_TP;
15453 else
15454 adv |= ADVERTISED_FIBRE;
15455
15456 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015457 tp->link_config.speed = SPEED_UNKNOWN;
15458 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015459 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015460 tp->link_config.active_speed = SPEED_UNKNOWN;
15461 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015462
15463 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015464}
15465
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015466static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015467{
15468 u32 hw_phy_id_1, hw_phy_id_2;
15469 u32 hw_phy_id, hw_phy_id_masked;
15470 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015471
Matt Carlsone256f8a2011-03-09 16:58:24 +000015472 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015473 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015474 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15475
Michael Chan8151ad52012-07-29 19:15:41 +000015476 if (tg3_flag(tp, ENABLE_APE)) {
15477 switch (tp->pci_fn) {
15478 case 0:
15479 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15480 break;
15481 case 1:
15482 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15483 break;
15484 case 2:
15485 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15486 break;
15487 case 3:
15488 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15489 break;
15490 }
15491 }
15492
Nithin Sujir942d1af2013-04-09 08:48:07 +000015493 if (!tg3_flag(tp, ENABLE_ASF) &&
15494 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15495 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15496 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15497 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15498
Joe Perches63c3a662011-04-26 08:12:10 +000015499 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015500 return tg3_phy_init(tp);
15501
Linus Torvalds1da177e2005-04-16 15:20:36 -070015502 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015503 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 */
15505 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015506 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015507 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 } else {
15509 /* Now read the physical PHY_ID from the chip and verify
15510 * that it is sane. If it doesn't look good, we fall back
15511 * to either the hard-coded table based PHY_ID and failing
15512 * that the value found in the eeprom area.
15513 */
15514 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15515 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15516
15517 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15518 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15519 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15520
Matt Carlson79eb6902010-02-17 15:17:03 +000015521 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015522 }
15523
Matt Carlson79eb6902010-02-17 15:17:03 +000015524 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015525 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015526 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015527 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015528 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015529 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015531 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015532 /* Do nothing, phy ID already set up in
15533 * tg3_get_eeprom_hw_cfg().
15534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015535 } else {
15536 struct subsys_tbl_ent *p;
15537
15538 /* No eeprom signature? Try the hardcoded
15539 * subsys device table.
15540 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015541 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015542 if (p) {
15543 tp->phy_id = p->phy_id;
15544 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15545 /* For now we saw the IDs 0xbc050cd0,
15546 * 0xbc050f80 and 0xbc050c30 on devices
15547 * connected to an BCM4785 and there are
15548 * probably more. Just assume that the phy is
15549 * supported when it is connected to a SSB core
15550 * for now.
15551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015552 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015554
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015556 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015557 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 }
15559 }
15560
Matt Carlsona6b68da2010-12-06 08:28:52 +000015561 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015562 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15563 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015564 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015565 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15566 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15567 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15568 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015569 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015570 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15571
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015572 tp->eee.supported = SUPPORTED_100baseT_Full |
15573 SUPPORTED_1000baseT_Full;
15574 tp->eee.advertised = ADVERTISED_100baseT_Full |
15575 ADVERTISED_1000baseT_Full;
15576 tp->eee.eee_enabled = 1;
15577 tp->eee.tx_lpi_enabled = 1;
15578 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15579 }
15580
Matt Carlsone256f8a2011-03-09 16:58:24 +000015581 tg3_phy_init_link_config(tp);
15582
Nithin Sujir942d1af2013-04-09 08:48:07 +000015583 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15584 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015585 !tg3_flag(tp, ENABLE_APE) &&
15586 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015587 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588
15589 tg3_readphy(tp, MII_BMSR, &bmsr);
15590 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15591 (bmsr & BMSR_LSTATUS))
15592 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015593
Linus Torvalds1da177e2005-04-16 15:20:36 -070015594 err = tg3_phy_reset(tp);
15595 if (err)
15596 return err;
15597
Matt Carlson42b64a42011-05-19 12:12:49 +000015598 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015599
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015600 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015601 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15602 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015603
15604 tg3_writephy(tp, MII_BMCR,
15605 BMCR_ANENABLE | BMCR_ANRESTART);
15606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015607 }
15608
15609skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015610 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015611 err = tg3_init_5401phy_dsp(tp);
15612 if (err)
15613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015614
Linus Torvalds1da177e2005-04-16 15:20:36 -070015615 err = tg3_init_5401phy_dsp(tp);
15616 }
15617
Linus Torvalds1da177e2005-04-16 15:20:36 -070015618 return err;
15619}
15620
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015621static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015622{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015623 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015624 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015625 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015626 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015627
Matt Carlson535a4902011-07-20 10:20:56 +000015628 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015629 if (!vpd_data)
15630 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015631
Matt Carlson535a4902011-07-20 10:20:56 +000015632 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015633 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015634 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015635
15636 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15637 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15638 i += PCI_VPD_LRDT_TAG_SIZE;
15639
Matt Carlson535a4902011-07-20 10:20:56 +000015640 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015641 goto out_not_found;
15642
Matt Carlson184b8902010-04-05 10:19:25 +000015643 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15644 PCI_VPD_RO_KEYWORD_MFR_ID);
15645 if (j > 0) {
15646 len = pci_vpd_info_field_size(&vpd_data[j]);
15647
15648 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15649 if (j + len > block_end || len != 4 ||
15650 memcmp(&vpd_data[j], "1028", 4))
15651 goto partno;
15652
15653 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15654 PCI_VPD_RO_KEYWORD_VENDOR0);
15655 if (j < 0)
15656 goto partno;
15657
15658 len = pci_vpd_info_field_size(&vpd_data[j]);
15659
15660 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15661 if (j + len > block_end)
15662 goto partno;
15663
Kees Cook715230a2013-03-27 06:40:50 +000015664 if (len >= sizeof(tp->fw_ver))
15665 len = sizeof(tp->fw_ver) - 1;
15666 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15667 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15668 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015669 }
15670
15671partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015672 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15673 PCI_VPD_RO_KEYWORD_PARTNO);
15674 if (i < 0)
15675 goto out_not_found;
15676
15677 len = pci_vpd_info_field_size(&vpd_data[i]);
15678
15679 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15680 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015681 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015682 goto out_not_found;
15683
15684 memcpy(tp->board_part_number, &vpd_data[i], len);
15685
Linus Torvalds1da177e2005-04-16 15:20:36 -070015686out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015687 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015688 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015689 return;
15690
15691out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015692 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015693 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15694 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015695 strcpy(tp->board_part_number, "BCM5717");
15696 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15697 strcpy(tp->board_part_number, "BCM5718");
15698 else
15699 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015700 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015701 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15702 strcpy(tp->board_part_number, "BCM57780");
15703 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15704 strcpy(tp->board_part_number, "BCM57760");
15705 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15706 strcpy(tp->board_part_number, "BCM57790");
15707 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15708 strcpy(tp->board_part_number, "BCM57788");
15709 else
15710 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015711 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015712 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15713 strcpy(tp->board_part_number, "BCM57761");
15714 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15715 strcpy(tp->board_part_number, "BCM57765");
15716 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15717 strcpy(tp->board_part_number, "BCM57781");
15718 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15719 strcpy(tp->board_part_number, "BCM57785");
15720 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15721 strcpy(tp->board_part_number, "BCM57791");
15722 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15723 strcpy(tp->board_part_number, "BCM57795");
15724 else
15725 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015726 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015727 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15728 strcpy(tp->board_part_number, "BCM57762");
15729 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15730 strcpy(tp->board_part_number, "BCM57766");
15731 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15732 strcpy(tp->board_part_number, "BCM57782");
15733 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15734 strcpy(tp->board_part_number, "BCM57786");
15735 else
15736 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015737 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015738 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015739 } else {
15740nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015741 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015743}
15744
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015745static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015746{
15747 u32 val;
15748
Matt Carlsone4f34112009-02-25 14:25:00 +000015749 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015750 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015751 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015752 val != 0)
15753 return 0;
15754
15755 return 1;
15756}
15757
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015758static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015759{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015760 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015761 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015762 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015763
15764 if (tg3_nvram_read(tp, 0xc, &offset) ||
15765 tg3_nvram_read(tp, 0x4, &start))
15766 return;
15767
15768 offset = tg3_nvram_logical_addr(tp, offset);
15769
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015770 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015771 return;
15772
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015773 if ((val & 0xfc000000) == 0x0c000000) {
15774 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015775 return;
15776
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015777 if (val == 0)
15778 newver = true;
15779 }
15780
Matt Carlson75f99362010-04-05 10:19:24 +000015781 dst_off = strlen(tp->fw_ver);
15782
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015783 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015784 if (TG3_VER_SIZE - dst_off < 16 ||
15785 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015786 return;
15787
15788 offset = offset + ver_offset - start;
15789 for (i = 0; i < 16; i += 4) {
15790 __be32 v;
15791 if (tg3_nvram_read_be32(tp, offset + i, &v))
15792 return;
15793
Matt Carlson75f99362010-04-05 10:19:24 +000015794 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015795 }
15796 } else {
15797 u32 major, minor;
15798
15799 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15800 return;
15801
15802 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15803 TG3_NVM_BCVER_MAJSFT;
15804 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015805 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15806 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015807 }
15808}
15809
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015810static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015811{
15812 u32 val, major, minor;
15813
15814 /* Use native endian representation */
15815 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15816 return;
15817
15818 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15819 TG3_NVM_HWSB_CFG1_MAJSFT;
15820 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15821 TG3_NVM_HWSB_CFG1_MINSFT;
15822
15823 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15824}
15825
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015826static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015827{
15828 u32 offset, major, minor, build;
15829
Matt Carlson75f99362010-04-05 10:19:24 +000015830 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015831
15832 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15833 return;
15834
15835 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15836 case TG3_EEPROM_SB_REVISION_0:
15837 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15838 break;
15839 case TG3_EEPROM_SB_REVISION_2:
15840 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15841 break;
15842 case TG3_EEPROM_SB_REVISION_3:
15843 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15844 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015845 case TG3_EEPROM_SB_REVISION_4:
15846 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15847 break;
15848 case TG3_EEPROM_SB_REVISION_5:
15849 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15850 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015851 case TG3_EEPROM_SB_REVISION_6:
15852 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15853 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015854 default:
15855 return;
15856 }
15857
Matt Carlsone4f34112009-02-25 14:25:00 +000015858 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015859 return;
15860
15861 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15862 TG3_EEPROM_SB_EDH_BLD_SHFT;
15863 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15864 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15865 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15866
15867 if (minor > 99 || build > 26)
15868 return;
15869
Matt Carlson75f99362010-04-05 10:19:24 +000015870 offset = strlen(tp->fw_ver);
15871 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15872 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015873
15874 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015875 offset = strlen(tp->fw_ver);
15876 if (offset < TG3_VER_SIZE - 1)
15877 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015878 }
15879}
15880
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015881static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015882{
15883 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015884 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015885
15886 for (offset = TG3_NVM_DIR_START;
15887 offset < TG3_NVM_DIR_END;
15888 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015889 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015890 return;
15891
15892 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15893 break;
15894 }
15895
15896 if (offset == TG3_NVM_DIR_END)
15897 return;
15898
Joe Perches63c3a662011-04-26 08:12:10 +000015899 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015900 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015901 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015902 return;
15903
Matt Carlsone4f34112009-02-25 14:25:00 +000015904 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015905 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015906 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015907 return;
15908
15909 offset += val - start;
15910
Matt Carlsonacd9c112009-02-25 14:26:33 +000015911 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015912
Matt Carlsonacd9c112009-02-25 14:26:33 +000015913 tp->fw_ver[vlen++] = ',';
15914 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015915
15916 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015917 __be32 v;
15918 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015919 return;
15920
Al Virob9fc7dc2007-12-17 22:59:57 -080015921 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015922
Matt Carlsonacd9c112009-02-25 14:26:33 +000015923 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15924 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015925 break;
15926 }
15927
Matt Carlsonacd9c112009-02-25 14:26:33 +000015928 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15929 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015930 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015931}
15932
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015933static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015934{
Matt Carlson7fd76442009-02-25 14:27:20 +000015935 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015936
15937 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15938 if (apedata != APE_SEG_SIG_MAGIC)
15939 return;
15940
15941 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15942 if (!(apedata & APE_FW_STATUS_READY))
15943 return;
15944
Michael Chan165f4d12012-07-16 16:23:59 +000015945 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15946 tg3_flag_set(tp, APE_HAS_NCSI);
15947}
15948
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015949static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015950{
15951 int vlen;
15952 u32 apedata;
15953 char *fwtype;
15954
Matt Carlson7fd76442009-02-25 14:27:20 +000015955 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15956
Michael Chan165f4d12012-07-16 16:23:59 +000015957 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015958 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015959 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15960 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015961 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015962 fwtype = "DASH";
15963
Matt Carlson7fd76442009-02-25 14:27:20 +000015964 vlen = strlen(tp->fw_ver);
15965
Matt Carlsonecc79642010-08-02 11:26:01 +000015966 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15967 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015968 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15969 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15970 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15971 (apedata & APE_FW_VERSION_BLDMSK));
15972}
15973
Michael Chanc86a8562013-01-06 12:51:08 +000015974static void tg3_read_otp_ver(struct tg3 *tp)
15975{
15976 u32 val, val2;
15977
Joe Perches41535772013-02-16 11:20:04 +000015978 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015979 return;
15980
15981 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15982 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15983 TG3_OTP_MAGIC0_VALID(val)) {
15984 u64 val64 = (u64) val << 32 | val2;
15985 u32 ver = 0;
15986 int i, vlen;
15987
15988 for (i = 0; i < 7; i++) {
15989 if ((val64 & 0xff) == 0)
15990 break;
15991 ver = val64 & 0xff;
15992 val64 >>= 8;
15993 }
15994 vlen = strlen(tp->fw_ver);
15995 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15996 }
15997}
15998
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015999static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000016000{
16001 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000016002 bool vpd_vers = false;
16003
16004 if (tp->fw_ver[0] != 0)
16005 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000016006
Joe Perches63c3a662011-04-26 08:12:10 +000016007 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000016008 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000016009 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000016010 return;
16011 }
16012
Matt Carlsonacd9c112009-02-25 14:26:33 +000016013 if (tg3_nvram_read(tp, 0, &val))
16014 return;
16015
16016 if (val == TG3_EEPROM_MAGIC)
16017 tg3_read_bc_ver(tp);
16018 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
16019 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000016020 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
16021 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000016022
Michael Chan165f4d12012-07-16 16:23:59 +000016023 if (tg3_flag(tp, ENABLE_ASF)) {
16024 if (tg3_flag(tp, ENABLE_APE)) {
16025 tg3_probe_ncsi(tp);
16026 if (!vpd_vers)
16027 tg3_read_dash_ver(tp);
16028 } else if (!vpd_vers) {
16029 tg3_read_mgmtfw_ver(tp);
16030 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000016031 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070016032
16033 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080016034}
16035
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016036static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
16037{
Joe Perches63c3a662011-04-26 08:12:10 +000016038 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016039 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000016040 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000016041 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016042 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000016043 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016044}
16045
Benoit Taine9baa3c32014-08-08 15:56:03 +020016046static const struct pci_device_id tg3_write_reorder_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080016047 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
16048 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
16049 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
16050 { },
16051};
16052
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016053static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000016054{
16055 struct pci_dev *peer;
16056 unsigned int func, devnr = tp->pdev->devfn & ~7;
16057
16058 for (func = 0; func < 8; func++) {
16059 peer = pci_get_slot(tp->pdev->bus, devnr | func);
16060 if (peer && peer != tp->pdev)
16061 break;
16062 pci_dev_put(peer);
16063 }
16064 /* 5704 can be configured in single-port mode, set peer to
16065 * tp->pdev in that case.
16066 */
16067 if (!peer) {
16068 peer = tp->pdev;
16069 return peer;
16070 }
16071
16072 /*
16073 * We don't need to keep the refcount elevated; there's no way
16074 * to remove one half of this device without removing the other
16075 */
16076 pci_dev_put(peer);
16077
16078 return peer;
16079}
16080
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016081static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000016082{
16083 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000016084 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000016085 u32 reg;
16086
16087 /* All devices that use the alternate
16088 * ASIC REV location have a CPMU.
16089 */
16090 tg3_flag_set(tp, CPMU_PRESENT);
16091
16092 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016093 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016094 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16095 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016096 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016097 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
16098 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016099 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16100 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070016101 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
16102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000016103 reg = TG3PCI_GEN2_PRODID_ASICREV;
16104 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
16105 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
16106 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
16107 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
16108 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
16109 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
16110 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
16111 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
16112 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
16113 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
16114 reg = TG3PCI_GEN15_PRODID_ASICREV;
16115 else
16116 reg = TG3PCI_PRODID_ASICREV;
16117
16118 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
16119 }
16120
16121 /* Wrong chip ID in 5752 A0. This code can be removed later
16122 * as A0 is not in production.
16123 */
Joe Perches41535772013-02-16 11:20:04 +000016124 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000016125 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
16126
Joe Perches41535772013-02-16 11:20:04 +000016127 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000016128 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
16129
Joe Perches41535772013-02-16 11:20:04 +000016130 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16131 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16132 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000016133 tg3_flag_set(tp, 5717_PLUS);
16134
Joe Perches41535772013-02-16 11:20:04 +000016135 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
16136 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000016137 tg3_flag_set(tp, 57765_CLASS);
16138
Michael Chanc65a17f2013-01-06 12:51:07 +000016139 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016140 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000016141 tg3_flag_set(tp, 57765_PLUS);
16142
16143 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000016144 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16145 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16146 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16147 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16148 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16149 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016150 tg3_flag(tp, 57765_PLUS))
16151 tg3_flag_set(tp, 5755_PLUS);
16152
Joe Perches41535772013-02-16 11:20:04 +000016153 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
16154 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000016155 tg3_flag_set(tp, 5780_CLASS);
16156
Joe Perches41535772013-02-16 11:20:04 +000016157 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16158 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16159 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016160 tg3_flag(tp, 5755_PLUS) ||
16161 tg3_flag(tp, 5780_CLASS))
16162 tg3_flag_set(tp, 5750_PLUS);
16163
Joe Perches41535772013-02-16 11:20:04 +000016164 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016165 tg3_flag(tp, 5750_PLUS))
16166 tg3_flag_set(tp, 5705_PLUS);
16167}
16168
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016169static bool tg3_10_100_only_device(struct tg3 *tp,
16170 const struct pci_device_id *ent)
16171{
16172 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16173
Joe Perches41535772013-02-16 11:20:04 +000016174 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16175 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016176 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16177 return true;
16178
16179 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016180 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016181 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16182 return true;
16183 } else {
16184 return true;
16185 }
16186 }
16187
16188 return false;
16189}
16190
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016191static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016192{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016193 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016194 u32 pci_state_reg, grc_misc_cfg;
16195 u32 val;
16196 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016198
Linus Torvalds1da177e2005-04-16 15:20:36 -070016199 /* Force memory write invalidate off. If we leave it on,
16200 * then on 5700_BX chips we have to enable a workaround.
16201 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16202 * to match the cacheline size. The Broadcom driver have this
16203 * workaround but turns MWI off all the times so never uses
16204 * it. This seems to suggest that the workaround is insufficient.
16205 */
16206 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16207 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16208 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16209
Matt Carlson16821282011-07-13 09:27:28 +000016210 /* Important! -- Make sure register accesses are byteswapped
16211 * correctly. Also, for those chips that require it, make
16212 * sure that indirect register accesses are enabled before
16213 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016214 */
16215 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16216 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016217 tp->misc_host_ctrl |= (misc_ctrl_reg &
16218 MISC_HOST_CTRL_CHIPREV);
16219 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16220 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016221
Matt Carlson42b123b2012-02-13 15:20:13 +000016222 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016223
Michael Chan68929142005-08-09 20:17:14 -070016224 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16225 * we need to disable memory and use config. cycles
16226 * only to access all registers. The 5702/03 chips
16227 * can mistakenly decode the special cycles from the
16228 * ICH chipsets as memory write cycles, causing corruption
16229 * of register and memory space. Only certain ICH bridges
16230 * will drive special cycles with non-zero data during the
16231 * address phase which can fall within the 5703's address
16232 * range. This is not an ICH bug as the PCI spec allows
16233 * non-zero address during special cycles. However, only
16234 * these ICH bridges are known to drive non-zero addresses
16235 * during special cycles.
16236 *
16237 * Since special cycles do not cross PCI bridges, we only
16238 * enable this workaround if the 5703 is on the secondary
16239 * bus of these ICH bridges.
16240 */
Joe Perches41535772013-02-16 11:20:04 +000016241 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16242 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016243 static struct tg3_dev_id {
16244 u32 vendor;
16245 u32 device;
16246 u32 rev;
16247 } ich_chipsets[] = {
16248 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16249 PCI_ANY_ID },
16250 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16251 PCI_ANY_ID },
16252 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16253 0xa },
16254 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16255 PCI_ANY_ID },
16256 { },
16257 };
16258 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16259 struct pci_dev *bridge = NULL;
16260
16261 while (pci_id->vendor != 0) {
16262 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16263 bridge);
16264 if (!bridge) {
16265 pci_id++;
16266 continue;
16267 }
16268 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016269 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016270 continue;
16271 }
16272 if (bridge->subordinate &&
16273 (bridge->subordinate->number ==
16274 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016275 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016276 pci_dev_put(bridge);
16277 break;
16278 }
16279 }
16280 }
16281
Joe Perches41535772013-02-16 11:20:04 +000016282 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070016283 static struct tg3_dev_id {
16284 u32 vendor;
16285 u32 device;
16286 } bridge_chipsets[] = {
16287 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16288 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16289 { },
16290 };
16291 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16292 struct pci_dev *bridge = NULL;
16293
16294 while (pci_id->vendor != 0) {
16295 bridge = pci_get_device(pci_id->vendor,
16296 pci_id->device,
16297 bridge);
16298 if (!bridge) {
16299 pci_id++;
16300 continue;
16301 }
16302 if (bridge->subordinate &&
16303 (bridge->subordinate->number <=
16304 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016305 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba12008-04-19 18:12:33 -070016306 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016307 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070016308 pci_dev_put(bridge);
16309 break;
16310 }
16311 }
16312 }
16313
Michael Chan4a29cc22006-03-19 13:21:12 -080016314 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16315 * DMA addresses > 40-bit. This bridge may have other additional
16316 * 57xx devices behind it in some 4-port NIC designs for example.
16317 * Any tg3 device found behind the bridge will also need the 40-bit
16318 * DMA workaround.
16319 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016320 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016321 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016322 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016323 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016324 struct pci_dev *bridge = NULL;
16325
16326 do {
16327 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16328 PCI_DEVICE_ID_SERVERWORKS_EPB,
16329 bridge);
16330 if (bridge && bridge->subordinate &&
16331 (bridge->subordinate->number <=
16332 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016333 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016334 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016335 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016336 pci_dev_put(bridge);
16337 break;
16338 }
16339 } while (bridge);
16340 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016341
Joe Perches41535772013-02-16 11:20:04 +000016342 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16343 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016344 tp->pdev_peer = tg3_find_peer(tp);
16345
Matt Carlson507399f2009-11-13 13:03:37 +000016346 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016347 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016348 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016349 else if (tg3_flag(tp, 57765_PLUS))
16350 tg3_flag_set(tp, HW_TSO_3);
16351 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016352 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016353 tg3_flag_set(tp, HW_TSO_2);
16354 else if (tg3_flag(tp, 5750_PLUS)) {
16355 tg3_flag_set(tp, HW_TSO_1);
16356 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016357 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16358 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016359 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016360 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16361 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16362 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016363 tg3_flag_set(tp, FW_TSO);
16364 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016365 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016366 tp->fw_needed = FIRMWARE_TG3TSO5;
16367 else
16368 tp->fw_needed = FIRMWARE_TG3TSO;
16369 }
16370
Matt Carlsondabc5c62011-05-19 12:12:52 +000016371 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016372 if (tg3_flag(tp, HW_TSO_1) ||
16373 tg3_flag(tp, HW_TSO_2) ||
16374 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016375 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016376 /* For firmware TSO, assume ASF is disabled.
16377 * We'll disable TSO later if we discover ASF
16378 * is enabled in tg3_get_eeprom_hw_cfg().
16379 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016380 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016381 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016382 tg3_flag_clear(tp, TSO_CAPABLE);
16383 tg3_flag_clear(tp, TSO_BUG);
16384 tp->fw_needed = NULL;
16385 }
16386
Joe Perches41535772013-02-16 11:20:04 +000016387 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016388 tp->fw_needed = FIRMWARE_TG3;
16389
Nithin Sujirc4dab502013-03-06 17:02:34 +000016390 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16391 tp->fw_needed = FIRMWARE_TG357766;
16392
Matt Carlson507399f2009-11-13 13:03:37 +000016393 tp->irq_max = 1;
16394
Joe Perches63c3a662011-04-26 08:12:10 +000016395 if (tg3_flag(tp, 5750_PLUS)) {
16396 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016397 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16398 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16399 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16400 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016401 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016402 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016403
Joe Perches63c3a662011-04-26 08:12:10 +000016404 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016405 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016406 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016408
Joe Perches63c3a662011-04-26 08:12:10 +000016409 if (tg3_flag(tp, 57765_PLUS)) {
16410 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016411 tp->irq_max = TG3_IRQ_MAX_VECS;
16412 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016413 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016414
Michael Chan91024262012-09-28 07:12:38 +000016415 tp->txq_max = 1;
16416 tp->rxq_max = 1;
16417 if (tp->irq_max > 1) {
16418 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16419 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16420
Joe Perches41535772013-02-16 11:20:04 +000016421 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16422 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016423 tp->txq_max = tp->irq_max - 1;
16424 }
16425
Matt Carlsonb7abee62012-06-07 12:56:54 +000016426 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016427 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016428 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016429
Joe Perches41535772013-02-16 11:20:04 +000016430 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016431 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016432
Joe Perches41535772013-02-16 11:20:04 +000016433 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16434 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16435 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16436 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016437 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016438
Joe Perches63c3a662011-04-26 08:12:10 +000016439 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016440 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016441 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016442
Joe Perches63c3a662011-04-26 08:12:10 +000016443 if (!tg3_flag(tp, 5705_PLUS) ||
16444 tg3_flag(tp, 5780_CLASS) ||
16445 tg3_flag(tp, USE_JUMBO_BDFLAG))
16446 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016447
Matt Carlson52f44902008-11-21 17:17:04 -080016448 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16449 &pci_state_reg);
16450
Jon Mason708ebb3a2011-06-27 12:56:50 +000016451 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016452 u16 lnkctl;
16453
Joe Perches63c3a662011-04-26 08:12:10 +000016454 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016455
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016456 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016457 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016458 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016459 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016460 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016461 }
Joe Perches41535772013-02-16 11:20:04 +000016462 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16463 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16464 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16465 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016466 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016467 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016468 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016469 }
Joe Perches41535772013-02-16 11:20:04 +000016470 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000016471 /* BCM5785 devices are effectively PCIe devices, and should
16472 * follow PCIe codepaths, but do not have a PCIe capabilities
16473 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016474 */
Joe Perches63c3a662011-04-26 08:12:10 +000016475 tg3_flag_set(tp, PCI_EXPRESS);
16476 } else if (!tg3_flag(tp, 5705_PLUS) ||
16477 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016478 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16479 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016480 dev_err(&tp->pdev->dev,
16481 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016482 return -EIO;
16483 }
16484
16485 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016486 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016488
Michael Chan399de502005-10-03 14:02:39 -070016489 /* If we have an AMD 762 or VIA K8T800 chipset, write
16490 * reordering to the mailbox registers done by the host
16491 * controller can cause major troubles. We read back from
16492 * every mailbox register write to force the writes to be
16493 * posted to the chip in order.
16494 */
Matt Carlson41434702011-03-09 16:58:22 +000016495 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016496 !tg3_flag(tp, PCI_EXPRESS))
16497 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016498
Matt Carlson69fc4052008-12-21 20:19:57 -080016499 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16500 &tp->pci_cacheline_sz);
16501 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16502 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016503 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016504 tp->pci_lat_timer < 64) {
16505 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016506 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16507 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016508 }
16509
Matt Carlson16821282011-07-13 09:27:28 +000016510 /* Important! -- It is critical that the PCI-X hw workaround
16511 * situation is decided before the first MMIO register access.
16512 */
Joe Perches41535772013-02-16 11:20:04 +000016513 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016514 /* 5700 BX chips need to have their TX producer index
16515 * mailboxes written twice to workaround a bug.
16516 */
Joe Perches63c3a662011-04-26 08:12:10 +000016517 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016518
Matt Carlson52f44902008-11-21 17:17:04 -080016519 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016520 *
16521 * The workaround is to use indirect register accesses
16522 * for all chip writes not to mailbox registers.
16523 */
Joe Perches63c3a662011-04-26 08:12:10 +000016524 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016525 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016526
Joe Perches63c3a662011-04-26 08:12:10 +000016527 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016528
16529 /* The chip can have it's power management PCI config
16530 * space registers clobbered due to this bug.
16531 * So explicitly force the chip into D0 here.
16532 */
Matt Carlson9974a352007-10-07 23:27:28 -070016533 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016534 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016535 &pm_reg);
16536 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16537 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016538 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016539 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016540 pm_reg);
16541
16542 /* Also, force SERR#/PERR# in PCI command. */
16543 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16544 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16545 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16546 }
16547 }
16548
Linus Torvalds1da177e2005-04-16 15:20:36 -070016549 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016550 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016551 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016552 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016553
16554 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016555 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016556 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16557 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16558 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16559 }
16560
Michael Chan1ee582d2005-08-09 20:16:46 -070016561 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016562 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016563 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016564 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016565 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016566 tp->write32_tx_mbox = tg3_write32;
16567 tp->write32_rx_mbox = tg3_write32;
16568
16569 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016570 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016571 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016572 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016573 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016574 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016575 /*
16576 * Back to back register writes can cause problems on these
16577 * chips, the workaround is to read back all reg writes
16578 * except those to mailbox regs.
16579 *
16580 * See tg3_write_indirect_reg32().
16581 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016582 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016583 }
16584
Joe Perches63c3a662011-04-26 08:12:10 +000016585 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016586 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016587 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016588 tp->write32_rx_mbox = tg3_write_flush_reg32;
16589 }
Michael Chan20094932005-08-09 20:16:32 -070016590
Joe Perches63c3a662011-04-26 08:12:10 +000016591 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016592 tp->read32 = tg3_read_indirect_reg32;
16593 tp->write32 = tg3_write_indirect_reg32;
16594 tp->read32_mbox = tg3_read_indirect_mbox;
16595 tp->write32_mbox = tg3_write_indirect_mbox;
16596 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16597 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16598
16599 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016600 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016601
16602 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16603 pci_cmd &= ~PCI_COMMAND_MEMORY;
16604 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16605 }
Joe Perches41535772013-02-16 11:20:04 +000016606 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016607 tp->read32_mbox = tg3_read32_mbox_5906;
16608 tp->write32_mbox = tg3_write32_mbox_5906;
16609 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16610 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16611 }
Michael Chan68929142005-08-09 20:17:14 -070016612
Michael Chanbbadf502006-04-06 21:46:34 -070016613 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016614 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016615 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16616 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016617 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016618
Matt Carlson16821282011-07-13 09:27:28 +000016619 /* The memory arbiter has to be enabled in order for SRAM accesses
16620 * to succeed. Normally on powerup the tg3 chip firmware will make
16621 * sure it is enabled, but other entities such as system netboot
16622 * code might disable it.
16623 */
16624 val = tr32(MEMARB_MODE);
16625 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16626
Matt Carlson9dc5e342011-11-04 09:15:02 +000016627 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016628 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016629 tg3_flag(tp, 5780_CLASS)) {
16630 if (tg3_flag(tp, PCIX_MODE)) {
16631 pci_read_config_dword(tp->pdev,
16632 tp->pcix_cap + PCI_X_STATUS,
16633 &val);
16634 tp->pci_fn = val & 0x7;
16635 }
Joe Perches41535772013-02-16 11:20:04 +000016636 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16637 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16638 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016639 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016640 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16641 val = tr32(TG3_CPMU_STATUS);
16642
Joe Perches41535772013-02-16 11:20:04 +000016643 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016644 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16645 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016646 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16647 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016648 }
16649
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016650 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16651 tp->write32_tx_mbox = tg3_write_flush_reg32;
16652 tp->write32_rx_mbox = tg3_write_flush_reg32;
16653 }
16654
Michael Chan7d0c41e2005-04-21 17:06:20 -070016655 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016656 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016657 * determined before calling tg3_set_power_state() so that
16658 * we know whether or not to switch out of Vaux power.
16659 * When the flag is set, it means that GPIO1 is used for eeprom
16660 * write protect and also implies that it is a LOM where GPIOs
16661 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016662 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016663 tg3_get_eeprom_hw_cfg(tp);
16664
Matt Carlson1caf13e2013-03-06 17:02:29 +000016665 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016666 tg3_flag_clear(tp, TSO_CAPABLE);
16667 tg3_flag_clear(tp, TSO_BUG);
16668 tp->fw_needed = NULL;
16669 }
16670
Joe Perches63c3a662011-04-26 08:12:10 +000016671 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016672 /* Allow reads and writes to the
16673 * APE register and memory space.
16674 */
16675 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016676 PCISTATE_ALLOW_APE_SHMEM_WR |
16677 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016678 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16679 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016680
16681 tg3_ape_lock_init(tp);
Prashant Sreedharan506b0a32018-02-19 12:27:04 +053016682 tp->ape_hb_interval =
16683 msecs_to_jiffies(APE_HOST_HEARTBEAT_INT_5SEC);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016684 }
16685
Matt Carlson16821282011-07-13 09:27:28 +000016686 /* Set up tp->grc_local_ctrl before calling
16687 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16688 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016689 * It is also used as eeprom write protect on LOMs.
16690 */
16691 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016692 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016693 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016694 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16695 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016696 /* Unused GPIO3 must be driven as output on 5752 because there
16697 * are no pull-up resistors on unused GPIO pins.
16698 */
Joe Perches41535772013-02-16 11:20:04 +000016699 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016700 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016701
Joe Perches41535772013-02-16 11:20:04 +000016702 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16703 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016704 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016705 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16706
Matt Carlson8d519ab2009-04-20 06:58:01 +000016707 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16708 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016709 /* Turn off the debug UART. */
16710 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016711 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016712 /* Keep VMain power. */
16713 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16714 GRC_LCLCTRL_GPIO_OUTPUT0;
16715 }
16716
Joe Perches41535772013-02-16 11:20:04 +000016717 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016718 tp->grc_local_ctrl |=
16719 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16720
Matt Carlson16821282011-07-13 09:27:28 +000016721 /* Switch out of Vaux if it is a NIC */
16722 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016723
Linus Torvalds1da177e2005-04-16 15:20:36 -070016724 /* Derive initial jumbo mode from MTU assigned in
16725 * ether_setup() via the alloc_etherdev() call
16726 */
Joe Perches63c3a662011-04-26 08:12:10 +000016727 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16728 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729
16730 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016731 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16732 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16733 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16734 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016735 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016736 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016737 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016738 }
16739
Joe Perches41535772013-02-16 11:20:04 +000016740 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016741 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016742
Linus Torvalds1da177e2005-04-16 15:20:36 -070016743 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016744 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16745 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16746 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16747 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016748 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16749 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16750 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016751
Joe Perches41535772013-02-16 11:20:04 +000016752 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16753 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016754 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016755 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016756 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016757
Joe Perches63c3a662011-04-26 08:12:10 +000016758 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016759 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016760 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16761 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016762 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016763 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16764 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16765 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16766 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016767 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16768 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016769 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016770 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016771 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016772 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016773 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016775
Joe Perches41535772013-02-16 11:20:04 +000016776 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16777 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016778 tp->phy_otp = tg3_read_otp_phycfg(tp);
16779 if (tp->phy_otp == 0)
16780 tp->phy_otp = TG3_OTP_DEFAULT;
16781 }
16782
Joe Perches63c3a662011-04-26 08:12:10 +000016783 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016784 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16785 else
16786 tp->mi_mode = MAC_MI_MODE_BASE;
16787
Linus Torvalds1da177e2005-04-16 15:20:36 -070016788 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016789 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16790 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016791 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16792
Matt Carlson4d958472011-04-20 07:57:35 +000016793 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016794 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016795 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016796 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16797 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016798 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16799 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16800 }
16801
Joe Perches41535772013-02-16 11:20:04 +000016802 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16803 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016804 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016805
Matt Carlson158d7ab2008-05-29 01:37:54 -070016806 err = tg3_mdio_init(tp);
16807 if (err)
16808 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809
16810 /* Initialize data/descriptor byte/word swapping. */
16811 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016812 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16813 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016814 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16815 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16816 GRC_MODE_B2HRX_ENABLE |
16817 GRC_MODE_HTX2B_ENABLE |
16818 GRC_MODE_HOST_STACKUP);
16819 else
16820 val &= GRC_MODE_HOST_STACKUP;
16821
Linus Torvalds1da177e2005-04-16 15:20:36 -070016822 tw32(GRC_MODE, val | tp->grc_mode);
16823
16824 tg3_switch_clocks(tp);
16825
16826 /* Clear this out for sanity. */
16827 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16828
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016829 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16830 tw32(TG3PCI_REG_BASE_ADDR, 0);
16831
Linus Torvalds1da177e2005-04-16 15:20:36 -070016832 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16833 &pci_state_reg);
16834 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016835 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016836 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16837 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16838 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16839 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016840 void __iomem *sram_base;
16841
16842 /* Write some dummy words into the SRAM status block
16843 * area, see if it reads back correctly. If the return
16844 * value is bad, force enable the PCIX workaround.
16845 */
16846 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16847
16848 writel(0x00000000, sram_base);
16849 writel(0x00000000, sram_base + 4);
16850 writel(0xffffffff, sram_base + 4);
16851 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016852 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016853 }
16854 }
16855
16856 udelay(50);
16857 tg3_nvram_init(tp);
16858
Nithin Sujirc4dab502013-03-06 17:02:34 +000016859 /* If the device has an NVRAM, no need to load patch firmware */
16860 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16861 !tg3_flag(tp, NO_NVRAM))
16862 tp->fw_needed = NULL;
16863
Linus Torvalds1da177e2005-04-16 15:20:36 -070016864 grc_misc_cfg = tr32(GRC_MISC_CFG);
16865 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16866
Joe Perches41535772013-02-16 11:20:04 +000016867 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016868 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16869 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016870 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016871
Joe Perches63c3a662011-04-26 08:12:10 +000016872 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016873 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016874 tg3_flag_set(tp, TAGGED_STATUS);
16875 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016876 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16877 HOSTCC_MODE_CLRTICK_TXBD);
16878
16879 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16880 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16881 tp->misc_host_ctrl);
16882 }
16883
Matt Carlson3bda1252008-08-15 14:08:22 -070016884 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016885 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016886 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016887 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016888 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016889
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016890 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016891 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016892
16893 err = tg3_phy_probe(tp);
16894 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016895 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016896 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016897 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016898 }
16899
Matt Carlson184b8902010-04-05 10:19:25 +000016900 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016901 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016902
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016903 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16904 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016905 } else {
Joe Perches41535772013-02-16 11:20:04 +000016906 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016907 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016908 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016909 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016910 }
16911
16912 /* 5700 {AX,BX} chips have a broken status block link
16913 * change bit implementation, so we must use the
16914 * status register in those cases.
16915 */
Joe Perches41535772013-02-16 11:20:04 +000016916 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016917 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016918 else
Joe Perches63c3a662011-04-26 08:12:10 +000016919 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016920
16921 /* The led_ctrl is set during tg3_phy_probe, here we might
16922 * have to force the link status polling mechanism based
16923 * upon subsystem IDs.
16924 */
16925 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016926 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016927 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16928 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016929 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016930 }
16931
16932 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016933 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016934 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016935 else
Joe Perches63c3a662011-04-26 08:12:10 +000016936 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016937
Nithin Sujir1743b832014-01-03 10:09:14 -080016938 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16939 tg3_flag_set(tp, POLL_CPMU_LINK);
16940
Eric Dumazet9205fd92011-11-18 06:47:01 +000016941 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016942 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016943 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016944 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016945 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016946#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016947 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016948#endif
16949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016950
Matt Carlson2c49a442010-09-30 10:34:35 +000016951 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16952 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016953 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16954
Matt Carlson2c49a442010-09-30 10:34:35 +000016955 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016956
16957 /* Increment the rx prod index on the rx std ring by at most
16958 * 8 for these chips to workaround hw errata.
16959 */
Joe Perches41535772013-02-16 11:20:04 +000016960 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16961 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16962 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016963 tp->rx_std_max_post = 8;
16964
Joe Perches63c3a662011-04-26 08:12:10 +000016965 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016966 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16967 PCIE_PWR_MGMT_L1_THRESH_MSK;
16968
Linus Torvalds1da177e2005-04-16 15:20:36 -070016969 return err;
16970}
16971
David S. Miller49b6e95f2007-03-29 01:38:42 -070016972#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016973static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016974{
16975 struct net_device *dev = tp->dev;
16976 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016977 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016978 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016979 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016980
David S. Miller49b6e95f2007-03-29 01:38:42 -070016981 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016982 if (addr && len == ETH_ALEN) {
16983 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016985 }
16986 return -ENODEV;
16987}
16988
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016989static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016990{
16991 struct net_device *dev = tp->dev;
16992
Joe Perchesd458cdf2013-10-01 19:04:40 -070016993 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016994 return 0;
16995}
16996#endif
16997
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016998static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016999{
17000 struct net_device *dev = tp->dev;
17001 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080017002 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017003 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017004
David S. Miller49b6e95f2007-03-29 01:38:42 -070017005#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070017006 if (!tg3_get_macaddr_sparc(tp))
17007 return 0;
17008#endif
17009
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017010 if (tg3_flag(tp, IS_SSB_CORE)) {
17011 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
17012 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
17013 return 0;
17014 }
17015
Linus Torvalds1da177e2005-04-16 15:20:36 -070017016 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000017017 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000017018 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017019 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
17020 mac_offset = 0xcc;
17021 if (tg3_nvram_lock(tp))
17022 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
17023 else
17024 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000017025 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000017026 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000017027 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000017028 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000017029 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000017030 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070017031 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017032
17033 /* First try to get it from MAC address mailbox. */
17034 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
17035 if ((hi >> 16) == 0x484b) {
17036 dev->dev_addr[0] = (hi >> 8) & 0xff;
17037 dev->dev_addr[1] = (hi >> 0) & 0xff;
17038
17039 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
17040 dev->dev_addr[2] = (lo >> 24) & 0xff;
17041 dev->dev_addr[3] = (lo >> 16) & 0xff;
17042 dev->dev_addr[4] = (lo >> 8) & 0xff;
17043 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017044
Michael Chan008652b2006-03-27 23:14:53 -080017045 /* Some old bootcode may report a 0 MAC address in SRAM */
17046 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
17047 }
17048 if (!addr_ok) {
17049 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000017050 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000017051 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000017052 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070017053 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
17054 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080017055 }
17056 /* Finally just fetch it out of the MAC control regs. */
17057 else {
17058 hi = tr32(MAC_ADDR_0_HIGH);
17059 lo = tr32(MAC_ADDR_0_LOW);
17060
17061 dev->dev_addr[5] = lo & 0xff;
17062 dev->dev_addr[4] = (lo >> 8) & 0xff;
17063 dev->dev_addr[3] = (lo >> 16) & 0xff;
17064 dev->dev_addr[2] = (lo >> 24) & 0xff;
17065 dev->dev_addr[1] = hi & 0xff;
17066 dev->dev_addr[0] = (hi >> 8) & 0xff;
17067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017068 }
17069
17070 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070017071#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070017072 if (!tg3_get_default_macaddr_sparc(tp))
17073 return 0;
17074#endif
17075 return -EINVAL;
17076 }
17077 return 0;
17078}
17079
David S. Miller59e6b432005-05-18 22:50:10 -070017080#define BOUNDARY_SINGLE_CACHELINE 1
17081#define BOUNDARY_MULTI_CACHELINE 2
17082
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017083static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070017084{
17085 int cacheline_size;
17086 u8 byte;
17087 int goal;
17088
17089 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
17090 if (byte == 0)
17091 cacheline_size = 1024;
17092 else
17093 cacheline_size = (int) byte * 4;
17094
17095 /* On 5703 and later chips, the boundary bits have no
17096 * effect.
17097 */
Joe Perches41535772013-02-16 11:20:04 +000017098 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17099 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017100 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070017101 goto out;
17102
17103#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
17104 goal = BOUNDARY_MULTI_CACHELINE;
17105#else
17106#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
17107 goal = BOUNDARY_SINGLE_CACHELINE;
17108#else
17109 goal = 0;
17110#endif
17111#endif
17112
Joe Perches63c3a662011-04-26 08:12:10 +000017113 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017114 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
17115 goto out;
17116 }
17117
David S. Miller59e6b432005-05-18 22:50:10 -070017118 if (!goal)
17119 goto out;
17120
17121 /* PCI controllers on most RISC systems tend to disconnect
17122 * when a device tries to burst across a cache-line boundary.
17123 * Therefore, letting tg3 do so just wastes PCI bandwidth.
17124 *
17125 * Unfortunately, for PCI-E there are only limited
17126 * write-side controls for this, and thus for reads
17127 * we will still get the disconnects. We'll also waste
17128 * these PCI cycles for both read and write for chips
17129 * other than 5700 and 5701 which do not implement the
17130 * boundary bits.
17131 */
Joe Perches63c3a662011-04-26 08:12:10 +000017132 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017133 switch (cacheline_size) {
17134 case 16:
17135 case 32:
17136 case 64:
17137 case 128:
17138 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17139 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
17140 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
17141 } else {
17142 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17143 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17144 }
17145 break;
17146
17147 case 256:
17148 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
17149 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
17150 break;
17151
17152 default:
17153 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
17154 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
17155 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017156 }
Joe Perches63c3a662011-04-26 08:12:10 +000017157 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070017158 switch (cacheline_size) {
17159 case 16:
17160 case 32:
17161 case 64:
17162 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17163 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17164 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17165 break;
17166 }
17167 /* fallthrough */
17168 case 128:
17169 default:
17170 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17171 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17172 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017173 }
David S. Miller59e6b432005-05-18 22:50:10 -070017174 } else {
17175 switch (cacheline_size) {
17176 case 16:
17177 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17178 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17179 DMA_RWCTRL_WRITE_BNDRY_16);
17180 break;
17181 }
17182 /* fallthrough */
17183 case 32:
17184 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17185 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17186 DMA_RWCTRL_WRITE_BNDRY_32);
17187 break;
17188 }
17189 /* fallthrough */
17190 case 64:
17191 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17192 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17193 DMA_RWCTRL_WRITE_BNDRY_64);
17194 break;
17195 }
17196 /* fallthrough */
17197 case 128:
17198 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17199 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17200 DMA_RWCTRL_WRITE_BNDRY_128);
17201 break;
17202 }
17203 /* fallthrough */
17204 case 256:
17205 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17206 DMA_RWCTRL_WRITE_BNDRY_256);
17207 break;
17208 case 512:
17209 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17210 DMA_RWCTRL_WRITE_BNDRY_512);
17211 break;
17212 case 1024:
17213 default:
17214 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17215 DMA_RWCTRL_WRITE_BNDRY_1024);
17216 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017217 }
David S. Miller59e6b432005-05-18 22:50:10 -070017218 }
17219
17220out:
17221 return val;
17222}
17223
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017224static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017225 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017226{
17227 struct tg3_internal_buffer_desc test_desc;
17228 u32 sram_dma_descs;
17229 int i, ret;
17230
17231 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17232
17233 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17234 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17235 tw32(RDMAC_STATUS, 0);
17236 tw32(WDMAC_STATUS, 0);
17237
17238 tw32(BUFMGR_MODE, 0);
17239 tw32(FTQ_RESET, 0);
17240
17241 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17242 test_desc.addr_lo = buf_dma & 0xffffffff;
17243 test_desc.nic_mbuf = 0x00002100;
17244 test_desc.len = size;
17245
17246 /*
17247 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17248 * the *second* time the tg3 driver was getting loaded after an
17249 * initial scan.
17250 *
17251 * Broadcom tells me:
17252 * ...the DMA engine is connected to the GRC block and a DMA
17253 * reset may affect the GRC block in some unpredictable way...
17254 * The behavior of resets to individual blocks has not been tested.
17255 *
17256 * Broadcom noted the GRC reset will also reset all sub-components.
17257 */
17258 if (to_device) {
17259 test_desc.cqid_sqid = (13 << 8) | 2;
17260
17261 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17262 udelay(40);
17263 } else {
17264 test_desc.cqid_sqid = (16 << 8) | 7;
17265
17266 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17267 udelay(40);
17268 }
17269 test_desc.flags = 0x00000005;
17270
17271 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17272 u32 val;
17273
17274 val = *(((u32 *)&test_desc) + i);
17275 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17276 sram_dma_descs + (i * sizeof(u32)));
17277 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17278 }
17279 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17280
Matt Carlson859a588792010-04-05 10:19:28 +000017281 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017282 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017284 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017285
17286 ret = -ENODEV;
17287 for (i = 0; i < 40; i++) {
17288 u32 val;
17289
17290 if (to_device)
17291 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17292 else
17293 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17294 if ((val & 0xffff) == sram_dma_descs) {
17295 ret = 0;
17296 break;
17297 }
17298
17299 udelay(100);
17300 }
17301
17302 return ret;
17303}
17304
David S. Millerded73402005-05-23 13:59:47 -070017305#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017306
Benoit Taine9baa3c32014-08-08 15:56:03 +020017307static const struct pci_device_id tg3_dma_wait_state_chipsets[] = {
Joe Perches895950c2010-12-21 02:16:08 -080017308 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17309 { },
17310};
17311
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017312static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017313{
17314 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017315 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017316 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017317
Matt Carlson4bae65c2010-11-24 08:31:52 +000017318 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17319 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017320 if (!buf) {
17321 ret = -ENOMEM;
17322 goto out_nofree;
17323 }
17324
17325 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17326 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17327
David S. Miller59e6b432005-05-18 22:50:10 -070017328 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017329
Joe Perches63c3a662011-04-26 08:12:10 +000017330 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017331 goto out;
17332
Joe Perches63c3a662011-04-26 08:12:10 +000017333 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017334 /* DMA read watermark not used on PCIE */
17335 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017336 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017337 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17338 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017339 tp->dma_rwctrl |= 0x003f0000;
17340 else
17341 tp->dma_rwctrl |= 0x003f000f;
17342 } else {
Joe Perches41535772013-02-16 11:20:04 +000017343 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17344 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017345 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017346 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017347
Michael Chan4a29cc22006-03-19 13:21:12 -080017348 /* If the 5704 is behind the EPB bridge, we can
17349 * do the less restrictive ONE_DMA workaround for
17350 * better performance.
17351 */
Joe Perches63c3a662011-04-26 08:12:10 +000017352 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017353 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017354 tp->dma_rwctrl |= 0x8000;
17355 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017356 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17357
Joe Perches41535772013-02-16 11:20:04 +000017358 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017359 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017360 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017361 tp->dma_rwctrl |=
17362 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17363 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17364 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017365 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017366 /* 5780 always in PCIX mode */
17367 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017368 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017369 /* 5714 always in PCIX mode */
17370 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017371 } else {
17372 tp->dma_rwctrl |= 0x001b000f;
17373 }
17374 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017375 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17376 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017377
Joe Perches41535772013-02-16 11:20:04 +000017378 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17379 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017380 tp->dma_rwctrl &= 0xfffffff0;
17381
Joe Perches41535772013-02-16 11:20:04 +000017382 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17383 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017384 /* Remove this if it causes problems for some boards. */
17385 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17386
17387 /* On 5700/5701 chips, we need to set this bit.
17388 * Otherwise the chip will issue cacheline transactions
17389 * to streamable DMA memory with not all the byte
17390 * enables turned on. This is an error on several
17391 * RISC PCI controllers, in particular sparc64.
17392 *
17393 * On 5703/5704 chips, this bit has been reassigned
17394 * a different meaning. In particular, it is used
17395 * on those chips to enable a PCI-X workaround.
17396 */
17397 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17398 }
17399
17400 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17401
Linus Torvalds1da177e2005-04-16 15:20:36 -070017402
Joe Perches41535772013-02-16 11:20:04 +000017403 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17404 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017405 goto out;
17406
David S. Miller59e6b432005-05-18 22:50:10 -070017407 /* It is best to perform DMA test with maximum write burst size
17408 * to expose the 5700/5701 write DMA bug.
17409 */
17410 saved_dma_rwctrl = tp->dma_rwctrl;
17411 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17412 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17413
Linus Torvalds1da177e2005-04-16 15:20:36 -070017414 while (1) {
17415 u32 *p = buf, i;
17416
17417 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17418 p[i] = i;
17419
17420 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017421 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017422 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017423 dev_err(&tp->pdev->dev,
17424 "%s: Buffer write failed. err = %d\n",
17425 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017426 break;
17427 }
17428
Linus Torvalds1da177e2005-04-16 15:20:36 -070017429 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017430 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017431 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017432 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17433 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017434 break;
17435 }
17436
17437 /* Verify it. */
17438 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17439 if (p[i] == i)
17440 continue;
17441
David S. Miller59e6b432005-05-18 22:50:10 -070017442 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17443 DMA_RWCTRL_WRITE_BNDRY_16) {
17444 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017445 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17446 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17447 break;
17448 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017449 dev_err(&tp->pdev->dev,
17450 "%s: Buffer corrupted on read back! "
17451 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017452 ret = -ENODEV;
17453 goto out;
17454 }
17455 }
17456
17457 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17458 /* Success. */
17459 ret = 0;
17460 break;
17461 }
17462 }
David S. Miller59e6b432005-05-18 22:50:10 -070017463 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17464 DMA_RWCTRL_WRITE_BNDRY_16) {
17465 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017466 * now look for chipsets that are known to expose the
17467 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017468 */
Matt Carlson41434702011-03-09 16:58:22 +000017469 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017470 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17471 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017472 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017473 /* Safe to use the calculated DMA boundary. */
17474 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017475 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017476
David S. Miller59e6b432005-05-18 22:50:10 -070017477 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017479
17480out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017481 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017482out_nofree:
17483 return ret;
17484}
17485
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017486static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017487{
Joe Perches63c3a662011-04-26 08:12:10 +000017488 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017489 tp->bufmgr_config.mbuf_read_dma_low_water =
17490 DEFAULT_MB_RDMA_LOW_WATER_5705;
17491 tp->bufmgr_config.mbuf_mac_rx_low_water =
17492 DEFAULT_MB_MACRX_LOW_WATER_57765;
17493 tp->bufmgr_config.mbuf_high_water =
17494 DEFAULT_MB_HIGH_WATER_57765;
17495
17496 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17497 DEFAULT_MB_RDMA_LOW_WATER_5705;
17498 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17499 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17500 tp->bufmgr_config.mbuf_high_water_jumbo =
17501 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017502 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017503 tp->bufmgr_config.mbuf_read_dma_low_water =
17504 DEFAULT_MB_RDMA_LOW_WATER_5705;
17505 tp->bufmgr_config.mbuf_mac_rx_low_water =
17506 DEFAULT_MB_MACRX_LOW_WATER_5705;
17507 tp->bufmgr_config.mbuf_high_water =
17508 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017509 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017510 tp->bufmgr_config.mbuf_mac_rx_low_water =
17511 DEFAULT_MB_MACRX_LOW_WATER_5906;
17512 tp->bufmgr_config.mbuf_high_water =
17513 DEFAULT_MB_HIGH_WATER_5906;
17514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017515
Michael Chanfdfec1722005-07-25 12:31:48 -070017516 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17517 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17518 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17519 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17520 tp->bufmgr_config.mbuf_high_water_jumbo =
17521 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17522 } else {
17523 tp->bufmgr_config.mbuf_read_dma_low_water =
17524 DEFAULT_MB_RDMA_LOW_WATER;
17525 tp->bufmgr_config.mbuf_mac_rx_low_water =
17526 DEFAULT_MB_MACRX_LOW_WATER;
17527 tp->bufmgr_config.mbuf_high_water =
17528 DEFAULT_MB_HIGH_WATER;
17529
17530 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17531 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17532 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17533 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17534 tp->bufmgr_config.mbuf_high_water_jumbo =
17535 DEFAULT_MB_HIGH_WATER_JUMBO;
17536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017537
17538 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17539 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17540}
17541
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017542static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017543{
Matt Carlson79eb6902010-02-17 15:17:03 +000017544 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17545 case TG3_PHY_ID_BCM5400: return "5400";
17546 case TG3_PHY_ID_BCM5401: return "5401";
17547 case TG3_PHY_ID_BCM5411: return "5411";
17548 case TG3_PHY_ID_BCM5701: return "5701";
17549 case TG3_PHY_ID_BCM5703: return "5703";
17550 case TG3_PHY_ID_BCM5704: return "5704";
17551 case TG3_PHY_ID_BCM5705: return "5705";
17552 case TG3_PHY_ID_BCM5750: return "5750";
17553 case TG3_PHY_ID_BCM5752: return "5752";
17554 case TG3_PHY_ID_BCM5714: return "5714";
17555 case TG3_PHY_ID_BCM5780: return "5780";
17556 case TG3_PHY_ID_BCM5755: return "5755";
17557 case TG3_PHY_ID_BCM5787: return "5787";
17558 case TG3_PHY_ID_BCM5784: return "5784";
17559 case TG3_PHY_ID_BCM5756: return "5722/5756";
17560 case TG3_PHY_ID_BCM5906: return "5906";
17561 case TG3_PHY_ID_BCM5761: return "5761";
17562 case TG3_PHY_ID_BCM5718C: return "5718C";
17563 case TG3_PHY_ID_BCM5718S: return "5718S";
17564 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017565 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017566 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017567 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017568 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017569 case 0: return "serdes";
17570 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017572}
17573
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017574static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017575{
Joe Perches63c3a662011-04-26 08:12:10 +000017576 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017577 strcpy(str, "PCI Express");
17578 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017579 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017580 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17581
17582 strcpy(str, "PCIX:");
17583
17584 if ((clock_ctrl == 7) ||
17585 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17586 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17587 strcat(str, "133MHz");
17588 else if (clock_ctrl == 0)
17589 strcat(str, "33MHz");
17590 else if (clock_ctrl == 2)
17591 strcat(str, "50MHz");
17592 else if (clock_ctrl == 4)
17593 strcat(str, "66MHz");
17594 else if (clock_ctrl == 6)
17595 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017596 } else {
17597 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017598 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017599 strcat(str, "66MHz");
17600 else
17601 strcat(str, "33MHz");
17602 }
Joe Perches63c3a662011-04-26 08:12:10 +000017603 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017604 strcat(str, ":32-bit");
17605 else
17606 strcat(str, ":64-bit");
17607 return str;
17608}
17609
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017610static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017611{
17612 struct ethtool_coalesce *ec = &tp->coal;
17613
17614 memset(ec, 0, sizeof(*ec));
17615 ec->cmd = ETHTOOL_GCOALESCE;
17616 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17617 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17618 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17619 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17620 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17621 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17622 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17623 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17624 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17625
17626 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17627 HOSTCC_MODE_CLRTICK_TXBD)) {
17628 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17629 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17630 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17631 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17632 }
Michael Chand244c892005-07-05 14:42:33 -070017633
Joe Perches63c3a662011-04-26 08:12:10 +000017634 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017635 ec->rx_coalesce_usecs_irq = 0;
17636 ec->tx_coalesce_usecs_irq = 0;
17637 ec->stats_block_coalesce_usecs = 0;
17638 }
David S. Miller15f98502005-05-18 22:49:26 -070017639}
17640
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017641static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017642 const struct pci_device_id *ent)
17643{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017644 struct net_device *dev;
17645 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017646 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017647 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017648 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017649 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017650 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017651
Joe Perches05dbe002010-02-17 19:44:19 +000017652 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017653
17654 err = pci_enable_device(pdev);
17655 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017656 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017657 return err;
17658 }
17659
Linus Torvalds1da177e2005-04-16 15:20:36 -070017660 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17661 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017662 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017663 goto err_out_disable_pdev;
17664 }
17665
17666 pci_set_master(pdev);
17667
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017668 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017669 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017670 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017671 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017672 }
17673
Linus Torvalds1da177e2005-04-16 15:20:36 -070017674 SET_NETDEV_DEV(dev, &pdev->dev);
17675
Linus Torvalds1da177e2005-04-16 15:20:36 -070017676 tp = netdev_priv(dev);
17677 tp->pdev = pdev;
17678 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017679 tp->rx_mode = TG3_DEF_RX_MODE;
17680 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017681 tp->irq_sync = 1;
Ivan Vecera0486a062014-09-01 14:21:57 +020017682 tp->pcierr_recovery = false;
Matt Carlson8ef21422008-05-02 16:47:53 -070017683
Linus Torvalds1da177e2005-04-16 15:20:36 -070017684 if (tg3_debug > 0)
17685 tp->msg_enable = tg3_debug;
17686 else
17687 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17688
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017689 if (pdev_is_ssb_gige_core(pdev)) {
17690 tg3_flag_set(tp, IS_SSB_CORE);
17691 if (ssb_gige_must_flush_posted_writes(pdev))
17692 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17693 if (ssb_gige_one_dma_at_once(pdev))
17694 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017695 if (ssb_gige_have_roboswitch(pdev)) {
17696 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017697 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017698 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017699 if (ssb_gige_is_rgmii(pdev))
17700 tg3_flag_set(tp, RGMII_MODE);
17701 }
17702
Linus Torvalds1da177e2005-04-16 15:20:36 -070017703 /* The word/byte swap controls here control register access byte
17704 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17705 * setting below.
17706 */
17707 tp->misc_host_ctrl =
17708 MISC_HOST_CTRL_MASK_PCI_INT |
17709 MISC_HOST_CTRL_WORD_SWAP |
17710 MISC_HOST_CTRL_INDIR_ACCESS |
17711 MISC_HOST_CTRL_PCISTATE_RW;
17712
17713 /* The NONFRM (non-frame) byte/word swap controls take effect
17714 * on descriptor entries, anything which isn't packet data.
17715 *
17716 * The StrongARM chips on the board (one for tx, one for rx)
17717 * are running in big-endian mode.
17718 */
17719 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17720 GRC_MODE_WSWAP_NONFRM_DATA);
17721#ifdef __BIG_ENDIAN
17722 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17723#endif
17724 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017725 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017726 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017727
Matt Carlsond5fe4882008-11-21 17:20:32 -080017728 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017729 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017730 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017731 err = -ENOMEM;
17732 goto err_out_free_dev;
17733 }
17734
Matt Carlsonc9cab242011-07-13 09:27:27 +000017735 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17736 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17737 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17738 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17739 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017740 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017741 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17742 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017743 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017744 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17745 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017746 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17747 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017748 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17749 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017750 tg3_flag_set(tp, ENABLE_APE);
17751 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17752 if (!tp->aperegs) {
17753 dev_err(&pdev->dev,
17754 "Cannot map APE registers, aborting\n");
17755 err = -ENOMEM;
17756 goto err_out_iounmap;
17757 }
17758 }
17759
Linus Torvalds1da177e2005-04-16 15:20:36 -070017760 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17761 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017762
Linus Torvalds1da177e2005-04-16 15:20:36 -070017763 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017764 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017765 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017766 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017767
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017768 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017769 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017770 dev_err(&pdev->dev,
17771 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017772 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017773 }
17774
Michael Chan4a29cc22006-03-19 13:21:12 -080017775 /* The EPB bridge inside 5714, 5715, and 5780 and any
17776 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017777 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17778 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17779 * do DMA address check in tg3_start_xmit().
17780 */
Joe Perches63c3a662011-04-26 08:12:10 +000017781 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017782 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017783 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017784 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017785#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017786 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017787#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017788 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017789 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017790
17791 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017792 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017793 err = pci_set_dma_mask(pdev, dma_mask);
17794 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017795 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017796 err = pci_set_consistent_dma_mask(pdev,
17797 persist_dma_mask);
17798 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017799 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17800 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017801 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017802 }
17803 }
17804 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017805 if (err || dma_mask == DMA_BIT_MASK(32)) {
17806 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017807 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017808 dev_err(&pdev->dev,
17809 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017810 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017811 }
17812 }
17813
Michael Chanfdfec1722005-07-25 12:31:48 -070017814 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017815
Matt Carlson0da06062011-05-19 12:12:53 +000017816 /* 5700 B0 chips do not support checksumming correctly due
17817 * to hardware bugs.
17818 */
Joe Perches41535772013-02-16 11:20:04 +000017819 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017820 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17821
17822 if (tg3_flag(tp, 5755_PLUS))
17823 features |= NETIF_F_IPV6_CSUM;
17824 }
17825
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017826 /* TSO is on by default on chips that support hardware TSO.
17827 * Firmware TSO on older chips gives lower performance, so it
17828 * is off by default, but can be enabled using ethtool.
17829 */
Joe Perches63c3a662011-04-26 08:12:10 +000017830 if ((tg3_flag(tp, HW_TSO_1) ||
17831 tg3_flag(tp, HW_TSO_2) ||
17832 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017833 (features & NETIF_F_IP_CSUM))
17834 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017835 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017836 if (features & NETIF_F_IPV6_CSUM)
17837 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017838 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017839 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17840 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17841 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17842 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17843 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017844 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017846
Vlad Yasevich51dfe7b2014-03-24 17:52:12 -040017847 dev->features |= features | NETIF_F_HW_VLAN_CTAG_TX |
17848 NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlsond542fe22011-05-19 16:02:43 +000017849 dev->vlan_features |= features;
17850
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017851 /*
17852 * Add loopback capability only for a subset of devices that support
17853 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17854 * loopback for the remaining devices.
17855 */
Joe Perches41535772013-02-16 11:20:04 +000017856 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017857 !tg3_flag(tp, CPMU_PRESENT))
17858 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017859 features |= NETIF_F_LOOPBACK;
17860
Matt Carlson0da06062011-05-19 12:12:53 +000017861 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017862 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017863
Jarod Wilsone1c6dcc2016-10-17 15:54:04 -040017864 /* MTU range: 60 - 9000 or 1500, depending on hardware */
17865 dev->min_mtu = TG3_MIN_MTU;
17866 dev->max_mtu = TG3_MAX_MTU(tp);
17867
Joe Perches41535772013-02-16 11:20:04 +000017868 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017869 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017870 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017871 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017872 tp->rx_pending = 63;
17873 }
17874
Linus Torvalds1da177e2005-04-16 15:20:36 -070017875 err = tg3_get_device_address(tp);
17876 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017877 dev_err(&pdev->dev,
17878 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017879 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017880 }
17881
Matt Carlson78f90dc2009-11-13 13:03:42 +000017882 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17883 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17884 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017885 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017886 struct tg3_napi *tnapi = &tp->napi[i];
17887
17888 tnapi->tp = tp;
17889 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17890
17891 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017892 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017893 intmbx += 0x8;
17894 else
17895 intmbx += 0x4;
17896
17897 tnapi->consmbox = rcvmbx;
17898 tnapi->prodmbox = sndmbx;
17899
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017900 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017901 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017902 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017903 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017904
Joe Perches63c3a662011-04-26 08:12:10 +000017905 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017906 break;
17907
17908 /*
17909 * If we support MSIX, we'll be using RSS. If we're using
17910 * RSS, the first vector only handles link interrupts and the
17911 * remaining vectors handle rx and tx interrupts. Reuse the
17912 * mailbox values for the next iteration. The values we setup
17913 * above are still useful for the single vectored mode.
17914 */
17915 if (!i)
17916 continue;
17917
17918 rcvmbx += 0x8;
17919
17920 if (sndmbx & 0x4)
17921 sndmbx -= 0x4;
17922 else
17923 sndmbx += 0xc;
17924 }
17925
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017926 /*
17927 * Reset chip in case UNDI or EFI driver did not shutdown
17928 * DMA self test will enable WDMAC and we'll see (spurious)
17929 * pending DMA on the PCI bus at that point.
17930 */
17931 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17932 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017933 tg3_full_lock(tp, 0);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017934 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17935 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Jun'ichi Nomura \(NEC\)d0af71a2015-02-12 01:26:24 +000017936 tg3_full_unlock(tp);
Prashant Sreedharan05b0aa52014-12-20 12:16:17 -080017937 }
17938
17939 err = tg3_test_dma(tp);
17940 if (err) {
17941 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
17942 goto err_out_apeunmap;
17943 }
17944
Matt Carlsonc88864d2007-11-12 21:07:01 -080017945 tg3_init_coal(tp);
17946
Michael Chanc49a1562006-12-17 17:07:29 -080017947 pci_set_drvdata(pdev, dev);
17948
Joe Perches41535772013-02-16 11:20:04 +000017949 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17950 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17951 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017952 tg3_flag_set(tp, PTP_CAPABLE);
17953
Matt Carlson21f76382012-02-22 12:35:21 +000017954 tg3_timer_init(tp);
17955
Michael Chan402e1392013-02-14 12:13:41 +000017956 tg3_carrier_off(tp);
17957
Linus Torvalds1da177e2005-04-16 15:20:36 -070017958 err = register_netdev(dev);
17959 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017960 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017961 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017962 }
17963
Ivan Vecera20d14a52015-01-08 16:13:07 +010017964 if (tg3_flag(tp, PTP_CAPABLE)) {
17965 tg3_ptp_init(tp);
17966 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
17967 &tp->pdev->dev);
17968 if (IS_ERR(tp->ptp_clock))
17969 tp->ptp_clock = NULL;
17970 }
17971
Joe Perches05dbe002010-02-17 19:44:19 +000017972 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17973 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017974 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017975 tg3_bus_string(tp, str),
17976 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017977
Andrew Lunn22209432016-01-06 20:11:13 +010017978 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017979 char *ethtype;
17980
17981 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17982 ethtype = "10/100Base-TX";
17983 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17984 ethtype = "1000Base-SX";
17985 else
17986 ethtype = "10/100/1000Base-T";
17987
Matt Carlson5129c3a2010-04-05 10:19:23 +000017988 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017989 "(WireSpeed[%d], EEE[%d])\n",
17990 tg3_phy_string(tp), ethtype,
17991 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17992 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017993 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017994
Joe Perches05dbe002010-02-17 19:44:19 +000017995 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017996 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017997 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017998 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017999 tg3_flag(tp, ENABLE_ASF) != 0,
18000 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000018001 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
18002 tp->dma_rwctrl,
18003 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
18004 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018005
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018006 pci_save_state(pdev);
18007
Linus Torvalds1da177e2005-04-16 15:20:36 -070018008 return 0;
18009
Matt Carlson0d3031d2007-10-10 18:02:43 -070018010err_out_apeunmap:
18011 if (tp->aperegs) {
18012 iounmap(tp->aperegs);
18013 tp->aperegs = NULL;
18014 }
18015
Linus Torvalds1da177e2005-04-16 15:20:36 -070018016err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070018017 if (tp->regs) {
18018 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070018019 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070018020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018021
18022err_out_free_dev:
18023 free_netdev(dev);
18024
18025err_out_free_res:
18026 pci_release_regions(pdev);
18027
18028err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080018029 if (pci_is_enabled(pdev))
18030 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018031 return err;
18032}
18033
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018034static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018035{
18036 struct net_device *dev = pci_get_drvdata(pdev);
18037
18038 if (dev) {
18039 struct tg3 *tp = netdev_priv(dev);
18040
Ivan Vecera20d14a52015-01-08 16:13:07 +010018041 tg3_ptp_fini(tp);
18042
Jesper Juhle3c55302012-04-09 22:50:15 +020018043 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080018044
Matt Carlsondb219972011-11-04 09:15:03 +000018045 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018046
David S. Miller1805b2f2011-10-24 18:18:09 -040018047 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018048 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070018049 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018050 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070018051
Linus Torvalds1da177e2005-04-16 15:20:36 -070018052 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070018053 if (tp->aperegs) {
18054 iounmap(tp->aperegs);
18055 tp->aperegs = NULL;
18056 }
Michael Chan68929142005-08-09 20:17:14 -070018057 if (tp->regs) {
18058 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070018059 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070018060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070018061 free_netdev(dev);
18062 pci_release_regions(pdev);
18063 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018064 }
18065}
18066
Eric Dumazetaa6027c2011-01-01 05:22:46 +000018067#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018068static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018069{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018070 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018071 struct net_device *dev = pci_get_drvdata(pdev);
18072 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018073 int err = 0;
18074
18075 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018076
18077 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018078 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018079
Matt Carlsondb219972011-11-04 09:15:03 +000018080 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018081 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018082 tg3_netif_stop(tp);
18083
Matt Carlson21f76382012-02-22 12:35:21 +000018084 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018085
David S. Millerf47c11e2005-06-24 20:18:35 -070018086 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018087 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070018088 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018089
18090 netif_device_detach(dev);
18091
David S. Millerf47c11e2005-06-24 20:18:35 -070018092 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070018093 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000018094 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070018095 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018096
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018097 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018098 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018099 int err2;
18100
David S. Millerf47c11e2005-06-24 20:18:35 -070018101 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018102
Joe Perches63c3a662011-04-26 08:12:10 +000018103 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018104 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018105 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070018106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018107
Matt Carlson21f76382012-02-22 12:35:21 +000018108 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018109
18110 netif_device_attach(dev);
18111 tg3_netif_start(tp);
18112
Michael Chanb9ec6c12006-07-25 16:37:27 -070018113out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018114 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018115
18116 if (!err2)
18117 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018118 }
18119
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018120unlock:
18121 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018122 return err;
18123}
18124
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018125static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018126{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018127 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018128 struct net_device *dev = pci_get_drvdata(pdev);
18129 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018130 int err = 0;
18131
18132 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070018133
18134 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018135 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018136
Linus Torvalds1da177e2005-04-16 15:20:36 -070018137 netif_device_attach(dev);
18138
David S. Millerf47c11e2005-06-24 20:18:35 -070018139 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018140
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018141 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
18142
Joe Perches63c3a662011-04-26 08:12:10 +000018143 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000018144 err = tg3_restart_hw(tp,
18145 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070018146 if (err)
18147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018148
Matt Carlson21f76382012-02-22 12:35:21 +000018149 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018150
Linus Torvalds1da177e2005-04-16 15:20:36 -070018151 tg3_netif_start(tp);
18152
Michael Chanb9ec6c12006-07-25 16:37:27 -070018153out:
David S. Millerf47c11e2005-06-24 20:18:35 -070018154 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018155
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070018156 if (!err)
18157 tg3_phy_start(tp);
18158
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010018159unlock:
18160 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070018161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018162}
Fabio Estevam42df36a2013-04-16 09:28:29 +000018163#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018164
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000018165static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
18166
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018167static void tg3_shutdown(struct pci_dev *pdev)
18168{
18169 struct net_device *dev = pci_get_drvdata(pdev);
18170 struct tg3 *tp = netdev_priv(dev);
18171
18172 rtnl_lock();
18173 netif_device_detach(dev);
18174
18175 if (netif_running(dev))
18176 dev_close(dev);
18177
18178 if (system_state == SYSTEM_POWER_OFF)
18179 tg3_power_down(tp);
18180
18181 rtnl_unlock();
18182}
18183
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018184/**
18185 * tg3_io_error_detected - called when PCI error is detected
18186 * @pdev: Pointer to PCI device
18187 * @state: The current pci connection state
18188 *
18189 * This function is called after a PCI bus error affecting
18190 * this device has been detected.
18191 */
18192static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18193 pci_channel_state_t state)
18194{
18195 struct net_device *netdev = pci_get_drvdata(pdev);
18196 struct tg3 *tp = netdev_priv(netdev);
18197 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18198
18199 netdev_info(netdev, "PCI I/O error detected\n");
18200
18201 rtnl_lock();
18202
Gavin Shand8af4df2013-07-24 17:25:08 +080018203 /* We probably don't have netdev yet */
18204 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018205 goto done;
18206
Milton Miller1b0ff892016-09-29 13:24:08 -030018207 /* We needn't recover from permanent error */
18208 if (state == pci_channel_io_frozen)
18209 tp->pcierr_recovery = true;
18210
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018211 tg3_phy_stop(tp);
18212
18213 tg3_netif_stop(tp);
18214
Matt Carlson21f76382012-02-22 12:35:21 +000018215 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018216
18217 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018218 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018219
18220 netif_device_detach(netdev);
18221
18222 /* Clean up software state, even if MMIO is blocked */
18223 tg3_full_lock(tp, 0);
18224 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18225 tg3_full_unlock(tp);
18226
18227done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018228 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018229 if (netdev) {
18230 tg3_napi_enable(tp);
18231 dev_close(netdev);
18232 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018233 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018234 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018235 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018236 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018237
18238 rtnl_unlock();
18239
18240 return err;
18241}
18242
18243/**
18244 * tg3_io_slot_reset - called after the pci bus has been reset.
18245 * @pdev: Pointer to PCI device
18246 *
18247 * Restart the card from scratch, as if from a cold-boot.
18248 * At this point, the card has exprienced a hard reset,
18249 * followed by fixups by BIOS, and has its config space
18250 * set up identically to what it was at cold boot.
18251 */
18252static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18253{
18254 struct net_device *netdev = pci_get_drvdata(pdev);
18255 struct tg3 *tp = netdev_priv(netdev);
18256 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18257 int err;
18258
18259 rtnl_lock();
18260
18261 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018262 dev_err(&pdev->dev,
18263 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018264 goto done;
18265 }
18266
18267 pci_set_master(pdev);
18268 pci_restore_state(pdev);
18269 pci_save_state(pdev);
18270
Daniel Borkmann68293092013-08-13 11:45:13 -070018271 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018272 rc = PCI_ERS_RESULT_RECOVERED;
18273 goto done;
18274 }
18275
18276 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018277 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018278 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018279
18280 rc = PCI_ERS_RESULT_RECOVERED;
18281
18282done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018283 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018284 tg3_napi_enable(tp);
18285 dev_close(netdev);
18286 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018287 rtnl_unlock();
18288
18289 return rc;
18290}
18291
18292/**
18293 * tg3_io_resume - called when traffic can start flowing again.
18294 * @pdev: Pointer to PCI device
18295 *
18296 * This callback is called when the error recovery driver tells
18297 * us that its OK to resume normal operation.
18298 */
18299static void tg3_io_resume(struct pci_dev *pdev)
18300{
18301 struct net_device *netdev = pci_get_drvdata(pdev);
18302 struct tg3 *tp = netdev_priv(netdev);
18303 int err;
18304
18305 rtnl_lock();
18306
Milton Miller1b0ff892016-09-29 13:24:08 -030018307 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018308 goto done;
18309
18310 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018311 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018312 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018313 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018314 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018315 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018316 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18317 goto done;
18318 }
18319
18320 netif_device_attach(netdev);
18321
Matt Carlson21f76382012-02-22 12:35:21 +000018322 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018323
18324 tg3_netif_start(tp);
18325
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018326 tg3_full_unlock(tp);
18327
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018328 tg3_phy_start(tp);
18329
18330done:
Ivan Vecera0486a062014-09-01 14:21:57 +020018331 tp->pcierr_recovery = false;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018332 rtnl_unlock();
18333}
18334
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018335static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018336 .error_detected = tg3_io_error_detected,
18337 .slot_reset = tg3_io_slot_reset,
18338 .resume = tg3_io_resume
18339};
18340
Linus Torvalds1da177e2005-04-16 15:20:36 -070018341static struct pci_driver tg3_driver = {
18342 .name = DRV_MODULE_NAME,
18343 .id_table = tg3_pci_tbl,
18344 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018345 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018346 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018347 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018348 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018349};
18350
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018351module_pci_driver(tg3_driver);