blob: 3b6d0ba86c714d34204bd359d61a54ece62cd139 [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.
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +00007 * Copyright (C) 2005-2013 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000028#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Michael Chane565eec2014-01-03 10:09:12 -080039#include <linux/if.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Nithin Sujir20170e72014-01-03 10:09:15 -080097#define TG3_MIN_NUM 136
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujir20170e72014-01-03 10:09:15 -0800100#define DRV_MODULE_RELDATE "Jan 03, 2014"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Michael Chane565eec2014-01-03 10:09:12 -0800211#define TG3_MAX_UCAST_ADDR(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 3)
212#define TG3_UCAST_ADDR_IDX(tp) (tg3_flag((tp), ENABLE_ASF) ? 2 : 1)
213
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000214#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000215#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800217#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000218#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
220#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
221
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500222static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000223 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
226MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
227MODULE_LICENSE("GPL");
228MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800229MODULE_FIRMWARE(FIRMWARE_TG3);
230MODULE_FIRMWARE(FIRMWARE_TG3TSO);
231MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
234module_param(tg3_debug, int, 0);
235MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
236
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000237#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
238#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
239
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000240static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
267 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
268 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
281 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000289 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
290 PCI_VENDOR_ID_LENOVO,
291 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
292 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
295 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
311 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000312 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
313 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
317 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
318 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
323 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
333 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
335 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000340 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
341 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
342 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Nithin Sujir68273712013-09-20 16:46:56 -0700343 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57764)},
344 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57767)},
345 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57787)},
346 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57782)},
347 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57786)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
349 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
350 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
351 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
352 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
353 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
354 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000355 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700356 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
359MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
360
Andreas Mohr50da8592006-08-14 23:54:30 -0700361static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000363} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 { "rx_octets" },
365 { "rx_fragments" },
366 { "rx_ucast_packets" },
367 { "rx_mcast_packets" },
368 { "rx_bcast_packets" },
369 { "rx_fcs_errors" },
370 { "rx_align_errors" },
371 { "rx_xon_pause_rcvd" },
372 { "rx_xoff_pause_rcvd" },
373 { "rx_mac_ctrl_rcvd" },
374 { "rx_xoff_entered" },
375 { "rx_frame_too_long_errors" },
376 { "rx_jabbers" },
377 { "rx_undersize_packets" },
378 { "rx_in_length_errors" },
379 { "rx_out_length_errors" },
380 { "rx_64_or_less_octet_packets" },
381 { "rx_65_to_127_octet_packets" },
382 { "rx_128_to_255_octet_packets" },
383 { "rx_256_to_511_octet_packets" },
384 { "rx_512_to_1023_octet_packets" },
385 { "rx_1024_to_1522_octet_packets" },
386 { "rx_1523_to_2047_octet_packets" },
387 { "rx_2048_to_4095_octet_packets" },
388 { "rx_4096_to_8191_octet_packets" },
389 { "rx_8192_to_9022_octet_packets" },
390
391 { "tx_octets" },
392 { "tx_collisions" },
393
394 { "tx_xon_sent" },
395 { "tx_xoff_sent" },
396 { "tx_flow_control" },
397 { "tx_mac_errors" },
398 { "tx_single_collisions" },
399 { "tx_mult_collisions" },
400 { "tx_deferred" },
401 { "tx_excessive_collisions" },
402 { "tx_late_collisions" },
403 { "tx_collide_2times" },
404 { "tx_collide_3times" },
405 { "tx_collide_4times" },
406 { "tx_collide_5times" },
407 { "tx_collide_6times" },
408 { "tx_collide_7times" },
409 { "tx_collide_8times" },
410 { "tx_collide_9times" },
411 { "tx_collide_10times" },
412 { "tx_collide_11times" },
413 { "tx_collide_12times" },
414 { "tx_collide_13times" },
415 { "tx_collide_14times" },
416 { "tx_collide_15times" },
417 { "tx_ucast_packets" },
418 { "tx_mcast_packets" },
419 { "tx_bcast_packets" },
420 { "tx_carrier_sense_errors" },
421 { "tx_discards" },
422 { "tx_errors" },
423
424 { "dma_writeq_full" },
425 { "dma_write_prioq_full" },
426 { "rxbds_empty" },
427 { "rx_discards" },
428 { "rx_errors" },
429 { "rx_threshold_hit" },
430
431 { "dma_readq_full" },
432 { "dma_read_prioq_full" },
433 { "tx_comp_queue_full" },
434
435 { "ring_set_send_prod_index" },
436 { "ring_status_update" },
437 { "nic_irqs" },
438 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000439 { "nic_tx_threshold_hit" },
440
441 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Matt Carlson48fa55a2011-04-13 11:05:06 +0000444#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000445#define TG3_NVRAM_TEST 0
446#define TG3_LINK_TEST 1
447#define TG3_REGISTER_TEST 2
448#define TG3_MEMORY_TEST 3
449#define TG3_MAC_LOOPB_TEST 4
450#define TG3_PHY_LOOPB_TEST 5
451#define TG3_EXT_LOOPB_TEST 6
452#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000453
454
Andreas Mohr50da8592006-08-14 23:54:30 -0700455static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700456 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000457} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000458 [TG3_NVRAM_TEST] = { "nvram test (online) " },
459 [TG3_LINK_TEST] = { "link test (online) " },
460 [TG3_REGISTER_TEST] = { "register test (offline)" },
461 [TG3_MEMORY_TEST] = { "memory test (offline)" },
462 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
463 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
464 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
465 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700466};
467
Matt Carlson48fa55a2011-04-13 11:05:06 +0000468#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
469
470
Michael Chanb401e9e2005-12-19 16:27:04 -0800471static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
472{
473 writel(val, tp->regs + off);
474}
475
476static u32 tg3_read32(struct tg3 *tp, u32 off)
477{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000478 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800479}
480
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
482{
483 writel(val, tp->aperegs + off);
484}
485
486static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
487{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000488 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
492{
Michael Chan68929142005-08-09 20:17:14 -0700493 unsigned long flags;
494
495 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700496 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
497 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700498 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700499}
500
501static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
502{
503 writel(val, tp->regs + off);
504 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Michael Chan68929142005-08-09 20:17:14 -0700507static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
508{
509 unsigned long flags;
510 u32 val;
511
512 spin_lock_irqsave(&tp->indirect_lock, flags);
513 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
514 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
515 spin_unlock_irqrestore(&tp->indirect_lock, flags);
516 return val;
517}
518
519static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
520{
521 unsigned long flags;
522
523 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
524 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
525 TG3_64BIT_REG_LOW, val);
526 return;
527 }
Matt Carlson66711e62009-11-13 13:03:49 +0000528 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700529 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
530 TG3_64BIT_REG_LOW, val);
531 return;
532 }
533
534 spin_lock_irqsave(&tp->indirect_lock, flags);
535 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
536 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
537 spin_unlock_irqrestore(&tp->indirect_lock, flags);
538
539 /* In indirect mode when disabling interrupts, we also need
540 * to clear the interrupt bit in the GRC local ctrl register.
541 */
542 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
543 (val == 0x1)) {
544 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
545 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
546 }
547}
548
549static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
550{
551 unsigned long flags;
552 u32 val;
553
554 spin_lock_irqsave(&tp->indirect_lock, flags);
555 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
556 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
557 spin_unlock_irqrestore(&tp->indirect_lock, flags);
558 return val;
559}
560
Michael Chanb401e9e2005-12-19 16:27:04 -0800561/* usec_wait specifies the wait time in usec when writing to certain registers
562 * where it is unsafe to read back the register without some delay.
563 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
564 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
565 */
566static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Joe Perches63c3a662011-04-26 08:12:10 +0000568 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800569 /* Non-posted methods */
570 tp->write32(tp, off, val);
571 else {
572 /* Posted method */
573 tg3_write32(tp, off, val);
574 if (usec_wait)
575 udelay(usec_wait);
576 tp->read32(tp, off);
577 }
578 /* Wait again after the read for the posted method to guarantee that
579 * the wait time is met.
580 */
581 if (usec_wait)
582 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Michael Chan09ee9292005-08-09 20:17:00 -0700585static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
586{
587 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000588 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
589 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
590 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700591 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700592}
593
Michael Chan20094932005-08-09 20:16:32 -0700594static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 void __iomem *mbox = tp->regs + off;
597 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000598 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000600 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
601 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 readl(mbox);
603}
604
Michael Chanb5d37722006-09-27 16:06:21 -0700605static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
606{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000607 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700608}
609
610static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
611{
612 writel(val, tp->regs + off + GRCMBOX_BASE);
613}
614
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000615#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700616#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000617#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
618#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
619#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700620
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000621#define tw32(reg, val) tp->write32(tp, reg, val)
622#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
623#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
624#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
627{
Michael Chan68929142005-08-09 20:17:14 -0700628 unsigned long flags;
629
Joe Perches41535772013-02-16 11:20:04 +0000630 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700631 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
632 return;
633
Michael Chan68929142005-08-09 20:17:14 -0700634 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000635 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700636 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
637 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Michael Chanbbadf502006-04-06 21:46:34 -0700639 /* Always leave this as zero. */
640 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
641 } else {
642 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
643 tw32_f(TG3PCI_MEM_WIN_DATA, val);
644
645 /* Always leave this as zero. */
646 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
647 }
Michael Chan68929142005-08-09 20:17:14 -0700648 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
652{
Michael Chan68929142005-08-09 20:17:14 -0700653 unsigned long flags;
654
Joe Perches41535772013-02-16 11:20:04 +0000655 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700656 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
657 *val = 0;
658 return;
659 }
660
Michael Chan68929142005-08-09 20:17:14 -0700661 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000662 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700663 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
664 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Michael Chanbbadf502006-04-06 21:46:34 -0700666 /* Always leave this as zero. */
667 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
668 } else {
669 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
670 *val = tr32(TG3PCI_MEM_WIN_DATA);
671
672 /* Always leave this as zero. */
673 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
674 }
Michael Chan68929142005-08-09 20:17:14 -0700675 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678static void tg3_ape_lock_init(struct tg3 *tp)
679{
680 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000681 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000682
Joe Perches41535772013-02-16 11:20:04 +0000683 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000684 regbase = TG3_APE_LOCK_GRANT;
685 else
686 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700687
688 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000689 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
690 switch (i) {
691 case TG3_APE_LOCK_PHY0:
692 case TG3_APE_LOCK_PHY1:
693 case TG3_APE_LOCK_PHY2:
694 case TG3_APE_LOCK_PHY3:
695 bit = APE_LOCK_GRANT_DRIVER;
696 break;
697 default:
698 if (!tp->pci_fn)
699 bit = APE_LOCK_GRANT_DRIVER;
700 else
701 bit = 1 << tp->pci_fn;
702 }
703 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 }
705
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706}
707
708static int tg3_ape_lock(struct tg3 *tp, int locknum)
709{
710 int i, off;
711 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700713
Joe Perches63c3a662011-04-26 08:12:10 +0000714 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700715 return 0;
716
717 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000718 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000719 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000720 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000721 case TG3_APE_LOCK_GRC:
722 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000723 if (!tp->pci_fn)
724 bit = APE_LOCK_REQ_DRIVER;
725 else
726 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000727 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000728 case TG3_APE_LOCK_PHY0:
729 case TG3_APE_LOCK_PHY1:
730 case TG3_APE_LOCK_PHY2:
731 case TG3_APE_LOCK_PHY3:
732 bit = APE_LOCK_REQ_DRIVER;
733 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000734 default:
735 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700736 }
737
Joe Perches41535772013-02-16 11:20:04 +0000738 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000739 req = TG3_APE_LOCK_REQ;
740 gnt = TG3_APE_LOCK_GRANT;
741 } else {
742 req = TG3_APE_PER_LOCK_REQ;
743 gnt = TG3_APE_PER_LOCK_GRANT;
744 }
745
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 off = 4 * locknum;
747
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000748 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700749
750 /* Wait for up to 1 millisecond to acquire lock. */
751 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000752 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000753 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +0800755 if (pci_channel_offline(tp->pdev))
756 break;
757
Matt Carlson0d3031d2007-10-10 18:02:43 -0700758 udelay(10);
759 }
760
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000763 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 ret = -EBUSY;
765 }
766
767 return ret;
768}
769
770static void tg3_ape_unlock(struct tg3 *tp, int locknum)
771{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000772 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700773
Joe Perches63c3a662011-04-26 08:12:10 +0000774 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700775 return;
776
777 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000778 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000779 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000780 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000781 case TG3_APE_LOCK_GRC:
782 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000783 if (!tp->pci_fn)
784 bit = APE_LOCK_GRANT_DRIVER;
785 else
786 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000787 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000788 case TG3_APE_LOCK_PHY0:
789 case TG3_APE_LOCK_PHY1:
790 case TG3_APE_LOCK_PHY2:
791 case TG3_APE_LOCK_PHY3:
792 bit = APE_LOCK_GRANT_DRIVER;
793 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000794 default:
795 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700796 }
797
Joe Perches41535772013-02-16 11:20:04 +0000798 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000799 gnt = TG3_APE_LOCK_GRANT;
800 else
801 gnt = TG3_APE_PER_LOCK_GRANT;
802
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000803 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700804}
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000807{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000808 u32 apedata;
809
Matt Carlsonb65a3722012-07-16 16:24:00 +0000810 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000812 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000813
814 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000815 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
816 break;
817
Matt Carlsonb65a3722012-07-16 16:24:00 +0000818 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
819
820 udelay(10);
821 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000822 }
823
Matt Carlsonb65a3722012-07-16 16:24:00 +0000824 return timeout_us ? 0 : -EBUSY;
825}
826
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000827static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
828{
829 u32 i, apedata;
830
831 for (i = 0; i < timeout_us / 10; i++) {
832 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
833
834 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
835 break;
836
837 udelay(10);
838 }
839
840 return i == timeout_us / 10;
841}
842
Michael Chan86449942012-10-02 20:31:14 -0700843static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
844 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000845{
846 int err;
847 u32 i, bufoff, msgoff, maxlen, apedata;
848
849 if (!tg3_flag(tp, APE_HAS_NCSI))
850 return 0;
851
852 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
853 if (apedata != APE_SEG_SIG_MAGIC)
854 return -ENODEV;
855
856 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
857 if (!(apedata & APE_FW_STATUS_READY))
858 return -EAGAIN;
859
860 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
861 TG3_APE_SHMEM_BASE;
862 msgoff = bufoff + 2 * sizeof(u32);
863 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
864
865 while (len) {
866 u32 length;
867
868 /* Cap xfer sizes to scratchpad limits. */
869 length = (len > maxlen) ? maxlen : len;
870 len -= length;
871
872 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
873 if (!(apedata & APE_FW_STATUS_READY))
874 return -EAGAIN;
875
876 /* Wait for up to 1 msec for APE to service previous event. */
877 err = tg3_ape_event_lock(tp, 1000);
878 if (err)
879 return err;
880
881 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
882 APE_EVENT_STATUS_SCRTCHPD_READ |
883 APE_EVENT_STATUS_EVENT_PENDING;
884 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
885
886 tg3_ape_write32(tp, bufoff, base_off);
887 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
888
889 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
890 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
891
892 base_off += length;
893
894 if (tg3_ape_wait_for_event(tp, 30000))
895 return -EAGAIN;
896
897 for (i = 0; length; i += 4, length -= 4) {
898 u32 val = tg3_ape_read32(tp, msgoff + i);
899 memcpy(data, &val, sizeof(u32));
900 data++;
901 }
902 }
903
904 return 0;
905}
906
Matt Carlsonb65a3722012-07-16 16:24:00 +0000907static int tg3_ape_send_event(struct tg3 *tp, u32 event)
908{
909 int err;
910 u32 apedata;
911
912 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
913 if (apedata != APE_SEG_SIG_MAGIC)
914 return -EAGAIN;
915
916 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
917 if (!(apedata & APE_FW_STATUS_READY))
918 return -EAGAIN;
919
920 /* Wait for up to 1 millisecond for APE to service previous event. */
921 err = tg3_ape_event_lock(tp, 1000);
922 if (err)
923 return err;
924
925 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
926 event | APE_EVENT_STATUS_EVENT_PENDING);
927
928 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
929 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
930
931 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000932}
933
934static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
935{
936 u32 event;
937 u32 apedata;
938
939 if (!tg3_flag(tp, ENABLE_APE))
940 return;
941
942 switch (kind) {
943 case RESET_KIND_INIT:
944 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
945 APE_HOST_SEG_SIG_MAGIC);
946 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
947 APE_HOST_SEG_LEN_MAGIC);
948 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
949 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
950 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
951 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
952 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
953 APE_HOST_BEHAV_NO_PHYLOCK);
954 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
955 TG3_APE_HOST_DRVR_STATE_START);
956
957 event = APE_EVENT_STATUS_STATE_START;
958 break;
959 case RESET_KIND_SHUTDOWN:
960 /* With the interface we are currently using,
961 * APE does not track driver state. Wiping
962 * out the HOST SEGMENT SIGNATURE forces
963 * the APE to assume OS absent status.
964 */
965 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
966
967 if (device_may_wakeup(&tp->pdev->dev) &&
968 tg3_flag(tp, WOL_ENABLE)) {
969 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
970 TG3_APE_HOST_WOL_SPEED_AUTO);
971 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
972 } else
973 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
974
975 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
976
977 event = APE_EVENT_STATUS_STATE_UNLOAD;
978 break;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000979 default:
980 return;
981 }
982
983 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
984
985 tg3_ape_send_event(tp, event);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void tg3_disable_ints(struct tg3 *tp)
989{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000990 int i;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 tw32(TG3PCI_MISC_HOST_CTRL,
993 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 for (i = 0; i < tp->irq_max; i++)
995 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static void tg3_enable_ints(struct tg3 *tp)
999{
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001000 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001
Michael Chanbbe832c2005-06-24 20:20:04 -07001002 tp->irq_sync = 0;
1003 wmb();
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 tw32(TG3PCI_MISC_HOST_CTRL,
1006 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 for (i = 0; i < tp->irq_cnt; i++) {
1010 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001011
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001012 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001013 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001014 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1015
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001017 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001018
1019 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001020 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001021 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1022 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1023 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001024 tw32(HOSTCC_MODE, tp->coal_now);
1025
1026 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Matt Carlson17375d22009-08-28 14:02:18 +00001029static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001030{
Matt Carlson17375d22009-08-28 14:02:18 +00001031 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001032 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001033 unsigned int work_exists = 0;
1034
1035 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001036 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001037 if (sblk->status & SD_STATUS_LINK_CHG)
1038 work_exists = 1;
1039 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001040
1041 /* check for TX work to do */
1042 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1043 work_exists = 1;
1044
1045 /* check for RX work to do */
1046 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001047 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001048 work_exists = 1;
1049
1050 return work_exists;
1051}
1052
Matt Carlson17375d22009-08-28 14:02:18 +00001053/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001054 * similar to tg3_enable_ints, but it accurately determines whether there
1055 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001056 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Matt Carlson17375d22009-08-28 14:02:18 +00001058static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Matt Carlson17375d22009-08-28 14:02:18 +00001060 struct tg3 *tp = tnapi->tp;
1061
Matt Carlson898a56f2009-08-28 14:02:40 +00001062 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 mmiowb();
1064
David S. Millerfac9b832005-05-18 22:46:34 -07001065 /* When doing tagged status, this work check is unnecessary.
1066 * The last_tag we write above tells the chip which piece of
1067 * work we've completed.
1068 */
Joe Perches63c3a662011-04-26 08:12:10 +00001069 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001070 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001071 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static void tg3_switch_clocks(struct tg3 *tp)
1075{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001076 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 u32 orig_clock_ctrl;
1078
Joe Perches63c3a662011-04-26 08:12:10 +00001079 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001080 return;
1081
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001082 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 orig_clock_ctrl = clock_ctrl;
1085 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1086 CLOCK_CTRL_CLKRUN_OENABLE |
1087 0x1f);
1088 tp->pci_clock_ctrl = clock_ctrl;
1089
Joe Perches63c3a662011-04-26 08:12:10 +00001090 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1097 clock_ctrl |
1098 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1099 40);
1100 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1101 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1102 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001104 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107#define PHY_BUSY_LOOPS 5000
1108
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001109static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1110 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 u32 frame_val;
1113 unsigned int loops;
1114 int ret;
1115
1116 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1117 tw32_f(MAC_MI_MODE,
1118 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1119 udelay(80);
1120 }
1121
Michael Chan8151ad52012-07-29 19:15:41 +00001122 tg3_ape_lock(tp, tp->phy_ape_lock);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 *val = 0x0;
1125
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001126 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 MI_COM_PHY_ADDR_MASK);
1128 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1129 MI_COM_REG_ADDR_MASK);
1130 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 tw32_f(MAC_MI_COM, frame_val);
1133
1134 loops = PHY_BUSY_LOOPS;
1135 while (loops != 0) {
1136 udelay(10);
1137 frame_val = tr32(MAC_MI_COM);
1138
1139 if ((frame_val & MI_COM_BUSY) == 0) {
1140 udelay(5);
1141 frame_val = tr32(MAC_MI_COM);
1142 break;
1143 }
1144 loops -= 1;
1145 }
1146
1147 ret = -EBUSY;
1148 if (loops != 0) {
1149 *val = frame_val & MI_COM_DATA_MASK;
1150 ret = 0;
1151 }
1152
1153 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1154 tw32_f(MAC_MI_MODE, tp->mi_mode);
1155 udelay(80);
1156 }
1157
Michael Chan8151ad52012-07-29 19:15:41 +00001158 tg3_ape_unlock(tp, tp->phy_ape_lock);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return ret;
1161}
1162
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001163static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1164{
1165 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1166}
1167
1168static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1169 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 u32 frame_val;
1172 unsigned int loops;
1173 int ret;
1174
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001175 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001176 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001177 return 0;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1180 tw32_f(MAC_MI_MODE,
1181 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1182 udelay(80);
1183 }
1184
Michael Chan8151ad52012-07-29 19:15:41 +00001185 tg3_ape_lock(tp, tp->phy_ape_lock);
1186
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001187 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 MI_COM_PHY_ADDR_MASK);
1189 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1190 MI_COM_REG_ADDR_MASK);
1191 frame_val |= (val & MI_COM_DATA_MASK);
1192 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 tw32_f(MAC_MI_COM, frame_val);
1195
1196 loops = PHY_BUSY_LOOPS;
1197 while (loops != 0) {
1198 udelay(10);
1199 frame_val = tr32(MAC_MI_COM);
1200 if ((frame_val & MI_COM_BUSY) == 0) {
1201 udelay(5);
1202 frame_val = tr32(MAC_MI_COM);
1203 break;
1204 }
1205 loops -= 1;
1206 }
1207
1208 ret = -EBUSY;
1209 if (loops != 0)
1210 ret = 0;
1211
1212 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1213 tw32_f(MAC_MI_MODE, tp->mi_mode);
1214 udelay(80);
1215 }
1216
Michael Chan8151ad52012-07-29 19:15:41 +00001217 tg3_ape_unlock(tp, tp->phy_ape_lock);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return ret;
1220}
1221
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001222static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1223{
1224 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1225}
1226
Matt Carlsonb0988c12011-04-20 07:57:39 +00001227static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1228{
1229 int err;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1236 if (err)
1237 goto done;
1238
1239 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1240 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1241 if (err)
1242 goto done;
1243
1244 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1245
1246done:
1247 return err;
1248}
1249
1250static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1251{
1252 int err;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1255 if (err)
1256 goto done;
1257
1258 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1259 if (err)
1260 goto done;
1261
1262 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1263 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1264 if (err)
1265 goto done;
1266
1267 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1268
1269done:
1270 return err;
1271}
1272
1273static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1274{
1275 int err;
1276
1277 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1278 if (!err)
1279 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1280
1281 return err;
1282}
1283
1284static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1285{
1286 int err;
1287
1288 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1289 if (!err)
1290 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1291
1292 return err;
1293}
1294
Matt Carlson15ee95c2011-04-20 07:57:40 +00001295static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1296{
1297 int err;
1298
1299 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1300 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1301 MII_TG3_AUXCTL_SHDWSEL_MISC);
1302 if (!err)
1303 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1304
1305 return err;
1306}
1307
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001308static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1309{
1310 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1311 set |= MII_TG3_AUXCTL_MISC_WREN;
1312
1313 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1314}
1315
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001316static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1317{
1318 u32 val;
1319 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001320
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001321 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1322
1323 if (err)
1324 return err;
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001325
Nithin Sujir7c10ee32013-05-23 11:11:26 +00001326 if (enable)
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001327 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1328 else
1329 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1330
1331 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1332 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1333
1334 return err;
1335}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001336
Nithin Sujir3ab71072013-09-20 16:46:55 -07001337static int tg3_phy_shdw_write(struct tg3 *tp, int reg, u32 val)
1338{
1339 return tg3_writephy(tp, MII_TG3_MISC_SHDW,
1340 reg | val | MII_TG3_MISC_SHDW_WREN);
1341}
1342
Matt Carlson95e28692008-05-25 23:44:14 -07001343static int tg3_bmcr_reset(struct tg3 *tp)
1344{
1345 u32 phy_control;
1346 int limit, err;
1347
1348 /* OK, reset it, and poll the BMCR_RESET bit until it
1349 * clears or we time out.
1350 */
1351 phy_control = BMCR_RESET;
1352 err = tg3_writephy(tp, MII_BMCR, phy_control);
1353 if (err != 0)
1354 return -EBUSY;
1355
1356 limit = 5000;
1357 while (limit--) {
1358 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1359 if (err != 0)
1360 return -EBUSY;
1361
1362 if ((phy_control & BMCR_RESET) == 0) {
1363 udelay(40);
1364 break;
1365 }
1366 udelay(10);
1367 }
Roel Kluind4675b52009-02-12 16:33:27 -08001368 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001369 return -EBUSY;
1370
1371 return 0;
1372}
1373
Matt Carlson158d7ab2008-05-29 01:37:54 -07001374static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1375{
Francois Romieu3d165432009-01-19 16:56:50 -08001376 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001377 u32 val;
1378
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001380
Hauke Mehrtensead24022013-09-28 23:15:26 +02001381 if (__tg3_readphy(tp, mii_id, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 val = -EIO;
1383
1384 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001385
1386 return val;
1387}
1388
1389static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1390{
Francois Romieu3d165432009-01-19 16:56:50 -08001391 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001392 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001393
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001394 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001395
Hauke Mehrtensead24022013-09-28 23:15:26 +02001396 if (__tg3_writephy(tp, mii_id, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001397 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001398
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001399 spin_unlock_bh(&tp->lock);
1400
1401 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001402}
1403
1404static int tg3_mdio_reset(struct mii_bus *bp)
1405{
1406 return 0;
1407}
1408
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001409static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001410{
1411 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001413
Hauke Mehrtensead24022013-09-28 23:15:26 +02001414 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001415 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001416 case PHY_ID_BCM50610:
1417 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001418 val = MAC_PHYCFG2_50610_LED_MODES;
1419 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001420 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001421 val = MAC_PHYCFG2_AC131_LED_MODES;
1422 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001423 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001424 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1425 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001426 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001427 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1428 break;
1429 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001430 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001431 }
1432
1433 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1434 tw32(MAC_PHYCFG2, val);
1435
1436 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001437 val &= ~(MAC_PHYCFG1_RGMII_INT |
1438 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1439 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001440 tw32(MAC_PHYCFG1, val);
1441
1442 return;
1443 }
1444
Joe Perches63c3a662011-04-26 08:12:10 +00001445 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001446 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1447 MAC_PHYCFG2_FMODE_MASK_MASK |
1448 MAC_PHYCFG2_GMODE_MASK_MASK |
1449 MAC_PHYCFG2_ACT_MASK_MASK |
1450 MAC_PHYCFG2_QUAL_MASK_MASK |
1451 MAC_PHYCFG2_INBAND_ENABLE;
1452
1453 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001454
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001455 val = tr32(MAC_PHYCFG1);
1456 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1457 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001458 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1459 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001460 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001462 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1463 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001464 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1465 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1466 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001467
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 val = tr32(MAC_EXT_RGMII_MODE);
1469 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1470 MAC_RGMII_MODE_RX_QUALITY |
1471 MAC_RGMII_MODE_RX_ACTIVITY |
1472 MAC_RGMII_MODE_RX_ENG_DET |
1473 MAC_RGMII_MODE_TX_ENABLE |
1474 MAC_RGMII_MODE_TX_LOWPWR |
1475 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001476 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1477 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001478 val |= MAC_RGMII_MODE_RX_INT_B |
1479 MAC_RGMII_MODE_RX_QUALITY |
1480 MAC_RGMII_MODE_RX_ACTIVITY |
1481 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001483 val |= MAC_RGMII_MODE_TX_ENABLE |
1484 MAC_RGMII_MODE_TX_LOWPWR |
1485 MAC_RGMII_MODE_TX_RESET;
1486 }
1487 tw32(MAC_EXT_RGMII_MODE, val);
1488}
1489
Matt Carlson158d7ab2008-05-29 01:37:54 -07001490static void tg3_mdio_start(struct tg3 *tp)
1491{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001492 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1493 tw32_f(MAC_MI_MODE, tp->mi_mode);
1494 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001495
Joe Perches63c3a662011-04-26 08:12:10 +00001496 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001497 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001498 tg3_mdio_config_5785(tp);
1499}
1500
1501static int tg3_mdio_init(struct tg3 *tp)
1502{
1503 int i;
1504 u32 reg;
1505 struct phy_device *phydev;
1506
Joe Perches63c3a662011-04-26 08:12:10 +00001507 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001508 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001509
Matt Carlson69f11c92011-07-13 09:27:30 +00001510 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001511
Joe Perches41535772013-02-16 11:20:04 +00001512 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001513 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1514 else
1515 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1516 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001517 if (is_serdes)
1518 tp->phy_addr += 7;
Hauke Mehrtensee002b62013-09-28 23:15:28 +02001519 } else if (tg3_flag(tp, IS_SSB_CORE) && tg3_flag(tp, ROBOSWITCH)) {
1520 int addr;
1521
1522 addr = ssb_gige_get_phyaddr(tp->pdev);
1523 if (addr < 0)
1524 return addr;
1525 tp->phy_addr = addr;
Matt Carlson882e9792009-09-01 13:21:36 +00001526 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001527 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001528
Matt Carlson158d7ab2008-05-29 01:37:54 -07001529 tg3_mdio_start(tp);
1530
Joe Perches63c3a662011-04-26 08:12:10 +00001531 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001532 return 0;
1533
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001534 tp->mdio_bus = mdiobus_alloc();
1535 if (tp->mdio_bus == NULL)
1536 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001537
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001538 tp->mdio_bus->name = "tg3 mdio bus";
1539 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001540 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001541 tp->mdio_bus->priv = tp;
1542 tp->mdio_bus->parent = &tp->pdev->dev;
1543 tp->mdio_bus->read = &tg3_mdio_read;
1544 tp->mdio_bus->write = &tg3_mdio_write;
1545 tp->mdio_bus->reset = &tg3_mdio_reset;
Hauke Mehrtensead24022013-09-28 23:15:26 +02001546 tp->mdio_bus->phy_mask = ~(1 << tp->phy_addr);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001547 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001548
1549 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001550 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001551
1552 /* The bus registration will look for all the PHYs on the mdio bus.
1553 * Unfortunately, it does not ensure the PHY is powered up before
1554 * accessing the PHY ID registers. A chip reset is the
1555 * quickest way to bring the device back to an operational state..
1556 */
1557 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1558 tg3_bmcr_reset(tp);
1559
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001560 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001561 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001562 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001563 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001564 return i;
1565 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001566
Hauke Mehrtensead24022013-09-28 23:15:26 +02001567 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsona9daf362008-05-25 23:49:44 -07001568
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001569 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001570 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001571 mdiobus_unregister(tp->mdio_bus);
1572 mdiobus_free(tp->mdio_bus);
1573 return -ENODEV;
1574 }
1575
1576 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001577 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001578 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001579 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001580 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001581 case PHY_ID_BCM50610:
1582 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001583 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001584 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001585 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001586 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001587 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001588 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001589 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001590 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001591 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001592 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001593 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001594 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001595 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001596 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001597 case PHY_ID_RTL8201E:
1598 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001599 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001600 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001601 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001602 break;
1603 }
1604
Joe Perches63c3a662011-04-26 08:12:10 +00001605 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001606
Joe Perches41535772013-02-16 11:20:04 +00001607 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001608 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001609
1610 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001611}
1612
1613static void tg3_mdio_fini(struct tg3 *tp)
1614{
Joe Perches63c3a662011-04-26 08:12:10 +00001615 if (tg3_flag(tp, MDIOBUS_INITED)) {
1616 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001617 mdiobus_unregister(tp->mdio_bus);
1618 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001619 }
1620}
1621
Matt Carlson95e28692008-05-25 23:44:14 -07001622/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001623static inline void tg3_generate_fw_event(struct tg3 *tp)
1624{
1625 u32 val;
1626
1627 val = tr32(GRC_RX_CPU_EVENT);
1628 val |= GRC_RX_CPU_DRIVER_EVENT;
1629 tw32_f(GRC_RX_CPU_EVENT, val);
1630
1631 tp->last_event_jiffies = jiffies;
1632}
1633
1634#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1635
1636/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001637static void tg3_wait_for_event_ack(struct tg3 *tp)
1638{
1639 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001640 unsigned int delay_cnt;
1641 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001642
Matt Carlson4ba526c2008-08-15 14:10:04 -07001643 /* If enough time has passed, no wait is necessary. */
1644 time_remain = (long)(tp->last_event_jiffies + 1 +
1645 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1646 (long)jiffies;
1647 if (time_remain < 0)
1648 return;
1649
1650 /* Check if we can shorten the wait time. */
1651 delay_cnt = jiffies_to_usecs(time_remain);
1652 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1653 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1654 delay_cnt = (delay_cnt >> 3) + 1;
1655
1656 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001657 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1658 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001659 if (pci_channel_offline(tp->pdev))
1660 break;
1661
Matt Carlson4ba526c2008-08-15 14:10:04 -07001662 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001663 }
1664}
1665
1666/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001667static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001668{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001669 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001670
1671 val = 0;
1672 if (!tg3_readphy(tp, MII_BMCR, &reg))
1673 val = reg << 16;
1674 if (!tg3_readphy(tp, MII_BMSR, &reg))
1675 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001676 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001677
1678 val = 0;
1679 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1680 val = reg << 16;
1681 if (!tg3_readphy(tp, MII_LPA, &reg))
1682 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001683 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001684
1685 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001686 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001687 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1688 val = reg << 16;
1689 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1690 val |= (reg & 0xffff);
1691 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001692 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001693
1694 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1695 val = reg << 16;
1696 else
1697 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001698 *data++ = val;
1699}
1700
1701/* tp->lock is held. */
1702static void tg3_ump_link_report(struct tg3 *tp)
1703{
1704 u32 data[4];
1705
1706 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1707 return;
1708
1709 tg3_phy_gather_ump_data(tp, data);
1710
1711 tg3_wait_for_event_ack(tp);
1712
1713 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1714 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1715 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1716 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1717 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1718 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001719
Matt Carlson4ba526c2008-08-15 14:10:04 -07001720 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001721}
1722
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001723/* tp->lock is held. */
1724static void tg3_stop_fw(struct tg3 *tp)
1725{
1726 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1727 /* Wait for RX cpu to ACK the previous event. */
1728 tg3_wait_for_event_ack(tp);
1729
1730 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1731
1732 tg3_generate_fw_event(tp);
1733
1734 /* Wait for RX cpu to ACK this event. */
1735 tg3_wait_for_event_ack(tp);
1736 }
1737}
1738
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001739/* tp->lock is held. */
1740static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1741{
1742 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1743 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1744
1745 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1746 switch (kind) {
1747 case RESET_KIND_INIT:
1748 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1749 DRV_STATE_START);
1750 break;
1751
1752 case RESET_KIND_SHUTDOWN:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_UNLOAD);
1755 break;
1756
1757 case RESET_KIND_SUSPEND:
1758 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1759 DRV_STATE_SUSPEND);
1760 break;
1761
1762 default:
1763 break;
1764 }
1765 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001766}
1767
1768/* tp->lock is held. */
1769static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1770{
1771 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1772 switch (kind) {
1773 case RESET_KIND_INIT:
1774 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1775 DRV_STATE_START_DONE);
1776 break;
1777
1778 case RESET_KIND_SHUTDOWN:
1779 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1780 DRV_STATE_UNLOAD_DONE);
1781 break;
1782
1783 default:
1784 break;
1785 }
1786 }
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001787}
1788
1789/* tp->lock is held. */
1790static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1791{
1792 if (tg3_flag(tp, ENABLE_ASF)) {
1793 switch (kind) {
1794 case RESET_KIND_INIT:
1795 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1796 DRV_STATE_START);
1797 break;
1798
1799 case RESET_KIND_SHUTDOWN:
1800 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1801 DRV_STATE_UNLOAD);
1802 break;
1803
1804 case RESET_KIND_SUSPEND:
1805 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1806 DRV_STATE_SUSPEND);
1807 break;
1808
1809 default:
1810 break;
1811 }
1812 }
1813}
1814
1815static int tg3_poll_fw(struct tg3 *tp)
1816{
1817 int i;
1818 u32 val;
1819
Nithin Sujirdf465ab2013-06-12 11:08:59 -07001820 if (tg3_flag(tp, NO_FWARE_REPORTED))
1821 return 0;
1822
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001823 if (tg3_flag(tp, IS_SSB_CORE)) {
1824 /* We don't use firmware. */
1825 return 0;
1826 }
1827
Joe Perches41535772013-02-16 11:20:04 +00001828 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001829 /* Wait up to 20ms for init done. */
1830 for (i = 0; i < 200; i++) {
1831 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1832 return 0;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001833 if (pci_channel_offline(tp->pdev))
1834 return -ENODEV;
1835
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001836 udelay(100);
1837 }
1838 return -ENODEV;
1839 }
1840
1841 /* Wait for firmware initialization to complete. */
1842 for (i = 0; i < 100000; i++) {
1843 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1844 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1845 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08001846 if (pci_channel_offline(tp->pdev)) {
1847 if (!tg3_flag(tp, NO_FWARE_REPORTED)) {
1848 tg3_flag_set(tp, NO_FWARE_REPORTED);
1849 netdev_info(tp->dev, "No firmware running\n");
1850 }
1851
1852 break;
1853 }
1854
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001855 udelay(10);
1856 }
1857
1858 /* Chip might not be fitted with firmware. Some Sun onboard
1859 * parts are configured like that. So don't signal the timeout
1860 * of the above loop as an error, but do report the lack of
1861 * running firmware once.
1862 */
1863 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1864 tg3_flag_set(tp, NO_FWARE_REPORTED);
1865
1866 netdev_info(tp->dev, "No firmware running\n");
1867 }
1868
Joe Perches41535772013-02-16 11:20:04 +00001869 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001870 /* The 57765 A0 needs a little more
1871 * time to do some important work.
1872 */
1873 mdelay(10);
1874 }
1875
1876 return 0;
1877}
1878
Matt Carlson95e28692008-05-25 23:44:14 -07001879static void tg3_link_report(struct tg3 *tp)
1880{
1881 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001882 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001883 tg3_ump_link_report(tp);
1884 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001885 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1886 (tp->link_config.active_speed == SPEED_1000 ?
1887 1000 :
1888 (tp->link_config.active_speed == SPEED_100 ?
1889 100 : 10)),
1890 (tp->link_config.active_duplex == DUPLEX_FULL ?
1891 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001892
Joe Perches05dbe002010-02-17 19:44:19 +00001893 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1894 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1895 "on" : "off",
1896 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1897 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001898
1899 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1900 netdev_info(tp->dev, "EEE is %s\n",
1901 tp->setlpicnt ? "enabled" : "disabled");
1902
Matt Carlson95e28692008-05-25 23:44:14 -07001903 tg3_ump_link_report(tp);
1904 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001905
1906 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001907}
1908
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001909static u32 tg3_decode_flowctrl_1000T(u32 adv)
1910{
1911 u32 flowctrl = 0;
1912
1913 if (adv & ADVERTISE_PAUSE_CAP) {
1914 flowctrl |= FLOW_CTRL_RX;
1915 if (!(adv & ADVERTISE_PAUSE_ASYM))
1916 flowctrl |= FLOW_CTRL_TX;
1917 } else if (adv & ADVERTISE_PAUSE_ASYM)
1918 flowctrl |= FLOW_CTRL_TX;
1919
1920 return flowctrl;
1921}
1922
Matt Carlson95e28692008-05-25 23:44:14 -07001923static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1924{
1925 u16 miireg;
1926
Steve Glendinninge18ce342008-12-16 02:00:00 -08001927 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001928 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001929 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001930 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001931 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001932 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1933 else
1934 miireg = 0;
1935
1936 return miireg;
1937}
1938
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001939static u32 tg3_decode_flowctrl_1000X(u32 adv)
1940{
1941 u32 flowctrl = 0;
1942
1943 if (adv & ADVERTISE_1000XPAUSE) {
1944 flowctrl |= FLOW_CTRL_RX;
1945 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1946 flowctrl |= FLOW_CTRL_TX;
1947 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1948 flowctrl |= FLOW_CTRL_TX;
1949
1950 return flowctrl;
1951}
1952
Matt Carlson95e28692008-05-25 23:44:14 -07001953static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1954{
1955 u8 cap = 0;
1956
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001957 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1958 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1959 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1960 if (lcladv & ADVERTISE_1000XPAUSE)
1961 cap = FLOW_CTRL_RX;
1962 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001963 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001964 }
1965
1966 return cap;
1967}
1968
Matt Carlsonf51f3562008-05-25 23:45:08 -07001969static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001970{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001971 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001972 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001973 u32 old_rx_mode = tp->rx_mode;
1974 u32 old_tx_mode = tp->tx_mode;
1975
Joe Perches63c3a662011-04-26 08:12:10 +00001976 if (tg3_flag(tp, USE_PHYLIB))
Hauke Mehrtensead24022013-09-28 23:15:26 +02001977 autoneg = tp->mdio_bus->phy_map[tp->phy_addr]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001978 else
1979 autoneg = tp->link_config.autoneg;
1980
Joe Perches63c3a662011-04-26 08:12:10 +00001981 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001982 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001983 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001984 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001985 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001986 } else
1987 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001988
Matt Carlsonf51f3562008-05-25 23:45:08 -07001989 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001990
Steve Glendinninge18ce342008-12-16 02:00:00 -08001991 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001992 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1993 else
1994 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1995
Matt Carlsonf51f3562008-05-25 23:45:08 -07001996 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001997 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001998
Steve Glendinninge18ce342008-12-16 02:00:00 -08001999 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07002000 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
2001 else
2002 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
2003
Matt Carlsonf51f3562008-05-25 23:45:08 -07002004 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07002005 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07002006}
2007
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002008static void tg3_adjust_link(struct net_device *dev)
2009{
2010 u8 oldflowctrl, linkmesg = 0;
2011 u32 mac_mode, lcl_adv, rmt_adv;
2012 struct tg3 *tp = netdev_priv(dev);
Hauke Mehrtensead24022013-09-28 23:15:26 +02002013 struct phy_device *phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002014
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002015 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002016
2017 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
2018 MAC_MODE_HALF_DUPLEX);
2019
2020 oldflowctrl = tp->link_config.active_flowctrl;
2021
2022 if (phydev->link) {
2023 lcl_adv = 0;
2024 rmt_adv = 0;
2025
2026 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
2027 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002028 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00002029 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002030 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00002031 else
2032 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033
2034 if (phydev->duplex == DUPLEX_HALF)
2035 mac_mode |= MAC_MODE_HALF_DUPLEX;
2036 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002037 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002038 tp->link_config.flowctrl);
2039
2040 if (phydev->pause)
2041 rmt_adv = LPA_PAUSE_CAP;
2042 if (phydev->asym_pause)
2043 rmt_adv |= LPA_PAUSE_ASYM;
2044 }
2045
2046 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2047 } else
2048 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2049
2050 if (mac_mode != tp->mac_mode) {
2051 tp->mac_mode = mac_mode;
2052 tw32_f(MAC_MODE, tp->mac_mode);
2053 udelay(40);
2054 }
2055
Joe Perches41535772013-02-16 11:20:04 +00002056 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002057 if (phydev->speed == SPEED_10)
2058 tw32(MAC_MI_STAT,
2059 MAC_MI_STAT_10MBPS_MODE |
2060 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2061 else
2062 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2063 }
2064
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002065 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2066 tw32(MAC_TX_LENGTHS,
2067 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2068 (6 << TX_LENGTHS_IPG_SHIFT) |
2069 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2070 else
2071 tw32(MAC_TX_LENGTHS,
2072 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2073 (6 << TX_LENGTHS_IPG_SHIFT) |
2074 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2075
Matt Carlson34655ad2012-02-22 12:35:18 +00002076 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 phydev->speed != tp->link_config.active_speed ||
2078 phydev->duplex != tp->link_config.active_duplex ||
2079 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002080 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002081
Matt Carlson34655ad2012-02-22 12:35:18 +00002082 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002083 tp->link_config.active_speed = phydev->speed;
2084 tp->link_config.active_duplex = phydev->duplex;
2085
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002086 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002087
2088 if (linkmesg)
2089 tg3_link_report(tp);
2090}
2091
2092static int tg3_phy_init(struct tg3 *tp)
2093{
2094 struct phy_device *phydev;
2095
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002096 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002097 return 0;
2098
2099 /* Bring the PHY back to a known state. */
2100 tg3_bmcr_reset(tp);
2101
Hauke Mehrtensead24022013-09-28 23:15:26 +02002102 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002103
2104 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002105 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2106 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002107 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002108 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002109 return PTR_ERR(phydev);
2110 }
2111
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002112 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002113 switch (phydev->interface) {
2114 case PHY_INTERFACE_MODE_GMII:
2115 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002116 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002117 phydev->supported &= (PHY_GBIT_FEATURES |
2118 SUPPORTED_Pause |
2119 SUPPORTED_Asym_Pause);
2120 break;
2121 }
2122 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002123 case PHY_INTERFACE_MODE_MII:
2124 phydev->supported &= (PHY_BASIC_FEATURES |
2125 SUPPORTED_Pause |
2126 SUPPORTED_Asym_Pause);
2127 break;
2128 default:
Hauke Mehrtensead24022013-09-28 23:15:26 +02002129 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002130 return -EINVAL;
2131 }
2132
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002133 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002134
2135 phydev->advertising = phydev->supported;
2136
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002137 return 0;
2138}
2139
2140static void tg3_phy_start(struct tg3 *tp)
2141{
2142 struct phy_device *phydev;
2143
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002144 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002145 return;
2146
Hauke Mehrtensead24022013-09-28 23:15:26 +02002147 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002148
Matt Carlson800960682010-08-02 11:26:06 +00002149 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2150 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002151 phydev->speed = tp->link_config.speed;
2152 phydev->duplex = tp->link_config.duplex;
2153 phydev->autoneg = tp->link_config.autoneg;
2154 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002155 }
2156
2157 phy_start(phydev);
2158
2159 phy_start_aneg(phydev);
2160}
2161
2162static void tg3_phy_stop(struct tg3 *tp)
2163{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002164 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002165 return;
2166
Hauke Mehrtensead24022013-09-28 23:15:26 +02002167 phy_stop(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002168}
2169
2170static void tg3_phy_fini(struct tg3 *tp)
2171{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002172 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Hauke Mehrtensead24022013-09-28 23:15:26 +02002173 phy_disconnect(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002174 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002175 }
2176}
2177
Matt Carlson941ec902011-08-19 13:58:23 +00002178static int tg3_phy_set_extloopbk(struct tg3 *tp)
2179{
2180 int err;
2181 u32 val;
2182
2183 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2184 return 0;
2185
2186 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2187 /* Cannot do read-modify-write on 5401 */
2188 err = tg3_phy_auxctl_write(tp,
2189 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2190 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2191 0x4c20);
2192 goto done;
2193 }
2194
2195 err = tg3_phy_auxctl_read(tp,
2196 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2197 if (err)
2198 return err;
2199
2200 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2201 err = tg3_phy_auxctl_write(tp,
2202 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2203
2204done:
2205 return err;
2206}
2207
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002208static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2209{
2210 u32 phytest;
2211
2212 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2213 u32 phy;
2214
2215 tg3_writephy(tp, MII_TG3_FET_TEST,
2216 phytest | MII_TG3_FET_SHADOW_EN);
2217 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2218 if (enable)
2219 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2220 else
2221 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2222 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2223 }
2224 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2225 }
2226}
2227
Matt Carlson6833c042008-11-21 17:18:59 -08002228static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2229{
2230 u32 reg;
2231
Joe Perches63c3a662011-04-26 08:12:10 +00002232 if (!tg3_flag(tp, 5705_PLUS) ||
2233 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002234 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002235 return;
2236
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002237 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002238 tg3_phy_fet_toggle_apd(tp, enable);
2239 return;
2240 }
2241
Nithin Sujir3ab71072013-09-20 16:46:55 -07002242 reg = MII_TG3_MISC_SHDW_SCR5_LPED |
Matt Carlson6833c042008-11-21 17:18:59 -08002243 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2244 MII_TG3_MISC_SHDW_SCR5_SDTL |
2245 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002246 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002247 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2248
Nithin Sujir3ab71072013-09-20 16:46:55 -07002249 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_SCR5_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002250
2251
Nithin Sujir3ab71072013-09-20 16:46:55 -07002252 reg = MII_TG3_MISC_SHDW_APD_WKTM_84MS;
Matt Carlson6833c042008-11-21 17:18:59 -08002253 if (enable)
2254 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2255
Nithin Sujir3ab71072013-09-20 16:46:55 -07002256 tg3_phy_shdw_write(tp, MII_TG3_MISC_SHDW_APD_SEL, reg);
Matt Carlson6833c042008-11-21 17:18:59 -08002257}
2258
Joe Perches953c96e2013-04-09 10:18:14 +00002259static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002260{
2261 u32 phy;
2262
Joe Perches63c3a662011-04-26 08:12:10 +00002263 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002264 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002265 return;
2266
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002267 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 u32 ephy;
2269
Matt Carlson535ef6e2009-08-25 10:09:36 +00002270 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2271 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2272
2273 tg3_writephy(tp, MII_TG3_FET_TEST,
2274 ephy | MII_TG3_FET_SHADOW_EN);
2275 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002276 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002277 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002278 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002279 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2280 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002281 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002282 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002283 }
2284 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002285 int ret;
2286
2287 ret = tg3_phy_auxctl_read(tp,
2288 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2289 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002290 if (enable)
2291 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2292 else
2293 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002294 tg3_phy_auxctl_write(tp,
2295 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002296 }
2297 }
2298}
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300static void tg3_phy_set_wirespeed(struct tg3 *tp)
2301{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002302 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 u32 val;
2304
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002305 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return;
2307
Matt Carlson15ee95c2011-04-20 07:57:40 +00002308 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2309 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002310 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2311 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002314static void tg3_phy_apply_otp(struct tg3 *tp)
2315{
2316 u32 otp, phy;
2317
2318 if (!tp->phy_otp)
2319 return;
2320
2321 otp = tp->phy_otp;
2322
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002323 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002324 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002325
2326 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2327 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2328 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2329
2330 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2331 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2332 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2333
2334 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2335 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2336 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2337
2338 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2339 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2340
2341 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2342 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2343
2344 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2345 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2346 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2347
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002348 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002349}
2350
Nithin Sujir400dfba2013-05-18 06:26:53 +00002351static void tg3_eee_pull_config(struct tg3 *tp, struct ethtool_eee *eee)
2352{
2353 u32 val;
2354 struct ethtool_eee *dest = &tp->eee;
2355
2356 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2357 return;
2358
2359 if (eee)
2360 dest = eee;
2361
2362 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, TG3_CL45_D7_EEERES_STAT, &val))
2363 return;
2364
2365 /* Pull eee_active */
2366 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2367 val == TG3_CL45_D7_EEERES_STAT_LP_100TX) {
2368 dest->eee_active = 1;
2369 } else
2370 dest->eee_active = 0;
2371
2372 /* Pull lp advertised settings */
2373 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE, &val))
2374 return;
2375 dest->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2376
2377 /* Pull advertised and eee_enabled settings */
2378 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
2379 return;
2380 dest->eee_enabled = !!val;
2381 dest->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
2382
2383 /* Pull tx_lpi_enabled */
2384 val = tr32(TG3_CPMU_EEE_MODE);
2385 dest->tx_lpi_enabled = !!(val & TG3_CPMU_EEEMD_LPI_IN_TX);
2386
2387 /* Pull lpi timer value */
2388 dest->tx_lpi_timer = tr32(TG3_CPMU_EEE_DBTMR1) & 0xffff;
2389}
2390
Joe Perches953c96e2013-04-09 10:18:14 +00002391static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002392{
2393 u32 val;
2394
2395 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2396 return;
2397
2398 tp->setlpicnt = 0;
2399
2400 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002401 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002402 tp->link_config.active_duplex == DUPLEX_FULL &&
2403 (tp->link_config.active_speed == SPEED_100 ||
2404 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002405 u32 eeectl;
2406
2407 if (tp->link_config.active_speed == SPEED_1000)
2408 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2409 else
2410 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2411
2412 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2413
Nithin Sujir400dfba2013-05-18 06:26:53 +00002414 tg3_eee_pull_config(tp, NULL);
2415 if (tp->eee.eee_active)
Matt Carlson52b02d02010-10-14 10:37:41 +00002416 tp->setlpicnt = 2;
2417 }
2418
2419 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002420 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002421 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002422 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002423 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002424 }
2425
Matt Carlson52b02d02010-10-14 10:37:41 +00002426 val = tr32(TG3_CPMU_EEE_MODE);
2427 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2428 }
2429}
2430
Matt Carlsonb0c59432011-05-19 12:12:48 +00002431static void tg3_phy_eee_enable(struct tg3 *tp)
2432{
2433 u32 val;
2434
2435 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002436 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2437 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002438 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002439 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002440 val = MII_TG3_DSP_TAP26_ALNOKO |
2441 MII_TG3_DSP_TAP26_RMRXSTO;
2442 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002443 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002444 }
2445
2446 val = tr32(TG3_CPMU_EEE_MODE);
2447 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static int tg3_wait_macro_done(struct tg3 *tp)
2451{
2452 int limit = 100;
2453
2454 while (limit--) {
2455 u32 tmp32;
2456
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002457 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if ((tmp32 & 0x1000) == 0)
2459 break;
2460 }
2461 }
Roel Kluind4675b52009-02-12 16:33:27 -08002462 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return -EBUSY;
2464
2465 return 0;
2466}
2467
2468static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2469{
2470 static const u32 test_pat[4][6] = {
2471 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2472 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2473 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2474 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2475 };
2476 int chan;
2477
2478 for (chan = 0; chan < 4; chan++) {
2479 int i;
2480
2481 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2482 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002483 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 for (i = 0; i < 6; i++)
2486 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2487 test_pat[chan][i]);
2488
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002489 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (tg3_wait_macro_done(tp)) {
2491 *resetp = 1;
2492 return -EBUSY;
2493 }
2494
2495 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2496 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002497 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (tg3_wait_macro_done(tp)) {
2499 *resetp = 1;
2500 return -EBUSY;
2501 }
2502
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002503 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (tg3_wait_macro_done(tp)) {
2505 *resetp = 1;
2506 return -EBUSY;
2507 }
2508
2509 for (i = 0; i < 6; i += 2) {
2510 u32 low, high;
2511
2512 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2513 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2514 tg3_wait_macro_done(tp)) {
2515 *resetp = 1;
2516 return -EBUSY;
2517 }
2518 low &= 0x7fff;
2519 high &= 0x000f;
2520 if (low != test_pat[chan][i] ||
2521 high != test_pat[chan][i+1]) {
2522 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2523 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2524 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2525
2526 return -EBUSY;
2527 }
2528 }
2529 }
2530
2531 return 0;
2532}
2533
2534static int tg3_phy_reset_chanpat(struct tg3 *tp)
2535{
2536 int chan;
2537
2538 for (chan = 0; chan < 4; chan++) {
2539 int i;
2540
2541 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2542 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002543 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 for (i = 0; i < 6; i++)
2545 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002546 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (tg3_wait_macro_done(tp))
2548 return -EBUSY;
2549 }
2550
2551 return 0;
2552}
2553
2554static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2555{
2556 u32 reg32, phy9_orig;
2557 int retries, do_phy_reset, err;
2558
2559 retries = 10;
2560 do_phy_reset = 1;
2561 do {
2562 if (do_phy_reset) {
2563 err = tg3_bmcr_reset(tp);
2564 if (err)
2565 return err;
2566 do_phy_reset = 0;
2567 }
2568
2569 /* Disable transmitter and interrupt. */
2570 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2571 continue;
2572
2573 reg32 |= 0x3000;
2574 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2575
2576 /* Set full-duplex, 1000 mbps. */
2577 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002578 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002581 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 continue;
2583
Matt Carlson221c5632011-06-13 13:39:01 +00002584 tg3_writephy(tp, MII_CTRL1000,
2585 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002587 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002588 if (err)
2589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002592 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2595 if (!err)
2596 break;
2597 } while (--retries);
2598
2599 err = tg3_phy_reset_chanpat(tp);
2600 if (err)
2601 return err;
2602
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002603 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002606 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002608 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Matt Carlson221c5632011-06-13 13:39:01 +00002610 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002612 err = tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
2613 if (err)
2614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Dan Carpenterc6e27f22014-02-05 16:29:21 +03002616 reg32 &= ~0x3000;
2617 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2618
2619 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002622static void tg3_carrier_off(struct tg3 *tp)
2623{
2624 netif_carrier_off(tp->dev);
2625 tp->link_up = false;
2626}
2627
Nithin Sujirce20f162013-04-09 08:48:04 +00002628static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2629{
2630 if (tg3_flag(tp, ENABLE_ASF))
2631 netdev_warn(tp->dev,
2632 "Management side-band traffic will be interrupted during phy settings change\n");
2633}
2634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635/* This will reset the tigon3 PHY if there is no valid
2636 * link unless the FORCE argument is non-zero.
2637 */
2638static int tg3_phy_reset(struct tg3 *tp)
2639{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002640 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 int err;
2642
Joe Perches41535772013-02-16 11:20:04 +00002643 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002644 val = tr32(GRC_MISC_CFG);
2645 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2646 udelay(40);
2647 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002648 err = tg3_readphy(tp, MII_BMSR, &val);
2649 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (err != 0)
2651 return -EBUSY;
2652
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002653 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002654 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002655 tg3_link_report(tp);
2656 }
2657
Joe Perches41535772013-02-16 11:20:04 +00002658 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2659 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2660 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 err = tg3_phy_reset_5703_4_5(tp);
2662 if (err)
2663 return err;
2664 goto out;
2665 }
2666
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002667 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002668 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2669 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002670 cpmuctrl = tr32(TG3_CPMU_CTRL);
2671 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2672 tw32(TG3_CPMU_CTRL,
2673 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2674 }
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 err = tg3_bmcr_reset(tp);
2677 if (err)
2678 return err;
2679
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002680 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002681 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2682 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002683
2684 tw32(TG3_CPMU_CTRL, cpmuctrl);
2685 }
2686
Joe Perches41535772013-02-16 11:20:04 +00002687 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2688 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002689 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2690 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2691 CPMU_LSPD_1000MB_MACCLK_12_5) {
2692 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2693 udelay(40);
2694 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2695 }
2696 }
2697
Joe Perches63c3a662011-04-26 08:12:10 +00002698 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002699 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002700 return 0;
2701
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002702 tg3_phy_apply_otp(tp);
2703
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002704 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002705 tg3_phy_toggle_apd(tp, true);
2706 else
2707 tg3_phy_toggle_apd(tp, false);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002710 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002711 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002712 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2713 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002714 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002717 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002718 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2719 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002721
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002722 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002723 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002724 tg3_phydsp_write(tp, 0x000a, 0x310b);
2725 tg3_phydsp_write(tp, 0x201f, 0x9506);
2726 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002727 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002728 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002729 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_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_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2732 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2733 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2734 tg3_writephy(tp, MII_TG3_TEST1,
2735 MII_TG3_TEST1_TRIM_EN | 0x4);
2736 } else
2737 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2738
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002739 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002740 }
Michael Chanc424cb22006-04-29 18:56:34 -07002741 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* Set Extended packet length bit (bit 14) on all chips that */
2744 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002745 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002747 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002748 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002750 err = tg3_phy_auxctl_read(tp,
2751 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2752 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002753 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2754 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
2756
2757 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2758 * jumbo frames transmission.
2759 */
Joe Perches63c3a662011-04-26 08:12:10 +00002760 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002761 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002762 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002763 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765
Joe Perches41535772013-02-16 11:20:04 +00002766 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002767 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002768 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002769 }
2770
Joe Perches41535772013-02-16 11:20:04 +00002771 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002772 tg3_phydsp_write(tp, 0xffb, 0x4000);
2773
Joe Perches953c96e2013-04-09 10:18:14 +00002774 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 tg3_phy_set_wirespeed(tp);
2776 return 0;
2777}
2778
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002779#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2780#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2781#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2782 TG3_GPIO_MSG_NEED_VAUX)
2783#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2784 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2785 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2786 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2787 (TG3_GPIO_MSG_DRVR_PRES << 12))
2788
2789#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2790 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2791 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2792 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2793 (TG3_GPIO_MSG_NEED_VAUX << 12))
2794
2795static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2796{
2797 u32 status, shift;
2798
Joe Perches41535772013-02-16 11:20:04 +00002799 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2800 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002801 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2802 else
2803 status = tr32(TG3_CPMU_DRV_STATUS);
2804
2805 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2806 status &= ~(TG3_GPIO_MSG_MASK << shift);
2807 status |= (newstat << shift);
2808
Joe Perches41535772013-02-16 11:20:04 +00002809 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2810 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002811 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2812 else
2813 tw32(TG3_CPMU_DRV_STATUS, status);
2814
2815 return status >> TG3_APE_GPIO_MSG_SHIFT;
2816}
2817
Matt Carlson520b2752011-06-13 13:39:02 +00002818static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2819{
2820 if (!tg3_flag(tp, IS_NIC))
2821 return 0;
2822
Joe Perches41535772013-02-16 11:20:04 +00002823 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2824 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2825 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002826 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2827 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002828
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002829 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2830
2831 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2832 TG3_GRC_LCLCTL_PWRSW_DELAY);
2833
2834 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2835 } else {
2836 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2837 TG3_GRC_LCLCTL_PWRSW_DELAY);
2838 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002839
Matt Carlson520b2752011-06-13 13:39:02 +00002840 return 0;
2841}
2842
2843static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2844{
2845 u32 grc_local_ctrl;
2846
2847 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002848 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2849 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002850 return;
2851
2852 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2853
2854 tw32_wait_f(GRC_LOCAL_CTRL,
2855 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2856 TG3_GRC_LCLCTL_PWRSW_DELAY);
2857
2858 tw32_wait_f(GRC_LOCAL_CTRL,
2859 grc_local_ctrl,
2860 TG3_GRC_LCLCTL_PWRSW_DELAY);
2861
2862 tw32_wait_f(GRC_LOCAL_CTRL,
2863 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2864 TG3_GRC_LCLCTL_PWRSW_DELAY);
2865}
2866
2867static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2868{
2869 if (!tg3_flag(tp, IS_NIC))
2870 return;
2871
Joe Perches41535772013-02-16 11:20:04 +00002872 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2873 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002874 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2875 (GRC_LCLCTRL_GPIO_OE0 |
2876 GRC_LCLCTRL_GPIO_OE1 |
2877 GRC_LCLCTRL_GPIO_OE2 |
2878 GRC_LCLCTRL_GPIO_OUTPUT0 |
2879 GRC_LCLCTRL_GPIO_OUTPUT1),
2880 TG3_GRC_LCLCTL_PWRSW_DELAY);
2881 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2882 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2883 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2884 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2885 GRC_LCLCTRL_GPIO_OE1 |
2886 GRC_LCLCTRL_GPIO_OE2 |
2887 GRC_LCLCTRL_GPIO_OUTPUT0 |
2888 GRC_LCLCTRL_GPIO_OUTPUT1 |
2889 tp->grc_local_ctrl;
2890 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2891 TG3_GRC_LCLCTL_PWRSW_DELAY);
2892
2893 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2894 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2895 TG3_GRC_LCLCTL_PWRSW_DELAY);
2896
2897 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2898 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2899 TG3_GRC_LCLCTL_PWRSW_DELAY);
2900 } else {
2901 u32 no_gpio2;
2902 u32 grc_local_ctrl = 0;
2903
2904 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002905 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002906 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2907 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2908 grc_local_ctrl,
2909 TG3_GRC_LCLCTL_PWRSW_DELAY);
2910 }
2911
2912 /* On 5753 and variants, GPIO2 cannot be used. */
2913 no_gpio2 = tp->nic_sram_data_cfg &
2914 NIC_SRAM_DATA_CFG_NO_GPIO2;
2915
2916 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2917 GRC_LCLCTRL_GPIO_OE1 |
2918 GRC_LCLCTRL_GPIO_OE2 |
2919 GRC_LCLCTRL_GPIO_OUTPUT1 |
2920 GRC_LCLCTRL_GPIO_OUTPUT2;
2921 if (no_gpio2) {
2922 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2923 GRC_LCLCTRL_GPIO_OUTPUT2);
2924 }
2925 tw32_wait_f(GRC_LOCAL_CTRL,
2926 tp->grc_local_ctrl | grc_local_ctrl,
2927 TG3_GRC_LCLCTL_PWRSW_DELAY);
2928
2929 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2930
2931 tw32_wait_f(GRC_LOCAL_CTRL,
2932 tp->grc_local_ctrl | grc_local_ctrl,
2933 TG3_GRC_LCLCTL_PWRSW_DELAY);
2934
2935 if (!no_gpio2) {
2936 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2937 tw32_wait_f(GRC_LOCAL_CTRL,
2938 tp->grc_local_ctrl | grc_local_ctrl,
2939 TG3_GRC_LCLCTL_PWRSW_DELAY);
2940 }
2941 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002942}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002943
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002944static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002945{
2946 u32 msg = 0;
2947
2948 /* Serialize power state transitions */
2949 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2950 return;
2951
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002952 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002953 msg = TG3_GPIO_MSG_NEED_VAUX;
2954
2955 msg = tg3_set_function_status(tp, msg);
2956
2957 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2958 goto done;
2959
2960 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2961 tg3_pwrsrc_switch_to_vaux(tp);
2962 else
2963 tg3_pwrsrc_die_with_vmain(tp);
2964
2965done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002966 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002967}
2968
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002969static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Matt Carlson683644b2011-03-09 16:58:23 +00002971 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Matt Carlson334355a2010-01-20 16:58:10 +00002973 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002974 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return;
2976
Joe Perches41535772013-02-16 11:20:04 +00002977 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2978 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2979 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002980 tg3_frob_aux_power_5717(tp, include_wol ?
2981 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002982 return;
2983 }
2984
2985 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002986 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002988 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002989
Michael Chanbc1c7562006-03-20 17:48:03 -08002990 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002991 if (dev_peer) {
2992 struct tg3 *tp_peer = netdev_priv(dev_peer);
2993
Joe Perches63c3a662011-04-26 08:12:10 +00002994 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002995 return;
2996
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002997 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002998 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002999 need_vaux = true;
3000 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08003001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003003 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
3004 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00003005 need_vaux = true;
3006
Matt Carlson520b2752011-06-13 13:39:02 +00003007 if (need_vaux)
3008 tg3_pwrsrc_switch_to_vaux(tp);
3009 else
3010 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003013static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
3014{
3015 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
3016 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00003017 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003018 if (speed != SPEED_10)
3019 return 1;
3020 } else if (speed == SPEED_10)
3021 return 1;
3022
3023 return 0;
3024}
3025
Nithin Sujir44f3b502013-05-13 11:04:15 +00003026static bool tg3_phy_power_bug(struct tg3 *tp)
3027{
3028 switch (tg3_asic_rev(tp)) {
3029 case ASIC_REV_5700:
3030 case ASIC_REV_5704:
3031 return true;
3032 case ASIC_REV_5780:
3033 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
3034 return true;
3035 return false;
3036 case ASIC_REV_5717:
3037 if (!tp->pci_fn)
3038 return true;
3039 return false;
3040 case ASIC_REV_5719:
3041 case ASIC_REV_5720:
3042 if ((tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
3043 !tp->pci_fn)
3044 return true;
3045 return false;
3046 }
3047
3048 return false;
3049}
3050
Nithin Sujir989038e2013-08-30 17:01:36 -07003051static bool tg3_phy_led_bug(struct tg3 *tp)
3052{
3053 switch (tg3_asic_rev(tp)) {
3054 case ASIC_REV_5719:
Nithin Sujir300cf9b2013-09-12 14:01:31 -07003055 case ASIC_REV_5720:
Nithin Sujir989038e2013-08-30 17:01:36 -07003056 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
3057 !tp->pci_fn)
3058 return true;
3059 return false;
3060 }
3061
3062 return false;
3063}
3064
Matt Carlson0a459aa2008-11-03 16:54:15 -08003065static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08003066{
Matt Carlsonce057f02007-11-12 21:08:03 -08003067 u32 val;
3068
Nithin Sujir942d1af2013-04-09 08:48:07 +00003069 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
3070 return;
3071
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003072 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00003073 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08003074 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3075 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
3076
3077 sg_dig_ctrl |=
3078 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
3079 tw32(SG_DIG_CTRL, sg_dig_ctrl);
3080 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
3081 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003082 return;
Michael Chan51297242007-02-13 12:17:57 -08003083 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003084
Joe Perches41535772013-02-16 11:20:04 +00003085 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08003086 tg3_bmcr_reset(tp);
3087 val = tr32(GRC_MISC_CFG);
3088 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
3089 udelay(40);
3090 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003091 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00003092 u32 phytest;
3093 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
3094 u32 phy;
3095
3096 tg3_writephy(tp, MII_ADVERTISE, 0);
3097 tg3_writephy(tp, MII_BMCR,
3098 BMCR_ANENABLE | BMCR_ANRESTART);
3099
3100 tg3_writephy(tp, MII_TG3_FET_TEST,
3101 phytest | MII_TG3_FET_SHADOW_EN);
3102 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
3103 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
3104 tg3_writephy(tp,
3105 MII_TG3_FET_SHDW_AUXMODE4,
3106 phy);
3107 }
3108 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3109 }
3110 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003111 } else if (do_low_power) {
Nithin Sujir989038e2013-08-30 17:01:36 -07003112 if (!tg3_phy_led_bug(tp))
3113 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3114 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003115
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003116 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3117 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3118 MII_TG3_AUXCTL_PCTL_VREG_11V;
3119 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003120 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003121
Michael Chan15c3b692006-03-22 01:06:52 -08003122 /* The PHY should not be powered down on some chips because
3123 * of bugs.
3124 */
Nithin Sujir44f3b502013-05-13 11:04:15 +00003125 if (tg3_phy_power_bug(tp))
Michael Chan15c3b692006-03-22 01:06:52 -08003126 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003127
Joe Perches41535772013-02-16 11:20:04 +00003128 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3129 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003130 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3131 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3132 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3133 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3134 }
3135
Michael Chan15c3b692006-03-22 01:06:52 -08003136 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3137}
3138
Matt Carlson3f007892008-11-03 16:51:36 -08003139/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003140static int tg3_nvram_lock(struct tg3 *tp)
3141{
Joe Perches63c3a662011-04-26 08:12:10 +00003142 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003143 int i;
3144
3145 if (tp->nvram_lock_cnt == 0) {
3146 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3147 for (i = 0; i < 8000; i++) {
3148 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3149 break;
3150 udelay(20);
3151 }
3152 if (i == 8000) {
3153 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3154 return -ENODEV;
3155 }
3156 }
3157 tp->nvram_lock_cnt++;
3158 }
3159 return 0;
3160}
3161
3162/* tp->lock is held. */
3163static void tg3_nvram_unlock(struct tg3 *tp)
3164{
Joe Perches63c3a662011-04-26 08:12:10 +00003165 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003166 if (tp->nvram_lock_cnt > 0)
3167 tp->nvram_lock_cnt--;
3168 if (tp->nvram_lock_cnt == 0)
3169 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3170 }
3171}
3172
3173/* tp->lock is held. */
3174static void tg3_enable_nvram_access(struct tg3 *tp)
3175{
Joe Perches63c3a662011-04-26 08:12:10 +00003176 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003177 u32 nvaccess = tr32(NVRAM_ACCESS);
3178
3179 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3180 }
3181}
3182
3183/* tp->lock is held. */
3184static void tg3_disable_nvram_access(struct tg3 *tp)
3185{
Joe Perches63c3a662011-04-26 08:12:10 +00003186 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003187 u32 nvaccess = tr32(NVRAM_ACCESS);
3188
3189 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3190 }
3191}
3192
3193static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3194 u32 offset, u32 *val)
3195{
3196 u32 tmp;
3197 int i;
3198
3199 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3200 return -EINVAL;
3201
3202 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3203 EEPROM_ADDR_DEVID_MASK |
3204 EEPROM_ADDR_READ);
3205 tw32(GRC_EEPROM_ADDR,
3206 tmp |
3207 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3208 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3209 EEPROM_ADDR_ADDR_MASK) |
3210 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3211
3212 for (i = 0; i < 1000; i++) {
3213 tmp = tr32(GRC_EEPROM_ADDR);
3214
3215 if (tmp & EEPROM_ADDR_COMPLETE)
3216 break;
3217 msleep(1);
3218 }
3219 if (!(tmp & EEPROM_ADDR_COMPLETE))
3220 return -EBUSY;
3221
Matt Carlson62cedd12009-04-20 14:52:29 -07003222 tmp = tr32(GRC_EEPROM_DATA);
3223
3224 /*
3225 * The data will always be opposite the native endian
3226 * format. Perform a blind byteswap to compensate.
3227 */
3228 *val = swab32(tmp);
3229
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003230 return 0;
3231}
3232
3233#define NVRAM_CMD_TIMEOUT 10000
3234
3235static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3236{
3237 int i;
3238
3239 tw32(NVRAM_CMD, nvram_cmd);
3240 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3241 udelay(10);
3242 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3243 udelay(10);
3244 break;
3245 }
3246 }
3247
3248 if (i == NVRAM_CMD_TIMEOUT)
3249 return -EBUSY;
3250
3251 return 0;
3252}
3253
3254static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3255{
Joe Perches63c3a662011-04-26 08:12:10 +00003256 if (tg3_flag(tp, NVRAM) &&
3257 tg3_flag(tp, NVRAM_BUFFERED) &&
3258 tg3_flag(tp, FLASH) &&
3259 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003260 (tp->nvram_jedecnum == JEDEC_ATMEL))
3261
3262 addr = ((addr / tp->nvram_pagesize) <<
3263 ATMEL_AT45DB0X1B_PAGE_POS) +
3264 (addr % tp->nvram_pagesize);
3265
3266 return addr;
3267}
3268
3269static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3270{
Joe Perches63c3a662011-04-26 08:12:10 +00003271 if (tg3_flag(tp, NVRAM) &&
3272 tg3_flag(tp, NVRAM_BUFFERED) &&
3273 tg3_flag(tp, FLASH) &&
3274 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003275 (tp->nvram_jedecnum == JEDEC_ATMEL))
3276
3277 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3278 tp->nvram_pagesize) +
3279 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3280
3281 return addr;
3282}
3283
Matt Carlsone4f34112009-02-25 14:25:00 +00003284/* NOTE: Data read in from NVRAM is byteswapped according to
3285 * the byteswapping settings for all other register accesses.
3286 * tg3 devices are BE devices, so on a BE machine, the data
3287 * returned will be exactly as it is seen in NVRAM. On a LE
3288 * machine, the 32-bit value will be byteswapped.
3289 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003290static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3291{
3292 int ret;
3293
Joe Perches63c3a662011-04-26 08:12:10 +00003294 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003295 return tg3_nvram_read_using_eeprom(tp, offset, val);
3296
3297 offset = tg3_nvram_phys_addr(tp, offset);
3298
3299 if (offset > NVRAM_ADDR_MSK)
3300 return -EINVAL;
3301
3302 ret = tg3_nvram_lock(tp);
3303 if (ret)
3304 return ret;
3305
3306 tg3_enable_nvram_access(tp);
3307
3308 tw32(NVRAM_ADDR, offset);
3309 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3310 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3311
3312 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003313 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003314
3315 tg3_disable_nvram_access(tp);
3316
3317 tg3_nvram_unlock(tp);
3318
3319 return ret;
3320}
3321
Matt Carlsona9dc5292009-02-25 14:25:30 +00003322/* Ensures NVRAM data is in bytestream format. */
3323static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003324{
3325 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003326 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003327 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003328 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003329 return res;
3330}
3331
Matt Carlsondbe9b922012-02-13 10:20:09 +00003332static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3333 u32 offset, u32 len, u8 *buf)
3334{
3335 int i, j, rc = 0;
3336 u32 val;
3337
3338 for (i = 0; i < len; i += 4) {
3339 u32 addr;
3340 __be32 data;
3341
3342 addr = offset + i;
3343
3344 memcpy(&data, buf + i, 4);
3345
3346 /*
3347 * The SEEPROM interface expects the data to always be opposite
3348 * the native endian format. We accomplish this by reversing
3349 * all the operations that would have been performed on the
3350 * data from a call to tg3_nvram_read_be32().
3351 */
3352 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3353
3354 val = tr32(GRC_EEPROM_ADDR);
3355 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3356
3357 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3358 EEPROM_ADDR_READ);
3359 tw32(GRC_EEPROM_ADDR, val |
3360 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3361 (addr & EEPROM_ADDR_ADDR_MASK) |
3362 EEPROM_ADDR_START |
3363 EEPROM_ADDR_WRITE);
3364
3365 for (j = 0; j < 1000; j++) {
3366 val = tr32(GRC_EEPROM_ADDR);
3367
3368 if (val & EEPROM_ADDR_COMPLETE)
3369 break;
3370 msleep(1);
3371 }
3372 if (!(val & EEPROM_ADDR_COMPLETE)) {
3373 rc = -EBUSY;
3374 break;
3375 }
3376 }
3377
3378 return rc;
3379}
3380
3381/* offset and length are dword aligned */
3382static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3383 u8 *buf)
3384{
3385 int ret = 0;
3386 u32 pagesize = tp->nvram_pagesize;
3387 u32 pagemask = pagesize - 1;
3388 u32 nvram_cmd;
3389 u8 *tmp;
3390
3391 tmp = kmalloc(pagesize, GFP_KERNEL);
3392 if (tmp == NULL)
3393 return -ENOMEM;
3394
3395 while (len) {
3396 int j;
3397 u32 phy_addr, page_off, size;
3398
3399 phy_addr = offset & ~pagemask;
3400
3401 for (j = 0; j < pagesize; j += 4) {
3402 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3403 (__be32 *) (tmp + j));
3404 if (ret)
3405 break;
3406 }
3407 if (ret)
3408 break;
3409
3410 page_off = offset & pagemask;
3411 size = pagesize;
3412 if (len < size)
3413 size = len;
3414
3415 len -= size;
3416
3417 memcpy(tmp + page_off, buf, size);
3418
3419 offset = offset + (pagesize - page_off);
3420
3421 tg3_enable_nvram_access(tp);
3422
3423 /*
3424 * Before we can erase the flash page, we need
3425 * to issue a special "write enable" command.
3426 */
3427 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3428
3429 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3430 break;
3431
3432 /* Erase the target page */
3433 tw32(NVRAM_ADDR, phy_addr);
3434
3435 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3436 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3437
3438 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3439 break;
3440
3441 /* Issue another write enable to start the write. */
3442 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3443
3444 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3445 break;
3446
3447 for (j = 0; j < pagesize; j += 4) {
3448 __be32 data;
3449
3450 data = *((__be32 *) (tmp + j));
3451
3452 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3453
3454 tw32(NVRAM_ADDR, phy_addr + j);
3455
3456 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3457 NVRAM_CMD_WR;
3458
3459 if (j == 0)
3460 nvram_cmd |= NVRAM_CMD_FIRST;
3461 else if (j == (pagesize - 4))
3462 nvram_cmd |= NVRAM_CMD_LAST;
3463
3464 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3465 if (ret)
3466 break;
3467 }
3468 if (ret)
3469 break;
3470 }
3471
3472 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3473 tg3_nvram_exec_cmd(tp, nvram_cmd);
3474
3475 kfree(tmp);
3476
3477 return ret;
3478}
3479
3480/* offset and length are dword aligned */
3481static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3482 u8 *buf)
3483{
3484 int i, ret = 0;
3485
3486 for (i = 0; i < len; i += 4, offset += 4) {
3487 u32 page_off, phy_addr, nvram_cmd;
3488 __be32 data;
3489
3490 memcpy(&data, buf + i, 4);
3491 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3492
3493 page_off = offset % tp->nvram_pagesize;
3494
3495 phy_addr = tg3_nvram_phys_addr(tp, offset);
3496
Matt Carlsondbe9b922012-02-13 10:20:09 +00003497 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3498
3499 if (page_off == 0 || i == 0)
3500 nvram_cmd |= NVRAM_CMD_FIRST;
3501 if (page_off == (tp->nvram_pagesize - 4))
3502 nvram_cmd |= NVRAM_CMD_LAST;
3503
3504 if (i == (len - 4))
3505 nvram_cmd |= NVRAM_CMD_LAST;
3506
Matt Carlson42278222012-02-13 15:20:11 +00003507 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3508 !tg3_flag(tp, FLASH) ||
3509 !tg3_flag(tp, 57765_PLUS))
3510 tw32(NVRAM_ADDR, phy_addr);
3511
Joe Perches41535772013-02-16 11:20:04 +00003512 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003513 !tg3_flag(tp, 5755_PLUS) &&
3514 (tp->nvram_jedecnum == JEDEC_ST) &&
3515 (nvram_cmd & NVRAM_CMD_FIRST)) {
3516 u32 cmd;
3517
3518 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3519 ret = tg3_nvram_exec_cmd(tp, cmd);
3520 if (ret)
3521 break;
3522 }
3523 if (!tg3_flag(tp, FLASH)) {
3524 /* We always do complete word writes to eeprom. */
3525 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3526 }
3527
3528 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3529 if (ret)
3530 break;
3531 }
3532 return ret;
3533}
3534
3535/* offset and length are dword aligned */
3536static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3537{
3538 int ret;
3539
3540 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3541 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3542 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3543 udelay(40);
3544 }
3545
3546 if (!tg3_flag(tp, NVRAM)) {
3547 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3548 } else {
3549 u32 grc_mode;
3550
3551 ret = tg3_nvram_lock(tp);
3552 if (ret)
3553 return ret;
3554
3555 tg3_enable_nvram_access(tp);
3556 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3557 tw32(NVRAM_WRITE1, 0x406);
3558
3559 grc_mode = tr32(GRC_MODE);
3560 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3561
3562 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3563 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3564 buf);
3565 } else {
3566 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3567 buf);
3568 }
3569
3570 grc_mode = tr32(GRC_MODE);
3571 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3572
3573 tg3_disable_nvram_access(tp);
3574 tg3_nvram_unlock(tp);
3575 }
3576
3577 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3578 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3579 udelay(40);
3580 }
3581
3582 return ret;
3583}
3584
Matt Carlson997b4f12011-08-31 11:44:53 +00003585#define RX_CPU_SCRATCH_BASE 0x30000
3586#define RX_CPU_SCRATCH_SIZE 0x04000
3587#define TX_CPU_SCRATCH_BASE 0x34000
3588#define TX_CPU_SCRATCH_SIZE 0x04000
3589
3590/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003591static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003592{
3593 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003594 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003595
Nithin Sujir837c45b2013-03-06 17:02:30 +00003596 for (i = 0; i < iters; i++) {
3597 tw32(cpu_base + CPU_STATE, 0xffffffff);
3598 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3599 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3600 break;
Gavin Shan6d446ec2013-06-25 15:24:32 +08003601 if (pci_channel_offline(tp->pdev))
3602 return -EBUSY;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003603 }
3604
3605 return (i == iters) ? -EBUSY : 0;
3606}
3607
3608/* tp->lock is held. */
3609static int tg3_rxcpu_pause(struct tg3 *tp)
3610{
3611 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3612
3613 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3614 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3615 udelay(10);
3616
3617 return rc;
3618}
3619
3620/* tp->lock is held. */
3621static int tg3_txcpu_pause(struct tg3 *tp)
3622{
3623 return tg3_pause_cpu(tp, TX_CPU_BASE);
3624}
3625
3626/* tp->lock is held. */
3627static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3628{
3629 tw32(cpu_base + CPU_STATE, 0xffffffff);
3630 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3631}
3632
3633/* tp->lock is held. */
3634static void tg3_rxcpu_resume(struct tg3 *tp)
3635{
3636 tg3_resume_cpu(tp, RX_CPU_BASE);
3637}
3638
3639/* tp->lock is held. */
3640static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3641{
3642 int rc;
3643
3644 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003645
Joe Perches41535772013-02-16 11:20:04 +00003646 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003647 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3648
3649 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3650 return 0;
3651 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003652 if (cpu_base == RX_CPU_BASE) {
3653 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003654 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003655 /*
3656 * There is only an Rx CPU for the 5750 derivative in the
3657 * BCM4785.
3658 */
3659 if (tg3_flag(tp, IS_SSB_CORE))
3660 return 0;
3661
Nithin Sujir837c45b2013-03-06 17:02:30 +00003662 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003663 }
3664
Nithin Sujir837c45b2013-03-06 17:02:30 +00003665 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003666 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003667 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003668 return -ENODEV;
3669 }
3670
3671 /* Clear firmware's nvram arbitration. */
3672 if (tg3_flag(tp, NVRAM))
3673 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3674 return 0;
3675}
3676
Nithin Sujir31f11a92013-03-06 17:02:33 +00003677static int tg3_fw_data_len(struct tg3 *tp,
3678 const struct tg3_firmware_hdr *fw_hdr)
3679{
3680 int fw_len;
3681
3682 /* Non fragmented firmware have one firmware header followed by a
3683 * contiguous chunk of data to be written. The length field in that
3684 * header is not the length of data to be written but the complete
3685 * length of the bss. The data length is determined based on
3686 * tp->fw->size minus headers.
3687 *
3688 * Fragmented firmware have a main header followed by multiple
3689 * fragments. Each fragment is identical to non fragmented firmware
3690 * with a firmware header followed by a contiguous chunk of data. In
3691 * the main header, the length field is unused and set to 0xffffffff.
3692 * In each fragment header the length is the entire size of that
3693 * fragment i.e. fragment data + header length. Data length is
3694 * therefore length field in the header minus TG3_FW_HDR_LEN.
3695 */
3696 if (tp->fw_len == 0xffffffff)
3697 fw_len = be32_to_cpu(fw_hdr->len);
3698 else
3699 fw_len = tp->fw->size;
3700
3701 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3702}
3703
Matt Carlson997b4f12011-08-31 11:44:53 +00003704/* tp->lock is held. */
3705static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3706 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003707 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003708{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003709 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003710 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003711 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003712
3713 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3714 netdev_err(tp->dev,
3715 "%s: Trying to load TX cpu firmware which is 5705\n",
3716 __func__);
3717 return -EINVAL;
3718 }
3719
Nithin Sujirc4dab502013-03-06 17:02:34 +00003720 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003721 write_op = tg3_write_mem;
3722 else
3723 write_op = tg3_write_indirect_reg32;
3724
Nithin Sujirc4dab502013-03-06 17:02:34 +00003725 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3726 /* It is possible that bootcode is still loading at this point.
3727 * Get the nvram lock first before halting the cpu.
3728 */
3729 int lock_err = tg3_nvram_lock(tp);
3730 err = tg3_halt_cpu(tp, cpu_base);
3731 if (!lock_err)
3732 tg3_nvram_unlock(tp);
3733 if (err)
3734 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003735
Nithin Sujirc4dab502013-03-06 17:02:34 +00003736 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3737 write_op(tp, cpu_scratch_base + i, 0);
3738 tw32(cpu_base + CPU_STATE, 0xffffffff);
3739 tw32(cpu_base + CPU_MODE,
3740 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3741 } else {
3742 /* Subtract additional main header for fragmented firmware and
3743 * advance to the first fragment
3744 */
3745 total_len -= TG3_FW_HDR_LEN;
3746 fw_hdr++;
3747 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003748
Nithin Sujir31f11a92013-03-06 17:02:33 +00003749 do {
3750 u32 *fw_data = (u32 *)(fw_hdr + 1);
3751 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3752 write_op(tp, cpu_scratch_base +
3753 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3754 (i * sizeof(u32)),
3755 be32_to_cpu(fw_data[i]));
3756
3757 total_len -= be32_to_cpu(fw_hdr->len);
3758
3759 /* Advance to next fragment */
3760 fw_hdr = (struct tg3_firmware_hdr *)
3761 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3762 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003763
3764 err = 0;
3765
3766out:
3767 return err;
3768}
3769
3770/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003771static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3772{
3773 int i;
3774 const int iters = 5;
3775
3776 tw32(cpu_base + CPU_STATE, 0xffffffff);
3777 tw32_f(cpu_base + CPU_PC, pc);
3778
3779 for (i = 0; i < iters; i++) {
3780 if (tr32(cpu_base + CPU_PC) == pc)
3781 break;
3782 tw32(cpu_base + CPU_STATE, 0xffffffff);
3783 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3784 tw32_f(cpu_base + CPU_PC, pc);
3785 udelay(1000);
3786 }
3787
3788 return (i == iters) ? -EBUSY : 0;
3789}
3790
3791/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003792static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3793{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003794 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003795 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003796
Nithin Sujir77997ea2013-03-06 17:02:32 +00003797 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003798
3799 /* Firmware blob starts with version numbers, followed by
3800 start address and length. We are setting complete length.
3801 length = end_address_of_bss - start_address_of_text.
3802 Remainder is the blob to be loaded contiguously
3803 from start address. */
3804
Matt Carlson997b4f12011-08-31 11:44:53 +00003805 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3806 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003807 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003808 if (err)
3809 return err;
3810
3811 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3812 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003813 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003814 if (err)
3815 return err;
3816
3817 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003818 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3819 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003820 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003821 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3822 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003823 tr32(RX_CPU_BASE + CPU_PC),
3824 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003825 return -ENODEV;
3826 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003827
3828 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003829
3830 return 0;
3831}
3832
Nithin Sujirc4dab502013-03-06 17:02:34 +00003833static int tg3_validate_rxcpu_state(struct tg3 *tp)
3834{
3835 const int iters = 1000;
3836 int i;
3837 u32 val;
3838
3839 /* Wait for boot code to complete initialization and enter service
3840 * loop. It is then safe to download service patches
3841 */
3842 for (i = 0; i < iters; i++) {
3843 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3844 break;
3845
3846 udelay(10);
3847 }
3848
3849 if (i == iters) {
3850 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3851 return -EBUSY;
3852 }
3853
3854 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3855 if (val & 0xff) {
3856 netdev_warn(tp->dev,
3857 "Other patches exist. Not downloading EEE patch\n");
3858 return -EEXIST;
3859 }
3860
3861 return 0;
3862}
3863
3864/* tp->lock is held. */
3865static void tg3_load_57766_firmware(struct tg3 *tp)
3866{
3867 struct tg3_firmware_hdr *fw_hdr;
3868
3869 if (!tg3_flag(tp, NO_NVRAM))
3870 return;
3871
3872 if (tg3_validate_rxcpu_state(tp))
3873 return;
3874
3875 if (!tp->fw)
3876 return;
3877
3878 /* This firmware blob has a different format than older firmware
3879 * releases as given below. The main difference is we have fragmented
3880 * data to be written to non-contiguous locations.
3881 *
3882 * In the beginning we have a firmware header identical to other
3883 * firmware which consists of version, base addr and length. The length
3884 * here is unused and set to 0xffffffff.
3885 *
3886 * This is followed by a series of firmware fragments which are
3887 * individually identical to previous firmware. i.e. they have the
3888 * firmware header and followed by data for that fragment. The version
3889 * field of the individual fragment header is unused.
3890 */
3891
3892 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3893 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3894 return;
3895
3896 if (tg3_rxcpu_pause(tp))
3897 return;
3898
3899 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3900 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3901
3902 tg3_rxcpu_resume(tp);
3903}
3904
Matt Carlson997b4f12011-08-31 11:44:53 +00003905/* tp->lock is held. */
3906static int tg3_load_tso_firmware(struct tg3 *tp)
3907{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003908 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003909 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003910 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003911
Matt Carlson1caf13e2013-03-06 17:02:29 +00003912 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003913 return 0;
3914
Nithin Sujir77997ea2013-03-06 17:02:32 +00003915 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003916
3917 /* Firmware blob starts with version numbers, followed by
3918 start address and length. We are setting complete length.
3919 length = end_address_of_bss - start_address_of_text.
3920 Remainder is the blob to be loaded contiguously
3921 from start address. */
3922
Matt Carlson997b4f12011-08-31 11:44:53 +00003923 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003924
Joe Perches41535772013-02-16 11:20:04 +00003925 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003926 cpu_base = RX_CPU_BASE;
3927 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3928 } else {
3929 cpu_base = TX_CPU_BASE;
3930 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3931 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3932 }
3933
3934 err = tg3_load_firmware_cpu(tp, cpu_base,
3935 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003936 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003937 if (err)
3938 return err;
3939
3940 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003941 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3942 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003943 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003944 netdev_err(tp->dev,
3945 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003946 __func__, tr32(cpu_base + CPU_PC),
3947 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003948 return -ENODEV;
3949 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003950
3951 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003952 return 0;
3953}
3954
Michael Chanf022ae62014-01-03 10:09:11 -08003955/* tp->lock is held. */
3956static void __tg3_set_one_mac_addr(struct tg3 *tp, u8 *mac_addr, int index)
3957{
3958 u32 addr_high, addr_low;
3959
3960 addr_high = ((mac_addr[0] << 8) | mac_addr[1]);
3961 addr_low = ((mac_addr[2] << 24) | (mac_addr[3] << 16) |
3962 (mac_addr[4] << 8) | mac_addr[5]);
3963
3964 if (index < 4) {
3965 tw32(MAC_ADDR_0_HIGH + (index * 8), addr_high);
3966 tw32(MAC_ADDR_0_LOW + (index * 8), addr_low);
3967 } else {
3968 index -= 4;
3969 tw32(MAC_EXTADDR_0_HIGH + (index * 8), addr_high);
3970 tw32(MAC_EXTADDR_0_LOW + (index * 8), addr_low);
3971 }
3972}
Matt Carlson997b4f12011-08-31 11:44:53 +00003973
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003974/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003975static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003976{
Michael Chanf022ae62014-01-03 10:09:11 -08003977 u32 addr_high;
Matt Carlson3f007892008-11-03 16:51:36 -08003978 int i;
3979
Matt Carlson3f007892008-11-03 16:51:36 -08003980 for (i = 0; i < 4; i++) {
3981 if (i == 1 && skip_mac_1)
3982 continue;
Michael Chanf022ae62014-01-03 10:09:11 -08003983 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003984 }
3985
Joe Perches41535772013-02-16 11:20:04 +00003986 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3987 tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chanf022ae62014-01-03 10:09:11 -08003988 for (i = 4; i < 16; i++)
3989 __tg3_set_one_mac_addr(tp, tp->dev->dev_addr, i);
Matt Carlson3f007892008-11-03 16:51:36 -08003990 }
3991
3992 addr_high = (tp->dev->dev_addr[0] +
3993 tp->dev->dev_addr[1] +
3994 tp->dev->dev_addr[2] +
3995 tp->dev->dev_addr[3] +
3996 tp->dev->dev_addr[4] +
3997 tp->dev->dev_addr[5]) &
3998 TX_BACKOFF_SEED_MASK;
3999 tw32(MAC_TX_BACKOFF_SEED, addr_high);
4000}
4001
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004002static void tg3_enable_register_access(struct tg3 *tp)
4003{
4004 /*
4005 * Make sure register accesses (indirect or otherwise) will function
4006 * correctly.
4007 */
4008 pci_write_config_dword(tp->pdev,
4009 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
4010}
4011
4012static int tg3_power_up(struct tg3 *tp)
4013{
Matt Carlsonbed98292011-07-13 09:27:29 +00004014 int err;
4015
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004016 tg3_enable_register_access(tp);
4017
Matt Carlsonbed98292011-07-13 09:27:29 +00004018 err = pci_set_power_state(tp->pdev, PCI_D0);
4019 if (!err) {
4020 /* Switch out of Vaux if it is a NIC */
4021 tg3_pwrsrc_switch_to_vmain(tp);
4022 } else {
4023 netdev_err(tp->dev, "Transition to D0 failed\n");
4024 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004025
Matt Carlsonbed98292011-07-13 09:27:29 +00004026 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004027}
4028
Joe Perches953c96e2013-04-09 10:18:14 +00004029static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00004030
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004031static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
4033 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004034 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004036 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004037
4038 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004039 if (tg3_flag(tp, CLKREQ_BUG))
4040 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4041 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
4044 tw32(TG3PCI_MISC_HOST_CTRL,
4045 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
4046
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004047 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004048 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004049
Joe Perches63c3a662011-04-26 08:12:10 +00004050 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08004051 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004052 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00004053 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004054 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004055 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004056
Hauke Mehrtensead24022013-09-28 23:15:26 +02004057 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004058
Matt Carlson800960682010-08-02 11:26:06 +00004059 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004060
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004061 tp->link_config.speed = phydev->speed;
4062 tp->link_config.duplex = phydev->duplex;
4063 tp->link_config.autoneg = phydev->autoneg;
4064 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004065
4066 advertising = ADVERTISED_TP |
4067 ADVERTISED_Pause |
4068 ADVERTISED_Autoneg |
4069 ADVERTISED_10baseT_Half;
4070
Joe Perches63c3a662011-04-26 08:12:10 +00004071 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
4072 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004073 advertising |=
4074 ADVERTISED_100baseT_Half |
4075 ADVERTISED_100baseT_Full |
4076 ADVERTISED_10baseT_Full;
4077 else
4078 advertising |= ADVERTISED_10baseT_Full;
4079 }
4080
4081 phydev->advertising = advertising;
4082
4083 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08004084
4085 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00004086 if (phyid != PHY_ID_BCMAC131) {
4087 phyid &= PHY_BCM_OUI_MASK;
4088 if (phyid == PHY_BCM_OUI_1 ||
4089 phyid == PHY_BCM_OUI_2 ||
4090 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08004091 do_low_power = true;
4092 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07004093 }
Matt Carlsondd477002008-05-25 23:45:58 -07004094 } else {
Matt Carlson20232762008-12-21 20:18:56 -08004095 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08004096
Matt Carlsonc6700ce2012-02-13 15:20:15 +00004097 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00004098 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Matt Carlson2855b9f2012-02-13 15:20:14 +00004100 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00004101 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103
Joe Perches41535772013-02-16 11:20:04 +00004104 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07004105 u32 val;
4106
4107 val = tr32(GRC_VCPU_EXT_CTRL);
4108 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00004109 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08004110 int i;
4111 u32 val;
4112
4113 for (i = 0; i < 200; i++) {
4114 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4115 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4116 break;
4117 msleep(1);
4118 }
4119 }
Joe Perches63c3a662011-04-26 08:12:10 +00004120 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004121 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4122 WOL_DRV_STATE_SHUTDOWN |
4123 WOL_DRV_WOL |
4124 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004125
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004126 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 u32 mac_mode;
4128
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004129 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004130 if (do_low_power &&
4131 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4132 tg3_phy_auxctl_write(tp,
4133 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4134 MII_TG3_AUXCTL_PCTL_WOL_EN |
4135 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4136 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004137 udelay(40);
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004140 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004141 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004142 else if (tp->phy_flags &
4143 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4144 if (tp->link_config.active_speed == SPEED_1000)
4145 mac_mode = MAC_MODE_PORT_MODE_GMII;
4146 else
4147 mac_mode = MAC_MODE_PORT_MODE_MII;
4148 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004149 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004151 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004152 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004153 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004154 SPEED_100 : SPEED_10;
4155 if (tg3_5700_link_polarity(tp, speed))
4156 mac_mode |= MAC_MODE_LINK_POLARITY;
4157 else
4158 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 } else {
4161 mac_mode = MAC_MODE_PORT_MODE_TBI;
4162 }
4163
Joe Perches63c3a662011-04-26 08:12:10 +00004164 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 tw32(MAC_LED_CTRL, tp->led_ctrl);
4166
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004167 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004168 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4169 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004170 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Joe Perches63c3a662011-04-26 08:12:10 +00004172 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004173 mac_mode |= MAC_MODE_APE_TX_EN |
4174 MAC_MODE_APE_RX_EN |
4175 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 tw32_f(MAC_MODE, mac_mode);
4178 udelay(100);
4179
4180 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4181 udelay(10);
4182 }
4183
Joe Perches63c3a662011-04-26 08:12:10 +00004184 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004185 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4186 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 u32 base_val;
4188
4189 base_val = tp->pci_clock_ctrl;
4190 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4191 CLOCK_CTRL_TXCLK_DISABLE);
4192
Michael Chanb401e9e2005-12-19 16:27:04 -08004193 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4194 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004195 } else if (tg3_flag(tp, 5780_CLASS) ||
4196 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004197 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004198 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004199 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 u32 newbits1, newbits2;
4201
Joe Perches41535772013-02-16 11:20:04 +00004202 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4203 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4205 CLOCK_CTRL_TXCLK_DISABLE |
4206 CLOCK_CTRL_ALTCLK);
4207 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004208 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 newbits1 = CLOCK_CTRL_625_CORE;
4210 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4211 } else {
4212 newbits1 = CLOCK_CTRL_ALTCLK;
4213 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4214 }
4215
Michael Chanb401e9e2005-12-19 16:27:04 -08004216 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4217 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Michael Chanb401e9e2005-12-19 16:27:04 -08004219 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4220 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Joe Perches63c3a662011-04-26 08:12:10 +00004222 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 u32 newbits3;
4224
Joe Perches41535772013-02-16 11:20:04 +00004225 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4226 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4228 CLOCK_CTRL_TXCLK_DISABLE |
4229 CLOCK_CTRL_44MHZ_CORE);
4230 } else {
4231 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4232 }
4233
Michael Chanb401e9e2005-12-19 16:27:04 -08004234 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4235 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 }
4237 }
4238
Joe Perches63c3a662011-04-26 08:12:10 +00004239 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004240 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004241
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004242 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004245 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004246 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4247 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 u32 val = tr32(0x7d00);
4249
4250 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4251 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004252 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004253 int err;
4254
4255 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004257 if (!err)
4258 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
Michael Chanbbadf502006-04-06 21:46:34 -07004262 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4263
Nithin Sujir2e460fc2013-05-23 11:11:22 +00004264 tg3_ape_driver_state_change(tp, RESET_KIND_SHUTDOWN);
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return 0;
4267}
4268
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004269static void tg3_power_down(struct tg3 *tp)
4270{
Joe Perches63c3a662011-04-26 08:12:10 +00004271 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004272 pci_set_power_state(tp->pdev, PCI_D3hot);
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4276{
4277 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4278 case MII_TG3_AUX_STAT_10HALF:
4279 *speed = SPEED_10;
4280 *duplex = DUPLEX_HALF;
4281 break;
4282
4283 case MII_TG3_AUX_STAT_10FULL:
4284 *speed = SPEED_10;
4285 *duplex = DUPLEX_FULL;
4286 break;
4287
4288 case MII_TG3_AUX_STAT_100HALF:
4289 *speed = SPEED_100;
4290 *duplex = DUPLEX_HALF;
4291 break;
4292
4293 case MII_TG3_AUX_STAT_100FULL:
4294 *speed = SPEED_100;
4295 *duplex = DUPLEX_FULL;
4296 break;
4297
4298 case MII_TG3_AUX_STAT_1000HALF:
4299 *speed = SPEED_1000;
4300 *duplex = DUPLEX_HALF;
4301 break;
4302
4303 case MII_TG3_AUX_STAT_1000FULL:
4304 *speed = SPEED_1000;
4305 *duplex = DUPLEX_FULL;
4306 break;
4307
4308 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004309 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004310 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4311 SPEED_10;
4312 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4313 DUPLEX_HALF;
4314 break;
4315 }
Matt Carlsone7405222012-02-13 15:20:16 +00004316 *speed = SPEED_UNKNOWN;
4317 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
Matt Carlson42b64a42011-05-19 12:12:49 +00004322static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Matt Carlson42b64a42011-05-19 12:12:49 +00004324 int err = 0;
4325 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Matt Carlson42b64a42011-05-19 12:12:49 +00004327 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004328 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004329 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Matt Carlson42b64a42011-05-19 12:12:49 +00004331 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4332 if (err)
4333 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Matt Carlson4f272092011-12-14 11:09:57 +00004335 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4336 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004337
Joe Perches41535772013-02-16 11:20:04 +00004338 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4339 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004340 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004341
Matt Carlson4f272092011-12-14 11:09:57 +00004342 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4343 if (err)
4344 goto done;
4345 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004346
Matt Carlson42b64a42011-05-19 12:12:49 +00004347 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4348 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Matt Carlson42b64a42011-05-19 12:12:49 +00004350 tw32(TG3_CPMU_EEE_MODE,
4351 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004352
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004353 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004354 if (!err) {
4355 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004356
Matt Carlsona6b68da2010-12-06 08:28:52 +00004357 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004358 /* Advertise 100-BaseTX EEE ability */
4359 if (advertise & ADVERTISED_100baseT_Full)
4360 val |= MDIO_AN_EEE_ADV_100TX;
4361 /* Advertise 1000-BaseT EEE ability */
4362 if (advertise & ADVERTISED_1000baseT_Full)
4363 val |= MDIO_AN_EEE_ADV_1000T;
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004364
4365 if (!tp->eee.eee_enabled) {
4366 val = 0;
4367 tp->eee.advertised = 0;
4368 } else {
4369 tp->eee.advertised = advertise &
4370 (ADVERTISED_100baseT_Full |
4371 ADVERTISED_1000baseT_Full);
4372 }
4373
Matt Carlson42b64a42011-05-19 12:12:49 +00004374 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004375 if (err)
4376 val = 0;
4377
Joe Perches41535772013-02-16 11:20:04 +00004378 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004379 case ASIC_REV_5717:
4380 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004381 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004382 case ASIC_REV_5719:
4383 /* If we advertised any eee advertisements above... */
4384 if (val)
4385 val = MII_TG3_DSP_TAP26_ALNOKO |
4386 MII_TG3_DSP_TAP26_RMRXSTO |
4387 MII_TG3_DSP_TAP26_OPCSINPT;
4388 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4389 /* Fall through */
4390 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004391 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004392 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4393 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4394 MII_TG3_DSP_CH34TP2_HIBW01);
4395 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004396
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004397 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004398 if (!err)
4399 err = err2;
4400 }
4401
4402done:
4403 return err;
4404}
4405
4406static void tg3_phy_copper_begin(struct tg3 *tp)
4407{
Matt Carlsond13ba512012-02-22 12:35:19 +00004408 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4409 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4410 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004411
Nithin Sujir942d1af2013-04-09 08:48:07 +00004412 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4413 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004414 adv = ADVERTISED_10baseT_Half |
4415 ADVERTISED_10baseT_Full;
4416 if (tg3_flag(tp, WOL_SPEED_100MB))
4417 adv |= ADVERTISED_100baseT_Half |
4418 ADVERTISED_100baseT_Full;
Nithin Sujir7c786062013-12-06 09:53:17 -08004419 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK) {
4420 if (!(tp->phy_flags &
4421 TG3_PHYFLG_DISABLE_1G_HD_ADV))
4422 adv |= ADVERTISED_1000baseT_Half;
4423 adv |= ADVERTISED_1000baseT_Full;
4424 }
Matt Carlson42b64a42011-05-19 12:12:49 +00004425
Matt Carlsond13ba512012-02-22 12:35:19 +00004426 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004427 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004428 adv = tp->link_config.advertising;
4429 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4430 adv &= ~(ADVERTISED_1000baseT_Half |
4431 ADVERTISED_1000baseT_Full);
4432
4433 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004434 }
4435
Matt Carlsond13ba512012-02-22 12:35:19 +00004436 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004437
Nithin Sujir942d1af2013-04-09 08:48:07 +00004438 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4439 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4440 /* Normally during power down we want to autonegotiate
4441 * the lowest possible speed for WOL. However, to avoid
4442 * link flap, we leave it untouched.
4443 */
4444 return;
4445 }
4446
Matt Carlsond13ba512012-02-22 12:35:19 +00004447 tg3_writephy(tp, MII_BMCR,
4448 BMCR_ANENABLE | BMCR_ANRESTART);
4449 } else {
4450 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 u32 bmcr, orig_bmcr;
4452
4453 tp->link_config.active_speed = tp->link_config.speed;
4454 tp->link_config.active_duplex = tp->link_config.duplex;
4455
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004456 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4457 /* With autoneg disabled, 5715 only links up when the
4458 * advertisement register has the configured speed
4459 * enabled.
4460 */
4461 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4462 }
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 bmcr = 0;
4465 switch (tp->link_config.speed) {
4466 default:
4467 case SPEED_10:
4468 break;
4469
4470 case SPEED_100:
4471 bmcr |= BMCR_SPEED100;
4472 break;
4473
4474 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004475 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 if (tp->link_config.duplex == DUPLEX_FULL)
4480 bmcr |= BMCR_FULLDPLX;
4481
4482 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4483 (bmcr != orig_bmcr)) {
4484 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4485 for (i = 0; i < 1500; i++) {
4486 u32 tmp;
4487
4488 udelay(10);
4489 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4490 tg3_readphy(tp, MII_BMSR, &tmp))
4491 continue;
4492 if (!(tmp & BMSR_LSTATUS)) {
4493 udelay(40);
4494 break;
4495 }
4496 }
4497 tg3_writephy(tp, MII_BMCR, bmcr);
4498 udelay(40);
4499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501}
4502
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004503static int tg3_phy_pull_config(struct tg3 *tp)
4504{
4505 int err;
4506 u32 val;
4507
4508 err = tg3_readphy(tp, MII_BMCR, &val);
4509 if (err)
4510 goto done;
4511
4512 if (!(val & BMCR_ANENABLE)) {
4513 tp->link_config.autoneg = AUTONEG_DISABLE;
4514 tp->link_config.advertising = 0;
4515 tg3_flag_clear(tp, PAUSE_AUTONEG);
4516
4517 err = -EIO;
4518
4519 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4520 case 0:
4521 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4522 goto done;
4523
4524 tp->link_config.speed = SPEED_10;
4525 break;
4526 case BMCR_SPEED100:
4527 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4528 goto done;
4529
4530 tp->link_config.speed = SPEED_100;
4531 break;
4532 case BMCR_SPEED1000:
4533 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4534 tp->link_config.speed = SPEED_1000;
4535 break;
4536 }
4537 /* Fall through */
4538 default:
4539 goto done;
4540 }
4541
4542 if (val & BMCR_FULLDPLX)
4543 tp->link_config.duplex = DUPLEX_FULL;
4544 else
4545 tp->link_config.duplex = DUPLEX_HALF;
4546
4547 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4548
4549 err = 0;
4550 goto done;
4551 }
4552
4553 tp->link_config.autoneg = AUTONEG_ENABLE;
4554 tp->link_config.advertising = ADVERTISED_Autoneg;
4555 tg3_flag_set(tp, PAUSE_AUTONEG);
4556
4557 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4558 u32 adv;
4559
4560 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4561 if (err)
4562 goto done;
4563
4564 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4565 tp->link_config.advertising |= adv | ADVERTISED_TP;
4566
4567 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4568 } else {
4569 tp->link_config.advertising |= ADVERTISED_FIBRE;
4570 }
4571
4572 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4573 u32 adv;
4574
4575 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4576 err = tg3_readphy(tp, MII_CTRL1000, &val);
4577 if (err)
4578 goto done;
4579
4580 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4581 } else {
4582 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4583 if (err)
4584 goto done;
4585
4586 adv = tg3_decode_flowctrl_1000X(val);
4587 tp->link_config.flowctrl = adv;
4588
4589 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4590 adv = mii_adv_to_ethtool_adv_x(val);
4591 }
4592
4593 tp->link_config.advertising |= adv;
4594 }
4595
4596done:
4597 return err;
4598}
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600static int tg3_init_5401phy_dsp(struct tg3 *tp)
4601{
4602 int err;
4603
4604 /* Turn off tap power management. */
4605 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004606 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004608 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4609 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4610 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4611 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4612 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 udelay(40);
4615
4616 return err;
4617}
4618
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004619static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4620{
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004621 struct ethtool_eee eee;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004622
4623 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4624 return true;
4625
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004626 tg3_eee_pull_config(tp, &eee);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004627
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004628 if (tp->eee.eee_enabled) {
4629 if (tp->eee.advertised != eee.advertised ||
4630 tp->eee.tx_lpi_timer != eee.tx_lpi_timer ||
4631 tp->eee.tx_lpi_enabled != eee.tx_lpi_enabled)
4632 return false;
4633 } else {
4634 /* EEE is disabled but we're advertising */
4635 if (eee.advertised)
4636 return false;
4637 }
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004638
4639 return true;
4640}
4641
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004642static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004644 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004645
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004646 advertising = tp->link_config.advertising;
4647 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004649 advmsk = ADVERTISE_ALL;
4650 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004651 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004652 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004655 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4656 return false;
4657
4658 if ((*lcladv & advmsk) != tgtadv)
4659 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004660
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004661 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 u32 tg3_ctrl;
4663
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004664 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004665
Matt Carlson221c5632011-06-13 13:39:01 +00004666 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004667 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Matt Carlson3198e072012-02-13 15:20:10 +00004669 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004670 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4671 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004672 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4673 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4674 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4675 } else {
4676 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4677 }
4678
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004679 if (tg3_ctrl != tgtadv)
4680 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004682
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004683 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004684}
4685
Matt Carlson859edb22011-12-08 14:40:16 +00004686static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4687{
4688 u32 lpeth = 0;
4689
4690 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4691 u32 val;
4692
4693 if (tg3_readphy(tp, MII_STAT1000, &val))
4694 return false;
4695
4696 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4697 }
4698
4699 if (tg3_readphy(tp, MII_LPA, rmtadv))
4700 return false;
4701
4702 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4703 tp->link_config.rmt_adv = lpeth;
4704
4705 return true;
4706}
4707
Joe Perches953c96e2013-04-09 10:18:14 +00004708static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004709{
4710 if (curr_link_up != tp->link_up) {
4711 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004712 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004713 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004714 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004715 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4716 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4717 }
4718
4719 tg3_link_report(tp);
4720 return true;
4721 }
4722
4723 return false;
4724}
4725
Michael Chan3310e242013-04-09 08:48:05 +00004726static void tg3_clear_mac_status(struct tg3 *tp)
4727{
4728 tw32(MAC_EVENT, 0);
4729
4730 tw32_f(MAC_STATUS,
4731 MAC_STATUS_SYNC_CHANGED |
4732 MAC_STATUS_CFG_CHANGED |
4733 MAC_STATUS_MI_COMPLETION |
4734 MAC_STATUS_LNKSTATE_CHANGED);
4735 udelay(40);
4736}
4737
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +00004738static void tg3_setup_eee(struct tg3 *tp)
4739{
4740 u32 val;
4741
4742 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
4743 TG3_CPMU_EEE_LNKIDL_UART_IDL;
4744 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
4745 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
4746
4747 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
4748
4749 tw32_f(TG3_CPMU_EEE_CTRL,
4750 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
4751
4752 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
4753 (tp->eee.tx_lpi_enabled ? TG3_CPMU_EEEMD_LPI_IN_TX : 0) |
4754 TG3_CPMU_EEEMD_LPI_IN_RX |
4755 TG3_CPMU_EEEMD_EEE_ENABLE;
4756
4757 if (tg3_asic_rev(tp) != ASIC_REV_5717)
4758 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
4759
4760 if (tg3_flag(tp, ENABLE_APE))
4761 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
4762
4763 tw32_f(TG3_CPMU_EEE_MODE, tp->eee.eee_enabled ? val : 0);
4764
4765 tw32_f(TG3_CPMU_EEE_DBTMR1,
4766 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
4767 (tp->eee.tx_lpi_timer & 0xffff));
4768
4769 tw32_f(TG3_CPMU_EEE_DBTMR2,
4770 TG3_CPMU_DBTMR2_APE_TX_2047US |
4771 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
4772}
4773
Joe Perches953c96e2013-04-09 10:18:14 +00004774static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Joe Perches953c96e2013-04-09 10:18:14 +00004776 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004777 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004778 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 u16 current_speed;
4780 u8 current_duplex;
4781 int i, err;
4782
Michael Chan3310e242013-04-09 08:48:05 +00004783 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Matt Carlson8ef21422008-05-02 16:47:53 -07004785 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4786 tw32_f(MAC_MI_MODE,
4787 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4788 udelay(80);
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004791 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* Some third-party PHYs need to be reset on link going
4794 * down.
4795 */
Joe Perches41535772013-02-16 11:20:04 +00004796 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4797 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4798 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004799 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 tg3_readphy(tp, MII_BMSR, &bmsr);
4801 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4802 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004803 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805 if (force_reset)
4806 tg3_phy_reset(tp);
4807
Matt Carlson79eb6902010-02-17 15:17:03 +00004808 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 tg3_readphy(tp, MII_BMSR, &bmsr);
4810 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004811 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 bmsr = 0;
4813
4814 if (!(bmsr & BMSR_LSTATUS)) {
4815 err = tg3_init_5401phy_dsp(tp);
4816 if (err)
4817 return err;
4818
4819 tg3_readphy(tp, MII_BMSR, &bmsr);
4820 for (i = 0; i < 1000; i++) {
4821 udelay(10);
4822 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4823 (bmsr & BMSR_LSTATUS)) {
4824 udelay(40);
4825 break;
4826 }
4827 }
4828
Matt Carlson79eb6902010-02-17 15:17:03 +00004829 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4830 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 !(bmsr & BMSR_LSTATUS) &&
4832 tp->link_config.active_speed == SPEED_1000) {
4833 err = tg3_phy_reset(tp);
4834 if (!err)
4835 err = tg3_init_5401phy_dsp(tp);
4836 if (err)
4837 return err;
4838 }
4839 }
Joe Perches41535772013-02-16 11:20:04 +00004840 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4841 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 /* 5701 {A0,B0} CRC bug workaround */
4843 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004844 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4845 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4846 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848
4849 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004850 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4851 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004853 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004855 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4857
Joe Perches41535772013-02-16 11:20:04 +00004858 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4859 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4861 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4862 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4863 else
4864 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4865 }
4866
Joe Perches953c96e2013-04-09 10:18:14 +00004867 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004868 current_speed = SPEED_UNKNOWN;
4869 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004870 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004871 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004873 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004874 err = tg3_phy_auxctl_read(tp,
4875 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4876 &val);
4877 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004878 tg3_phy_auxctl_write(tp,
4879 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4880 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 goto relink;
4882 }
4883 }
4884
4885 bmsr = 0;
4886 for (i = 0; i < 100; i++) {
4887 tg3_readphy(tp, MII_BMSR, &bmsr);
4888 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4889 (bmsr & BMSR_LSTATUS))
4890 break;
4891 udelay(40);
4892 }
4893
4894 if (bmsr & BMSR_LSTATUS) {
4895 u32 aux_stat, bmcr;
4896
4897 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4898 for (i = 0; i < 2000; i++) {
4899 udelay(10);
4900 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4901 aux_stat)
4902 break;
4903 }
4904
4905 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4906 &current_speed,
4907 &current_duplex);
4908
4909 bmcr = 0;
4910 for (i = 0; i < 200; i++) {
4911 tg3_readphy(tp, MII_BMCR, &bmcr);
4912 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4913 continue;
4914 if (bmcr && bmcr != 0x7fff)
4915 break;
4916 udelay(10);
4917 }
4918
Matt Carlsonef167e22007-12-20 20:10:01 -08004919 lcl_adv = 0;
4920 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Matt Carlsonef167e22007-12-20 20:10:01 -08004922 tp->link_config.active_speed = current_speed;
4923 tp->link_config.active_duplex = current_duplex;
4924
4925 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004926 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4927
Matt Carlsonef167e22007-12-20 20:10:01 -08004928 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004929 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004930 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004931 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004932 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004933
4934 /* EEE settings changes take effect only after a phy
4935 * reset. If we have skipped a reset due to Link Flap
4936 * Avoidance being enabled, do it now.
4937 */
4938 if (!eee_config_ok &&
4939 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004940 !force_reset) {
4941 tg3_setup_eee(tp);
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004942 tg3_phy_reset(tp);
Nithin Sujir5b6c2732013-05-18 06:26:54 +00004943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 } else {
4945 if (!(bmcr & BMCR_ANENABLE) &&
4946 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004947 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004948 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 }
4950 }
4951
Joe Perches953c96e2013-04-09 10:18:14 +00004952 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004953 tp->link_config.active_duplex == DUPLEX_FULL) {
4954 u32 reg, bit;
4955
4956 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4957 reg = MII_TG3_FET_GEN_STAT;
4958 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4959 } else {
4960 reg = MII_TG3_EXT_STAT;
4961 bit = MII_TG3_EXT_STAT_MDIX;
4962 }
4963
4964 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4965 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4966
Matt Carlsonef167e22007-12-20 20:10:01 -08004967 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004972 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 tg3_phy_copper_begin(tp);
4974
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004975 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004976 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004977 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4978 current_speed = SPEED_1000;
4979 current_duplex = DUPLEX_FULL;
4980 tp->link_config.active_speed = current_speed;
4981 tp->link_config.active_duplex = current_duplex;
4982 }
4983
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004984 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004985 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4986 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004987 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 }
4989
4990 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004991 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (tp->link_config.active_speed == SPEED_100 ||
4993 tp->link_config.active_speed == SPEED_10)
4994 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4995 else
4996 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004997 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004998 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4999 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5001
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00005002 /* In order for the 5750 core in BCM4785 chip to work properly
5003 * in RGMII mode, the Led Control Register must be set up.
5004 */
5005 if (tg3_flag(tp, RGMII_MODE)) {
5006 u32 led_ctrl = tr32(MAC_LED_CTRL);
5007 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
5008
5009 if (tp->link_config.active_speed == SPEED_10)
5010 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
5011 else if (tp->link_config.active_speed == SPEED_100)
5012 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5013 LED_CTRL_100MBPS_ON);
5014 else if (tp->link_config.active_speed == SPEED_1000)
5015 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
5016 LED_CTRL_1000MBPS_ON);
5017
5018 tw32(MAC_LED_CTRL, led_ctrl);
5019 udelay(40);
5020 }
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5023 if (tp->link_config.active_duplex == DUPLEX_HALF)
5024 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5025
Joe Perches41535772013-02-16 11:20:04 +00005026 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00005027 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005028 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005030 else
5031 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033
5034 /* ??? Without this setting Netgear GA302T PHY does not
5035 * ??? send/receive packets...
5036 */
Matt Carlson79eb6902010-02-17 15:17:03 +00005037 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00005038 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
5040 tw32_f(MAC_MI_MODE, tp->mi_mode);
5041 udelay(80);
5042 }
5043
5044 tw32_f(MAC_MODE, tp->mac_mode);
5045 udelay(40);
5046
Matt Carlson52b02d02010-10-14 10:37:41 +00005047 tg3_phy_eee_adjust(tp, current_link_up);
5048
Joe Perches63c3a662011-04-26 08:12:10 +00005049 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 /* Polled via timer. */
5051 tw32_f(MAC_EVENT, 0);
5052 } else {
5053 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5054 }
5055 udelay(40);
5056
Joe Perches41535772013-02-16 11:20:04 +00005057 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00005058 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00005060 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 udelay(120);
5062 tw32_f(MAC_STATUS,
5063 (MAC_STATUS_SYNC_CHANGED |
5064 MAC_STATUS_CFG_CHANGED));
5065 udelay(40);
5066 tg3_write_mem(tp,
5067 NIC_SRAM_FIRMWARE_MBOX,
5068 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
5069 }
5070
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005071 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00005072 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005073 if (tp->link_config.active_speed == SPEED_100 ||
5074 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005075 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
5076 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005077 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06005078 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
5079 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08005080 }
5081
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005082 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 return 0;
5085}
5086
5087struct tg3_fiber_aneginfo {
5088 int state;
5089#define ANEG_STATE_UNKNOWN 0
5090#define ANEG_STATE_AN_ENABLE 1
5091#define ANEG_STATE_RESTART_INIT 2
5092#define ANEG_STATE_RESTART 3
5093#define ANEG_STATE_DISABLE_LINK_OK 4
5094#define ANEG_STATE_ABILITY_DETECT_INIT 5
5095#define ANEG_STATE_ABILITY_DETECT 6
5096#define ANEG_STATE_ACK_DETECT_INIT 7
5097#define ANEG_STATE_ACK_DETECT 8
5098#define ANEG_STATE_COMPLETE_ACK_INIT 9
5099#define ANEG_STATE_COMPLETE_ACK 10
5100#define ANEG_STATE_IDLE_DETECT_INIT 11
5101#define ANEG_STATE_IDLE_DETECT 12
5102#define ANEG_STATE_LINK_OK 13
5103#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
5104#define ANEG_STATE_NEXT_PAGE_WAIT 15
5105
5106 u32 flags;
5107#define MR_AN_ENABLE 0x00000001
5108#define MR_RESTART_AN 0x00000002
5109#define MR_AN_COMPLETE 0x00000004
5110#define MR_PAGE_RX 0x00000008
5111#define MR_NP_LOADED 0x00000010
5112#define MR_TOGGLE_TX 0x00000020
5113#define MR_LP_ADV_FULL_DUPLEX 0x00000040
5114#define MR_LP_ADV_HALF_DUPLEX 0x00000080
5115#define MR_LP_ADV_SYM_PAUSE 0x00000100
5116#define MR_LP_ADV_ASYM_PAUSE 0x00000200
5117#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
5118#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
5119#define MR_LP_ADV_NEXT_PAGE 0x00001000
5120#define MR_TOGGLE_RX 0x00002000
5121#define MR_NP_RX 0x00004000
5122
5123#define MR_LINK_OK 0x80000000
5124
5125 unsigned long link_time, cur_time;
5126
5127 u32 ability_match_cfg;
5128 int ability_match_count;
5129
5130 char ability_match, idle_match, ack_match;
5131
5132 u32 txconfig, rxconfig;
5133#define ANEG_CFG_NP 0x00000080
5134#define ANEG_CFG_ACK 0x00000040
5135#define ANEG_CFG_RF2 0x00000020
5136#define ANEG_CFG_RF1 0x00000010
5137#define ANEG_CFG_PS2 0x00000001
5138#define ANEG_CFG_PS1 0x00008000
5139#define ANEG_CFG_HD 0x00004000
5140#define ANEG_CFG_FD 0x00002000
5141#define ANEG_CFG_INVAL 0x00001f06
5142
5143};
5144#define ANEG_OK 0
5145#define ANEG_DONE 1
5146#define ANEG_TIMER_ENAB 2
5147#define ANEG_FAILED -1
5148
5149#define ANEG_STATE_SETTLE_TIME 10000
5150
5151static int tg3_fiber_aneg_smachine(struct tg3 *tp,
5152 struct tg3_fiber_aneginfo *ap)
5153{
Matt Carlson5be73b42007-12-20 20:09:29 -08005154 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 unsigned long delta;
5156 u32 rx_cfg_reg;
5157 int ret;
5158
5159 if (ap->state == ANEG_STATE_UNKNOWN) {
5160 ap->rxconfig = 0;
5161 ap->link_time = 0;
5162 ap->cur_time = 0;
5163 ap->ability_match_cfg = 0;
5164 ap->ability_match_count = 0;
5165 ap->ability_match = 0;
5166 ap->idle_match = 0;
5167 ap->ack_match = 0;
5168 }
5169 ap->cur_time++;
5170
5171 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5172 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5173
5174 if (rx_cfg_reg != ap->ability_match_cfg) {
5175 ap->ability_match_cfg = rx_cfg_reg;
5176 ap->ability_match = 0;
5177 ap->ability_match_count = 0;
5178 } else {
5179 if (++ap->ability_match_count > 1) {
5180 ap->ability_match = 1;
5181 ap->ability_match_cfg = rx_cfg_reg;
5182 }
5183 }
5184 if (rx_cfg_reg & ANEG_CFG_ACK)
5185 ap->ack_match = 1;
5186 else
5187 ap->ack_match = 0;
5188
5189 ap->idle_match = 0;
5190 } else {
5191 ap->idle_match = 1;
5192 ap->ability_match_cfg = 0;
5193 ap->ability_match_count = 0;
5194 ap->ability_match = 0;
5195 ap->ack_match = 0;
5196
5197 rx_cfg_reg = 0;
5198 }
5199
5200 ap->rxconfig = rx_cfg_reg;
5201 ret = ANEG_OK;
5202
Matt Carlson33f401a2010-04-05 10:19:27 +00005203 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 case ANEG_STATE_UNKNOWN:
5205 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5206 ap->state = ANEG_STATE_AN_ENABLE;
5207
5208 /* fallthru */
5209 case ANEG_STATE_AN_ENABLE:
5210 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5211 if (ap->flags & MR_AN_ENABLE) {
5212 ap->link_time = 0;
5213 ap->cur_time = 0;
5214 ap->ability_match_cfg = 0;
5215 ap->ability_match_count = 0;
5216 ap->ability_match = 0;
5217 ap->idle_match = 0;
5218 ap->ack_match = 0;
5219
5220 ap->state = ANEG_STATE_RESTART_INIT;
5221 } else {
5222 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5223 }
5224 break;
5225
5226 case ANEG_STATE_RESTART_INIT:
5227 ap->link_time = ap->cur_time;
5228 ap->flags &= ~(MR_NP_LOADED);
5229 ap->txconfig = 0;
5230 tw32(MAC_TX_AUTO_NEG, 0);
5231 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5232 tw32_f(MAC_MODE, tp->mac_mode);
5233 udelay(40);
5234
5235 ret = ANEG_TIMER_ENAB;
5236 ap->state = ANEG_STATE_RESTART;
5237
5238 /* fallthru */
5239 case ANEG_STATE_RESTART:
5240 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005241 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005243 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 break;
5246
5247 case ANEG_STATE_DISABLE_LINK_OK:
5248 ret = ANEG_DONE;
5249 break;
5250
5251 case ANEG_STATE_ABILITY_DETECT_INIT:
5252 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005253 ap->txconfig = ANEG_CFG_FD;
5254 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5255 if (flowctrl & ADVERTISE_1000XPAUSE)
5256 ap->txconfig |= ANEG_CFG_PS1;
5257 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5258 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5260 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5261 tw32_f(MAC_MODE, tp->mac_mode);
5262 udelay(40);
5263
5264 ap->state = ANEG_STATE_ABILITY_DETECT;
5265 break;
5266
5267 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005268 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 break;
5271
5272 case ANEG_STATE_ACK_DETECT_INIT:
5273 ap->txconfig |= ANEG_CFG_ACK;
5274 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5275 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5276 tw32_f(MAC_MODE, tp->mac_mode);
5277 udelay(40);
5278
5279 ap->state = ANEG_STATE_ACK_DETECT;
5280
5281 /* fallthru */
5282 case ANEG_STATE_ACK_DETECT:
5283 if (ap->ack_match != 0) {
5284 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5285 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5286 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5287 } else {
5288 ap->state = ANEG_STATE_AN_ENABLE;
5289 }
5290 } else if (ap->ability_match != 0 &&
5291 ap->rxconfig == 0) {
5292 ap->state = ANEG_STATE_AN_ENABLE;
5293 }
5294 break;
5295
5296 case ANEG_STATE_COMPLETE_ACK_INIT:
5297 if (ap->rxconfig & ANEG_CFG_INVAL) {
5298 ret = ANEG_FAILED;
5299 break;
5300 }
5301 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5302 MR_LP_ADV_HALF_DUPLEX |
5303 MR_LP_ADV_SYM_PAUSE |
5304 MR_LP_ADV_ASYM_PAUSE |
5305 MR_LP_ADV_REMOTE_FAULT1 |
5306 MR_LP_ADV_REMOTE_FAULT2 |
5307 MR_LP_ADV_NEXT_PAGE |
5308 MR_TOGGLE_RX |
5309 MR_NP_RX);
5310 if (ap->rxconfig & ANEG_CFG_FD)
5311 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5312 if (ap->rxconfig & ANEG_CFG_HD)
5313 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5314 if (ap->rxconfig & ANEG_CFG_PS1)
5315 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5316 if (ap->rxconfig & ANEG_CFG_PS2)
5317 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5318 if (ap->rxconfig & ANEG_CFG_RF1)
5319 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5320 if (ap->rxconfig & ANEG_CFG_RF2)
5321 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5322 if (ap->rxconfig & ANEG_CFG_NP)
5323 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5324
5325 ap->link_time = ap->cur_time;
5326
5327 ap->flags ^= (MR_TOGGLE_TX);
5328 if (ap->rxconfig & 0x0008)
5329 ap->flags |= MR_TOGGLE_RX;
5330 if (ap->rxconfig & ANEG_CFG_NP)
5331 ap->flags |= MR_NP_RX;
5332 ap->flags |= MR_PAGE_RX;
5333
5334 ap->state = ANEG_STATE_COMPLETE_ACK;
5335 ret = ANEG_TIMER_ENAB;
5336 break;
5337
5338 case ANEG_STATE_COMPLETE_ACK:
5339 if (ap->ability_match != 0 &&
5340 ap->rxconfig == 0) {
5341 ap->state = ANEG_STATE_AN_ENABLE;
5342 break;
5343 }
5344 delta = ap->cur_time - ap->link_time;
5345 if (delta > ANEG_STATE_SETTLE_TIME) {
5346 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5347 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5348 } else {
5349 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5350 !(ap->flags & MR_NP_RX)) {
5351 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5352 } else {
5353 ret = ANEG_FAILED;
5354 }
5355 }
5356 }
5357 break;
5358
5359 case ANEG_STATE_IDLE_DETECT_INIT:
5360 ap->link_time = ap->cur_time;
5361 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5362 tw32_f(MAC_MODE, tp->mac_mode);
5363 udelay(40);
5364
5365 ap->state = ANEG_STATE_IDLE_DETECT;
5366 ret = ANEG_TIMER_ENAB;
5367 break;
5368
5369 case ANEG_STATE_IDLE_DETECT:
5370 if (ap->ability_match != 0 &&
5371 ap->rxconfig == 0) {
5372 ap->state = ANEG_STATE_AN_ENABLE;
5373 break;
5374 }
5375 delta = ap->cur_time - ap->link_time;
5376 if (delta > ANEG_STATE_SETTLE_TIME) {
5377 /* XXX another gem from the Broadcom driver :( */
5378 ap->state = ANEG_STATE_LINK_OK;
5379 }
5380 break;
5381
5382 case ANEG_STATE_LINK_OK:
5383 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5384 ret = ANEG_DONE;
5385 break;
5386
5387 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5388 /* ??? unimplemented */
5389 break;
5390
5391 case ANEG_STATE_NEXT_PAGE_WAIT:
5392 /* ??? unimplemented */
5393 break;
5394
5395 default:
5396 ret = ANEG_FAILED;
5397 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
5400 return ret;
5401}
5402
Matt Carlson5be73b42007-12-20 20:09:29 -08005403static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
5405 int res = 0;
5406 struct tg3_fiber_aneginfo aninfo;
5407 int status = ANEG_FAILED;
5408 unsigned int tick;
5409 u32 tmp;
5410
5411 tw32_f(MAC_TX_AUTO_NEG, 0);
5412
5413 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5414 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5415 udelay(40);
5416
5417 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5418 udelay(40);
5419
5420 memset(&aninfo, 0, sizeof(aninfo));
5421 aninfo.flags |= MR_AN_ENABLE;
5422 aninfo.state = ANEG_STATE_UNKNOWN;
5423 aninfo.cur_time = 0;
5424 tick = 0;
5425 while (++tick < 195000) {
5426 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5427 if (status == ANEG_DONE || status == ANEG_FAILED)
5428 break;
5429
5430 udelay(1);
5431 }
5432
5433 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5434 tw32_f(MAC_MODE, tp->mac_mode);
5435 udelay(40);
5436
Matt Carlson5be73b42007-12-20 20:09:29 -08005437 *txflags = aninfo.txconfig;
5438 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
5440 if (status == ANEG_DONE &&
5441 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5442 MR_LP_ADV_FULL_DUPLEX)))
5443 res = 1;
5444
5445 return res;
5446}
5447
5448static void tg3_init_bcm8002(struct tg3 *tp)
5449{
5450 u32 mac_status = tr32(MAC_STATUS);
5451 int i;
5452
5453 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005454 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 !(mac_status & MAC_STATUS_PCS_SYNCED))
5456 return;
5457
5458 /* Set PLL lock range. */
5459 tg3_writephy(tp, 0x16, 0x8007);
5460
5461 /* SW reset */
5462 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5463
5464 /* Wait for reset to complete. */
5465 /* XXX schedule_timeout() ... */
5466 for (i = 0; i < 500; i++)
5467 udelay(10);
5468
5469 /* Config mode; select PMA/Ch 1 regs. */
5470 tg3_writephy(tp, 0x10, 0x8411);
5471
5472 /* Enable auto-lock and comdet, select txclk for tx. */
5473 tg3_writephy(tp, 0x11, 0x0a10);
5474
5475 tg3_writephy(tp, 0x18, 0x00a0);
5476 tg3_writephy(tp, 0x16, 0x41ff);
5477
5478 /* Assert and deassert POR. */
5479 tg3_writephy(tp, 0x13, 0x0400);
5480 udelay(40);
5481 tg3_writephy(tp, 0x13, 0x0000);
5482
5483 tg3_writephy(tp, 0x11, 0x0a50);
5484 udelay(40);
5485 tg3_writephy(tp, 0x11, 0x0a10);
5486
5487 /* Wait for signal to stabilize */
5488 /* XXX schedule_timeout() ... */
5489 for (i = 0; i < 15000; i++)
5490 udelay(10);
5491
5492 /* Deselect the channel register so we can read the PHYID
5493 * later.
5494 */
5495 tg3_writephy(tp, 0x10, 0x8011);
5496}
5497
Joe Perches953c96e2013-04-09 10:18:14 +00005498static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005500 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005501 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 u32 sg_dig_ctrl, sg_dig_status;
5503 u32 serdes_cfg, expected_sg_dig_ctrl;
5504 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 serdes_cfg = 0;
5507 expected_sg_dig_ctrl = 0;
5508 workaround = 0;
5509 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005510 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Joe Perches41535772013-02-16 11:20:04 +00005512 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5513 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 workaround = 1;
5515 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5516 port_a = 0;
5517
5518 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5519 /* preserve bits 20-23 for voltage regulator */
5520 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5521 }
5522
5523 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5524
5525 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005526 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 if (workaround) {
5528 u32 val = serdes_cfg;
5529
5530 if (port_a)
5531 val |= 0xc010000;
5532 else
5533 val |= 0x4010000;
5534 tw32_f(MAC_SERDES_CFG, val);
5535 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005536
5537 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 }
5539 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5540 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005541 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
5543 goto out;
5544 }
5545
5546 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005547 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Matt Carlson82cd3d12007-12-20 20:09:00 -08005549 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5550 if (flowctrl & ADVERTISE_1000XPAUSE)
5551 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5552 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5553 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005556 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005557 tp->serdes_counter &&
5558 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5559 MAC_STATUS_RCVD_CFG)) ==
5560 MAC_STATUS_PCS_SYNCED)) {
5561 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005562 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005563 goto out;
5564 }
5565restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (workaround)
5567 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005568 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 udelay(5);
5570 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5571
Michael Chan3d3ebe72006-09-27 15:59:15 -07005572 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005573 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5575 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005576 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 mac_status = tr32(MAC_STATUS);
5578
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005579 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005581 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Matt Carlson82cd3d12007-12-20 20:09:00 -08005583 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5584 local_adv |= ADVERTISE_1000XPAUSE;
5585 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5586 local_adv |= ADVERTISE_1000XPSE_ASYM;
5587
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005588 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005589 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005590 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005591 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Matt Carlson859edb22011-12-08 14:40:16 +00005593 tp->link_config.rmt_adv =
5594 mii_adv_to_ethtool_adv_x(remote_adv);
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005597 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005598 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005599 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005600 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005601 if (tp->serdes_counter)
5602 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 else {
5604 if (workaround) {
5605 u32 val = serdes_cfg;
5606
5607 if (port_a)
5608 val |= 0xc010000;
5609 else
5610 val |= 0x4010000;
5611
5612 tw32_f(MAC_SERDES_CFG, val);
5613 }
5614
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005615 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 udelay(40);
5617
5618 /* Link parallel detection - link is up */
5619 /* only if we have PCS_SYNC and not */
5620 /* receiving config code words */
5621 mac_status = tr32(MAC_STATUS);
5622 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5623 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5624 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005625 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005626 tp->phy_flags |=
5627 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005628 tp->serdes_counter =
5629 SERDES_PARALLEL_DET_TIMEOUT;
5630 } else
5631 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 }
5633 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005634 } else {
5635 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005636 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
5638
5639out:
5640 return current_link_up;
5641}
5642
Joe Perches953c96e2013-04-09 10:18:14 +00005643static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644{
Joe Perches953c96e2013-04-09 10:18:14 +00005645 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005647 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005651 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005653
Matt Carlson5be73b42007-12-20 20:09:29 -08005654 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5655 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Matt Carlson5be73b42007-12-20 20:09:29 -08005657 if (txflags & ANEG_CFG_PS1)
5658 local_adv |= ADVERTISE_1000XPAUSE;
5659 if (txflags & ANEG_CFG_PS2)
5660 local_adv |= ADVERTISE_1000XPSE_ASYM;
5661
5662 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5663 remote_adv |= LPA_1000XPAUSE;
5664 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5665 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Matt Carlson859edb22011-12-08 14:40:16 +00005667 tp->link_config.rmt_adv =
5668 mii_adv_to_ethtool_adv_x(remote_adv);
5669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 tg3_setup_flow_control(tp, local_adv, remote_adv);
5671
Joe Perches953c96e2013-04-09 10:18:14 +00005672 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674 for (i = 0; i < 30; i++) {
5675 udelay(20);
5676 tw32_f(MAC_STATUS,
5677 (MAC_STATUS_SYNC_CHANGED |
5678 MAC_STATUS_CFG_CHANGED));
5679 udelay(40);
5680 if ((tr32(MAC_STATUS) &
5681 (MAC_STATUS_SYNC_CHANGED |
5682 MAC_STATUS_CFG_CHANGED)) == 0)
5683 break;
5684 }
5685
5686 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005687 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5689 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005690 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005692 tg3_setup_flow_control(tp, 0, 0);
5693
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005695 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5698 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005699
5700 tw32_f(MAC_MODE, tp->mac_mode);
5701 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 }
5703
5704out:
5705 return current_link_up;
5706}
5707
Joe Perches953c96e2013-04-09 10:18:14 +00005708static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 u32 orig_pause_cfg;
5711 u16 orig_active_speed;
5712 u8 orig_active_duplex;
5713 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005714 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 int i;
5716
Matt Carlson8d018622007-12-20 20:05:44 -08005717 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 orig_active_speed = tp->link_config.active_speed;
5719 orig_active_duplex = tp->link_config.active_duplex;
5720
Joe Perches63c3a662011-04-26 08:12:10 +00005721 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005722 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005723 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 mac_status = tr32(MAC_STATUS);
5725 mac_status &= (MAC_STATUS_PCS_SYNCED |
5726 MAC_STATUS_SIGNAL_DET |
5727 MAC_STATUS_CFG_CHANGED |
5728 MAC_STATUS_RCVD_CFG);
5729 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5730 MAC_STATUS_SIGNAL_DET)) {
5731 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5732 MAC_STATUS_CFG_CHANGED));
5733 return 0;
5734 }
5735 }
5736
5737 tw32_f(MAC_TX_AUTO_NEG, 0);
5738
5739 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5740 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5741 tw32_f(MAC_MODE, tp->mac_mode);
5742 udelay(40);
5743
Matt Carlson79eb6902010-02-17 15:17:03 +00005744 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 tg3_init_bcm8002(tp);
5746
5747 /* Enable link change event even when serdes polling. */
5748 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5749 udelay(40);
5750
Joe Perches953c96e2013-04-09 10:18:14 +00005751 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005752 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 mac_status = tr32(MAC_STATUS);
5754
Joe Perches63c3a662011-04-26 08:12:10 +00005755 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5757 else
5758 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5759
Matt Carlson898a56f2009-08-28 14:02:40 +00005760 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005762 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
5764 for (i = 0; i < 100; i++) {
5765 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5766 MAC_STATUS_CFG_CHANGED));
5767 udelay(5);
5768 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005769 MAC_STATUS_CFG_CHANGED |
5770 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 break;
5772 }
5773
5774 mac_status = tr32(MAC_STATUS);
5775 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005776 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005777 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5778 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 tw32_f(MAC_MODE, (tp->mac_mode |
5780 MAC_MODE_SEND_CONFIGS));
5781 udelay(1);
5782 tw32_f(MAC_MODE, tp->mac_mode);
5783 }
5784 }
5785
Joe Perches953c96e2013-04-09 10:18:14 +00005786 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 tp->link_config.active_speed = SPEED_1000;
5788 tp->link_config.active_duplex = DUPLEX_FULL;
5789 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5790 LED_CTRL_LNKLED_OVERRIDE |
5791 LED_CTRL_1000MBPS_ON));
5792 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005793 tp->link_config.active_speed = SPEED_UNKNOWN;
5794 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5796 LED_CTRL_LNKLED_OVERRIDE |
5797 LED_CTRL_TRAFFIC_OVERRIDE));
5798 }
5799
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005800 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005801 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 if (orig_pause_cfg != now_pause_cfg ||
5803 orig_active_speed != tp->link_config.active_speed ||
5804 orig_active_duplex != tp->link_config.active_duplex)
5805 tg3_link_report(tp);
5806 }
5807
5808 return 0;
5809}
5810
Joe Perches953c96e2013-04-09 10:18:14 +00005811static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005812{
Joe Perches953c96e2013-04-09 10:18:14 +00005813 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005814 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005815 u16 current_speed = SPEED_UNKNOWN;
5816 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005817 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005818 u32 local_adv, remote_adv, sgsr;
5819
5820 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5821 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5822 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5823 (sgsr & SERDES_TG3_SGMII_MODE)) {
5824
5825 if (force_reset)
5826 tg3_phy_reset(tp);
5827
5828 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5829
5830 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5831 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5832 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005833 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005834 if (sgsr & SERDES_TG3_SPEED_1000) {
5835 current_speed = SPEED_1000;
5836 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5837 } else if (sgsr & SERDES_TG3_SPEED_100) {
5838 current_speed = SPEED_100;
5839 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5840 } else {
5841 current_speed = SPEED_10;
5842 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5843 }
5844
5845 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5846 current_duplex = DUPLEX_FULL;
5847 else
5848 current_duplex = DUPLEX_HALF;
5849 }
5850
5851 tw32_f(MAC_MODE, tp->mac_mode);
5852 udelay(40);
5853
5854 tg3_clear_mac_status(tp);
5855
5856 goto fiber_setup_done;
5857 }
Michael Chan747e8f82005-07-25 12:33:22 -07005858
5859 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5860 tw32_f(MAC_MODE, tp->mac_mode);
5861 udelay(40);
5862
Michael Chan3310e242013-04-09 08:48:05 +00005863 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005864
5865 if (force_reset)
5866 tg3_phy_reset(tp);
5867
Matt Carlson859edb22011-12-08 14:40:16 +00005868 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005869
5870 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5871 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005872 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005873 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5874 bmsr |= BMSR_LSTATUS;
5875 else
5876 bmsr &= ~BMSR_LSTATUS;
5877 }
Michael Chan747e8f82005-07-25 12:33:22 -07005878
5879 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5880
5881 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005882 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005883 /* do nothing, just check for link up at the end */
5884 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005885 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005886
5887 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005888 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5889 ADVERTISE_1000XPAUSE |
5890 ADVERTISE_1000XPSE_ASYM |
5891 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005892
Matt Carlson28011cf2011-11-16 18:36:59 -05005893 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005894 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005895
Matt Carlson28011cf2011-11-16 18:36:59 -05005896 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5897 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005898 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5899 tg3_writephy(tp, MII_BMCR, bmcr);
5900
5901 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005902 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005903 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005904
5905 return err;
5906 }
5907 } else {
5908 u32 new_bmcr;
5909
5910 bmcr &= ~BMCR_SPEED1000;
5911 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5912
5913 if (tp->link_config.duplex == DUPLEX_FULL)
5914 new_bmcr |= BMCR_FULLDPLX;
5915
5916 if (new_bmcr != bmcr) {
5917 /* BMCR_SPEED1000 is a reserved bit that needs
5918 * to be set on write.
5919 */
5920 new_bmcr |= BMCR_SPEED1000;
5921
5922 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005923 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005924 u32 adv;
5925
5926 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5927 adv &= ~(ADVERTISE_1000XFULL |
5928 ADVERTISE_1000XHALF |
5929 ADVERTISE_SLCT);
5930 tg3_writephy(tp, MII_ADVERTISE, adv);
5931 tg3_writephy(tp, MII_BMCR, bmcr |
5932 BMCR_ANRESTART |
5933 BMCR_ANENABLE);
5934 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005935 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005936 }
5937 tg3_writephy(tp, MII_BMCR, new_bmcr);
5938 bmcr = new_bmcr;
5939 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5940 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005941 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005942 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5943 bmsr |= BMSR_LSTATUS;
5944 else
5945 bmsr &= ~BMSR_LSTATUS;
5946 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005947 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005948 }
5949 }
5950
5951 if (bmsr & BMSR_LSTATUS) {
5952 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005953 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005954 if (bmcr & BMCR_FULLDPLX)
5955 current_duplex = DUPLEX_FULL;
5956 else
5957 current_duplex = DUPLEX_HALF;
5958
Matt Carlsonef167e22007-12-20 20:10:01 -08005959 local_adv = 0;
5960 remote_adv = 0;
5961
Michael Chan747e8f82005-07-25 12:33:22 -07005962 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005963 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005964
5965 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5966 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5967 common = local_adv & remote_adv;
5968 if (common & (ADVERTISE_1000XHALF |
5969 ADVERTISE_1000XFULL)) {
5970 if (common & ADVERTISE_1000XFULL)
5971 current_duplex = DUPLEX_FULL;
5972 else
5973 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005974
5975 tp->link_config.rmt_adv =
5976 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005977 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005978 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005979 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005980 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005981 }
Michael Chan747e8f82005-07-25 12:33:22 -07005982 }
5983 }
5984
Michael Chan85730a62013-04-09 08:48:06 +00005985fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005986 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005987 tg3_setup_flow_control(tp, local_adv, remote_adv);
5988
Michael Chan747e8f82005-07-25 12:33:22 -07005989 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5990 if (tp->link_config.active_duplex == DUPLEX_HALF)
5991 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5992
5993 tw32_f(MAC_MODE, tp->mac_mode);
5994 udelay(40);
5995
5996 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5997
5998 tp->link_config.active_speed = current_speed;
5999 tp->link_config.active_duplex = current_duplex;
6000
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006001 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07006002 return err;
6003}
6004
6005static void tg3_serdes_parallel_detect(struct tg3 *tp)
6006{
Michael Chan3d3ebe72006-09-27 15:59:15 -07006007 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07006008 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07006009 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07006010 return;
6011 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006012
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006013 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07006014 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
6015 u32 bmcr;
6016
6017 tg3_readphy(tp, MII_BMCR, &bmcr);
6018 if (bmcr & BMCR_ANENABLE) {
6019 u32 phy1, phy2;
6020
6021 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006022 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
6023 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07006024
6025 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006026 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6027 MII_TG3_DSP_EXP1_INT_STAT);
6028 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
6029 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006030
6031 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
6032 /* We have signal detect and not receiving
6033 * config code words, link is up by parallel
6034 * detection.
6035 */
6036
6037 bmcr &= ~BMCR_ANENABLE;
6038 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
6039 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006040 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006041 }
6042 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006043 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00006044 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006045 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07006046 u32 phy2;
6047
6048 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00006049 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
6050 MII_TG3_DSP_EXP1_INT_STAT);
6051 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07006052 if (phy2 & 0x20) {
6053 u32 bmcr;
6054
6055 /* Config code words received, turn on autoneg. */
6056 tg3_readphy(tp, MII_BMCR, &bmcr);
6057 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
6058
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006059 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07006060
6061 }
6062 }
6063}
6064
Joe Perches953c96e2013-04-09 10:18:14 +00006065static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Matt Carlsonf2096f92011-04-05 14:22:48 +00006067 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 int err;
6069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006070 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00006072 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07006073 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00006074 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Joe Perches41535772013-02-16 11:20:04 +00006077 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006078 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08006079
6080 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
6081 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
6082 scale = 65;
6083 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
6084 scale = 6;
6085 else
6086 scale = 12;
6087
6088 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
6089 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
6090 tw32(GRC_MISC_CFG, val);
6091 }
6092
Matt Carlsonf2096f92011-04-05 14:22:48 +00006093 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
6094 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00006095 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
6096 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006097 val |= tr32(MAC_TX_LENGTHS) &
6098 (TX_LENGTHS_JMB_FRM_LEN_MSK |
6099 TX_LENGTHS_CNT_DWN_VAL_MSK);
6100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 if (tp->link_config.active_speed == SPEED_1000 &&
6102 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00006103 tw32(MAC_TX_LENGTHS, val |
6104 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00006106 tw32(MAC_TX_LENGTHS, val |
6107 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Joe Perches63c3a662011-04-26 08:12:10 +00006109 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006110 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07006112 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 } else {
6114 tw32(HOSTCC_STAT_COAL_TICKS, 0);
6115 }
6116 }
6117
Joe Perches63c3a662011-04-26 08:12:10 +00006118 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00006119 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006120 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07006121 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
6122 tp->pwrmgmt_thresh;
6123 else
6124 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
6125 tw32(PCIE_PWR_MGMT_THRESH, val);
6126 }
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 return err;
6129}
6130
Matt Carlsonbe947302012-12-03 19:36:57 +00006131/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00006132static u64 tg3_refclk_read(struct tg3 *tp)
6133{
6134 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
6135 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
6136}
6137
6138/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00006139static void tg3_refclk_write(struct tg3 *tp, u64 newval)
6140{
Nithin Sujir92e64572013-07-29 13:58:38 -07006141 u32 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6142
6143 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_STOP);
Matt Carlsonbe947302012-12-03 19:36:57 +00006144 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
6145 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
Nithin Sujir92e64572013-07-29 13:58:38 -07006146 tw32_f(TG3_EAV_REF_CLCK_CTL, clock_ctl | TG3_EAV_REF_CLCK_CTL_RESUME);
Matt Carlsonbe947302012-12-03 19:36:57 +00006147}
6148
Matt Carlson7d41e492012-12-03 19:36:58 +00006149static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
6150static inline void tg3_full_unlock(struct tg3 *tp);
6151static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
6152{
6153 struct tg3 *tp = netdev_priv(dev);
6154
6155 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
6156 SOF_TIMESTAMPING_RX_SOFTWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006157 SOF_TIMESTAMPING_SOFTWARE;
6158
6159 if (tg3_flag(tp, PTP_CAPABLE)) {
Flavio Leitner32e19272013-04-30 07:20:34 +00006160 info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
Flavio Leitnerf233a972013-04-29 07:08:07 +00006161 SOF_TIMESTAMPING_RX_HARDWARE |
6162 SOF_TIMESTAMPING_RAW_HARDWARE;
6163 }
Matt Carlson7d41e492012-12-03 19:36:58 +00006164
6165 if (tp->ptp_clock)
6166 info->phc_index = ptp_clock_index(tp->ptp_clock);
6167 else
6168 info->phc_index = -1;
6169
6170 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6171
6172 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6173 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6174 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6175 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6176 return 0;
6177}
6178
6179static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6180{
6181 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6182 bool neg_adj = false;
6183 u32 correction = 0;
6184
6185 if (ppb < 0) {
6186 neg_adj = true;
6187 ppb = -ppb;
6188 }
6189
6190 /* Frequency adjustment is performed using hardware with a 24 bit
6191 * accumulator and a programmable correction value. On each clk, the
6192 * correction value gets added to the accumulator and when it
6193 * overflows, the time counter is incremented/decremented.
6194 *
6195 * So conversion from ppb to correction value is
6196 * ppb * (1 << 24) / 1000000000
6197 */
6198 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6199 TG3_EAV_REF_CLK_CORRECT_MASK;
6200
6201 tg3_full_lock(tp, 0);
6202
6203 if (correction)
6204 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6205 TG3_EAV_REF_CLK_CORRECT_EN |
6206 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6207 else
6208 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6209
6210 tg3_full_unlock(tp);
6211
6212 return 0;
6213}
6214
6215static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6216{
6217 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6218
6219 tg3_full_lock(tp, 0);
6220 tp->ptp_adjust += delta;
6221 tg3_full_unlock(tp);
6222
6223 return 0;
6224}
6225
6226static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6227{
6228 u64 ns;
6229 u32 remainder;
6230 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6231
6232 tg3_full_lock(tp, 0);
6233 ns = tg3_refclk_read(tp);
6234 ns += tp->ptp_adjust;
6235 tg3_full_unlock(tp);
6236
6237 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6238 ts->tv_nsec = remainder;
6239
6240 return 0;
6241}
6242
6243static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6244 const struct timespec *ts)
6245{
6246 u64 ns;
6247 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6248
6249 ns = timespec_to_ns(ts);
6250
6251 tg3_full_lock(tp, 0);
6252 tg3_refclk_write(tp, ns);
6253 tp->ptp_adjust = 0;
6254 tg3_full_unlock(tp);
6255
6256 return 0;
6257}
6258
6259static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6260 struct ptp_clock_request *rq, int on)
6261{
Nithin Sujir92e64572013-07-29 13:58:38 -07006262 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6263 u32 clock_ctl;
6264 int rval = 0;
6265
6266 switch (rq->type) {
6267 case PTP_CLK_REQ_PEROUT:
6268 if (rq->perout.index != 0)
6269 return -EINVAL;
6270
6271 tg3_full_lock(tp, 0);
6272 clock_ctl = tr32(TG3_EAV_REF_CLCK_CTL);
6273 clock_ctl &= ~TG3_EAV_CTL_TSYNC_GPIO_MASK;
6274
6275 if (on) {
6276 u64 nsec;
6277
6278 nsec = rq->perout.start.sec * 1000000000ULL +
6279 rq->perout.start.nsec;
6280
6281 if (rq->perout.period.sec || rq->perout.period.nsec) {
6282 netdev_warn(tp->dev,
6283 "Device supports only a one-shot timesync output, period must be 0\n");
6284 rval = -EINVAL;
6285 goto err_out;
6286 }
6287
6288 if (nsec & (1ULL << 63)) {
6289 netdev_warn(tp->dev,
6290 "Start value (nsec) is over limit. Maximum size of start is only 63 bits\n");
6291 rval = -EINVAL;
6292 goto err_out;
6293 }
6294
6295 tw32(TG3_EAV_WATCHDOG0_LSB, (nsec & 0xffffffff));
6296 tw32(TG3_EAV_WATCHDOG0_MSB,
6297 TG3_EAV_WATCHDOG0_EN |
6298 ((nsec >> 32) & TG3_EAV_WATCHDOG_MSB_MASK));
6299
6300 tw32(TG3_EAV_REF_CLCK_CTL,
6301 clock_ctl | TG3_EAV_CTL_TSYNC_WDOG0);
6302 } else {
6303 tw32(TG3_EAV_WATCHDOG0_MSB, 0);
6304 tw32(TG3_EAV_REF_CLCK_CTL, clock_ctl);
6305 }
6306
6307err_out:
6308 tg3_full_unlock(tp);
6309 return rval;
6310
6311 default:
6312 break;
6313 }
6314
Matt Carlson7d41e492012-12-03 19:36:58 +00006315 return -EOPNOTSUPP;
6316}
6317
6318static const struct ptp_clock_info tg3_ptp_caps = {
6319 .owner = THIS_MODULE,
6320 .name = "tg3 clock",
6321 .max_adj = 250000000,
6322 .n_alarm = 0,
6323 .n_ext_ts = 0,
Nithin Sujir92e64572013-07-29 13:58:38 -07006324 .n_per_out = 1,
Matt Carlson7d41e492012-12-03 19:36:58 +00006325 .pps = 0,
6326 .adjfreq = tg3_ptp_adjfreq,
6327 .adjtime = tg3_ptp_adjtime,
6328 .gettime = tg3_ptp_gettime,
6329 .settime = tg3_ptp_settime,
6330 .enable = tg3_ptp_enable,
6331};
6332
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006333static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6334 struct skb_shared_hwtstamps *timestamp)
6335{
6336 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6337 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6338 tp->ptp_adjust);
6339}
6340
Matt Carlsonbe947302012-12-03 19:36:57 +00006341/* tp->lock must be held */
6342static void tg3_ptp_init(struct tg3 *tp)
6343{
6344 if (!tg3_flag(tp, PTP_CAPABLE))
6345 return;
6346
6347 /* Initialize the hardware clock to the system time. */
6348 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6349 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006350 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006351}
6352
6353/* tp->lock must be held */
6354static void tg3_ptp_resume(struct tg3 *tp)
6355{
6356 if (!tg3_flag(tp, PTP_CAPABLE))
6357 return;
6358
6359 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6360 tp->ptp_adjust = 0;
6361}
6362
6363static void tg3_ptp_fini(struct tg3 *tp)
6364{
6365 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6366 return;
6367
Matt Carlson7d41e492012-12-03 19:36:58 +00006368 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006369 tp->ptp_clock = NULL;
6370 tp->ptp_adjust = 0;
6371}
6372
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006373static inline int tg3_irq_sync(struct tg3 *tp)
6374{
6375 return tp->irq_sync;
6376}
6377
Matt Carlson97bd8e42011-04-13 11:05:04 +00006378static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6379{
6380 int i;
6381
6382 dst = (u32 *)((u8 *)dst + off);
6383 for (i = 0; i < len; i += sizeof(u32))
6384 *dst++ = tr32(off + i);
6385}
6386
6387static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6388{
6389 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6390 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6391 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6392 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6393 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6394 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6395 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6396 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6397 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6398 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6399 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6400 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6401 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6402 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6403 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6404 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6405 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6406 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6407 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6408
Joe Perches63c3a662011-04-26 08:12:10 +00006409 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006410 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6411
6412 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6413 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6414 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6415 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6416 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6417 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6418 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6419 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6420
Joe Perches63c3a662011-04-26 08:12:10 +00006421 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006422 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6423 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6424 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6425 }
6426
6427 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6428 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6429 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6430 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6431 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6432
Joe Perches63c3a662011-04-26 08:12:10 +00006433 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006434 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6435}
6436
6437static void tg3_dump_state(struct tg3 *tp)
6438{
6439 int i;
6440 u32 *regs;
6441
6442 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006443 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006444 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006445
Joe Perches63c3a662011-04-26 08:12:10 +00006446 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006447 /* Read up to but not including private PCI registers */
6448 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6449 regs[i / sizeof(u32)] = tr32(i);
6450 } else
6451 tg3_dump_legacy_regs(tp, regs);
6452
6453 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6454 if (!regs[i + 0] && !regs[i + 1] &&
6455 !regs[i + 2] && !regs[i + 3])
6456 continue;
6457
6458 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6459 i * 4,
6460 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6461 }
6462
6463 kfree(regs);
6464
6465 for (i = 0; i < tp->irq_cnt; i++) {
6466 struct tg3_napi *tnapi = &tp->napi[i];
6467
6468 /* SW status block */
6469 netdev_err(tp->dev,
6470 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6471 i,
6472 tnapi->hw_status->status,
6473 tnapi->hw_status->status_tag,
6474 tnapi->hw_status->rx_jumbo_consumer,
6475 tnapi->hw_status->rx_consumer,
6476 tnapi->hw_status->rx_mini_consumer,
6477 tnapi->hw_status->idx[0].rx_producer,
6478 tnapi->hw_status->idx[0].tx_consumer);
6479
6480 netdev_err(tp->dev,
6481 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6482 i,
6483 tnapi->last_tag, tnapi->last_irq_tag,
6484 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6485 tnapi->rx_rcb_ptr,
6486 tnapi->prodring.rx_std_prod_idx,
6487 tnapi->prodring.rx_std_cons_idx,
6488 tnapi->prodring.rx_jmb_prod_idx,
6489 tnapi->prodring.rx_jmb_cons_idx);
6490 }
6491}
6492
Michael Chandf3e6542006-05-26 17:48:07 -07006493/* This is called whenever we suspect that the system chipset is re-
6494 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6495 * is bogus tx completions. We try to recover by setting the
6496 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6497 * in the workqueue.
6498 */
6499static void tg3_tx_recover(struct tg3 *tp)
6500{
Joe Perches63c3a662011-04-26 08:12:10 +00006501 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006502 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6503
Matt Carlson5129c3a2010-04-05 10:19:23 +00006504 netdev_warn(tp->dev,
6505 "The system may be re-ordering memory-mapped I/O "
6506 "cycles to the network device, attempting to recover. "
6507 "Please report the problem to the driver maintainer "
6508 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006509
Joe Perches63c3a662011-04-26 08:12:10 +00006510 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006511}
6512
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006513static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006514{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006515 /* Tell compiler to fetch tx indices from memory. */
6516 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006517 return tnapi->tx_pending -
6518 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006519}
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521/* Tigon3 never reports partial packet sends. So we do not
6522 * need special logic to handle SKBs that have not had all
6523 * of their frags sent yet, like SunGEM does.
6524 */
Matt Carlson17375d22009-08-28 14:02:18 +00006525static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526{
Matt Carlson17375d22009-08-28 14:02:18 +00006527 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006528 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006529 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006530 struct netdev_queue *txq;
6531 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006532 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006533
Joe Perches63c3a662011-04-26 08:12:10 +00006534 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006535 index--;
6536
6537 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
6539 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006540 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006542 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
Michael Chandf3e6542006-05-26 17:48:07 -07006544 if (unlikely(skb == NULL)) {
6545 tg3_tx_recover(tp);
6546 return;
6547 }
6548
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006549 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6550 struct skb_shared_hwtstamps timestamp;
6551 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6552 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6553
6554 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6555
6556 skb_tstamp_tx(skb, &timestamp);
6557 }
6558
Alexander Duyckf4188d82009-12-02 16:48:38 +00006559 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006560 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006561 skb_headlen(skb),
6562 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
6564 ri->skb = NULL;
6565
Matt Carlsone01ee142011-07-27 14:20:50 +00006566 while (ri->fragmented) {
6567 ri->fragmented = false;
6568 sw_idx = NEXT_TX(sw_idx);
6569 ri = &tnapi->tx_buffers[sw_idx];
6570 }
6571
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 sw_idx = NEXT_TX(sw_idx);
6573
6574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006575 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006576 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6577 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006578
6579 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006580 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006581 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006582 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006583
6584 while (ri->fragmented) {
6585 ri->fragmented = false;
6586 sw_idx = NEXT_TX(sw_idx);
6587 ri = &tnapi->tx_buffers[sw_idx];
6588 }
6589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 sw_idx = NEXT_TX(sw_idx);
6591 }
6592
Tom Herbert298376d2011-11-28 16:33:30 +00006593 pkts_compl++;
6594 bytes_compl += skb->len;
6595
David S. Millerf47c11e2005-06-24 20:18:35 -07006596 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006597
6598 if (unlikely(tx_bug)) {
6599 tg3_tx_recover(tp);
6600 return;
6601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 }
6603
Tom Herbert5cb917b2012-03-05 19:53:50 +00006604 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006605
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006606 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
Michael Chan1b2a7202006-08-07 21:46:02 -07006608 /* Need to make the tx_cons update visible to tg3_start_xmit()
6609 * before checking for netif_queue_stopped(). Without the
6610 * memory barrier, there is a small possibility that tg3_start_xmit()
6611 * will miss it and cause the queue to be stopped forever.
6612 */
6613 smp_mb();
6614
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006615 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006616 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006617 __netif_tx_lock(txq, smp_processor_id());
6618 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006619 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006620 netif_tx_wake_queue(txq);
6621 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006625static void tg3_frag_free(bool is_frag, void *data)
6626{
6627 if (is_frag)
6628 put_page(virt_to_head_page(data));
6629 else
6630 kfree(data);
6631}
6632
Eric Dumazet9205fd92011-11-18 06:47:01 +00006633static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006634{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006635 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6636 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6637
Eric Dumazet9205fd92011-11-18 06:47:01 +00006638 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006639 return;
6640
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006641 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006642 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006643 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006644 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006645}
6646
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648/* Returns size of skb allocated or < 0 on error.
6649 *
6650 * We only need to fill in the address because the other members
6651 * of the RX descriptor are invariant, see tg3_init_rings.
6652 *
6653 * Note the purposeful assymetry of cpu vs. chip accesses. For
6654 * posting buffers we only dirty the first cache line of the RX
6655 * descriptor (containing the address). Whereas for the RX status
6656 * buffers the cpu only reads the last cacheline of the RX descriptor
6657 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6658 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006659static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006660 u32 opaque_key, u32 dest_idx_unmasked,
6661 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662{
6663 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006664 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006665 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006667 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 switch (opaque_key) {
6670 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006671 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006672 desc = &tpr->rx_std[dest_idx];
6673 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006674 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 break;
6676
6677 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006678 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006679 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006680 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006681 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 break;
6683
6684 default:
6685 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688 /* Do not overwrite any of the map or rp information
6689 * until we are sure we can commit to a new buffer.
6690 *
6691 * Callers depend upon this behavior and assume that
6692 * we leave everything unchanged if we fail.
6693 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006694 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6695 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006696 if (skb_size <= PAGE_SIZE) {
6697 data = netdev_alloc_frag(skb_size);
6698 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006699 } else {
6700 data = kmalloc(skb_size, GFP_ATOMIC);
6701 *frag_size = 0;
6702 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006703 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 return -ENOMEM;
6705
Eric Dumazet9205fd92011-11-18 06:47:01 +00006706 mapping = pci_map_single(tp->pdev,
6707 data + TG3_RX_OFFSET(tp),
6708 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006710 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006711 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006712 return -EIO;
6713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Eric Dumazet9205fd92011-11-18 06:47:01 +00006715 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006716 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 desc->addr_hi = ((u64)mapping >> 32);
6719 desc->addr_lo = ((u64)mapping & 0xffffffff);
6720
Eric Dumazet9205fd92011-11-18 06:47:01 +00006721 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722}
6723
6724/* We only need to move over in the address because the other
6725 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006726 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 */
Matt Carlsona3896162009-11-13 13:03:44 +00006728static void tg3_recycle_rx(struct tg3_napi *tnapi,
6729 struct tg3_rx_prodring_set *dpr,
6730 u32 opaque_key, int src_idx,
6731 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732{
Matt Carlson17375d22009-08-28 14:02:18 +00006733 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6735 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006736 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006737 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
6739 switch (opaque_key) {
6740 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006741 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006742 dest_desc = &dpr->rx_std[dest_idx];
6743 dest_map = &dpr->rx_std_buffers[dest_idx];
6744 src_desc = &spr->rx_std[src_idx];
6745 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 break;
6747
6748 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006749 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006750 dest_desc = &dpr->rx_jmb[dest_idx].std;
6751 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6752 src_desc = &spr->rx_jmb[src_idx].std;
6753 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 break;
6755
6756 default:
6757 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Eric Dumazet9205fd92011-11-18 06:47:01 +00006760 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006761 dma_unmap_addr_set(dest_map, mapping,
6762 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 dest_desc->addr_hi = src_desc->addr_hi;
6764 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006765
6766 /* Ensure that the update to the skb happens after the physical
6767 * addresses have been transferred to the new BD location.
6768 */
6769 smp_wmb();
6770
Eric Dumazet9205fd92011-11-18 06:47:01 +00006771 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772}
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774/* The RX ring scheme is composed of multiple rings which post fresh
6775 * buffers to the chip, and one special ring the chip uses to report
6776 * status back to the host.
6777 *
6778 * The special ring reports the status of received packets to the
6779 * host. The chip does not write into the original descriptor the
6780 * RX buffer was obtained from. The chip simply takes the original
6781 * descriptor as provided by the host, updates the status and length
6782 * field, then writes this into the next status ring entry.
6783 *
6784 * Each ring the host uses to post buffers to the chip is described
6785 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6786 * it is first placed into the on-chip ram. When the packet's length
6787 * is known, it walks down the TG3_BDINFO entries to select the ring.
6788 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6789 * which is within the range of the new packet's length is chosen.
6790 *
6791 * The "separate ring for rx status" scheme may sound queer, but it makes
6792 * sense from a cache coherency perspective. If only the host writes
6793 * to the buffer post rings, and only the chip writes to the rx status
6794 * rings, then cache lines never move beyond shared-modified state.
6795 * If both the host and chip were to write into the same ring, cache line
6796 * eviction could occur since both entities want it in an exclusive state.
6797 */
Matt Carlson17375d22009-08-28 14:02:18 +00006798static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799{
Matt Carlson17375d22009-08-28 14:02:18 +00006800 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006801 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006802 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006803 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006804 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006806 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006808 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 /*
6810 * We need to order the read of hw_idx and the read of
6811 * the opaque cookie.
6812 */
6813 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 work_mask = 0;
6815 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006816 std_prod_idx = tpr->rx_std_prod_idx;
6817 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006819 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006820 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 unsigned int len;
6822 struct sk_buff *skb;
6823 dma_addr_t dma_addr;
6824 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006825 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006826 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
6828 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6829 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6830 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006831 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006832 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006833 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006834 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006835 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006837 ri = &tp->napi[0].prodring.rx_jmb_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 = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006841 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
6844 work_mask |= opaque_key;
6845
Michael Chand7b95312014-02-28 15:05:10 -08006846 if (desc->err_vlan & RXD_ERR_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006848 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 desc_idx, *post_ptr);
6850 drop_it_no_recycle:
6851 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006852 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 goto next_pkt;
6854 }
6855
Eric Dumazet9205fd92011-11-18 06:47:01 +00006856 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006857 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6858 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006860 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6861 RXD_FLAG_PTPSTAT_PTPV1 ||
6862 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6863 RXD_FLAG_PTPSTAT_PTPV2) {
6864 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6865 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6866 }
6867
Matt Carlsond2757fc2010-04-12 06:58:27 +00006868 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006870 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
Eric Dumazet9205fd92011-11-18 06:47:01 +00006872 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006873 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 if (skb_size < 0)
6875 goto drop_it;
6876
Matt Carlson287be122009-08-28 13:58:46 +00006877 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 PCI_DMA_FROMDEVICE);
6879
Eric Dumazet9205fd92011-11-18 06:47:01 +00006880 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006881 * after the usage of the old DMA mapping.
6882 */
6883 smp_wmb();
6884
Eric Dumazet9205fd92011-11-18 06:47:01 +00006885 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006886
Ivan Vecera85aec732013-11-06 14:02:36 +01006887 skb = build_skb(data, frag_size);
6888 if (!skb) {
6889 tg3_frag_free(frag_size != 0, data);
6890 goto drop_it_no_recycle;
6891 }
6892 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006894 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 desc_idx, *post_ptr);
6896
Eric Dumazet9205fd92011-11-18 06:47:01 +00006897 skb = netdev_alloc_skb(tp->dev,
6898 len + TG3_RAW_IP_ALIGN);
6899 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 goto drop_it_no_recycle;
6901
Eric Dumazet9205fd92011-11-18 06:47:01 +00006902 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006904 memcpy(skb->data,
6905 data + TG3_RX_OFFSET(tp),
6906 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 }
6909
Eric Dumazet9205fd92011-11-18 06:47:01 +00006910 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006911 if (tstamp)
6912 tg3_hwclock_to_timestamp(tp, tstamp,
6913 skb_hwtstamps(skb));
6914
Michał Mirosławdc668912011-04-07 03:35:07 +00006915 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6917 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6918 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6919 skb->ip_summed = CHECKSUM_UNNECESSARY;
6920 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006921 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922
6923 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006924
6925 if (len > (tp->dev->mtu + ETH_HLEN) &&
6926 skb->protocol != htons(ETH_P_8021Q)) {
6927 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006928 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006929 }
6930
Matt Carlson9dc7a112010-04-12 06:58:28 +00006931 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006932 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
Patrick McHardy86a9bad2013-04-19 02:04:30 +00006933 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
Matt Carlsonbf933c82011-01-25 15:58:49 +00006934 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006935
Matt Carlsonbf933c82011-01-25 15:58:49 +00006936 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 received++;
6939 budget--;
6940
6941next_pkt:
6942 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006943
6944 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006945 tpr->rx_std_prod_idx = std_prod_idx &
6946 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006947 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6948 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006949 work_mask &= ~RXD_OPAQUE_RING_STD;
6950 rx_std_posted = 0;
6951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006953 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006954 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006955
6956 /* Refresh hw_idx to see if there is new work */
6957 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006958 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006959 rmb();
6960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 }
6962
6963 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006964 tnapi->rx_rcb_ptr = sw_idx;
6965 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
6967 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006968 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006969 /* Sync BD data before updating mailbox */
6970 wmb();
6971
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006972 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006973 tpr->rx_std_prod_idx = std_prod_idx &
6974 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006975 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6976 tpr->rx_std_prod_idx);
6977 }
6978 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006979 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6980 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006981 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6982 tpr->rx_jmb_prod_idx);
6983 }
6984 mmiowb();
6985 } else if (work_mask) {
6986 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6987 * updated before the producer indices can be updated.
6988 */
6989 smp_wmb();
6990
Matt Carlson2c49a442010-09-30 10:34:35 +00006991 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6992 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006993
Michael Chan7ae52892012-03-21 15:38:33 +00006994 if (tnapi != &tp->napi[1]) {
6995 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006996 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
7000 return received;
7001}
7002
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007003static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00007006 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007007 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
7008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 if (sblk->status & SD_STATUS_LINK_CHG) {
7010 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007011 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07007012 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00007013 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07007014 tw32_f(MAC_STATUS,
7015 (MAC_STATUS_SYNC_CHANGED |
7016 MAC_STATUS_CFG_CHANGED |
7017 MAC_STATUS_MI_COMPLETION |
7018 MAC_STATUS_LNKSTATE_CHANGED));
7019 udelay(40);
7020 } else
Joe Perches953c96e2013-04-09 10:18:14 +00007021 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07007022 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 }
7024 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007025}
7026
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007027static int tg3_rx_prodring_xfer(struct tg3 *tp,
7028 struct tg3_rx_prodring_set *dpr,
7029 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007030{
7031 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007032 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007033
7034 while (1) {
7035 src_prod_idx = spr->rx_std_prod_idx;
7036
7037 /* Make sure updates to the rx_std_buffers[] entries and the
7038 * standard producer index are seen in the correct order.
7039 */
7040 smp_rmb();
7041
7042 if (spr->rx_std_cons_idx == src_prod_idx)
7043 break;
7044
7045 if (spr->rx_std_cons_idx < src_prod_idx)
7046 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
7047 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007048 cpycnt = tp->rx_std_ring_mask + 1 -
7049 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007050
Matt Carlson2c49a442010-09-30 10:34:35 +00007051 cpycnt = min(cpycnt,
7052 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007053
7054 si = spr->rx_std_cons_idx;
7055 di = dpr->rx_std_prod_idx;
7056
Matt Carlsone92967b2010-02-12 14:47:06 +00007057 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007058 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007059 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007060 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007061 break;
7062 }
7063 }
7064
7065 if (!cpycnt)
7066 break;
7067
7068 /* Ensure that updates to the rx_std_buffers ring and the
7069 * shadowed hardware producer ring from tg3_recycle_skb() are
7070 * ordered correctly WRT the skb check above.
7071 */
7072 smp_rmb();
7073
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007074 memcpy(&dpr->rx_std_buffers[di],
7075 &spr->rx_std_buffers[si],
7076 cpycnt * sizeof(struct ring_info));
7077
7078 for (i = 0; i < cpycnt; i++, di++, si++) {
7079 struct tg3_rx_buffer_desc *sbd, *dbd;
7080 sbd = &spr->rx_std[si];
7081 dbd = &dpr->rx_std[di];
7082 dbd->addr_hi = sbd->addr_hi;
7083 dbd->addr_lo = sbd->addr_lo;
7084 }
7085
Matt Carlson2c49a442010-09-30 10:34:35 +00007086 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
7087 tp->rx_std_ring_mask;
7088 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
7089 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007090 }
7091
7092 while (1) {
7093 src_prod_idx = spr->rx_jmb_prod_idx;
7094
7095 /* Make sure updates to the rx_jmb_buffers[] entries and
7096 * the jumbo producer index are seen in the correct order.
7097 */
7098 smp_rmb();
7099
7100 if (spr->rx_jmb_cons_idx == src_prod_idx)
7101 break;
7102
7103 if (spr->rx_jmb_cons_idx < src_prod_idx)
7104 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
7105 else
Matt Carlson2c49a442010-09-30 10:34:35 +00007106 cpycnt = tp->rx_jmb_ring_mask + 1 -
7107 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007108
7109 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00007110 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007111
7112 si = spr->rx_jmb_cons_idx;
7113 di = dpr->rx_jmb_prod_idx;
7114
Matt Carlsone92967b2010-02-12 14:47:06 +00007115 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007116 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00007117 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007118 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00007119 break;
7120 }
7121 }
7122
7123 if (!cpycnt)
7124 break;
7125
7126 /* Ensure that updates to the rx_jmb_buffers ring and the
7127 * shadowed hardware producer ring from tg3_recycle_skb() are
7128 * ordered correctly WRT the skb check above.
7129 */
7130 smp_rmb();
7131
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007132 memcpy(&dpr->rx_jmb_buffers[di],
7133 &spr->rx_jmb_buffers[si],
7134 cpycnt * sizeof(struct ring_info));
7135
7136 for (i = 0; i < cpycnt; i++, di++, si++) {
7137 struct tg3_rx_buffer_desc *sbd, *dbd;
7138 sbd = &spr->rx_jmb[si].std;
7139 dbd = &dpr->rx_jmb[di].std;
7140 dbd->addr_hi = sbd->addr_hi;
7141 dbd->addr_lo = sbd->addr_lo;
7142 }
7143
Matt Carlson2c49a442010-09-30 10:34:35 +00007144 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
7145 tp->rx_jmb_ring_mask;
7146 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
7147 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007148 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007149
7150 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007151}
7152
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007153static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
7154{
7155 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
7157 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007158 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00007159 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00007160 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07007161 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 }
7163
Matt Carlsonf891ea12012-04-24 13:37:01 +00007164 if (!tnapi->rx_rcb_prod_idx)
7165 return work_done;
7166
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167 /* run RX thread, within the bounds set by NAPI.
7168 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007169 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007171 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00007172 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173
Joe Perches63c3a662011-04-26 08:12:10 +00007174 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00007175 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007176 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007177 u32 std_prod_idx = dpr->rx_std_prod_idx;
7178 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007179
Michael Chan7ae52892012-03-21 15:38:33 +00007180 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00007181 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007182 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00007183 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007184
7185 wmb();
7186
Matt Carlsone4af1af2010-02-12 14:47:05 +00007187 if (std_prod_idx != dpr->rx_std_prod_idx)
7188 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
7189 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007190
Matt Carlsone4af1af2010-02-12 14:47:05 +00007191 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
7192 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
7193 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007194
7195 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00007196
7197 if (err)
7198 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007199 }
7200
David S. Miller6f535762007-10-11 18:08:29 -07007201 return work_done;
7202}
David S. Millerf7383c22005-05-18 22:50:53 -07007203
Matt Carlsondb219972011-11-04 09:15:03 +00007204static inline void tg3_reset_task_schedule(struct tg3 *tp)
7205{
7206 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
7207 schedule_work(&tp->reset_task);
7208}
7209
7210static inline void tg3_reset_task_cancel(struct tg3 *tp)
7211{
7212 cancel_work_sync(&tp->reset_task);
7213 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00007214 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007215}
7216
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007217static int tg3_poll_msix(struct napi_struct *napi, int budget)
7218{
7219 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7220 struct tg3 *tp = tnapi->tp;
7221 int work_done = 0;
7222 struct tg3_hw_status *sblk = tnapi->hw_status;
7223
7224 while (1) {
7225 work_done = tg3_poll_work(tnapi, work_done, budget);
7226
Joe Perches63c3a662011-04-26 08:12:10 +00007227 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007228 goto tx_recovery;
7229
7230 if (unlikely(work_done >= budget))
7231 break;
7232
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007233 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007234 * to tell the hw how much work has been processed,
7235 * so we must read it before checking for more work.
7236 */
7237 tnapi->last_tag = sblk->status_tag;
7238 tnapi->last_irq_tag = tnapi->last_tag;
7239 rmb();
7240
7241 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007242 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7243 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007244
7245 /* This test here is not race free, but will reduce
7246 * the number of interrupts by looping again.
7247 */
7248 if (tnapi == &tp->napi[1] && tp->rx_refill)
7249 continue;
7250
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007251 napi_complete(napi);
7252 /* Reenable interrupts. */
7253 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007254
7255 /* This test here is synchronized by napi_schedule()
7256 * and napi_complete() to close the race condition.
7257 */
7258 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7259 tw32(HOSTCC_MODE, tp->coalesce_mode |
7260 HOSTCC_MODE_ENABLE |
7261 tnapi->coal_now);
7262 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007263 mmiowb();
7264 break;
7265 }
7266 }
7267
7268 return work_done;
7269
7270tx_recovery:
7271 /* work_done is guaranteed to be less than budget. */
7272 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007273 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007274 return work_done;
7275}
7276
Matt Carlsone64de4e2011-04-13 11:05:05 +00007277static void tg3_process_error(struct tg3 *tp)
7278{
7279 u32 val;
7280 bool real_error = false;
7281
Joe Perches63c3a662011-04-26 08:12:10 +00007282 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007283 return;
7284
7285 /* Check Flow Attention register */
7286 val = tr32(HOSTCC_FLOW_ATTN);
7287 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7288 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7289 real_error = true;
7290 }
7291
7292 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7293 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7294 real_error = true;
7295 }
7296
7297 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7298 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7299 real_error = true;
7300 }
7301
7302 if (!real_error)
7303 return;
7304
7305 tg3_dump_state(tp);
7306
Joe Perches63c3a662011-04-26 08:12:10 +00007307 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007308 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007309}
7310
David S. Miller6f535762007-10-11 18:08:29 -07007311static int tg3_poll(struct napi_struct *napi, int budget)
7312{
Matt Carlson8ef04422009-08-28 14:01:37 +00007313 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7314 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007315 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007316 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007317
7318 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007319 if (sblk->status & SD_STATUS_ERROR)
7320 tg3_process_error(tp);
7321
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007322 tg3_poll_link(tp);
7323
Matt Carlson17375d22009-08-28 14:02:18 +00007324 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007325
Joe Perches63c3a662011-04-26 08:12:10 +00007326 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007327 goto tx_recovery;
7328
7329 if (unlikely(work_done >= budget))
7330 break;
7331
Joe Perches63c3a662011-04-26 08:12:10 +00007332 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007333 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007334 * to tell the hw how much work has been processed,
7335 * so we must read it before checking for more work.
7336 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007337 tnapi->last_tag = sblk->status_tag;
7338 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007339 rmb();
7340 } else
7341 sblk->status &= ~SD_STATUS_UPDATED;
7342
Matt Carlson17375d22009-08-28 14:02:18 +00007343 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007344 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007345 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007346 break;
7347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 }
7349
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007350 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007351
7352tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007353 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007354 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007355 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007356 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357}
7358
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007359static void tg3_napi_disable(struct tg3 *tp)
7360{
7361 int i;
7362
7363 for (i = tp->irq_cnt - 1; i >= 0; i--)
7364 napi_disable(&tp->napi[i].napi);
7365}
7366
7367static void tg3_napi_enable(struct tg3 *tp)
7368{
7369 int i;
7370
7371 for (i = 0; i < tp->irq_cnt; i++)
7372 napi_enable(&tp->napi[i].napi);
7373}
7374
7375static void tg3_napi_init(struct tg3 *tp)
7376{
7377 int i;
7378
7379 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7380 for (i = 1; i < tp->irq_cnt; i++)
7381 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7382}
7383
7384static void tg3_napi_fini(struct tg3 *tp)
7385{
7386 int i;
7387
7388 for (i = 0; i < tp->irq_cnt; i++)
7389 netif_napi_del(&tp->napi[i].napi);
7390}
7391
7392static inline void tg3_netif_stop(struct tg3 *tp)
7393{
7394 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7395 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007396 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007397 netif_tx_disable(tp->dev);
7398}
7399
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007400/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007401static inline void tg3_netif_start(struct tg3 *tp)
7402{
Matt Carlsonbe947302012-12-03 19:36:57 +00007403 tg3_ptp_resume(tp);
7404
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007405 /* NOTE: unconditional netif_tx_wake_all_queues is only
7406 * appropriate so long as all callers are assured to
7407 * have free tx slots (such as after tg3_init_hw)
7408 */
7409 netif_tx_wake_all_queues(tp->dev);
7410
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007411 if (tp->link_up)
7412 netif_carrier_on(tp->dev);
7413
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007414 tg3_napi_enable(tp);
7415 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7416 tg3_enable_ints(tp);
7417}
7418
David S. Millerf47c11e2005-06-24 20:18:35 -07007419static void tg3_irq_quiesce(struct tg3 *tp)
7420{
Matt Carlson4f125f42009-09-01 12:55:02 +00007421 int i;
7422
David S. Millerf47c11e2005-06-24 20:18:35 -07007423 BUG_ON(tp->irq_sync);
7424
7425 tp->irq_sync = 1;
7426 smp_mb();
7427
Matt Carlson4f125f42009-09-01 12:55:02 +00007428 for (i = 0; i < tp->irq_cnt; i++)
7429 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007430}
7431
David S. Millerf47c11e2005-06-24 20:18:35 -07007432/* Fully shutdown all tg3 driver activity elsewhere in the system.
7433 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7434 * with as well. Most of the time, this is not necessary except when
7435 * shutting down the device.
7436 */
7437static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7438{
Michael Chan46966542007-07-11 19:47:19 -07007439 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007440 if (irq_sync)
7441 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007442}
7443
7444static inline void tg3_full_unlock(struct tg3 *tp)
7445{
David S. Millerf47c11e2005-06-24 20:18:35 -07007446 spin_unlock_bh(&tp->lock);
7447}
7448
Michael Chanfcfa0a32006-03-20 22:28:41 -08007449/* One-shot MSI handler - Chip automatically disables interrupt
7450 * after sending MSI so driver doesn't have to do it.
7451 */
David Howells7d12e782006-10-05 14:55:46 +01007452static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007453{
Matt Carlson09943a12009-08-28 14:01:57 +00007454 struct tg3_napi *tnapi = dev_id;
7455 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007456
Matt Carlson898a56f2009-08-28 14:02:40 +00007457 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007458 if (tnapi->rx_rcb)
7459 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007460
7461 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007462 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007463
7464 return IRQ_HANDLED;
7465}
7466
Michael Chan88b06bc22005-04-21 17:13:25 -07007467/* MSI ISR - No need to check for interrupt sharing and no need to
7468 * flush status block and interrupt mailbox. PCI ordering rules
7469 * guarantee that MSI will arrive after the status block.
7470 */
David Howells7d12e782006-10-05 14:55:46 +01007471static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007472{
Matt Carlson09943a12009-08-28 14:01:57 +00007473 struct tg3_napi *tnapi = dev_id;
7474 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007475
Matt Carlson898a56f2009-08-28 14:02:40 +00007476 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007477 if (tnapi->rx_rcb)
7478 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007479 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007480 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007481 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007482 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007483 * NIC to stop sending us irqs, engaging "in-intr-handler"
7484 * event coalescing.
7485 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007486 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007487 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007488 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007489
Michael Chan88b06bc22005-04-21 17:13:25 -07007490 return IRQ_RETVAL(1);
7491}
7492
David Howells7d12e782006-10-05 14:55:46 +01007493static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494{
Matt Carlson09943a12009-08-28 14:01:57 +00007495 struct tg3_napi *tnapi = dev_id;
7496 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007497 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 unsigned int handled = 1;
7499
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500 /* In INTx mode, it is possible for the interrupt to arrive at
7501 * the CPU before the status block posted prior to the interrupt.
7502 * Reading the PCI State register will confirm whether the
7503 * interrupt is ours and will flush the status block.
7504 */
Michael Chand18edcb2007-03-24 20:57:11 -07007505 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007506 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007507 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7508 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007509 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007510 }
Michael Chand18edcb2007-03-24 20:57:11 -07007511 }
7512
7513 /*
7514 * Writing any value to intr-mbox-0 clears PCI INTA# and
7515 * chip-internal interrupt pending events.
7516 * Writing non-zero to intr-mbox-0 additional tells the
7517 * NIC to stop sending us irqs, engaging "in-intr-handler"
7518 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007519 *
7520 * Flush the mailbox to de-assert the IRQ immediately to prevent
7521 * spurious interrupts. The flush impacts performance but
7522 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007523 */
Michael Chanc04cb342007-05-07 00:26:15 -07007524 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007525 if (tg3_irq_sync(tp))
7526 goto out;
7527 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007528 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007529 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007530 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007531 } else {
7532 /* No work, shared interrupt perhaps? re-enable
7533 * interrupts, and flush that PCI write
7534 */
7535 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7536 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007537 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007538out:
David S. Millerfac9b832005-05-18 22:46:34 -07007539 return IRQ_RETVAL(handled);
7540}
7541
David Howells7d12e782006-10-05 14:55:46 +01007542static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007543{
Matt Carlson09943a12009-08-28 14:01:57 +00007544 struct tg3_napi *tnapi = dev_id;
7545 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007546 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007547 unsigned int handled = 1;
7548
David S. Millerfac9b832005-05-18 22:46:34 -07007549 /* In INTx mode, it is possible for the interrupt to arrive at
7550 * the CPU before the status block posted prior to the interrupt.
7551 * Reading the PCI State register will confirm whether the
7552 * interrupt is ours and will flush the status block.
7553 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007554 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007555 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007556 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7557 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007558 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 }
Michael Chand18edcb2007-03-24 20:57:11 -07007560 }
7561
7562 /*
7563 * writing any value to intr-mbox-0 clears PCI INTA# and
7564 * chip-internal interrupt pending events.
7565 * writing non-zero to intr-mbox-0 additional tells the
7566 * NIC to stop sending us irqs, engaging "in-intr-handler"
7567 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007568 *
7569 * Flush the mailbox to de-assert the IRQ immediately to prevent
7570 * spurious interrupts. The flush impacts performance but
7571 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007572 */
Michael Chanc04cb342007-05-07 00:26:15 -07007573 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007574
7575 /*
7576 * In a shared interrupt configuration, sometimes other devices'
7577 * interrupts will scream. We record the current status tag here
7578 * so that the above check can report that the screaming interrupts
7579 * are unhandled. Eventually they will be silenced.
7580 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007581 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007582
Michael Chand18edcb2007-03-24 20:57:11 -07007583 if (tg3_irq_sync(tp))
7584 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007585
Matt Carlson72334482009-08-28 14:03:01 +00007586 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007587
Matt Carlson09943a12009-08-28 14:01:57 +00007588 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007589
David S. Millerf47c11e2005-06-24 20:18:35 -07007590out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 return IRQ_RETVAL(handled);
7592}
7593
Michael Chan79381092005-04-21 17:13:59 -07007594/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007595static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007596{
Matt Carlson09943a12009-08-28 14:01:57 +00007597 struct tg3_napi *tnapi = dev_id;
7598 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007599 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007600
Michael Chanf9804dd2005-09-27 12:13:10 -07007601 if ((sblk->status & SD_STATUS_UPDATED) ||
7602 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007603 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007604 return IRQ_RETVAL(1);
7605 }
7606 return IRQ_RETVAL(0);
7607}
7608
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609#ifdef CONFIG_NET_POLL_CONTROLLER
7610static void tg3_poll_controller(struct net_device *dev)
7611{
Matt Carlson4f125f42009-09-01 12:55:02 +00007612 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007613 struct tg3 *tp = netdev_priv(dev);
7614
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007615 if (tg3_irq_sync(tp))
7616 return;
7617
Matt Carlson4f125f42009-09-01 12:55:02 +00007618 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007619 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620}
7621#endif
7622
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623static void tg3_tx_timeout(struct net_device *dev)
7624{
7625 struct tg3 *tp = netdev_priv(dev);
7626
Michael Chanb0408752007-02-13 12:18:30 -08007627 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007628 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007629 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
Matt Carlsondb219972011-11-04 09:15:03 +00007632 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633}
7634
Michael Chanc58ec932005-09-17 00:46:27 -07007635/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7636static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7637{
7638 u32 base = (u32) mapping & 0xffffffff;
7639
Nithin Sujir37567912013-12-19 17:44:11 -08007640 return base + len + 8 < base;
Michael Chanc58ec932005-09-17 00:46:27 -07007641}
7642
Michael Chan0f0d1512013-05-13 11:04:16 +00007643/* Test for TSO DMA buffers that cross into regions which are within MSS bytes
7644 * of any 4GB boundaries: 4G, 8G, etc
7645 */
7646static inline int tg3_4g_tso_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7647 u32 len, u32 mss)
7648{
7649 if (tg3_asic_rev(tp) == ASIC_REV_5762 && mss) {
7650 u32 base = (u32) mapping & 0xffffffff;
7651
7652 return ((base + len + (mss & 0x3fff)) < base);
7653 }
7654 return 0;
7655}
7656
Michael Chan72f2afb2006-03-06 19:28:35 -08007657/* Test for DMA addresses > 40-bit */
7658static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7659 int len)
7660{
7661#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007662 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007663 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007664 return 0;
7665#else
7666 return 0;
7667#endif
7668}
7669
Matt Carlsond1a3b732011-07-27 14:20:51 +00007670static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007671 dma_addr_t mapping, u32 len, u32 flags,
7672 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007673{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007674 txbd->addr_hi = ((u64) mapping >> 32);
7675 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7676 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7677 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007678}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
Matt Carlson84b67b22011-07-27 14:20:52 +00007680static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007681 dma_addr_t map, u32 len, u32 flags,
7682 u32 mss, u32 vlan)
7683{
7684 struct tg3 *tp = tnapi->tp;
7685 bool hwbug = false;
7686
7687 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007688 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007689
7690 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007691 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007692
Michael Chan0f0d1512013-05-13 11:04:16 +00007693 if (tg3_4g_tso_overflow_test(tp, map, len, mss))
7694 hwbug = true;
7695
Matt Carlsond1a3b732011-07-27 14:20:51 +00007696 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007697 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007698
Matt Carlsona4cb4282011-12-14 11:09:58 +00007699 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007700 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007701 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007702 while (len > tp->dma_limit && *budget) {
7703 u32 frag_len = tp->dma_limit;
7704 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007705
Matt Carlsonb9e45482011-11-04 09:14:59 +00007706 /* Avoid the 8byte DMA problem */
7707 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007708 len += tp->dma_limit / 2;
7709 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007710 }
7711
Matt Carlsonb9e45482011-11-04 09:14:59 +00007712 tnapi->tx_buffers[*entry].fragmented = true;
7713
7714 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7715 frag_len, tmp_flag, mss, vlan);
7716 *budget -= 1;
7717 prvidx = *entry;
7718 *entry = NEXT_TX(*entry);
7719
Matt Carlsone31aa982011-07-27 14:20:53 +00007720 map += frag_len;
7721 }
7722
7723 if (len) {
7724 if (*budget) {
7725 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7726 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007727 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007728 *entry = NEXT_TX(*entry);
7729 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007730 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007731 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007732 }
7733 }
7734 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007735 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7736 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007737 *entry = NEXT_TX(*entry);
7738 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007739
7740 return hwbug;
7741}
7742
Matt Carlson0d681b22011-07-27 14:20:49 +00007743static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007744{
7745 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007746 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007747 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007748
Matt Carlson0d681b22011-07-27 14:20:49 +00007749 skb = txb->skb;
7750 txb->skb = NULL;
7751
Matt Carlson432aa7e2011-05-19 12:12:45 +00007752 pci_unmap_single(tnapi->tp->pdev,
7753 dma_unmap_addr(txb, mapping),
7754 skb_headlen(skb),
7755 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007756
7757 while (txb->fragmented) {
7758 txb->fragmented = false;
7759 entry = NEXT_TX(entry);
7760 txb = &tnapi->tx_buffers[entry];
7761 }
7762
Matt Carlsonba1142e2011-11-04 09:15:00 +00007763 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007764 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007765
7766 entry = NEXT_TX(entry);
7767 txb = &tnapi->tx_buffers[entry];
7768
7769 pci_unmap_page(tnapi->tp->pdev,
7770 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007771 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007772
7773 while (txb->fragmented) {
7774 txb->fragmented = false;
7775 entry = NEXT_TX(entry);
7776 txb = &tnapi->tx_buffers[entry];
7777 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007778 }
7779}
7780
Michael Chan72f2afb2006-03-06 19:28:35 -08007781/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007782static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007783 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007784 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007785 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007786{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007787 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007788 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007789 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007790 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
Joe Perches41535772013-02-16 11:20:04 +00007792 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007793 new_skb = skb_copy(skb, GFP_ATOMIC);
7794 else {
7795 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7796
7797 new_skb = skb_copy_expand(skb,
7798 skb_headroom(skb) + more_headroom,
7799 skb_tailroom(skb), GFP_ATOMIC);
7800 }
7801
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007803 ret = -1;
7804 } else {
7805 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007806 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7807 PCI_DMA_TODEVICE);
7808 /* Make sure the mapping succeeded */
7809 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007810 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007811 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007812 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007813 u32 save_entry = *entry;
7814
Matt Carlson92cd3a12011-07-27 14:20:47 +00007815 base_flags |= TXD_FLAG_END;
7816
Matt Carlson84b67b22011-07-27 14:20:52 +00007817 tnapi->tx_buffers[*entry].skb = new_skb;
7818 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007819 mapping, new_addr);
7820
Matt Carlson84b67b22011-07-27 14:20:52 +00007821 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007822 new_skb->len, base_flags,
7823 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007824 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007825 dev_kfree_skb(new_skb);
7826 ret = -1;
7827 }
Michael Chanc58ec932005-09-17 00:46:27 -07007828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 }
7830
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007832 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007834}
7835
Matt Carlson2ffcc982011-05-19 12:12:44 +00007836static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007837
7838/* Use GSO to workaround a rare TSO bug that may be triggered when the
7839 * TSO header is greater than 80 bytes.
7840 */
7841static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7842{
7843 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007844 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007845
7846 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007847 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007848 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007849
7850 /* netif_tx_stop_queue() must be done before checking
7851 * checking tx index in tg3_tx_avail() below, because in
7852 * tg3_tx(), we update tx index before checking for
7853 * netif_tx_queue_stopped().
7854 */
7855 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007856 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007857 return NETDEV_TX_BUSY;
7858
7859 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007860 }
7861
7862 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007863 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007864 goto tg3_tso_bug_end;
7865
7866 do {
7867 nskb = segs;
7868 segs = segs->next;
7869 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007870 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007871 } while (segs);
7872
7873tg3_tso_bug_end:
7874 dev_kfree_skb(skb);
7875
7876 return NETDEV_TX_OK;
7877}
Michael Chan52c0fd82006-06-29 20:15:54 -07007878
Michael Chan5a6f3072006-03-20 22:28:05 -08007879/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007880 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007881 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007882static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007883{
7884 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007885 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007886 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007887 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007888 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007889 struct tg3_napi *tnapi;
7890 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007891 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007892
Matt Carlson24f4efd2009-11-13 13:03:35 +00007893 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7894 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007895 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007896 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897
Matt Carlson84b67b22011-07-27 14:20:52 +00007898 budget = tg3_tx_avail(tnapi);
7899
Michael Chan00b70502006-06-17 21:58:45 -07007900 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007901 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007902 * interrupt. Furthermore, IRQ processing runs lockless so we have
7903 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007905 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007906 if (!netif_tx_queue_stopped(txq)) {
7907 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007908
7909 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007910 netdev_err(dev,
7911 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 return NETDEV_TX_BUSY;
7914 }
7915
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007916 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007918 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007920
Matt Carlsonbe98da62010-07-11 09:31:46 +00007921 mss = skb_shinfo(skb)->gso_size;
7922 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007923 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007924 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
7926 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007927 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7928 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929
Matt Carlson34195c32010-07-11 09:31:42 +00007930 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007931 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
Eric Dumazeta5a11952012-01-23 01:22:09 +00007933 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007934
Eric Dumazeta5a11952012-01-23 01:22:09 +00007935 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007936 iph->check = 0;
7937 iph->tot_len = htons(mss + hdr_len);
7938 }
7939
Michael Chan52c0fd82006-06-29 20:15:54 -07007940 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007941 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007942 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007943
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7945 TXD_FLAG_CPU_POST_DMA);
7946
Joe Perches63c3a662011-04-26 08:12:10 +00007947 if (tg3_flag(tp, HW_TSO_1) ||
7948 tg3_flag(tp, HW_TSO_2) ||
7949 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007950 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007952 } else
7953 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7954 iph->daddr, 0,
7955 IPPROTO_TCP,
7956 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
Joe Perches63c3a662011-04-26 08:12:10 +00007958 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007959 mss |= (hdr_len & 0xc) << 12;
7960 if (hdr_len & 0x10)
7961 base_flags |= 0x00000010;
7962 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007963 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007964 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007965 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007966 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007967 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968 int tsflags;
7969
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007970 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971 mss |= (tsflags << 11);
7972 }
7973 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007974 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 int tsflags;
7976
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007977 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 base_flags |= tsflags << 12;
7979 }
7980 }
7981 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007982
Matt Carlson93a700a2011-08-31 11:44:54 +00007983 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7984 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7985 base_flags |= TXD_FLAG_JMB_PKT;
7986
Matt Carlson92cd3a12011-07-27 14:20:47 +00007987 if (vlan_tx_tag_present(skb)) {
7988 base_flags |= TXD_FLAG_VLAN;
7989 vlan = vlan_tx_tag_get(skb);
7990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007992 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7993 tg3_flag(tp, TX_TSTAMP_EN)) {
7994 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7995 base_flags |= TXD_FLAG_HWTSTAMP;
7996 }
7997
Alexander Duyckf4188d82009-12-02 16:48:38 +00007998 len = skb_headlen(skb);
7999
8000 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00008001 if (pci_dma_mapping_error(tp->pdev, mapping))
8002 goto drop;
8003
David S. Miller90079ce2008-09-11 04:52:51 -07008004
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008005 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008006 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008 would_hit_hwbug = 0;
8009
Joe Perches63c3a662011-04-26 08:12:10 +00008010 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07008011 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
Matt Carlson84b67b22011-07-27 14:20:52 +00008013 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00008014 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00008015 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00008016 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00008017 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00008018 u32 tmp_mss = mss;
8019
8020 if (!tg3_flag(tp, HW_TSO_1) &&
8021 !tg3_flag(tp, HW_TSO_2) &&
8022 !tg3_flag(tp, HW_TSO_3))
8023 tmp_mss = 0;
8024
Matt Carlsonc5665a52012-02-13 10:20:12 +00008025 /* Now loop through additional data
8026 * fragments, and queue them.
8027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 last = skb_shinfo(skb)->nr_frags - 1;
8029 for (i = 0; i <= last; i++) {
8030 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
8031
Eric Dumazet9e903e02011-10-18 21:00:24 +00008032 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00008033 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008034 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008036 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00008037 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00008038 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01008039 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00008040 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041
Matt Carlsonb9e45482011-11-04 09:14:59 +00008042 if (!budget ||
8043 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00008044 len, base_flags |
8045 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00008046 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00008047 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00008048 break;
8049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050 }
8051 }
8052
8053 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00008054 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055
8056 /* If the workaround fails due to memory/mapping
8057 * failure, silently drop this packet.
8058 */
Matt Carlson84b67b22011-07-27 14:20:52 +00008059 entry = tnapi->tx_prod;
8060 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04008061 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00008062 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00008063 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 }
8065
Richard Cochrand515b452011-06-19 03:31:41 +00008066 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00008067 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00008068
Michael Chan6541b802012-03-04 14:48:14 +00008069 /* Sync BD data before updating mailbox */
8070 wmb();
8071
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00008073 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008075 tnapi->tx_prod = entry;
8076 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00008077 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00008078
8079 /* netif_tx_stop_queue() must be done before checking
8080 * checking tx index in tg3_tx_avail() below, because in
8081 * tg3_tx(), we update tx index before checking for
8082 * netif_tx_queue_stopped().
8083 */
8084 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00008085 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00008086 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07008087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
Eric Dumazetcdd0db02009-05-28 00:00:41 +00008089 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008091
8092dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00008093 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00008094 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00008095drop:
8096 dev_kfree_skb(skb);
8097drop_nofree:
8098 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00008099 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100}
8101
Matt Carlson6e01b202011-08-19 13:58:20 +00008102static void tg3_mac_loopback(struct tg3 *tp, bool enable)
8103{
8104 if (enable) {
8105 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
8106 MAC_MODE_PORT_MODE_MASK);
8107
8108 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
8109
8110 if (!tg3_flag(tp, 5705_PLUS))
8111 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
8112
8113 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
8114 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
8115 else
8116 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
8117 } else {
8118 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
8119
8120 if (tg3_flag(tp, 5705_PLUS) ||
8121 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00008122 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00008123 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
8124 }
8125
8126 tw32(MAC_MODE, tp->mac_mode);
8127 udelay(40);
8128}
8129
Matt Carlson941ec902011-08-19 13:58:23 +00008130static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008131{
Matt Carlson941ec902011-08-19 13:58:23 +00008132 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008133
8134 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00008135 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008136
Matt Carlson941ec902011-08-19 13:58:23 +00008137 if (extlpbk && tg3_phy_set_extloopbk(tp))
8138 return -EIO;
8139
8140 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008141 switch (speed) {
8142 case SPEED_10:
8143 break;
8144 case SPEED_100:
8145 bmcr |= BMCR_SPEED100;
8146 break;
8147 case SPEED_1000:
8148 default:
8149 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
8150 speed = SPEED_100;
8151 bmcr |= BMCR_SPEED100;
8152 } else {
8153 speed = SPEED_1000;
8154 bmcr |= BMCR_SPEED1000;
8155 }
8156 }
8157
Matt Carlson941ec902011-08-19 13:58:23 +00008158 if (extlpbk) {
8159 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
8160 tg3_readphy(tp, MII_CTRL1000, &val);
8161 val |= CTL1000_AS_MASTER |
8162 CTL1000_ENABLE_MASTER;
8163 tg3_writephy(tp, MII_CTRL1000, val);
8164 } else {
8165 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
8166 MII_TG3_FET_PTEST_TRIM_2;
8167 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
8168 }
8169 } else
8170 bmcr |= BMCR_LOOPBACK;
8171
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008172 tg3_writephy(tp, MII_BMCR, bmcr);
8173
8174 /* The write needs to be flushed for the FETs */
8175 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
8176 tg3_readphy(tp, MII_BMCR, &bmcr);
8177
8178 udelay(40);
8179
8180 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00008181 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00008182 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008183 MII_TG3_FET_PTEST_FRC_TX_LINK |
8184 MII_TG3_FET_PTEST_FRC_TX_LOCK);
8185
8186 /* The write needs to be flushed for the AC131 */
8187 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
8188 }
8189
8190 /* Reset to prevent losing 1st rx packet intermittently */
8191 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
8192 tg3_flag(tp, 5780_CLASS)) {
8193 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8194 udelay(10);
8195 tw32_f(MAC_RX_MODE, tp->rx_mode);
8196 }
8197
8198 mac_mode = tp->mac_mode &
8199 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
8200 if (speed == SPEED_1000)
8201 mac_mode |= MAC_MODE_PORT_MODE_GMII;
8202 else
8203 mac_mode |= MAC_MODE_PORT_MODE_MII;
8204
Joe Perches41535772013-02-16 11:20:04 +00008205 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008206 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
8207
8208 if (masked_phy_id == TG3_PHY_ID_BCM5401)
8209 mac_mode &= ~MAC_MODE_LINK_POLARITY;
8210 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
8211 mac_mode |= MAC_MODE_LINK_POLARITY;
8212
8213 tg3_writephy(tp, MII_TG3_EXT_CTRL,
8214 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
8215 }
8216
8217 tw32(MAC_MODE, mac_mode);
8218 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00008219
8220 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00008221}
8222
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008223static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008224{
8225 struct tg3 *tp = netdev_priv(dev);
8226
8227 if (features & NETIF_F_LOOPBACK) {
8228 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
8229 return;
8230
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008231 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008232 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008233 netif_carrier_on(tp->dev);
8234 spin_unlock_bh(&tp->lock);
8235 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8236 } else {
8237 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8238 return;
8239
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008240 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008241 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008242 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008243 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008244 spin_unlock_bh(&tp->lock);
8245 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8246 }
8247}
8248
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008249static netdev_features_t tg3_fix_features(struct net_device *dev,
8250 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008251{
8252 struct tg3 *tp = netdev_priv(dev);
8253
Joe Perches63c3a662011-04-26 08:12:10 +00008254 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008255 features &= ~NETIF_F_ALL_TSO;
8256
8257 return features;
8258}
8259
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008260static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008261{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008262 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008263
8264 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8265 tg3_set_loopback(dev, features);
8266
8267 return 0;
8268}
8269
Matt Carlson21f581a2009-08-28 14:00:25 +00008270static void tg3_rx_prodring_free(struct tg3 *tp,
8271 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008273 int i;
8274
Matt Carlson8fea32b2010-09-15 08:59:58 +00008275 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008276 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008277 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008278 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008279 tp->rx_pkt_map_sz);
8280
Joe Perches63c3a662011-04-26 08:12:10 +00008281 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008282 for (i = tpr->rx_jmb_cons_idx;
8283 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008284 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008285 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008286 TG3_RX_JMB_MAP_SZ);
8287 }
8288 }
8289
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008290 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292
Matt Carlson2c49a442010-09-30 10:34:35 +00008293 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008294 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008295 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008296
Joe Perches63c3a662011-04-26 08:12:10 +00008297 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008298 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008299 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008300 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 }
8302}
8303
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008304/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 *
8306 * The chip has been shut down and the driver detached from
8307 * the networking, so no interrupts or new tx packets will
8308 * end up in the driver. tp->{tx,}lock are held and thus
8309 * we may not sleep.
8310 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008311static int tg3_rx_prodring_alloc(struct tg3 *tp,
8312 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008313{
Matt Carlson287be122009-08-28 13:58:46 +00008314 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008316 tpr->rx_std_cons_idx = 0;
8317 tpr->rx_std_prod_idx = 0;
8318 tpr->rx_jmb_cons_idx = 0;
8319 tpr->rx_jmb_prod_idx = 0;
8320
Matt Carlson8fea32b2010-09-15 08:59:58 +00008321 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008322 memset(&tpr->rx_std_buffers[0], 0,
8323 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008324 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008325 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008326 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008327 goto done;
8328 }
8329
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008331 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332
Matt Carlson287be122009-08-28 13:58:46 +00008333 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008334 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008335 tp->dev->mtu > ETH_DATA_LEN)
8336 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8337 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008338
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 /* Initialize invariants of the rings, we only set this
8340 * stuff once. This works because the card does not
8341 * write into the rx buffer posting rings.
8342 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008343 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008344 struct tg3_rx_buffer_desc *rxd;
8345
Matt Carlson21f581a2009-08-28 14:00:25 +00008346 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008347 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8349 rxd->opaque = (RXD_OPAQUE_RING_STD |
8350 (i << RXD_OPAQUE_INDEX_SHIFT));
8351 }
8352
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008353 /* Now allocate fresh SKBs for each rx ring. */
8354 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008355 unsigned int frag_size;
8356
8357 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8358 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008359 netdev_warn(tp->dev,
8360 "Using a smaller RX standard ring. Only "
8361 "%d out of %d buffers were allocated "
8362 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008363 if (i == 0)
8364 goto initfail;
8365 tp->rx_pending = i;
8366 break;
8367 }
8368 }
8369
Joe Perches63c3a662011-04-26 08:12:10 +00008370 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008371 goto done;
8372
Matt Carlson2c49a442010-09-30 10:34:35 +00008373 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008374
Joe Perches63c3a662011-04-26 08:12:10 +00008375 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008376 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377
Matt Carlson2c49a442010-09-30 10:34:35 +00008378 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008379 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380
Matt Carlson0d86df82010-02-17 15:17:00 +00008381 rxd = &tpr->rx_jmb[i].std;
8382 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8383 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8384 RXD_FLAG_JUMBO;
8385 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8386 (i << RXD_OPAQUE_INDEX_SHIFT));
8387 }
8388
8389 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008390 unsigned int frag_size;
8391
8392 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8393 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008394 netdev_warn(tp->dev,
8395 "Using a smaller RX jumbo ring. Only %d "
8396 "out of %d buffers were allocated "
8397 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008398 if (i == 0)
8399 goto initfail;
8400 tp->rx_jumbo_pending = i;
8401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 }
8403 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008404
8405done:
Michael Chan32d8c572006-07-25 16:38:29 -07008406 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008407
8408initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008409 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008410 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411}
8412
Matt Carlson21f581a2009-08-28 14:00:25 +00008413static void tg3_rx_prodring_fini(struct tg3 *tp,
8414 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415{
Matt Carlson21f581a2009-08-28 14:00:25 +00008416 kfree(tpr->rx_std_buffers);
8417 tpr->rx_std_buffers = NULL;
8418 kfree(tpr->rx_jmb_buffers);
8419 tpr->rx_jmb_buffers = NULL;
8420 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008421 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8422 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008423 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008425 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008426 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8427 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008428 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008430}
8431
Matt Carlson21f581a2009-08-28 14:00:25 +00008432static int tg3_rx_prodring_init(struct tg3 *tp,
8433 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008434{
Matt Carlson2c49a442010-09-30 10:34:35 +00008435 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8436 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008437 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008438 return -ENOMEM;
8439
Matt Carlson4bae65c2010-11-24 08:31:52 +00008440 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8441 TG3_RX_STD_RING_BYTES(tp),
8442 &tpr->rx_std_mapping,
8443 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008444 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008445 goto err_out;
8446
Joe Perches63c3a662011-04-26 08:12:10 +00008447 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008448 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008449 GFP_KERNEL);
8450 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008451 goto err_out;
8452
Matt Carlson4bae65c2010-11-24 08:31:52 +00008453 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8454 TG3_RX_JMB_RING_BYTES(tp),
8455 &tpr->rx_jmb_mapping,
8456 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008457 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008458 goto err_out;
8459 }
8460
8461 return 0;
8462
8463err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008464 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008465 return -ENOMEM;
8466}
8467
8468/* Free up pending packets in all rx/tx rings.
8469 *
8470 * The chip has been shut down and the driver detached from
8471 * the networking, so no interrupts or new tx packets will
8472 * end up in the driver. tp->{tx,}lock is not held and we are not
8473 * in an interrupt context and thus may sleep.
8474 */
8475static void tg3_free_rings(struct tg3 *tp)
8476{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008477 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008478
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008479 for (j = 0; j < tp->irq_cnt; j++) {
8480 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008481
Matt Carlson8fea32b2010-09-15 08:59:58 +00008482 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008483
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008484 if (!tnapi->tx_buffers)
8485 continue;
8486
Matt Carlson0d681b22011-07-27 14:20:49 +00008487 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8488 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008489
Matt Carlson0d681b22011-07-27 14:20:49 +00008490 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008491 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008492
Matt Carlsonba1142e2011-11-04 09:15:00 +00008493 tg3_tx_skb_unmap(tnapi, i,
8494 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008495
8496 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008497 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008498 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008499 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008500}
8501
8502/* Initialize tx/rx rings for packet processing.
8503 *
8504 * The chip has been shut down and the driver detached from
8505 * the networking, so no interrupts or new tx packets will
8506 * end up in the driver. tp->{tx,}lock are held and thus
8507 * we may not sleep.
8508 */
8509static int tg3_init_rings(struct tg3 *tp)
8510{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008511 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008512
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008513 /* Free up all the SKBs. */
8514 tg3_free_rings(tp);
8515
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008516 for (i = 0; i < tp->irq_cnt; i++) {
8517 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008518
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008519 tnapi->last_tag = 0;
8520 tnapi->last_irq_tag = 0;
8521 tnapi->hw_status->status = 0;
8522 tnapi->hw_status->status_tag = 0;
8523 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8524
8525 tnapi->tx_prod = 0;
8526 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008527 if (tnapi->tx_ring)
8528 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008529
8530 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008531 if (tnapi->rx_rcb)
8532 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008533
Matt Carlson8fea32b2010-09-15 08:59:58 +00008534 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008535 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008536 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008537 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008538 }
Matt Carlson72334482009-08-28 14:03:01 +00008539
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008540 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008541}
8542
Michael Chan49a359e2012-09-28 07:12:37 +00008543static void tg3_mem_tx_release(struct tg3 *tp)
8544{
8545 int i;
8546
8547 for (i = 0; i < tp->irq_max; i++) {
8548 struct tg3_napi *tnapi = &tp->napi[i];
8549
8550 if (tnapi->tx_ring) {
8551 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8552 tnapi->tx_ring, tnapi->tx_desc_mapping);
8553 tnapi->tx_ring = NULL;
8554 }
8555
8556 kfree(tnapi->tx_buffers);
8557 tnapi->tx_buffers = NULL;
8558 }
8559}
8560
8561static int tg3_mem_tx_acquire(struct tg3 *tp)
8562{
8563 int i;
8564 struct tg3_napi *tnapi = &tp->napi[0];
8565
8566 /* If multivector TSS is enabled, vector 0 does not handle
8567 * tx interrupts. Don't allocate any resources for it.
8568 */
8569 if (tg3_flag(tp, ENABLE_TSS))
8570 tnapi++;
8571
8572 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8573 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8574 TG3_TX_RING_SIZE, GFP_KERNEL);
8575 if (!tnapi->tx_buffers)
8576 goto err_out;
8577
8578 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8579 TG3_TX_RING_BYTES,
8580 &tnapi->tx_desc_mapping,
8581 GFP_KERNEL);
8582 if (!tnapi->tx_ring)
8583 goto err_out;
8584 }
8585
8586 return 0;
8587
8588err_out:
8589 tg3_mem_tx_release(tp);
8590 return -ENOMEM;
8591}
8592
8593static void tg3_mem_rx_release(struct tg3 *tp)
8594{
8595 int i;
8596
8597 for (i = 0; i < tp->irq_max; i++) {
8598 struct tg3_napi *tnapi = &tp->napi[i];
8599
8600 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8601
8602 if (!tnapi->rx_rcb)
8603 continue;
8604
8605 dma_free_coherent(&tp->pdev->dev,
8606 TG3_RX_RCB_RING_BYTES(tp),
8607 tnapi->rx_rcb,
8608 tnapi->rx_rcb_mapping);
8609 tnapi->rx_rcb = NULL;
8610 }
8611}
8612
8613static int tg3_mem_rx_acquire(struct tg3 *tp)
8614{
8615 unsigned int i, limit;
8616
8617 limit = tp->rxq_cnt;
8618
8619 /* If RSS is enabled, we need a (dummy) producer ring
8620 * set on vector zero. This is the true hw prodring.
8621 */
8622 if (tg3_flag(tp, ENABLE_RSS))
8623 limit++;
8624
8625 for (i = 0; i < limit; i++) {
8626 struct tg3_napi *tnapi = &tp->napi[i];
8627
8628 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8629 goto err_out;
8630
8631 /* If multivector RSS is enabled, vector 0
8632 * does not handle rx or tx interrupts.
8633 * Don't allocate any resources for it.
8634 */
8635 if (!i && tg3_flag(tp, ENABLE_RSS))
8636 continue;
8637
Joe Perchesede23fa2013-08-26 22:45:23 -07008638 tnapi->rx_rcb = dma_zalloc_coherent(&tp->pdev->dev,
8639 TG3_RX_RCB_RING_BYTES(tp),
8640 &tnapi->rx_rcb_mapping,
8641 GFP_KERNEL);
Michael Chan49a359e2012-09-28 07:12:37 +00008642 if (!tnapi->rx_rcb)
8643 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008644 }
8645
8646 return 0;
8647
8648err_out:
8649 tg3_mem_rx_release(tp);
8650 return -ENOMEM;
8651}
8652
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008653/*
8654 * Must not be invoked with interrupt sources disabled and
8655 * the hardware shutdown down.
8656 */
8657static void tg3_free_consistent(struct tg3 *tp)
8658{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008659 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008660
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008661 for (i = 0; i < tp->irq_cnt; i++) {
8662 struct tg3_napi *tnapi = &tp->napi[i];
8663
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008664 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008665 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8666 tnapi->hw_status,
8667 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008668 tnapi->hw_status = NULL;
8669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008670 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008671
Michael Chan49a359e2012-09-28 07:12:37 +00008672 tg3_mem_rx_release(tp);
8673 tg3_mem_tx_release(tp);
8674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008676 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8677 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 tp->hw_stats = NULL;
8679 }
8680}
8681
8682/*
8683 * Must not be invoked with interrupt sources disabled and
8684 * the hardware shutdown down. Can sleep.
8685 */
8686static int tg3_alloc_consistent(struct tg3 *tp)
8687{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008688 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008689
Joe Perchesede23fa2013-08-26 22:45:23 -07008690 tp->hw_stats = dma_zalloc_coherent(&tp->pdev->dev,
8691 sizeof(struct tg3_hw_stats),
8692 &tp->stats_mapping, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 if (!tp->hw_stats)
8694 goto err_out;
8695
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008696 for (i = 0; i < tp->irq_cnt; i++) {
8697 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008698 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008699
Joe Perchesede23fa2013-08-26 22:45:23 -07008700 tnapi->hw_status = dma_zalloc_coherent(&tp->pdev->dev,
8701 TG3_HW_STATUS_SIZE,
8702 &tnapi->status_mapping,
8703 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008704 if (!tnapi->hw_status)
8705 goto err_out;
8706
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008707 sblk = tnapi->hw_status;
8708
Michael Chan49a359e2012-09-28 07:12:37 +00008709 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008710 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008711
Michael Chan49a359e2012-09-28 07:12:37 +00008712 /*
8713 * When RSS is enabled, the status block format changes
8714 * slightly. The "rx_jumbo_consumer", "reserved",
8715 * and "rx_mini_consumer" members get mapped to the
8716 * other three rx return ring producer indexes.
8717 */
8718 switch (i) {
8719 case 1:
8720 prodptr = &sblk->idx[0].rx_producer;
8721 break;
8722 case 2:
8723 prodptr = &sblk->rx_jumbo_consumer;
8724 break;
8725 case 3:
8726 prodptr = &sblk->reserved;
8727 break;
8728 case 4:
8729 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008730 break;
8731 }
Michael Chan49a359e2012-09-28 07:12:37 +00008732 tnapi->rx_rcb_prod_idx = prodptr;
8733 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008734 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008735 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008736 }
8737
Michael Chan49a359e2012-09-28 07:12:37 +00008738 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8739 goto err_out;
8740
Linus Torvalds1da177e2005-04-16 15:20:36 -07008741 return 0;
8742
8743err_out:
8744 tg3_free_consistent(tp);
8745 return -ENOMEM;
8746}
8747
8748#define MAX_WAIT_CNT 1000
8749
8750/* To stop a block, clear the enable bit and poll till it
8751 * clears. tp->lock is held.
8752 */
Joe Perches953c96e2013-04-09 10:18:14 +00008753static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008754{
8755 unsigned int i;
8756 u32 val;
8757
Joe Perches63c3a662011-04-26 08:12:10 +00008758 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 switch (ofs) {
8760 case RCVLSC_MODE:
8761 case DMAC_MODE:
8762 case MBFREE_MODE:
8763 case BUFMGR_MODE:
8764 case MEMARB_MODE:
8765 /* We can't enable/disable these bits of the
8766 * 5705/5750, just say success.
8767 */
8768 return 0;
8769
8770 default:
8771 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 }
8774
8775 val = tr32(ofs);
8776 val &= ~enable_bit;
8777 tw32_f(ofs, val);
8778
8779 for (i = 0; i < MAX_WAIT_CNT; i++) {
Gavin Shan6d446ec2013-06-25 15:24:32 +08008780 if (pci_channel_offline(tp->pdev)) {
8781 dev_err(&tp->pdev->dev,
8782 "tg3_stop_block device offline, "
8783 "ofs=%lx enable_bit=%x\n",
8784 ofs, enable_bit);
8785 return -ENODEV;
8786 }
8787
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788 udelay(100);
8789 val = tr32(ofs);
8790 if ((val & enable_bit) == 0)
8791 break;
8792 }
8793
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008794 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008795 dev_err(&tp->pdev->dev,
8796 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8797 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 return -ENODEV;
8799 }
8800
8801 return 0;
8802}
8803
8804/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008805static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806{
8807 int i, err;
8808
8809 tg3_disable_ints(tp);
8810
Gavin Shan6d446ec2013-06-25 15:24:32 +08008811 if (pci_channel_offline(tp->pdev)) {
8812 tp->rx_mode &= ~(RX_MODE_ENABLE | TX_MODE_ENABLE);
8813 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8814 err = -ENODEV;
8815 goto err_no_dev;
8816 }
8817
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 tp->rx_mode &= ~RX_MODE_ENABLE;
8819 tw32_f(MAC_RX_MODE, tp->rx_mode);
8820 udelay(10);
8821
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008822 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8823 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8824 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8825 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8826 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8827 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008829 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8830 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8831 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8832 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8833 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8834 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8835 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008836
8837 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8838 tw32_f(MAC_MODE, tp->mac_mode);
8839 udelay(40);
8840
8841 tp->tx_mode &= ~TX_MODE_ENABLE;
8842 tw32_f(MAC_TX_MODE, tp->tx_mode);
8843
8844 for (i = 0; i < MAX_WAIT_CNT; i++) {
8845 udelay(100);
8846 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8847 break;
8848 }
8849 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008850 dev_err(&tp->pdev->dev,
8851 "%s timed out, TX_MODE_ENABLE will not clear "
8852 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008853 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 }
8855
Michael Chane6de8ad2005-05-05 14:42:41 -07008856 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008857 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8858 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859
8860 tw32(FTQ_RESET, 0xffffffff);
8861 tw32(FTQ_RESET, 0x00000000);
8862
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008863 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8864 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865
Gavin Shan6d446ec2013-06-25 15:24:32 +08008866err_no_dev:
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008867 for (i = 0; i < tp->irq_cnt; i++) {
8868 struct tg3_napi *tnapi = &tp->napi[i];
8869 if (tnapi->hw_status)
8870 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 return err;
8874}
8875
Michael Chanee6a99b2007-07-18 21:49:10 -07008876/* Save PCI command register before chip reset */
8877static void tg3_save_pci_state(struct tg3 *tp)
8878{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008879 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008880}
8881
8882/* Restore PCI state after chip reset */
8883static void tg3_restore_pci_state(struct tg3 *tp)
8884{
8885 u32 val;
8886
8887 /* Re-enable indirect register accesses. */
8888 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8889 tp->misc_host_ctrl);
8890
8891 /* Set MAX PCI retry to zero. */
8892 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008893 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008894 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008895 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008896 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008897 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008898 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008899 PCISTATE_ALLOW_APE_SHMEM_WR |
8900 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008901 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8902
Matt Carlson8a6eac92007-10-21 16:17:55 -07008903 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008904
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008905 if (!tg3_flag(tp, PCI_EXPRESS)) {
8906 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8907 tp->pci_cacheline_sz);
8908 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8909 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008910 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008911
Michael Chanee6a99b2007-07-18 21:49:10 -07008912 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008913 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008914 u16 pcix_cmd;
8915
8916 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8917 &pcix_cmd);
8918 pcix_cmd &= ~PCI_X_CMD_ERO;
8919 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8920 pcix_cmd);
8921 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008922
Joe Perches63c3a662011-04-26 08:12:10 +00008923 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008924
8925 /* Chip reset on 5780 will reset MSI enable bit,
8926 * so need to restore it.
8927 */
Joe Perches63c3a662011-04-26 08:12:10 +00008928 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008929 u16 ctrl;
8930
8931 pci_read_config_word(tp->pdev,
8932 tp->msi_cap + PCI_MSI_FLAGS,
8933 &ctrl);
8934 pci_write_config_word(tp->pdev,
8935 tp->msi_cap + PCI_MSI_FLAGS,
8936 ctrl | PCI_MSI_FLAGS_ENABLE);
8937 val = tr32(MSGINT_MODE);
8938 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8939 }
8940 }
8941}
8942
Nithin Sujirf82995b2014-01-03 10:09:13 -08008943static void tg3_override_clk(struct tg3 *tp)
8944{
8945 u32 val;
8946
8947 switch (tg3_asic_rev(tp)) {
8948 case ASIC_REV_5717:
8949 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8950 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE, val |
8951 TG3_CPMU_MAC_ORIDE_ENABLE);
8952 break;
8953
8954 case ASIC_REV_5719:
8955 case ASIC_REV_5720:
8956 tw32(TG3_CPMU_CLCK_ORIDE, CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8957 break;
8958
8959 default:
8960 return;
8961 }
8962}
8963
8964static void tg3_restore_clk(struct tg3 *tp)
8965{
8966 u32 val;
8967
8968 switch (tg3_asic_rev(tp)) {
8969 case ASIC_REV_5717:
8970 val = tr32(TG3_CPMU_CLCK_ORIDE_ENABLE);
8971 tw32(TG3_CPMU_CLCK_ORIDE_ENABLE,
8972 val & ~TG3_CPMU_MAC_ORIDE_ENABLE);
8973 break;
8974
8975 case ASIC_REV_5719:
8976 case ASIC_REV_5720:
8977 val = tr32(TG3_CPMU_CLCK_ORIDE);
8978 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8979 break;
8980
8981 default:
8982 return;
8983 }
8984}
8985
Linus Torvalds1da177e2005-04-16 15:20:36 -07008986/* tp->lock is held. */
8987static int tg3_chip_reset(struct tg3 *tp)
8988{
8989 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008990 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008991 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008992
Rafael J. Wysocki8496e852013-12-01 02:34:37 +01008993 if (!pci_device_is_present(tp->pdev))
8994 return -ENODEV;
8995
David S. Millerf49639e2006-06-09 11:58:36 -07008996 tg3_nvram_lock(tp);
8997
Matt Carlson77b483f2008-08-15 14:07:24 -07008998 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8999
David S. Millerf49639e2006-06-09 11:58:36 -07009000 /* No matching tg3_nvram_unlock() after this because
9001 * chip reset below will undo the nvram lock.
9002 */
9003 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004
Michael Chanee6a99b2007-07-18 21:49:10 -07009005 /* GRC_MISC_CFG core clock reset will clear the memory
9006 * enable bit in PCI register 4 and the MSI enable bit
9007 * on some chips, so we save relevant registers here.
9008 */
9009 tg3_save_pci_state(tp);
9010
Joe Perches41535772013-02-16 11:20:04 +00009011 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009012 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08009013 tw32(GRC_FASTBOOT_PC, 0);
9014
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 /*
9016 * We must avoid the readl() that normally takes place.
9017 * It locks machines, causes machine checks, and other
9018 * fun things. So, temporarily disable the 5701
9019 * hardware workaround, while we do the reset.
9020 */
Michael Chan1ee582d2005-08-09 20:16:46 -07009021 write_op = tp->write32;
9022 if (write_op == tg3_write_flush_reg32)
9023 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024
Michael Chand18edcb2007-03-24 20:57:11 -07009025 /* Prevent the irq handler from reading or writing PCI registers
9026 * during chip reset when the memory enable bit in the PCI command
9027 * register may be cleared. The chip does not generate interrupt
9028 * at this time, but the irq handler may still be called due to irq
9029 * sharing or irqpoll.
9030 */
Joe Perches63c3a662011-04-26 08:12:10 +00009031 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009032 for (i = 0; i < tp->irq_cnt; i++) {
9033 struct tg3_napi *tnapi = &tp->napi[i];
9034 if (tnapi->hw_status) {
9035 tnapi->hw_status->status = 0;
9036 tnapi->hw_status->status_tag = 0;
9037 }
9038 tnapi->last_tag = 0;
9039 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07009040 }
Michael Chand18edcb2007-03-24 20:57:11 -07009041 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00009042
9043 for (i = 0; i < tp->irq_cnt; i++)
9044 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07009045
Joe Perches41535772013-02-16 11:20:04 +00009046 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00009047 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9048 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
9049 }
9050
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 /* do the reset */
9052 val = GRC_MISC_CFG_CORECLK_RESET;
9053
Joe Perches63c3a662011-04-26 08:12:10 +00009054 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00009055 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00009056 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009057 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00009058 tr32(TG3_PCIE_PHY_TSTCTL) ==
9059 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
9060 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
9061
Joe Perches41535772013-02-16 11:20:04 +00009062 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009063 tw32(GRC_MISC_CFG, (1 << 29));
9064 val |= (1 << 29);
9065 }
9066 }
9067
Joe Perches41535772013-02-16 11:20:04 +00009068 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07009069 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
9070 tw32(GRC_VCPU_EXT_CTRL,
9071 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
9072 }
9073
Nithin Sujirf82995b2014-01-03 10:09:13 -08009074 /* Set the clock to the highest frequency to avoid timeouts. With link
9075 * aware mode, the clock speed could be slow and bootcode does not
9076 * complete within the expected time. Override the clock to allow the
9077 * bootcode to finish sooner and then restore it.
9078 */
9079 tg3_override_clk(tp);
9080
Matt Carlsonf37500d2010-08-02 11:25:59 +00009081 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00009082 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00009084
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085 tw32(GRC_MISC_CFG, val);
9086
Michael Chan1ee582d2005-08-09 20:16:46 -07009087 /* restore 5701 hardware bug workaround write method */
9088 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009089
9090 /* Unfortunately, we have to delay before the PCI read back.
9091 * Some 575X chips even will not respond to a PCI cfg access
9092 * when the reset command is given to the chip.
9093 *
9094 * How do these hardware designers expect things to work
9095 * properly if the PCI write is posted for a long period
9096 * of time? It is always necessary to have some method by
9097 * which a register read back can occur to push the write
9098 * out which does the reset.
9099 *
9100 * For most tg3 variants the trick below was working.
9101 * Ho hum...
9102 */
9103 udelay(120);
9104
9105 /* Flush PCI posted writes. The normal MMIO registers
9106 * are inaccessible at this time so this is the only
9107 * way to make this reliably (actually, this is no longer
9108 * the case, see above). I tried to use indirect
9109 * register read/write but this upset some 5701 variants.
9110 */
9111 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
9112
9113 udelay(120);
9114
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009115 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00009116 u16 val16;
9117
Joe Perches41535772013-02-16 11:20:04 +00009118 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07009119 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120 u32 cfg_val;
9121
9122 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07009123 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 udelay(100);
9125
9126 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
9127 pci_write_config_dword(tp->pdev, 0xc4,
9128 cfg_val | (1 << 15));
9129 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009130
Matt Carlsone7126992009-08-25 10:08:16 +00009131 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009132 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00009133 /*
9134 * Older PCIe devices only support the 128 byte
9135 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009136 */
Joe Perches63c3a662011-04-26 08:12:10 +00009137 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009138 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
9139 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009140
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009141 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06009142 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08009143 PCI_EXP_DEVSTA_CED |
9144 PCI_EXP_DEVSTA_NFED |
9145 PCI_EXP_DEVSTA_FED |
9146 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009147 }
9148
Michael Chanee6a99b2007-07-18 21:49:10 -07009149 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009150
Joe Perches63c3a662011-04-26 08:12:10 +00009151 tg3_flag_clear(tp, CHIP_RESETTING);
9152 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07009153
Michael Chanee6a99b2007-07-18 21:49:10 -07009154 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009155 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07009156 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07009157 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158
Joe Perches41535772013-02-16 11:20:04 +00009159 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160 tg3_stop_fw(tp);
9161 tw32(0x5000, 0x400);
9162 }
9163
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00009164 if (tg3_flag(tp, IS_SSB_CORE)) {
9165 /*
9166 * BCM4785: In order to avoid repercussions from using
9167 * potentially defective internal ROM, stop the Rx RISC CPU,
9168 * which is not required.
9169 */
9170 tg3_stop_fw(tp);
9171 tg3_halt_cpu(tp, RX_CPU_BASE);
9172 }
9173
Nithin Sujirfb03a432013-05-21 12:57:32 +00009174 err = tg3_poll_fw(tp);
9175 if (err)
9176 return err;
9177
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 tw32(GRC_MODE, tp->grc_mode);
9179
Joe Perches41535772013-02-16 11:20:04 +00009180 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009181 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182
9183 tw32(0xc4, val | (1 << 15));
9184 }
9185
9186 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00009187 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009189 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
9191 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9192 }
9193
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009194 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009195 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009196 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009197 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00009198 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009199 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00009201 val = 0;
9202
9203 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204 udelay(40);
9205
Matt Carlson77b483f2008-08-15 14:07:24 -07009206 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
9207
Matt Carlson0a9140c2009-08-28 12:27:50 +00009208 tg3_mdio_start(tp);
9209
Joe Perches63c3a662011-04-26 08:12:10 +00009210 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00009211 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
9212 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009213 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009214 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
9216 tw32(0x7c00, val | (1 << 25));
9217 }
9218
Nithin Sujirf82995b2014-01-03 10:09:13 -08009219 tg3_restore_clk(tp);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009220
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00009222 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009223 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
9224 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
9225
Joe Perches63c3a662011-04-26 08:12:10 +00009226 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
9228 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
9229 u32 nic_cfg;
9230
9231 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
9232 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00009233 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009234 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00009235 if (tg3_flag(tp, 5750_PLUS))
9236 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00009237
9238 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
9239 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
9240 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
9241 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
9242 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243 }
9244 }
9245
9246 return 0;
9247}
9248
Matt Carlson65ec6982012-02-28 23:33:37 +00009249static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
9250static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Michael Chane565eec2014-01-03 10:09:12 -08009251static void __tg3_set_rx_mode(struct net_device *);
Matt Carlson92feeab2011-12-08 14:40:14 +00009252
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009254static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255{
9256 int err;
9257
9258 tg3_stop_fw(tp);
9259
Michael Chan944d9802005-05-29 14:57:48 -07009260 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261
David S. Millerb3b7d6b2005-05-05 14:40:20 -07009262 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263 err = tg3_chip_reset(tp);
9264
Joe Perches953c96e2013-04-09 10:18:14 +00009265 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00009266
Michael Chan944d9802005-05-29 14:57:48 -07009267 tg3_write_sig_legacy(tp, kind);
9268 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269
Matt Carlson92feeab2011-12-08 14:40:14 +00009270 if (tp->hw_stats) {
9271 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05009272 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00009273 tg3_get_estats(tp, &tp->estats_prev);
9274
9275 /* And make sure the next sample is new data */
9276 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
9277 }
9278
Nithin Sujir4bc814a2013-09-20 16:46:59 -07009279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280}
9281
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282static int tg3_set_mac_addr(struct net_device *dev, void *p)
9283{
9284 struct tg3 *tp = netdev_priv(dev);
9285 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00009286 int err = 0;
9287 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288
Michael Chanf9804dd2005-09-27 12:13:10 -07009289 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00009290 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07009291
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
9293
Michael Chane75f7c92006-03-20 21:33:26 -08009294 if (!netif_running(dev))
9295 return 0;
9296
Joe Perches63c3a662011-04-26 08:12:10 +00009297 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009298 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009299
Michael Chan986e0ae2007-05-05 12:10:20 -07009300 addr0_high = tr32(MAC_ADDR_0_HIGH);
9301 addr0_low = tr32(MAC_ADDR_0_LOW);
9302 addr1_high = tr32(MAC_ADDR_1_HIGH);
9303 addr1_low = tr32(MAC_ADDR_1_LOW);
9304
9305 /* Skip MAC addr 1 if ASF is using it. */
9306 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9307 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009308 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009309 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009310 spin_lock_bh(&tp->lock);
9311 __tg3_set_mac_addr(tp, skip_mac_1);
Michael Chane565eec2014-01-03 10:09:12 -08009312 __tg3_set_rx_mode(dev);
Michael Chan986e0ae2007-05-05 12:10:20 -07009313 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009314
Michael Chanb9ec6c12006-07-25 16:37:27 -07009315 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316}
9317
9318/* tp->lock is held. */
9319static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9320 dma_addr_t mapping, u32 maxlen_flags,
9321 u32 nic_addr)
9322{
9323 tg3_write_mem(tp,
9324 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9325 ((u64) mapping >> 32));
9326 tg3_write_mem(tp,
9327 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9328 ((u64) mapping & 0xffffffff));
9329 tg3_write_mem(tp,
9330 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9331 maxlen_flags);
9332
Joe Perches63c3a662011-04-26 08:12:10 +00009333 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334 tg3_write_mem(tp,
9335 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9336 nic_addr);
9337}
9338
Michael Chana489b6d2012-09-28 07:12:39 +00009339
9340static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009341{
Michael Chana489b6d2012-09-28 07:12:39 +00009342 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009343
Joe Perches63c3a662011-04-26 08:12:10 +00009344 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009345 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9346 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9347 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009348 } else {
9349 tw32(HOSTCC_TXCOL_TICKS, 0);
9350 tw32(HOSTCC_TXMAX_FRAMES, 0);
9351 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009352
9353 for (; i < tp->txq_cnt; i++) {
9354 u32 reg;
9355
9356 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9357 tw32(reg, ec->tx_coalesce_usecs);
9358 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9359 tw32(reg, ec->tx_max_coalesced_frames);
9360 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9361 tw32(reg, ec->tx_max_coalesced_frames_irq);
9362 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009363 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009364
Michael Chana489b6d2012-09-28 07:12:39 +00009365 for (; i < tp->irq_max - 1; i++) {
9366 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9367 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9368 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9369 }
9370}
9371
9372static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9373{
9374 int i = 0;
9375 u32 limit = tp->rxq_cnt;
9376
Joe Perches63c3a662011-04-26 08:12:10 +00009377 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009378 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9379 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9380 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009381 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009382 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009383 tw32(HOSTCC_RXCOL_TICKS, 0);
9384 tw32(HOSTCC_RXMAX_FRAMES, 0);
9385 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009386 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009387
Michael Chana489b6d2012-09-28 07:12:39 +00009388 for (; i < limit; i++) {
9389 u32 reg;
9390
9391 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9392 tw32(reg, ec->rx_coalesce_usecs);
9393 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9394 tw32(reg, ec->rx_max_coalesced_frames);
9395 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9396 tw32(reg, ec->rx_max_coalesced_frames_irq);
9397 }
9398
9399 for (; i < tp->irq_max - 1; i++) {
9400 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9401 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9402 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9403 }
9404}
9405
9406static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9407{
9408 tg3_coal_tx_init(tp, ec);
9409 tg3_coal_rx_init(tp, ec);
9410
Joe Perches63c3a662011-04-26 08:12:10 +00009411 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009412 u32 val = ec->stats_block_coalesce_usecs;
9413
Matt Carlsonb6080e12009-09-01 13:12:00 +00009414 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9415 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9416
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009417 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009418 val = 0;
9419
9420 tw32(HOSTCC_STAT_COAL_TICKS, val);
9421 }
9422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
9424/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009425static void tg3_tx_rcbs_disable(struct tg3 *tp)
9426{
9427 u32 txrcb, limit;
9428
9429 /* Disable all transmit rings but the first. */
9430 if (!tg3_flag(tp, 5705_PLUS))
9431 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
9432 else if (tg3_flag(tp, 5717_PLUS))
9433 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
9434 else if (tg3_flag(tp, 57765_CLASS) ||
9435 tg3_asic_rev(tp) == ASIC_REV_5762)
9436 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
9437 else
9438 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9439
9440 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9441 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9442 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9443 BDINFO_FLAGS_DISABLED);
9444}
9445
9446/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009447static void tg3_tx_rcbs_init(struct tg3 *tp)
9448{
9449 int i = 0;
9450 u32 txrcb = NIC_SRAM_SEND_RCB;
9451
9452 if (tg3_flag(tp, ENABLE_TSS))
9453 i++;
9454
9455 for (; i < tp->irq_max; i++, txrcb += TG3_BDINFO_SIZE) {
9456 struct tg3_napi *tnapi = &tp->napi[i];
9457
9458 if (!tnapi->tx_ring)
9459 continue;
9460
9461 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9462 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
9463 NIC_SRAM_TX_BUFFER_DESC);
9464 }
9465}
9466
9467/* tp->lock is held. */
Nithin Sujir328947f2013-05-23 11:11:24 +00009468static void tg3_rx_ret_rcbs_disable(struct tg3 *tp)
9469{
9470 u32 rxrcb, limit;
9471
9472 /* Disable all receive return rings but the first. */
9473 if (tg3_flag(tp, 5717_PLUS))
9474 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
9475 else if (!tg3_flag(tp, 5705_PLUS))
9476 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
9477 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9478 tg3_asic_rev(tp) == ASIC_REV_5762 ||
9479 tg3_flag(tp, 57765_CLASS))
9480 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9481 else
9482 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9483
9484 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9485 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9486 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9487 BDINFO_FLAGS_DISABLED);
9488}
9489
9490/* tp->lock is held. */
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009491static void tg3_rx_ret_rcbs_init(struct tg3 *tp)
9492{
9493 int i = 0;
9494 u32 rxrcb = NIC_SRAM_RCV_RET_RCB;
9495
9496 if (tg3_flag(tp, ENABLE_RSS))
9497 i++;
9498
9499 for (; i < tp->irq_max; i++, rxrcb += TG3_BDINFO_SIZE) {
9500 struct tg3_napi *tnapi = &tp->napi[i];
9501
9502 if (!tnapi->rx_rcb)
9503 continue;
9504
9505 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
9506 (tp->rx_ret_ring_mask + 1) <<
9507 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
9508 }
9509}
9510
9511/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009512static void tg3_rings_reset(struct tg3 *tp)
9513{
9514 int i;
Nithin Sujir328947f2013-05-23 11:11:24 +00009515 u32 stblk;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009516 struct tg3_napi *tnapi = &tp->napi[0];
9517
Nithin Sujir328947f2013-05-23 11:11:24 +00009518 tg3_tx_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009519
Nithin Sujir328947f2013-05-23 11:11:24 +00009520 tg3_rx_ret_rcbs_disable(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009521
9522 /* Disable interrupts */
9523 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009524 tp->napi[0].chk_msi_cnt = 0;
9525 tp->napi[0].last_rx_cons = 0;
9526 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009527
9528 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009529 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009530 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009531 tp->napi[i].tx_prod = 0;
9532 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009533 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009534 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009535 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9536 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009537 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009538 tp->napi[i].last_rx_cons = 0;
9539 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009540 }
Joe Perches63c3a662011-04-26 08:12:10 +00009541 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009542 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009543 } else {
9544 tp->napi[0].tx_prod = 0;
9545 tp->napi[0].tx_cons = 0;
9546 tw32_mailbox(tp->napi[0].prodmbox, 0);
9547 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9548 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009549
9550 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009551 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009552 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9553 for (i = 0; i < 16; i++)
9554 tw32_tx_mbox(mbox + i * 8, 0);
9555 }
9556
Matt Carlson2d31eca2009-09-01 12:53:31 +00009557 /* Clear status block in ram. */
9558 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9559
9560 /* Set status block DMA address */
9561 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9562 ((u64) tnapi->status_mapping >> 32));
9563 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9564 ((u64) tnapi->status_mapping & 0xffffffff));
9565
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009566 stblk = HOSTCC_STATBLCK_RING1;
9567
9568 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9569 u64 mapping = (u64)tnapi->status_mapping;
9570 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9571 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009572 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009573
9574 /* Clear status block in ram. */
9575 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009576 }
Nithin Sujir32ba19e2013-05-23 11:11:23 +00009577
9578 tg3_tx_rcbs_init(tp);
9579 tg3_rx_ret_rcbs_init(tp);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009580}
9581
Matt Carlsoneb07a942011-04-20 07:57:36 +00009582static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9583{
9584 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9585
Joe Perches63c3a662011-04-26 08:12:10 +00009586 if (!tg3_flag(tp, 5750_PLUS) ||
9587 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009588 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9589 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009590 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009591 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009592 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9593 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009594 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9595 else
9596 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9597
9598 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9599 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9600
9601 val = min(nic_rep_thresh, host_rep_thresh);
9602 tw32(RCVBDI_STD_THRESH, val);
9603
Joe Perches63c3a662011-04-26 08:12:10 +00009604 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009605 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9606
Joe Perches63c3a662011-04-26 08:12:10 +00009607 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009608 return;
9609
Matt Carlson513aa6e2011-11-21 15:01:18 +00009610 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009611
9612 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9613
9614 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9615 tw32(RCVBDI_JUMBO_THRESH, val);
9616
Joe Perches63c3a662011-04-26 08:12:10 +00009617 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009618 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9619}
9620
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009621static inline u32 calc_crc(unsigned char *buf, int len)
9622{
9623 u32 reg;
9624 u32 tmp;
9625 int j, k;
9626
9627 reg = 0xffffffff;
9628
9629 for (j = 0; j < len; j++) {
9630 reg ^= buf[j];
9631
9632 for (k = 0; k < 8; k++) {
9633 tmp = reg & 0x01;
9634
9635 reg >>= 1;
9636
9637 if (tmp)
9638 reg ^= 0xedb88320;
9639 }
9640 }
9641
9642 return ~reg;
9643}
9644
9645static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9646{
9647 /* accept or reject all multicast frames */
9648 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9649 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9650 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9651 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9652}
9653
9654static void __tg3_set_rx_mode(struct net_device *dev)
9655{
9656 struct tg3 *tp = netdev_priv(dev);
9657 u32 rx_mode;
9658
9659 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9660 RX_MODE_KEEP_VLAN_TAG);
9661
9662#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9663 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9664 * flag clear.
9665 */
9666 if (!tg3_flag(tp, ENABLE_ASF))
9667 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9668#endif
9669
9670 if (dev->flags & IFF_PROMISC) {
9671 /* Promiscuous mode. */
9672 rx_mode |= RX_MODE_PROMISC;
9673 } else if (dev->flags & IFF_ALLMULTI) {
9674 /* Accept all multicast. */
9675 tg3_set_multi(tp, 1);
9676 } else if (netdev_mc_empty(dev)) {
9677 /* Reject all multicast. */
9678 tg3_set_multi(tp, 0);
9679 } else {
9680 /* Accept one or more multicast(s). */
9681 struct netdev_hw_addr *ha;
9682 u32 mc_filter[4] = { 0, };
9683 u32 regidx;
9684 u32 bit;
9685 u32 crc;
9686
9687 netdev_for_each_mc_addr(ha, dev) {
9688 crc = calc_crc(ha->addr, ETH_ALEN);
9689 bit = ~crc & 0x7f;
9690 regidx = (bit & 0x60) >> 5;
9691 bit &= 0x1f;
9692 mc_filter[regidx] |= (1 << bit);
9693 }
9694
9695 tw32(MAC_HASH_REG_0, mc_filter[0]);
9696 tw32(MAC_HASH_REG_1, mc_filter[1]);
9697 tw32(MAC_HASH_REG_2, mc_filter[2]);
9698 tw32(MAC_HASH_REG_3, mc_filter[3]);
9699 }
9700
Michael Chane565eec2014-01-03 10:09:12 -08009701 if (netdev_uc_count(dev) > TG3_MAX_UCAST_ADDR(tp)) {
9702 rx_mode |= RX_MODE_PROMISC;
9703 } else if (!(dev->flags & IFF_PROMISC)) {
9704 /* Add all entries into to the mac addr filter list */
9705 int i = 0;
9706 struct netdev_hw_addr *ha;
9707
9708 netdev_for_each_uc_addr(ha, dev) {
9709 __tg3_set_one_mac_addr(tp, ha->addr,
9710 i + TG3_UCAST_ADDR_IDX(tp));
9711 i++;
9712 }
9713 }
9714
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009715 if (rx_mode != tp->rx_mode) {
9716 tp->rx_mode = rx_mode;
9717 tw32_f(MAC_RX_MODE, rx_mode);
9718 udelay(10);
9719 }
9720}
9721
Michael Chan91024262012-09-28 07:12:38 +00009722static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009723{
9724 int i;
9725
9726 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009727 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009728}
9729
9730static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009731{
9732 int i;
9733
9734 if (!tg3_flag(tp, SUPPORT_MSIX))
9735 return;
9736
Michael Chan0b3ba052012-11-14 14:44:29 +00009737 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009738 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009739 return;
9740 }
9741
9742 /* Validate table against current IRQ count */
9743 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009744 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009745 break;
9746 }
9747
9748 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009749 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009750}
9751
Matt Carlson90415472011-12-16 13:33:23 +00009752static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009753{
9754 int i = 0;
9755 u32 reg = MAC_RSS_INDIR_TBL_0;
9756
9757 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9758 u32 val = tp->rss_ind_tbl[i];
9759 i++;
9760 for (; i % 8; i++) {
9761 val <<= 4;
9762 val |= tp->rss_ind_tbl[i];
9763 }
9764 tw32(reg, val);
9765 reg += 4;
9766 }
9767}
9768
Nithin Sujir9bc297e2013-06-03 09:19:34 +00009769static inline u32 tg3_lso_rd_dma_workaround_bit(struct tg3 *tp)
9770{
9771 if (tg3_asic_rev(tp) == ASIC_REV_5719)
9772 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5719;
9773 else
9774 return TG3_LSO_RD_DMA_TX_LENGTH_WA_5720;
9775}
9776
Matt Carlson2d31eca2009-09-01 12:53:31 +00009777/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009778static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009779{
9780 u32 val, rdmac_mode;
9781 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009782 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009783
9784 tg3_disable_ints(tp);
9785
9786 tg3_stop_fw(tp);
9787
9788 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9789
Joe Perches63c3a662011-04-26 08:12:10 +00009790 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009791 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009793 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9794 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9795 tg3_phy_pull_config(tp);
Nithin Sujir400dfba2013-05-18 06:26:53 +00009796 tg3_eee_pull_config(tp, NULL);
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009797 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9798 }
9799
Nithin Sujir400dfba2013-05-18 06:26:53 +00009800 /* Enable MAC control of LPI */
9801 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
9802 tg3_setup_eee(tp);
9803
Matt Carlson603f1172010-02-12 14:47:10 +00009804 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009805 tg3_phy_reset(tp);
9806
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 err = tg3_chip_reset(tp);
9808 if (err)
9809 return err;
9810
9811 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9812
Joe Perches41535772013-02-16 11:20:04 +00009813 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009814 val = tr32(TG3_CPMU_CTRL);
9815 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9816 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009817
9818 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9819 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9820 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9821 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9822
9823 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9824 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9825 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9826 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9827
9828 val = tr32(TG3_CPMU_HST_ACC);
9829 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9830 val |= CPMU_HST_ACC_MACCLK_6_25;
9831 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009832 }
9833
Joe Perches41535772013-02-16 11:20:04 +00009834 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009835 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9836 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9837 PCIE_PWR_MGMT_L1_THRESH_4MS;
9838 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009839
9840 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9841 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9842
9843 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009844
Matt Carlsonf40386c2009-11-02 14:24:02 +00009845 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9846 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009847 }
9848
Joe Perches63c3a662011-04-26 08:12:10 +00009849 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009850 u32 grc_mode = tr32(GRC_MODE);
9851
9852 /* Access the lower 1K of PL PCIE block registers. */
9853 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9854 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9855
9856 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9857 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9858 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9859
9860 tw32(GRC_MODE, grc_mode);
9861 }
9862
Matt Carlson55086ad2011-12-14 11:09:59 +00009863 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009864 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009865 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009866
Matt Carlson5093eed2010-11-24 08:31:45 +00009867 /* Access the lower 1K of PL PCIE block registers. */
9868 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9869 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009870
Matt Carlson5093eed2010-11-24 08:31:45 +00009871 val = tr32(TG3_PCIE_TLDLPL_PORT +
9872 TG3_PCIE_PL_LO_PHYCTL5);
9873 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9874 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009875
Matt Carlson5093eed2010-11-24 08:31:45 +00009876 tw32(GRC_MODE, grc_mode);
9877 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009878
Joe Perches41535772013-02-16 11:20:04 +00009879 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009880 u32 grc_mode;
9881
9882 /* Fix transmit hangs */
9883 val = tr32(TG3_CPMU_PADRNG_CTL);
9884 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9885 tw32(TG3_CPMU_PADRNG_CTL, val);
9886
9887 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009888
9889 /* Access the lower 1K of DL PCIE block registers. */
9890 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9891 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9892
9893 val = tr32(TG3_PCIE_TLDLPL_PORT +
9894 TG3_PCIE_DL_LO_FTSMAX);
9895 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9896 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9897 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9898
9899 tw32(GRC_MODE, grc_mode);
9900 }
9901
Matt Carlsona977dbe2010-04-12 06:58:26 +00009902 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9903 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9904 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9905 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009906 }
9907
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 /* This works around an issue with Athlon chipsets on
9909 * B3 tigon3 silicon. This bit has no effect on any
9910 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009911 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912 */
Joe Perches63c3a662011-04-26 08:12:10 +00009913 if (!tg3_flag(tp, CPMU_PRESENT)) {
9914 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009915 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9916 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009918
Joe Perches41535772013-02-16 11:20:04 +00009919 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009920 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 val = tr32(TG3PCI_PCISTATE);
9922 val |= PCISTATE_RETRY_SAME_DMA;
9923 tw32(TG3PCI_PCISTATE, val);
9924 }
9925
Joe Perches63c3a662011-04-26 08:12:10 +00009926 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009927 /* Allow reads and writes to the
9928 * APE register and memory space.
9929 */
9930 val = tr32(TG3PCI_PCISTATE);
9931 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009932 PCISTATE_ALLOW_APE_SHMEM_WR |
9933 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009934 tw32(TG3PCI_PCISTATE, val);
9935 }
9936
Joe Perches41535772013-02-16 11:20:04 +00009937 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009938 /* Enable some hw fixes. */
9939 val = tr32(TG3PCI_MSI_DATA);
9940 val |= (1 << 26) | (1 << 28) | (1 << 29);
9941 tw32(TG3PCI_MSI_DATA, val);
9942 }
9943
9944 /* Descriptor ring init may make accesses to the
9945 * NIC SRAM area to setup the TX descriptors, so we
9946 * can only do this after the hardware has been
9947 * successfully reset.
9948 */
Michael Chan32d8c572006-07-25 16:38:29 -07009949 err = tg3_init_rings(tp);
9950 if (err)
9951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952
Joe Perches63c3a662011-04-26 08:12:10 +00009953 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009954 val = tr32(TG3PCI_DMA_RW_CTRL) &
9955 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009956 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009957 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009958 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009959 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9960 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009961 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009962 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009963 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9964 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009965 /* This value is determined during the probe time DMA
9966 * engine test, tg3_test_dma.
9967 */
9968 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970
9971 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9972 GRC_MODE_4X_NIC_SEND_RINGS |
9973 GRC_MODE_NO_TX_PHDR_CSUM |
9974 GRC_MODE_NO_RX_PHDR_CSUM);
9975 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009976
9977 /* Pseudo-header checksum is done by hardware logic and not
9978 * the offload processers, so make the chip do the pseudo-
9979 * header checksums on receive. For transmit it is more
9980 * convenient to do the pseudo-header checksum in software
9981 * as Linux does that on transmit for us in all cases.
9982 */
9983 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009985 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9986 if (tp->rxptpctl)
9987 tw32(TG3_RX_PTP_CTL,
9988 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9989
9990 if (tg3_flag(tp, PTP_CAPABLE))
9991 val |= GRC_MODE_TIME_SYNC_ENABLE;
9992
9993 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994
9995 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9996 val = tr32(GRC_MISC_CFG);
9997 val &= ~0xff;
9998 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9999 tw32(GRC_MISC_CFG, val);
10000
10001 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +000010002 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +000010004 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010005 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +000010006 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
10008 else
10009 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
10010 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
10011 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +000010012 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 int fw_len;
10014
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080010015 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
10017 tw32(BUFMGR_MB_POOL_ADDR,
10018 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
10019 tw32(BUFMGR_MB_POOL_SIZE,
10020 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
10021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022
Michael Chan0f893dc2005-07-25 12:30:38 -070010023 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10025 tp->bufmgr_config.mbuf_read_dma_low_water);
10026 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10027 tp->bufmgr_config.mbuf_mac_rx_low_water);
10028 tw32(BUFMGR_MB_HIGH_WATER,
10029 tp->bufmgr_config.mbuf_high_water);
10030 } else {
10031 tw32(BUFMGR_MB_RDMA_LOW_WATER,
10032 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
10033 tw32(BUFMGR_MB_MACRX_LOW_WATER,
10034 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
10035 tw32(BUFMGR_MB_HIGH_WATER,
10036 tp->bufmgr_config.mbuf_high_water_jumbo);
10037 }
10038 tw32(BUFMGR_DMA_LOW_WATER,
10039 tp->bufmgr_config.dma_low_water);
10040 tw32(BUFMGR_DMA_HIGH_WATER,
10041 tp->bufmgr_config.dma_high_water);
10042
Matt Carlsond309a462010-09-30 10:34:31 +000010043 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +000010044 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +000010045 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +000010046 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080010047 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000010048 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10049 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +000010050 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +000010051 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052 for (i = 0; i < 2000; i++) {
10053 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
10054 break;
10055 udelay(10);
10056 }
10057 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +000010058 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 return -ENODEV;
10060 }
10061
Joe Perches41535772013-02-16 11:20:04 +000010062 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +000010063 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -070010064
Matt Carlsoneb07a942011-04-20 07:57:36 +000010065 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
10067 /* Initialize TG3_BDINFO's at:
10068 * RCVDBDI_STD_BD: standard eth size rx ring
10069 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
10070 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
10071 *
10072 * like so:
10073 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
10074 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
10075 * ring attribute flags
10076 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
10077 *
10078 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
10079 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
10080 *
10081 * The size of each ring is fixed in the firmware, but the location is
10082 * configurable.
10083 */
10084 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010085 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010087 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +000010088 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +000010089 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
10090 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010092 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +000010093 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
10095 BDINFO_FLAGS_DISABLED);
10096
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010097 /* Program the jumbo buffer descriptor ring control
10098 * blocks on those devices that have them.
10099 */
Joe Perches41535772013-02-16 11:20:04 +000010100 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010101 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102
Joe Perches63c3a662011-04-26 08:12:10 +000010103 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +000010105 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +000010107 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +000010108 val = TG3_RX_JMB_RING_SIZE(tp) <<
10109 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +000010111 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +000010112 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +000010113 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000010114 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +000010115 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
10116 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117 } else {
10118 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
10119 BDINFO_FLAGS_DISABLED);
10120 }
10121
Joe Perches63c3a662011-04-26 08:12:10 +000010122 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000010123 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010124 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
10125 val |= (TG3_RX_STD_DMA_SZ << 2);
10126 } else
Matt Carlson04380d42010-04-12 06:58:29 +000010127 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010128 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +000010129 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +000010130
10131 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132
Matt Carlson411da642009-11-13 13:03:46 +000010133 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +000010134 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135
Joe Perches63c3a662011-04-26 08:12:10 +000010136 tpr->rx_jmb_prod_idx =
10137 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +000010138 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139
Matt Carlson2d31eca2009-09-01 12:53:31 +000010140 tg3_rings_reset(tp);
10141
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +000010143 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144
10145 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +000010146 tw32(MAC_RX_MTU_SIZE,
10147 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148
10149 /* The slot time is changed by tg3_setup_phy if we
10150 * run at gigabit with half duplex.
10151 */
Matt Carlsonf2096f92011-04-05 14:22:48 +000010152 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
10153 (6 << TX_LENGTHS_IPG_SHIFT) |
10154 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
10155
Joe Perches41535772013-02-16 11:20:04 +000010156 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10157 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010158 val |= tr32(MAC_TX_LENGTHS) &
10159 (TX_LENGTHS_JMB_FRM_LEN_MSK |
10160 TX_LENGTHS_CNT_DWN_VAL_MSK);
10161
10162 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163
10164 /* Receive rules. */
10165 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
10166 tw32(RCVLPC_CONFIG, 0x0181);
10167
10168 /* Calculate RDMAC_MODE setting early, we need it to determine
10169 * the RCVLPC_STATE_ENABLE mask.
10170 */
10171 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
10172 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
10173 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
10174 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
10175 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -070010176
Joe Perches41535772013-02-16 11:20:04 +000010177 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +000010178 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
10179
Joe Perches41535772013-02-16 11:20:04 +000010180 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
10181 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10182 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -070010183 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
10184 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
10185 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
10186
Joe Perches41535772013-02-16 11:20:04 +000010187 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10188 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010189 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010190 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
10192 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010193 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010194 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10195 }
10196 }
10197
Joe Perches63c3a662011-04-26 08:12:10 +000010198 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -070010199 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
10200
Joe Perches41535772013-02-16 11:20:04 +000010201 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +000010202 tp->dma_limit = 0;
10203 if (tp->dev->mtu <= ETH_DATA_LEN) {
10204 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
10205 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
10206 }
10207 }
10208
Joe Perches63c3a662011-04-26 08:12:10 +000010209 if (tg3_flag(tp, HW_TSO_1) ||
10210 tg3_flag(tp, HW_TSO_2) ||
10211 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -080010212 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
10213
Matt Carlson108a6c12011-05-19 12:12:47 +000010214 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010215 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10216 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -080010217 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218
Joe Perches41535772013-02-16 11:20:04 +000010219 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10220 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000010221 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
10222
Joe Perches41535772013-02-16 11:20:04 +000010223 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
10224 tg3_asic_rev(tp) == ASIC_REV_5784 ||
10225 tg3_asic_rev(tp) == ASIC_REV_5785 ||
10226 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000010227 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010228 u32 tgtreg;
10229
Joe Perches41535772013-02-16 11:20:04 +000010230 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010231 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
10232 else
10233 tgtreg = TG3_RDMA_RSRVCTRL_REG;
10234
10235 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +000010236 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
10237 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +000010238 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
10239 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
10240 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
10241 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
10242 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
10243 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +000010244 }
Michael Chanc65a17f2013-01-06 12:51:07 +000010245 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +000010246 }
10247
Joe Perches41535772013-02-16 11:20:04 +000010248 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10249 tg3_asic_rev(tp) == ASIC_REV_5720 ||
10250 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +000010251 u32 tgtreg;
10252
Joe Perches41535772013-02-16 11:20:04 +000010253 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +000010254 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
10255 else
10256 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
10257
10258 val = tr32(tgtreg);
10259 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +000010260 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
10261 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
10262 }
10263
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +000010265 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -070010266 val = tr32(RCVLPC_STATS_ENABLE);
10267 val &= ~RCVLPC_STATSENAB_DACK_FIX;
10268 tw32(RCVLPC_STATS_ENABLE, val);
10269 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010270 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010271 val = tr32(RCVLPC_STATS_ENABLE);
10272 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
10273 tw32(RCVLPC_STATS_ENABLE, val);
10274 } else {
10275 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
10276 }
10277 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
10278 tw32(SNDDATAI_STATSENAB, 0xffffff);
10279 tw32(SNDDATAI_STATSCTRL,
10280 (SNDDATAI_SCTRL_ENABLE |
10281 SNDDATAI_SCTRL_FASTUPD));
10282
10283 /* Setup host coalescing engine. */
10284 tw32(HOSTCC_MODE, 0);
10285 for (i = 0; i < 2000; i++) {
10286 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
10287 break;
10288 udelay(10);
10289 }
10290
Michael Chand244c892005-07-05 14:42:33 -070010291 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010292
Joe Perches63c3a662011-04-26 08:12:10 +000010293 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294 /* Status/statistics block address. See tg3_timer,
10295 * the tg3_periodic_fetch_stats call there, and
10296 * tg3_get_stats to see how this works for 5705/5750 chips.
10297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
10299 ((u64) tp->stats_mapping >> 32));
10300 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
10301 ((u64) tp->stats_mapping & 0xffffffff));
10302 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010303
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +000010305
10306 /* Clear statistics and status block memory areas */
10307 for (i = NIC_SRAM_STATS_BLK;
10308 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
10309 i += sizeof(u32)) {
10310 tg3_write_mem(tp, i, 0);
10311 udelay(40);
10312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 }
10314
10315 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
10316
10317 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10318 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010319 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010320 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10321
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010322 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10323 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010324 /* reset to prevent losing 1st rx packet intermittently */
10325 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10326 udelay(10);
10327 }
10328
Matt Carlson3bda1252008-08-15 14:08:22 -070010329 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010330 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10331 MAC_MODE_FHDE_ENABLE;
10332 if (tg3_flag(tp, ENABLE_APE))
10333 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010335 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010336 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010337 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010338 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10339 udelay(40);
10340
Michael Chan314fba32005-04-21 17:07:04 -070010341 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010342 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010343 * register to preserve the GPIO settings for LOMs. The GPIOs,
10344 * whether used as inputs or outputs, are set by boot code after
10345 * reset.
10346 */
Joe Perches63c3a662011-04-26 08:12:10 +000010347 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010348 u32 gpio_mask;
10349
Michael Chan9d26e212006-12-07 00:21:14 -080010350 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10351 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10352 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010353
Joe Perches41535772013-02-16 11:20:04 +000010354 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010355 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10356 GRC_LCLCTRL_GPIO_OUTPUT3;
10357
Joe Perches41535772013-02-16 11:20:04 +000010358 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010359 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10360
Gary Zambranoaaf84462007-05-05 11:51:45 -070010361 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010362 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10363
10364 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010365 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010366 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10367 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10370 udelay(100);
10371
Matt Carlsonc3b50032012-01-17 15:27:23 +000010372 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010373 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010374 val |= MSGINT_MODE_ENABLE;
10375 if (tp->irq_cnt > 1)
10376 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010377 if (!tg3_flag(tp, 1SHOT_MSI))
10378 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010379 tw32(MSGINT_MODE, val);
10380 }
10381
Joe Perches63c3a662011-04-26 08:12:10 +000010382 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10384 udelay(40);
10385 }
10386
10387 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10388 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10389 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10390 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10391 WDMAC_MODE_LNGREAD_ENAB);
10392
Joe Perches41535772013-02-16 11:20:04 +000010393 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10394 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010395 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010396 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10397 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 /* nothing */
10399 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010400 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 val |= WDMAC_MODE_RX_ACCEL;
10402 }
10403 }
10404
Michael Chand9ab5ad2006-03-20 22:27:35 -080010405 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010406 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010407 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -080010408
Joe Perches41535772013-02-16 11:20:04 +000010409 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010410 val |= WDMAC_MODE_BURST_ALL_DATA;
10411
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 tw32_f(WDMAC_MODE, val);
10413 udelay(40);
10414
Joe Perches63c3a662011-04-26 08:12:10 +000010415 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010416 u16 pcix_cmd;
10417
10418 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10419 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010420 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010421 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10422 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010423 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010424 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10425 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010426 }
Matt Carlson9974a352007-10-07 23:27:28 -070010427 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10428 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 }
10430
10431 tw32_f(RDMAC_MODE, rdmac_mode);
10432 udelay(40);
10433
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010434 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
10435 tg3_asic_rev(tp) == ASIC_REV_5720) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010436 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10437 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10438 break;
10439 }
10440 if (i < TG3_NUM_RDMA_CHANNELS) {
10441 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010442 val |= tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010443 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010444 tg3_flag_set(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010445 }
10446 }
10447
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010449 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010451
Joe Perches41535772013-02-16 11:20:04 +000010452 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010453 tw32(SNDDATAC_MODE,
10454 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10455 else
10456 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10457
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10459 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010460 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010461 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010462 val |= RCVDBDI_MODE_LRG_RING_SZ;
10463 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010465 if (tg3_flag(tp, HW_TSO_1) ||
10466 tg3_flag(tp, HW_TSO_2) ||
10467 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010469 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010470 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010471 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10472 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10474
Joe Perches41535772013-02-16 11:20:04 +000010475 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 err = tg3_load_5701_a0_firmware_fix(tp);
10477 if (err)
10478 return err;
10479 }
10480
Nithin Sujirc4dab502013-03-06 17:02:34 +000010481 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10482 /* Ignore any errors for the firmware download. If download
10483 * fails, the device will operate with EEE disabled
10484 */
10485 tg3_load_57766_firmware(tp);
10486 }
10487
Joe Perches63c3a662011-04-26 08:12:10 +000010488 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 err = tg3_load_tso_firmware(tp);
10490 if (err)
10491 return err;
10492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010493
10494 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010495
Joe Perches63c3a662011-04-26 08:12:10 +000010496 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010497 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010498 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010499
Joe Perches41535772013-02-16 11:20:04 +000010500 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10501 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010502 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10503 tp->tx_mode &= ~val;
10504 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10505 }
10506
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 tw32_f(MAC_TX_MODE, tp->tx_mode);
10508 udelay(100);
10509
Joe Perches63c3a662011-04-26 08:12:10 +000010510 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010511 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010512
10513 /* Setup the "secret" hash key. */
10514 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10515 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10516 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10517 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10518 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10519 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10520 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10521 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10522 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10523 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10524 }
10525
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010527 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010528 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10529
Nithin Sujir378b72c2013-07-29 13:58:39 -070010530 if (tg3_asic_rev(tp) == ASIC_REV_5762)
10531 tp->rx_mode |= RX_MODE_IPV4_FRAG_FIX;
10532
Joe Perches63c3a662011-04-26 08:12:10 +000010533 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010534 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10535 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10536 RX_MODE_RSS_IPV6_HASH_EN |
10537 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10538 RX_MODE_RSS_IPV4_HASH_EN |
10539 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10540
Linus Torvalds1da177e2005-04-16 15:20:36 -070010541 tw32_f(MAC_RX_MODE, tp->rx_mode);
10542 udelay(10);
10543
Linus Torvalds1da177e2005-04-16 15:20:36 -070010544 tw32(MAC_LED_CTRL, tp->led_ctrl);
10545
10546 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010547 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10549 udelay(10);
10550 }
10551 tw32_f(MAC_RX_MODE, tp->rx_mode);
10552 udelay(10);
10553
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010554 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010555 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10556 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 /* Set drive transmission level to 1.2V */
10558 /* only if the signal pre-emphasis bit is not set */
10559 val = tr32(MAC_SERDES_CFG);
10560 val &= 0xfffff000;
10561 val |= 0x880;
10562 tw32(MAC_SERDES_CFG, val);
10563 }
Joe Perches41535772013-02-16 11:20:04 +000010564 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565 tw32(MAC_SERDES_CFG, 0x616000);
10566 }
10567
10568 /* Prevent chip from dropping frames when flow control
10569 * is enabled.
10570 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010571 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010572 val = 1;
10573 else
10574 val = 2;
10575 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576
Joe Perches41535772013-02-16 11:20:04 +000010577 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010578 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010580 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581 }
10582
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010583 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010584 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010585 u32 tmp;
10586
10587 tmp = tr32(SERDES_RX_CTRL);
10588 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10589 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10590 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10591 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10592 }
10593
Joe Perches63c3a662011-04-26 08:12:10 +000010594 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010595 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +000010596 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597
Joe Perches953c96e2013-04-09 10:18:14 +000010598 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010599 if (err)
10600 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010602 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10603 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010604 u32 tmp;
10605
10606 /* Clear CRC stats. */
10607 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10608 tg3_writephy(tp, MII_TG3_TEST1,
10609 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010610 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010612 }
10613 }
10614
10615 __tg3_set_rx_mode(tp->dev);
10616
10617 /* Initialize receive rules. */
10618 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10619 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10620 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10621 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10622
Joe Perches63c3a662011-04-26 08:12:10 +000010623 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624 limit = 8;
10625 else
10626 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010627 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628 limit -= 4;
10629 switch (limit) {
10630 case 16:
10631 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10632 case 15:
10633 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10634 case 14:
10635 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10636 case 13:
10637 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10638 case 12:
10639 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10640 case 11:
10641 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10642 case 10:
10643 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10644 case 9:
10645 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10646 case 8:
10647 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10648 case 7:
10649 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10650 case 6:
10651 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10652 case 5:
10653 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10654 case 4:
10655 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10656 case 3:
10657 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10658 case 2:
10659 case 1:
10660
10661 default:
10662 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010664
Joe Perches63c3a662011-04-26 08:12:10 +000010665 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010666 /* Write our heartbeat update interval to APE. */
10667 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10668 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010669
Linus Torvalds1da177e2005-04-16 15:20:36 -070010670 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10671
Linus Torvalds1da177e2005-04-16 15:20:36 -070010672 return 0;
10673}
10674
10675/* Called at device open time to get the chip ready for
10676 * packet processing. Invoked with tp->lock held.
10677 */
Joe Perches953c96e2013-04-09 10:18:14 +000010678static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679{
Nithin Sujirdf465ab2013-06-12 11:08:59 -070010680 /* Chip may have been just powered on. If so, the boot code may still
10681 * be running initialization. Wait for it to finish to avoid races in
10682 * accessing the hardware.
10683 */
10684 tg3_enable_register_access(tp);
10685 tg3_poll_fw(tp);
10686
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687 tg3_switch_clocks(tp);
10688
10689 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10690
Matt Carlson2f751b62008-08-04 23:17:34 -070010691 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010692}
10693
Michael Chanaed93e02012-07-16 16:24:02 +000010694static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10695{
10696 int i;
10697
10698 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10699 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10700
10701 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10702 off += len;
10703
10704 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10705 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10706 memset(ocir, 0, TG3_OCIR_LEN);
10707 }
10708}
10709
10710/* sysfs attributes for hwmon */
10711static ssize_t tg3_show_temp(struct device *dev,
10712 struct device_attribute *devattr, char *buf)
10713{
Michael Chanaed93e02012-07-16 16:24:02 +000010714 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010715 struct tg3 *tp = dev_get_drvdata(dev);
Michael Chanaed93e02012-07-16 16:24:02 +000010716 u32 temperature;
10717
10718 spin_lock_bh(&tp->lock);
10719 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10720 sizeof(temperature));
10721 spin_unlock_bh(&tp->lock);
10722 return sprintf(buf, "%u\n", temperature);
10723}
10724
10725
10726static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10727 TG3_TEMP_SENSOR_OFFSET);
10728static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10729 TG3_TEMP_CAUTION_OFFSET);
10730static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10731 TG3_TEMP_MAX_OFFSET);
10732
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010733static struct attribute *tg3_attrs[] = {
Michael Chanaed93e02012-07-16 16:24:02 +000010734 &sensor_dev_attr_temp1_input.dev_attr.attr,
10735 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10736 &sensor_dev_attr_temp1_max.dev_attr.attr,
10737 NULL
10738};
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010739ATTRIBUTE_GROUPS(tg3);
Michael Chanaed93e02012-07-16 16:24:02 +000010740
Michael Chanaed93e02012-07-16 16:24:02 +000010741static void tg3_hwmon_close(struct tg3 *tp)
10742{
Michael Chanaed93e02012-07-16 16:24:02 +000010743 if (tp->hwmon_dev) {
10744 hwmon_device_unregister(tp->hwmon_dev);
10745 tp->hwmon_dev = NULL;
Michael Chanaed93e02012-07-16 16:24:02 +000010746 }
Michael Chanaed93e02012-07-16 16:24:02 +000010747}
10748
10749static void tg3_hwmon_open(struct tg3 *tp)
10750{
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010751 int i;
Michael Chanaed93e02012-07-16 16:24:02 +000010752 u32 size = 0;
10753 struct pci_dev *pdev = tp->pdev;
10754 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10755
10756 tg3_sd_scan_scratchpad(tp, ocirs);
10757
10758 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10759 if (!ocirs[i].src_data_length)
10760 continue;
10761
10762 size += ocirs[i].src_hdr_length;
10763 size += ocirs[i].src_data_length;
10764 }
10765
10766 if (!size)
10767 return;
10768
Guenter Roecka2f4dfb2013-11-22 22:07:57 -080010769 tp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, "tg3",
10770 tp, tg3_groups);
Michael Chanaed93e02012-07-16 16:24:02 +000010771 if (IS_ERR(tp->hwmon_dev)) {
10772 tp->hwmon_dev = NULL;
10773 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
Michael Chanaed93e02012-07-16 16:24:02 +000010774 }
Michael Chanaed93e02012-07-16 16:24:02 +000010775}
10776
10777
Linus Torvalds1da177e2005-04-16 15:20:36 -070010778#define TG3_STAT_ADD32(PSTAT, REG) \
10779do { u32 __val = tr32(REG); \
10780 (PSTAT)->low += __val; \
10781 if ((PSTAT)->low < __val) \
10782 (PSTAT)->high += 1; \
10783} while (0)
10784
10785static void tg3_periodic_fetch_stats(struct tg3 *tp)
10786{
10787 struct tg3_hw_stats *sp = tp->hw_stats;
10788
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010789 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010790 return;
10791
10792 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10793 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10794 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10795 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10796 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10797 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10798 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10799 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10800 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10801 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10802 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10803 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10804 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010805 if (unlikely(tg3_flag(tp, 5719_5720_RDMA_BUG) &&
Michael Chan091f0ea2012-07-29 19:15:43 +000010806 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10807 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10808 u32 val;
10809
10810 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010811 val &= ~tg3_lso_rd_dma_workaround_bit(tp);
Michael Chan091f0ea2012-07-29 19:15:43 +000010812 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
Nithin Sujir9bc297e2013-06-03 09:19:34 +000010813 tg3_flag_clear(tp, 5719_5720_RDMA_BUG);
Michael Chan091f0ea2012-07-29 19:15:43 +000010814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010815
10816 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10817 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10818 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10819 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10820 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10821 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10822 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10823 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10824 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10825 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10826 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10827 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10828 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10829 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010830
10831 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010832 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
Nithin Sujir94962f72013-12-06 09:53:19 -080010833 tg3_asic_rev(tp) != ASIC_REV_5762 &&
Joe Perches41535772013-02-16 11:20:04 +000010834 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10835 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010836 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10837 } else {
10838 u32 val = tr32(HOSTCC_FLOW_ATTN);
10839 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10840 if (val) {
10841 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10842 sp->rx_discards.low += val;
10843 if (sp->rx_discards.low < val)
10844 sp->rx_discards.high += 1;
10845 }
10846 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10847 }
Michael Chan463d3052006-05-22 16:36:27 -070010848 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010849}
10850
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010851static void tg3_chk_missed_msi(struct tg3 *tp)
10852{
10853 u32 i;
10854
10855 for (i = 0; i < tp->irq_cnt; i++) {
10856 struct tg3_napi *tnapi = &tp->napi[i];
10857
10858 if (tg3_has_work(tnapi)) {
10859 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10860 tnapi->last_tx_cons == tnapi->tx_cons) {
10861 if (tnapi->chk_msi_cnt < 1) {
10862 tnapi->chk_msi_cnt++;
10863 return;
10864 }
Matt Carlson7f230732011-08-31 11:44:48 +000010865 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010866 }
10867 }
10868 tnapi->chk_msi_cnt = 0;
10869 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10870 tnapi->last_tx_cons = tnapi->tx_cons;
10871 }
10872}
10873
Linus Torvalds1da177e2005-04-16 15:20:36 -070010874static void tg3_timer(unsigned long __opaque)
10875{
10876 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010877
Matt Carlson5b190622011-11-04 09:15:04 +000010878 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010879 goto restart_timer;
10880
David S. Millerf47c11e2005-06-24 20:18:35 -070010881 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010882
Joe Perches41535772013-02-16 11:20:04 +000010883 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010884 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010885 tg3_chk_missed_msi(tp);
10886
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010887 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10888 /* BCM4785: Flush posted writes from GbE to host memory. */
10889 tr32(HOSTCC_MODE);
10890 }
10891
Joe Perches63c3a662011-04-26 08:12:10 +000010892 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010893 /* All of this garbage is because when using non-tagged
10894 * IRQ status the mailbox/status_block protocol the chip
10895 * uses with the cpu is race prone.
10896 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010897 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010898 tw32(GRC_LOCAL_CTRL,
10899 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10900 } else {
10901 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010902 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010904
David S. Millerfac9b832005-05-18 22:46:34 -070010905 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010906 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010907 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010908 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010910 }
10911
Linus Torvalds1da177e2005-04-16 15:20:36 -070010912 /* This part only runs once per second. */
10913 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010914 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010915 tg3_periodic_fetch_stats(tp);
10916
Matt Carlsonb0c59432011-05-19 12:12:48 +000010917 if (tp->setlpicnt && !--tp->setlpicnt)
10918 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010919
Joe Perches63c3a662011-04-26 08:12:10 +000010920 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921 u32 mac_stat;
10922 int phy_event;
10923
10924 mac_stat = tr32(MAC_STATUS);
10925
10926 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010927 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010928 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10929 phy_event = 1;
10930 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10931 phy_event = 1;
10932
10933 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010934 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010935 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010936 u32 mac_stat = tr32(MAC_STATUS);
10937 int need_setup = 0;
10938
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010939 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10941 need_setup = 1;
10942 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010943 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010944 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10945 MAC_STATUS_SIGNAL_DET))) {
10946 need_setup = 1;
10947 }
10948 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010949 if (!tp->serdes_counter) {
10950 tw32_f(MAC_MODE,
10951 (tp->mac_mode &
10952 ~MAC_MODE_PORT_MODE_MASK));
10953 udelay(40);
10954 tw32_f(MAC_MODE, tp->mac_mode);
10955 udelay(40);
10956 }
Joe Perches953c96e2013-04-09 10:18:14 +000010957 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010959 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010960 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010961 tg3_serdes_parallel_detect(tp);
Nithin Sujir1743b832014-01-03 10:09:14 -080010962 } else if (tg3_flag(tp, POLL_CPMU_LINK)) {
10963 u32 cpmu = tr32(TG3_CPMU_STATUS);
10964 bool link_up = !((cpmu & TG3_CPMU_STATUS_LINK_MASK) ==
10965 TG3_CPMU_STATUS_LINK_MASK);
10966
10967 if (link_up != tp->link_up)
10968 tg3_setup_phy(tp, false);
Matt Carlson57d8b882010-06-05 17:24:35 +000010969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010970
10971 tp->timer_counter = tp->timer_multiplier;
10972 }
10973
Michael Chan130b8e42006-09-27 16:00:40 -070010974 /* Heartbeat is only sent once every 2 seconds.
10975 *
10976 * The heartbeat is to tell the ASF firmware that the host
10977 * driver is still alive. In the event that the OS crashes,
10978 * ASF needs to reset the hardware to free up the FIFO space
10979 * that may be filled with rx packets destined for the host.
10980 * If the FIFO is full, ASF will no longer function properly.
10981 *
10982 * Unintended resets have been reported on real time kernels
10983 * where the timer doesn't run on time. Netpoll will also have
10984 * same problem.
10985 *
10986 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10987 * to check the ring condition when the heartbeat is expiring
10988 * before doing the reset. This will prevent most unintended
10989 * resets.
10990 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010991 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010992 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010993 tg3_wait_for_event_ack(tp);
10994
Michael Chanbbadf502006-04-06 21:46:34 -070010995 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010996 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010997 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010998 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10999 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070011000
11001 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011002 }
11003 tp->asf_counter = tp->asf_multiplier;
11004 }
11005
David S. Millerf47c11e2005-06-24 20:18:35 -070011006 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011007
Michael Chanf475f162006-03-27 23:20:14 -080011008restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 tp->timer.expires = jiffies + tp->timer_offset;
11010 add_timer(&tp->timer);
11011}
11012
Bill Pemberton229b1ad2012-12-03 09:22:59 -050011013static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000011014{
11015 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000011016 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000011017 !tg3_flag(tp, 57765_CLASS))
11018 tp->timer_offset = HZ;
11019 else
11020 tp->timer_offset = HZ / 10;
11021
11022 BUG_ON(tp->timer_offset > HZ);
11023
11024 tp->timer_multiplier = (HZ / tp->timer_offset);
11025 tp->asf_multiplier = (HZ / tp->timer_offset) *
11026 TG3_FW_UPDATE_FREQ_SEC;
11027
11028 init_timer(&tp->timer);
11029 tp->timer.data = (unsigned long) tp;
11030 tp->timer.function = tg3_timer;
11031}
11032
11033static void tg3_timer_start(struct tg3 *tp)
11034{
11035 tp->asf_counter = tp->asf_multiplier;
11036 tp->timer_counter = tp->timer_multiplier;
11037
11038 tp->timer.expires = jiffies + tp->timer_offset;
11039 add_timer(&tp->timer);
11040}
11041
11042static void tg3_timer_stop(struct tg3 *tp)
11043{
11044 del_timer_sync(&tp->timer);
11045}
11046
11047/* Restart hardware after configuration changes, self-test, etc.
11048 * Invoked with tp->lock held.
11049 */
Joe Perches953c96e2013-04-09 10:18:14 +000011050static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000011051 __releases(tp->lock)
11052 __acquires(tp->lock)
11053{
11054 int err;
11055
11056 err = tg3_init_hw(tp, reset_phy);
11057 if (err) {
11058 netdev_err(tp->dev,
11059 "Failed to re-initialize device, aborting\n");
11060 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11061 tg3_full_unlock(tp);
11062 tg3_timer_stop(tp);
11063 tp->irq_sync = 0;
11064 tg3_napi_enable(tp);
11065 dev_close(tp->dev);
11066 tg3_full_lock(tp, 0);
11067 }
11068 return err;
11069}
11070
11071static void tg3_reset_task(struct work_struct *work)
11072{
11073 struct tg3 *tp = container_of(work, struct tg3, reset_task);
11074 int err;
11075
11076 tg3_full_lock(tp, 0);
11077
11078 if (!netif_running(tp->dev)) {
11079 tg3_flag_clear(tp, RESET_TASK_PENDING);
11080 tg3_full_unlock(tp);
11081 return;
11082 }
11083
11084 tg3_full_unlock(tp);
11085
11086 tg3_phy_stop(tp);
11087
11088 tg3_netif_stop(tp);
11089
11090 tg3_full_lock(tp, 1);
11091
11092 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
11093 tp->write32_tx_mbox = tg3_write32_tx_mbox;
11094 tp->write32_rx_mbox = tg3_write_flush_reg32;
11095 tg3_flag_set(tp, MBOX_WRITE_REORDER);
11096 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
11097 }
11098
11099 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000011100 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000011101 if (err)
11102 goto out;
11103
11104 tg3_netif_start(tp);
11105
11106out:
11107 tg3_full_unlock(tp);
11108
11109 if (!err)
11110 tg3_phy_start(tp);
11111
11112 tg3_flag_clear(tp, RESET_TASK_PENDING);
11113}
11114
Matt Carlson4f125f42009-09-01 12:55:02 +000011115static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080011116{
David Howells7d12e782006-10-05 14:55:46 +010011117 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011118 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000011119 char *name;
11120 struct tg3_napi *tnapi = &tp->napi[irq_num];
11121
11122 if (tp->irq_cnt == 1)
11123 name = tp->dev->name;
11124 else {
11125 name = &tnapi->irq_lbl[0];
Nithin Sujir21e315e2013-09-20 16:47:00 -070011126 if (tnapi->tx_buffers && tnapi->rx_rcb)
11127 snprintf(name, IFNAMSIZ,
11128 "%s-txrx-%d", tp->dev->name, irq_num);
11129 else if (tnapi->tx_buffers)
11130 snprintf(name, IFNAMSIZ,
11131 "%s-tx-%d", tp->dev->name, irq_num);
11132 else if (tnapi->rx_rcb)
11133 snprintf(name, IFNAMSIZ,
11134 "%s-rx-%d", tp->dev->name, irq_num);
11135 else
11136 snprintf(name, IFNAMSIZ,
11137 "%s-%d", tp->dev->name, irq_num);
Matt Carlson4f125f42009-09-01 12:55:02 +000011138 name[IFNAMSIZ-1] = 0;
11139 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011140
Joe Perches63c3a662011-04-26 08:12:10 +000011141 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080011142 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000011143 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011144 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011145 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011146 } else {
11147 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000011148 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080011149 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000011150 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080011151 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011152
11153 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011154}
11155
Michael Chan79381092005-04-21 17:13:59 -070011156static int tg3_test_interrupt(struct tg3 *tp)
11157{
Matt Carlson09943a12009-08-28 14:01:57 +000011158 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070011159 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070011160 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011161 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070011162
Michael Chand4bc3922005-05-29 14:59:20 -070011163 if (!netif_running(dev))
11164 return -ENODEV;
11165
Michael Chan79381092005-04-21 17:13:59 -070011166 tg3_disable_ints(tp);
11167
Matt Carlson4f125f42009-09-01 12:55:02 +000011168 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011169
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011170 /*
11171 * Turn off MSI one shot mode. Otherwise this test has no
11172 * observable way to know whether the interrupt was delivered.
11173 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011174 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011175 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
11176 tw32(MSGINT_MODE, val);
11177 }
11178
Matt Carlson4f125f42009-09-01 12:55:02 +000011179 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000011180 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070011181 if (err)
11182 return err;
11183
Matt Carlson898a56f2009-08-28 14:02:40 +000011184 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070011185 tg3_enable_ints(tp);
11186
11187 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011188 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070011189
11190 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070011191 u32 int_mbox, misc_host_ctrl;
11192
Matt Carlson898a56f2009-08-28 14:02:40 +000011193 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070011194 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
11195
11196 if ((int_mbox != 0) ||
11197 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
11198 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070011199 break;
Michael Chanb16250e2006-09-27 16:10:14 -070011200 }
11201
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000011202 if (tg3_flag(tp, 57765_PLUS) &&
11203 tnapi->hw_status->status_tag != tnapi->last_tag)
11204 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
11205
Michael Chan79381092005-04-21 17:13:59 -070011206 msleep(10);
11207 }
11208
11209 tg3_disable_ints(tp);
11210
Matt Carlson4f125f42009-09-01 12:55:02 +000011211 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011212
Matt Carlson4f125f42009-09-01 12:55:02 +000011213 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011214
11215 if (err)
11216 return err;
11217
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011218 if (intr_ok) {
11219 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000011220 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011221 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
11222 tw32(MSGINT_MODE, val);
11223 }
Michael Chan79381092005-04-21 17:13:59 -070011224 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011225 }
Michael Chan79381092005-04-21 17:13:59 -070011226
11227 return -EIO;
11228}
11229
11230/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
11231 * successfully restored
11232 */
11233static int tg3_test_msi(struct tg3 *tp)
11234{
Michael Chan79381092005-04-21 17:13:59 -070011235 int err;
11236 u16 pci_cmd;
11237
Joe Perches63c3a662011-04-26 08:12:10 +000011238 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070011239 return 0;
11240
11241 /* Turn off SERR reporting in case MSI terminates with Master
11242 * Abort.
11243 */
11244 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
11245 pci_write_config_word(tp->pdev, PCI_COMMAND,
11246 pci_cmd & ~PCI_COMMAND_SERR);
11247
11248 err = tg3_test_interrupt(tp);
11249
11250 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
11251
11252 if (!err)
11253 return 0;
11254
11255 /* other failures */
11256 if (err != -EIO)
11257 return err;
11258
11259 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011260 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
11261 "to INTx mode. Please report this failure to the PCI "
11262 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070011263
Matt Carlson4f125f42009-09-01 12:55:02 +000011264 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000011265
Michael Chan79381092005-04-21 17:13:59 -070011266 pci_disable_msi(tp->pdev);
11267
Joe Perches63c3a662011-04-26 08:12:10 +000011268 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000011269 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070011270
Matt Carlson4f125f42009-09-01 12:55:02 +000011271 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070011272 if (err)
11273 return err;
11274
11275 /* Need to reset the chip because the MSI cycle may have terminated
11276 * with Master Abort.
11277 */
David S. Millerf47c11e2005-06-24 20:18:35 -070011278 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070011279
Michael Chan944d9802005-05-29 14:57:48 -070011280 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011281 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070011282
David S. Millerf47c11e2005-06-24 20:18:35 -070011283 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011284
11285 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000011286 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070011287
11288 return err;
11289}
11290
Matt Carlson9e9fd122009-01-19 16:57:45 -080011291static int tg3_request_firmware(struct tg3 *tp)
11292{
Nithin Sujir77997ea2013-03-06 17:02:32 +000011293 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011294
11295 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011296 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
11297 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011298 return -ENOENT;
11299 }
11300
Nithin Sujir77997ea2013-03-06 17:02:32 +000011301 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080011302
11303 /* Firmware blob starts with version numbers, followed by
11304 * start address and _full_ length including BSS sections
11305 * (which must be longer than the actual data, of course
11306 */
11307
Nithin Sujir77997ea2013-03-06 17:02:32 +000011308 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
11309 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000011310 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
11311 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080011312 release_firmware(tp->fw);
11313 tp->fw = NULL;
11314 return -EINVAL;
11315 }
11316
11317 /* We no longer need firmware; we have it. */
11318 tp->fw_needed = NULL;
11319 return 0;
11320}
11321
Michael Chan91024262012-09-28 07:12:38 +000011322static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000011323{
Michael Chan91024262012-09-28 07:12:38 +000011324 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000011325
Michael Chan91024262012-09-28 07:12:38 +000011326 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000011327 /* We want as many rx rings enabled as there are cpus.
11328 * In multiqueue MSI-X mode, the first MSI-X vector
11329 * only deals with link interrupts, etc, so we add
11330 * one to the number of vectors we are requesting.
11331 */
Michael Chan91024262012-09-28 07:12:38 +000011332 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000011333 }
Matt Carlson679563f2009-09-01 12:55:46 +000011334
Michael Chan91024262012-09-28 07:12:38 +000011335 return irq_cnt;
11336}
11337
11338static bool tg3_enable_msix(struct tg3 *tp)
11339{
11340 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011341 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011342
Michael Chan09681692012-09-28 07:12:42 +000011343 tp->txq_cnt = tp->txq_req;
11344 tp->rxq_cnt = tp->rxq_req;
11345 if (!tp->rxq_cnt)
11346 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011347 if (tp->rxq_cnt > tp->rxq_max)
11348 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011349
11350 /* Disable multiple TX rings by default. Simple round-robin hardware
11351 * scheduling of the TX rings can cause starvation of rings with
11352 * small packets when other rings have TSO or jumbo packets.
11353 */
11354 if (!tp->txq_req)
11355 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011356
11357 tp->irq_cnt = tg3_irq_count(tp);
11358
Matt Carlson679563f2009-09-01 12:55:46 +000011359 for (i = 0; i < tp->irq_max; i++) {
11360 msix_ent[i].entry = i;
11361 msix_ent[i].vector = 0;
11362 }
11363
11364 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011365 if (rc < 0) {
11366 return false;
11367 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011368 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11369 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011370 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11371 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011372 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011373 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011374 if (tp->txq_cnt)
11375 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011376 }
11377
11378 for (i = 0; i < tp->irq_max; i++)
11379 tp->napi[i].irq_vec = msix_ent[i].vector;
11380
Michael Chan49a359e2012-09-28 07:12:37 +000011381 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011382 pci_disable_msix(tp->pdev);
11383 return false;
11384 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011385
Michael Chan91024262012-09-28 07:12:38 +000011386 if (tp->irq_cnt == 1)
11387 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011388
Michael Chan91024262012-09-28 07:12:38 +000011389 tg3_flag_set(tp, ENABLE_RSS);
11390
11391 if (tp->txq_cnt > 1)
11392 tg3_flag_set(tp, ENABLE_TSS);
11393
11394 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011395
Matt Carlson679563f2009-09-01 12:55:46 +000011396 return true;
11397}
11398
Matt Carlson07b01732009-08-28 14:01:15 +000011399static void tg3_ints_init(struct tg3 *tp)
11400{
Joe Perches63c3a662011-04-26 08:12:10 +000011401 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11402 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011403 /* All MSI supporting chips should support tagged
11404 * status. Assert that this is the case.
11405 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011406 netdev_warn(tp->dev,
11407 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011408 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011409 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011410
Joe Perches63c3a662011-04-26 08:12:10 +000011411 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11412 tg3_flag_set(tp, USING_MSIX);
11413 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11414 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011415
Joe Perches63c3a662011-04-26 08:12:10 +000011416 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011417 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011418 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011419 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011420 if (!tg3_flag(tp, 1SHOT_MSI))
11421 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011422 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11423 }
11424defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011425 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011426 tp->irq_cnt = 1;
11427 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011428 }
11429
11430 if (tp->irq_cnt == 1) {
11431 tp->txq_cnt = 1;
11432 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011433 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011434 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011435 }
Matt Carlson07b01732009-08-28 14:01:15 +000011436}
11437
11438static void tg3_ints_fini(struct tg3 *tp)
11439{
Joe Perches63c3a662011-04-26 08:12:10 +000011440 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011441 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011442 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011443 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011444 tg3_flag_clear(tp, USING_MSI);
11445 tg3_flag_clear(tp, USING_MSIX);
11446 tg3_flag_clear(tp, ENABLE_RSS);
11447 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011448}
11449
Matt Carlsonbe947302012-12-03 19:36:57 +000011450static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11451 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011452{
Michael Chand8f4cd32012-09-28 07:12:40 +000011453 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011454 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011455
Matt Carlson679563f2009-09-01 12:55:46 +000011456 /*
11457 * Setup interrupts first so we know how
11458 * many NAPI resources to allocate
11459 */
11460 tg3_ints_init(tp);
11461
Matt Carlson90415472011-12-16 13:33:23 +000011462 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011463
Linus Torvalds1da177e2005-04-16 15:20:36 -070011464 /* The placement of this call is tied
11465 * to the setup and use of Host TX descriptors.
11466 */
11467 err = tg3_alloc_consistent(tp);
11468 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011469 goto out_ints_fini;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011470
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011471 tg3_napi_init(tp);
11472
Matt Carlsonfed97812009-09-01 13:10:19 +000011473 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011474
Matt Carlson4f125f42009-09-01 12:55:02 +000011475 for (i = 0; i < tp->irq_cnt; i++) {
11476 struct tg3_napi *tnapi = &tp->napi[i];
11477 err = tg3_request_irq(tp, i);
11478 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011479 for (i--; i >= 0; i--) {
11480 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011481 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011482 }
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011483 goto out_napi_fini;
Matt Carlson4f125f42009-09-01 12:55:02 +000011484 }
11485 }
Matt Carlson07b01732009-08-28 14:01:15 +000011486
David S. Millerf47c11e2005-06-24 20:18:35 -070011487 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011488
Nithin Sujir2e460fc2013-05-23 11:11:22 +000011489 if (init)
11490 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
11491
Michael Chand8f4cd32012-09-28 07:12:40 +000011492 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011493 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011494 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011496 }
11497
David S. Millerf47c11e2005-06-24 20:18:35 -070011498 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011499
Matt Carlson07b01732009-08-28 14:01:15 +000011500 if (err)
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011501 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011502
Michael Chand8f4cd32012-09-28 07:12:40 +000011503 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011504 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011505
Michael Chan79381092005-04-21 17:13:59 -070011506 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011507 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011508 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011509 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011510 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011511
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011512 goto out_napi_fini;
Michael Chan79381092005-04-21 17:13:59 -070011513 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011514
Joe Perches63c3a662011-04-26 08:12:10 +000011515 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011516 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011517
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011518 tw32(PCIE_TRANSACTION_CFG,
11519 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011520 }
Michael Chan79381092005-04-21 17:13:59 -070011521 }
11522
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011523 tg3_phy_start(tp);
11524
Michael Chanaed93e02012-07-16 16:24:02 +000011525 tg3_hwmon_open(tp);
11526
David S. Millerf47c11e2005-06-24 20:18:35 -070011527 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011528
Matt Carlson21f76382012-02-22 12:35:21 +000011529 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011530 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011531 tg3_enable_ints(tp);
11532
Matt Carlsonbe947302012-12-03 19:36:57 +000011533 if (init)
11534 tg3_ptp_init(tp);
11535 else
11536 tg3_ptp_resume(tp);
11537
11538
David S. Millerf47c11e2005-06-24 20:18:35 -070011539 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011540
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011541 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011542
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011543 /*
11544 * Reset loopback feature if it was turned on while the device was down
11545 * make sure that it's installed properly now.
11546 */
11547 if (dev->features & NETIF_F_LOOPBACK)
11548 tg3_set_loopback(dev, dev->features);
11549
Linus Torvalds1da177e2005-04-16 15:20:36 -070011550 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011551
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011552out_free_irq:
Matt Carlson4f125f42009-09-01 12:55:02 +000011553 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11554 struct tg3_napi *tnapi = &tp->napi[i];
11555 free_irq(tnapi->irq_vec, tnapi);
11556 }
Matt Carlson07b01732009-08-28 14:01:15 +000011557
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011558out_napi_fini:
Matt Carlsonfed97812009-09-01 13:10:19 +000011559 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011560 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011561 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011562
Nithin Sujir4a5f46f2013-05-23 11:11:25 +000011563out_ints_fini:
Matt Carlson679563f2009-09-01 12:55:46 +000011564 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011565
Matt Carlson07b01732009-08-28 14:01:15 +000011566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567}
11568
Michael Chan65138592012-09-28 07:12:41 +000011569static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011570{
Matt Carlson4f125f42009-09-01 12:55:02 +000011571 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572
Matt Carlsondb219972011-11-04 09:15:03 +000011573 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011574 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011575
Matt Carlson21f76382012-02-22 12:35:21 +000011576 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011577
Michael Chanaed93e02012-07-16 16:24:02 +000011578 tg3_hwmon_close(tp);
11579
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011580 tg3_phy_stop(tp);
11581
David S. Millerf47c11e2005-06-24 20:18:35 -070011582 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011583
11584 tg3_disable_ints(tp);
11585
Michael Chan944d9802005-05-29 14:57:48 -070011586 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011587 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011588 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011589
David S. Millerf47c11e2005-06-24 20:18:35 -070011590 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011591
Matt Carlson4f125f42009-09-01 12:55:02 +000011592 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11593 struct tg3_napi *tnapi = &tp->napi[i];
11594 free_irq(tnapi->irq_vec, tnapi);
11595 }
Matt Carlson07b01732009-08-28 14:01:15 +000011596
11597 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011599 tg3_napi_fini(tp);
11600
Linus Torvalds1da177e2005-04-16 15:20:36 -070011601 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011602}
11603
Michael Chand8f4cd32012-09-28 07:12:40 +000011604static int tg3_open(struct net_device *dev)
11605{
11606 struct tg3 *tp = netdev_priv(dev);
11607 int err;
11608
11609 if (tp->fw_needed) {
11610 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011611 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11612 if (err) {
11613 netdev_warn(tp->dev, "EEE capability disabled\n");
11614 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11615 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11616 netdev_warn(tp->dev, "EEE capability restored\n");
11617 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11618 }
11619 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011620 if (err)
11621 return err;
11622 } else if (err) {
11623 netdev_warn(tp->dev, "TSO capability disabled\n");
11624 tg3_flag_clear(tp, TSO_CAPABLE);
11625 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11626 netdev_notice(tp->dev, "TSO capability restored\n");
11627 tg3_flag_set(tp, TSO_CAPABLE);
11628 }
11629 }
11630
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011631 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011632
11633 err = tg3_power_up(tp);
11634 if (err)
11635 return err;
11636
11637 tg3_full_lock(tp, 0);
11638
11639 tg3_disable_ints(tp);
11640 tg3_flag_clear(tp, INIT_COMPLETE);
11641
11642 tg3_full_unlock(tp);
11643
Nithin Sujir942d1af2013-04-09 08:48:07 +000011644 err = tg3_start(tp,
11645 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11646 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011647 if (err) {
11648 tg3_frob_aux_power(tp, false);
11649 pci_set_power_state(tp->pdev, PCI_D3hot);
11650 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011651
Matt Carlson7d41e492012-12-03 19:36:58 +000011652 if (tg3_flag(tp, PTP_CAPABLE)) {
11653 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11654 &tp->pdev->dev);
11655 if (IS_ERR(tp->ptp_clock))
11656 tp->ptp_clock = NULL;
11657 }
11658
Linus Torvalds1da177e2005-04-16 15:20:36 -070011659 return err;
11660}
11661
11662static int tg3_close(struct net_device *dev)
11663{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664 struct tg3 *tp = netdev_priv(dev);
11665
Matt Carlsonbe947302012-12-03 19:36:57 +000011666 tg3_ptp_fini(tp);
11667
Michael Chan65138592012-09-28 07:12:41 +000011668 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011669
11670 /* Clear stats across close / open calls */
11671 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11672 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011673
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011674 if (pci_device_is_present(tp->pdev)) {
11675 tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010011677 tg3_carrier_off(tp);
11678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679 return 0;
11680}
11681
11682static inline u64 get_stat64(tg3_stat64_t *val)
11683{
11684 return ((u64)val->high << 32) | ((u64)val->low);
11685}
11686
11687static u64 tg3_calc_crc_errors(struct tg3 *tp)
11688{
11689 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11690
11691 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011692 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11693 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694 u32 val;
11695
11696 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11697 tg3_writephy(tp, MII_TG3_TEST1,
11698 val | MII_TG3_TEST1_CRC_EN);
11699 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11700 } else
11701 val = 0;
11702
11703 tp->phy_crc_errors += val;
11704
11705 return tp->phy_crc_errors;
11706 }
11707
11708 return get_stat64(&hw_stats->rx_fcs_errors);
11709}
11710
11711#define ESTAT_ADD(member) \
11712 estats->member = old_estats->member + \
11713 get_stat64(&hw_stats->member)
11714
11715static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11716{
11717 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11718 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11719
11720 ESTAT_ADD(rx_octets);
11721 ESTAT_ADD(rx_fragments);
11722 ESTAT_ADD(rx_ucast_packets);
11723 ESTAT_ADD(rx_mcast_packets);
11724 ESTAT_ADD(rx_bcast_packets);
11725 ESTAT_ADD(rx_fcs_errors);
11726 ESTAT_ADD(rx_align_errors);
11727 ESTAT_ADD(rx_xon_pause_rcvd);
11728 ESTAT_ADD(rx_xoff_pause_rcvd);
11729 ESTAT_ADD(rx_mac_ctrl_rcvd);
11730 ESTAT_ADD(rx_xoff_entered);
11731 ESTAT_ADD(rx_frame_too_long_errors);
11732 ESTAT_ADD(rx_jabbers);
11733 ESTAT_ADD(rx_undersize_packets);
11734 ESTAT_ADD(rx_in_length_errors);
11735 ESTAT_ADD(rx_out_length_errors);
11736 ESTAT_ADD(rx_64_or_less_octet_packets);
11737 ESTAT_ADD(rx_65_to_127_octet_packets);
11738 ESTAT_ADD(rx_128_to_255_octet_packets);
11739 ESTAT_ADD(rx_256_to_511_octet_packets);
11740 ESTAT_ADD(rx_512_to_1023_octet_packets);
11741 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11742 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11743 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11744 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11745 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11746
11747 ESTAT_ADD(tx_octets);
11748 ESTAT_ADD(tx_collisions);
11749 ESTAT_ADD(tx_xon_sent);
11750 ESTAT_ADD(tx_xoff_sent);
11751 ESTAT_ADD(tx_flow_control);
11752 ESTAT_ADD(tx_mac_errors);
11753 ESTAT_ADD(tx_single_collisions);
11754 ESTAT_ADD(tx_mult_collisions);
11755 ESTAT_ADD(tx_deferred);
11756 ESTAT_ADD(tx_excessive_collisions);
11757 ESTAT_ADD(tx_late_collisions);
11758 ESTAT_ADD(tx_collide_2times);
11759 ESTAT_ADD(tx_collide_3times);
11760 ESTAT_ADD(tx_collide_4times);
11761 ESTAT_ADD(tx_collide_5times);
11762 ESTAT_ADD(tx_collide_6times);
11763 ESTAT_ADD(tx_collide_7times);
11764 ESTAT_ADD(tx_collide_8times);
11765 ESTAT_ADD(tx_collide_9times);
11766 ESTAT_ADD(tx_collide_10times);
11767 ESTAT_ADD(tx_collide_11times);
11768 ESTAT_ADD(tx_collide_12times);
11769 ESTAT_ADD(tx_collide_13times);
11770 ESTAT_ADD(tx_collide_14times);
11771 ESTAT_ADD(tx_collide_15times);
11772 ESTAT_ADD(tx_ucast_packets);
11773 ESTAT_ADD(tx_mcast_packets);
11774 ESTAT_ADD(tx_bcast_packets);
11775 ESTAT_ADD(tx_carrier_sense_errors);
11776 ESTAT_ADD(tx_discards);
11777 ESTAT_ADD(tx_errors);
11778
11779 ESTAT_ADD(dma_writeq_full);
11780 ESTAT_ADD(dma_write_prioq_full);
11781 ESTAT_ADD(rxbds_empty);
11782 ESTAT_ADD(rx_discards);
11783 ESTAT_ADD(rx_errors);
11784 ESTAT_ADD(rx_threshold_hit);
11785
11786 ESTAT_ADD(dma_readq_full);
11787 ESTAT_ADD(dma_read_prioq_full);
11788 ESTAT_ADD(tx_comp_queue_full);
11789
11790 ESTAT_ADD(ring_set_send_prod_index);
11791 ESTAT_ADD(ring_status_update);
11792 ESTAT_ADD(nic_irqs);
11793 ESTAT_ADD(nic_avoided_irqs);
11794 ESTAT_ADD(nic_tx_threshold_hit);
11795
Matt Carlson4452d092011-05-19 12:12:51 +000011796 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797}
11798
Matt Carlson65ec6982012-02-28 23:33:37 +000011799static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011800{
Eric Dumazet511d2222010-07-07 20:44:24 +000011801 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011802 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11803
Linus Torvalds1da177e2005-04-16 15:20:36 -070011804 stats->rx_packets = old_stats->rx_packets +
11805 get_stat64(&hw_stats->rx_ucast_packets) +
11806 get_stat64(&hw_stats->rx_mcast_packets) +
11807 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011808
Linus Torvalds1da177e2005-04-16 15:20:36 -070011809 stats->tx_packets = old_stats->tx_packets +
11810 get_stat64(&hw_stats->tx_ucast_packets) +
11811 get_stat64(&hw_stats->tx_mcast_packets) +
11812 get_stat64(&hw_stats->tx_bcast_packets);
11813
11814 stats->rx_bytes = old_stats->rx_bytes +
11815 get_stat64(&hw_stats->rx_octets);
11816 stats->tx_bytes = old_stats->tx_bytes +
11817 get_stat64(&hw_stats->tx_octets);
11818
11819 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011820 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 stats->tx_errors = old_stats->tx_errors +
11822 get_stat64(&hw_stats->tx_errors) +
11823 get_stat64(&hw_stats->tx_mac_errors) +
11824 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11825 get_stat64(&hw_stats->tx_discards);
11826
11827 stats->multicast = old_stats->multicast +
11828 get_stat64(&hw_stats->rx_mcast_packets);
11829 stats->collisions = old_stats->collisions +
11830 get_stat64(&hw_stats->tx_collisions);
11831
11832 stats->rx_length_errors = old_stats->rx_length_errors +
11833 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11834 get_stat64(&hw_stats->rx_undersize_packets);
11835
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836 stats->rx_frame_errors = old_stats->rx_frame_errors +
11837 get_stat64(&hw_stats->rx_align_errors);
11838 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11839 get_stat64(&hw_stats->tx_discards);
11840 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11841 get_stat64(&hw_stats->tx_carrier_sense_errors);
11842
11843 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011844 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011845
John W. Linville4f63b872005-09-12 14:43:18 -070011846 stats->rx_missed_errors = old_stats->rx_missed_errors +
11847 get_stat64(&hw_stats->rx_discards);
11848
Eric Dumazetb0057c52010-10-10 19:55:52 +000011849 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011850 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851}
11852
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853static int tg3_get_regs_len(struct net_device *dev)
11854{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011855 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011856}
11857
11858static void tg3_get_regs(struct net_device *dev,
11859 struct ethtool_regs *regs, void *_p)
11860{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011861 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862
11863 regs->version = 0;
11864
Matt Carlson97bd8e42011-04-13 11:05:04 +000011865 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011866
Matt Carlson800960682010-08-02 11:26:06 +000011867 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011868 return;
11869
David S. Millerf47c11e2005-06-24 20:18:35 -070011870 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011871
Matt Carlson97bd8e42011-04-13 11:05:04 +000011872 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011873
David S. Millerf47c11e2005-06-24 20:18:35 -070011874 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011875}
11876
11877static int tg3_get_eeprom_len(struct net_device *dev)
11878{
11879 struct tg3 *tp = netdev_priv(dev);
11880
11881 return tp->nvram_size;
11882}
11883
Linus Torvalds1da177e2005-04-16 15:20:36 -070011884static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11885{
11886 struct tg3 *tp = netdev_priv(dev);
11887 int ret;
11888 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011889 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011890 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011891
Joe Perches63c3a662011-04-26 08:12:10 +000011892 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011893 return -EINVAL;
11894
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895 offset = eeprom->offset;
11896 len = eeprom->len;
11897 eeprom->len = 0;
11898
11899 eeprom->magic = TG3_EEPROM_MAGIC;
11900
11901 if (offset & 3) {
11902 /* adjustments to start on required 4 byte boundary */
11903 b_offset = offset & 3;
11904 b_count = 4 - b_offset;
11905 if (b_count > len) {
11906 /* i.e. offset=1 len=2 */
11907 b_count = len;
11908 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011909 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011910 if (ret)
11911 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011912 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011913 len -= b_count;
11914 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011915 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 }
11917
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011918 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011919 pd = &data[eeprom->len];
11920 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011921 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011922 if (ret) {
11923 eeprom->len += i;
11924 return ret;
11925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926 memcpy(pd + i, &val, 4);
11927 }
11928 eeprom->len += i;
11929
11930 if (len & 3) {
11931 /* read last bytes not ending on 4 byte boundary */
11932 pd = &data[eeprom->len];
11933 b_count = len & 3;
11934 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011935 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 if (ret)
11937 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011938 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011939 eeprom->len += b_count;
11940 }
11941 return 0;
11942}
11943
Linus Torvalds1da177e2005-04-16 15:20:36 -070011944static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11945{
11946 struct tg3 *tp = netdev_priv(dev);
11947 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011948 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011950 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951
Joe Perches63c3a662011-04-26 08:12:10 +000011952 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011953 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954 return -EINVAL;
11955
11956 offset = eeprom->offset;
11957 len = eeprom->len;
11958
11959 if ((b_offset = (offset & 3))) {
11960 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011961 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962 if (ret)
11963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964 len += b_offset;
11965 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011966 if (len < 4)
11967 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 }
11969
11970 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011971 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972 /* adjustments to end on required 4 byte boundary */
11973 odd_len = 1;
11974 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011975 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976 if (ret)
11977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 }
11979
11980 buf = data;
11981 if (b_offset || odd_len) {
11982 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011983 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011984 return -ENOMEM;
11985 if (b_offset)
11986 memcpy(buf, &start, 4);
11987 if (odd_len)
11988 memcpy(buf+len-4, &end, 4);
11989 memcpy(buf + b_offset, data, eeprom->len);
11990 }
11991
11992 ret = tg3_nvram_write_block(tp, offset, len, buf);
11993
11994 if (buf != data)
11995 kfree(buf);
11996
11997 return ret;
11998}
11999
12000static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12001{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012002 struct tg3 *tp = netdev_priv(dev);
12003
Joe Perches63c3a662011-04-26 08:12:10 +000012004 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012005 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012006 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012007 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012008 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012009 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012010 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012012 cmd->supported = (SUPPORTED_Autoneg);
12013
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012014 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012015 cmd->supported |= (SUPPORTED_1000baseT_Half |
12016 SUPPORTED_1000baseT_Full);
12017
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012018 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012019 cmd->supported |= (SUPPORTED_100baseT_Half |
12020 SUPPORTED_100baseT_Full |
12021 SUPPORTED_10baseT_Half |
12022 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080012023 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070012024 cmd->port = PORT_TP;
12025 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012026 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070012027 cmd->port = PORT_FIBRE;
12028 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012029
Linus Torvalds1da177e2005-04-16 15:20:36 -070012030 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000012031 if (tg3_flag(tp, PAUSE_AUTONEG)) {
12032 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
12033 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12034 cmd->advertising |= ADVERTISED_Pause;
12035 } else {
12036 cmd->advertising |= ADVERTISED_Pause |
12037 ADVERTISED_Asym_Pause;
12038 }
12039 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
12040 cmd->advertising |= ADVERTISED_Asym_Pause;
12041 }
12042 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012043 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000012044 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000012046 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012047 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
12048 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
12049 cmd->eth_tp_mdix = ETH_TP_MDI_X;
12050 else
12051 cmd->eth_tp_mdix = ETH_TP_MDI;
12052 }
Matt Carlson64c22182010-10-14 10:37:44 +000012053 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000012054 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
12055 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000012056 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012057 }
Matt Carlson882e9792009-09-01 13:21:36 +000012058 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012059 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012060 cmd->autoneg = tp->link_config.autoneg;
12061 cmd->maxtxpkt = 0;
12062 cmd->maxrxpkt = 0;
12063 return 0;
12064}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012065
Linus Torvalds1da177e2005-04-16 15:20:36 -070012066static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
12067{
12068 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000012069 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012070
Joe Perches63c3a662011-04-26 08:12:10 +000012071 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012072 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012073 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012074 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012075 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012076 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012077 }
12078
Matt Carlson7e5856b2009-02-25 14:23:01 +000012079 if (cmd->autoneg != AUTONEG_ENABLE &&
12080 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070012081 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000012082
12083 if (cmd->autoneg == AUTONEG_DISABLE &&
12084 cmd->duplex != DUPLEX_FULL &&
12085 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070012086 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087
Matt Carlson7e5856b2009-02-25 14:23:01 +000012088 if (cmd->autoneg == AUTONEG_ENABLE) {
12089 u32 mask = ADVERTISED_Autoneg |
12090 ADVERTISED_Pause |
12091 ADVERTISED_Asym_Pause;
12092
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012093 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012094 mask |= ADVERTISED_1000baseT_Half |
12095 ADVERTISED_1000baseT_Full;
12096
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012097 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000012098 mask |= ADVERTISED_100baseT_Half |
12099 ADVERTISED_100baseT_Full |
12100 ADVERTISED_10baseT_Half |
12101 ADVERTISED_10baseT_Full |
12102 ADVERTISED_TP;
12103 else
12104 mask |= ADVERTISED_FIBRE;
12105
12106 if (cmd->advertising & ~mask)
12107 return -EINVAL;
12108
12109 mask &= (ADVERTISED_1000baseT_Half |
12110 ADVERTISED_1000baseT_Full |
12111 ADVERTISED_100baseT_Half |
12112 ADVERTISED_100baseT_Full |
12113 ADVERTISED_10baseT_Half |
12114 ADVERTISED_10baseT_Full);
12115
12116 cmd->advertising &= mask;
12117 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012118 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000012119 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012120 return -EINVAL;
12121
12122 if (cmd->duplex != DUPLEX_FULL)
12123 return -EINVAL;
12124 } else {
David Decotigny25db0332011-04-27 18:32:39 +000012125 if (speed != SPEED_100 &&
12126 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000012127 return -EINVAL;
12128 }
12129 }
12130
David S. Millerf47c11e2005-06-24 20:18:35 -070012131 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012132
12133 tp->link_config.autoneg = cmd->autoneg;
12134 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070012135 tp->link_config.advertising = (cmd->advertising |
12136 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000012137 tp->link_config.speed = SPEED_UNKNOWN;
12138 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012139 } else {
12140 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000012141 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012143 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012144
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012145 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12146
Nithin Sujirce20f162013-04-09 08:48:04 +000012147 tg3_warn_mgmt_link_flap(tp);
12148
Linus Torvalds1da177e2005-04-16 15:20:36 -070012149 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000012150 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012151
David S. Millerf47c11e2005-06-24 20:18:35 -070012152 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012153
Linus Torvalds1da177e2005-04-16 15:20:36 -070012154 return 0;
12155}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012156
Linus Torvalds1da177e2005-04-16 15:20:36 -070012157static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
12158{
12159 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012160
Rick Jones68aad782011-11-07 13:29:27 +000012161 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
12162 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
12163 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
12164 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012165}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012166
Linus Torvalds1da177e2005-04-16 15:20:36 -070012167static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12168{
12169 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012170
Joe Perches63c3a662011-04-26 08:12:10 +000012171 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070012172 wol->supported = WAKE_MAGIC;
12173 else
12174 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012175 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012176 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012177 wol->wolopts = WAKE_MAGIC;
12178 memset(&wol->sopass, 0, sizeof(wol->sopass));
12179}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012180
Linus Torvalds1da177e2005-04-16 15:20:36 -070012181static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
12182{
12183 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012184 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012185
Linus Torvalds1da177e2005-04-16 15:20:36 -070012186 if (wol->wolopts & ~WAKE_MAGIC)
12187 return -EINVAL;
12188 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012189 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012190 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012191
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012192 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
12193
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012194 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000012195 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000012196 else
Joe Perches63c3a662011-04-26 08:12:10 +000012197 tg3_flag_clear(tp, WOL_ENABLE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012198
Linus Torvalds1da177e2005-04-16 15:20:36 -070012199 return 0;
12200}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012201
Linus Torvalds1da177e2005-04-16 15:20:36 -070012202static u32 tg3_get_msglevel(struct net_device *dev)
12203{
12204 struct tg3 *tp = netdev_priv(dev);
12205 return tp->msg_enable;
12206}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012207
Linus Torvalds1da177e2005-04-16 15:20:36 -070012208static void tg3_set_msglevel(struct net_device *dev, u32 value)
12209{
12210 struct tg3 *tp = netdev_priv(dev);
12211 tp->msg_enable = value;
12212}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012213
Linus Torvalds1da177e2005-04-16 15:20:36 -070012214static int tg3_nway_reset(struct net_device *dev)
12215{
12216 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012217 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012218
Linus Torvalds1da177e2005-04-16 15:20:36 -070012219 if (!netif_running(dev))
12220 return -EAGAIN;
12221
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012222 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070012223 return -EINVAL;
12224
Nithin Sujirce20f162013-04-09 08:48:04 +000012225 tg3_warn_mgmt_link_flap(tp);
12226
Joe Perches63c3a662011-04-26 08:12:10 +000012227 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012228 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012229 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020012230 r = phy_start_aneg(tp->mdio_bus->phy_map[tp->phy_addr]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012231 } else {
12232 u32 bmcr;
12233
12234 spin_lock_bh(&tp->lock);
12235 r = -EINVAL;
12236 tg3_readphy(tp, MII_BMCR, &bmcr);
12237 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
12238 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012239 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012240 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
12241 BMCR_ANENABLE);
12242 r = 0;
12243 }
12244 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012245 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012246
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247 return r;
12248}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012249
Linus Torvalds1da177e2005-04-16 15:20:36 -070012250static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12251{
12252 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012253
Matt Carlson2c49a442010-09-30 10:34:35 +000012254 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000012255 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000012256 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080012257 else
12258 ering->rx_jumbo_max_pending = 0;
12259
12260 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012261
12262 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000012263 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080012264 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
12265 else
12266 ering->rx_jumbo_pending = 0;
12267
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012268 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012270
Linus Torvalds1da177e2005-04-16 15:20:36 -070012271static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
12272{
12273 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000012274 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012275
Matt Carlson2c49a442010-09-30 10:34:35 +000012276 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
12277 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070012278 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
12279 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000012280 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070012281 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012282 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012283
Michael Chanbbe832c2005-06-24 20:20:04 -070012284 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012285 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012286 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012287 irq_sync = 1;
12288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012289
Michael Chanbbe832c2005-06-24 20:20:04 -070012290 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012291
Linus Torvalds1da177e2005-04-16 15:20:36 -070012292 tp->rx_pending = ering->rx_pending;
12293
Joe Perches63c3a662011-04-26 08:12:10 +000012294 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012295 tp->rx_pending > 63)
12296 tp->rx_pending = 63;
12297 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000012298
Matt Carlson6fd45cb2010-09-15 08:59:57 +000012299 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000012300 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301
12302 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070012303 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012304 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070012305 if (!err)
12306 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012307 }
12308
David S. Millerf47c11e2005-06-24 20:18:35 -070012309 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012310
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012311 if (irq_sync && !err)
12312 tg3_phy_start(tp);
12313
Michael Chanb9ec6c12006-07-25 16:37:27 -070012314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012315}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012316
Linus Torvalds1da177e2005-04-16 15:20:36 -070012317static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12318{
12319 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012320
Joe Perches63c3a662011-04-26 08:12:10 +000012321 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080012322
Matt Carlson4a2db502011-12-08 14:40:17 +000012323 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080012324 epause->rx_pause = 1;
12325 else
12326 epause->rx_pause = 0;
12327
Matt Carlson4a2db502011-12-08 14:40:17 +000012328 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012329 epause->tx_pause = 1;
12330 else
12331 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012332}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012333
Linus Torvalds1da177e2005-04-16 15:20:36 -070012334static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12335{
12336 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012337 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012338
Nithin Sujirce20f162013-04-09 08:48:04 +000012339 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12340 tg3_warn_mgmt_link_flap(tp);
12341
Joe Perches63c3a662011-04-26 08:12:10 +000012342 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012343 u32 newadv;
12344 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012345
Hauke Mehrtensead24022013-09-28 23:15:26 +020012346 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012347
Matt Carlson27121682010-02-17 15:16:57 +000012348 if (!(phydev->supported & SUPPORTED_Pause) ||
12349 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012350 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012351 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012352
Matt Carlson27121682010-02-17 15:16:57 +000012353 tp->link_config.flowctrl = 0;
12354 if (epause->rx_pause) {
12355 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012356
Matt Carlson27121682010-02-17 15:16:57 +000012357 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012358 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012359 newadv = ADVERTISED_Pause;
12360 } else
12361 newadv = ADVERTISED_Pause |
12362 ADVERTISED_Asym_Pause;
12363 } else if (epause->tx_pause) {
12364 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12365 newadv = ADVERTISED_Asym_Pause;
12366 } else
12367 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012368
Matt Carlson27121682010-02-17 15:16:57 +000012369 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012370 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012371 else
Joe Perches63c3a662011-04-26 08:12:10 +000012372 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012373
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012374 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012375 u32 oldadv = phydev->advertising &
12376 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12377 if (oldadv != newadv) {
12378 phydev->advertising &=
12379 ~(ADVERTISED_Pause |
12380 ADVERTISED_Asym_Pause);
12381 phydev->advertising |= newadv;
12382 if (phydev->autoneg) {
12383 /*
12384 * Always renegotiate the link to
12385 * inform our link partner of our
12386 * flow control settings, even if the
12387 * flow control is forced. Let
12388 * tg3_adjust_link() do the final
12389 * flow control setup.
12390 */
12391 return phy_start_aneg(phydev);
12392 }
12393 }
12394
12395 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012396 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012397 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012398 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012399 ~(ADVERTISED_Pause |
12400 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012401 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012402 }
12403 } else {
12404 int irq_sync = 0;
12405
12406 if (netif_running(dev)) {
12407 tg3_netif_stop(tp);
12408 irq_sync = 1;
12409 }
12410
12411 tg3_full_lock(tp, irq_sync);
12412
12413 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012414 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012415 else
Joe Perches63c3a662011-04-26 08:12:10 +000012416 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012417 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012418 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012419 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012420 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012421 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012422 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012423 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012424 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012425
12426 if (netif_running(dev)) {
12427 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012428 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012429 if (!err)
12430 tg3_netif_start(tp);
12431 }
12432
12433 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012435
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012436 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12437
Michael Chanb9ec6c12006-07-25 16:37:27 -070012438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012440
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012441static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012443 switch (sset) {
12444 case ETH_SS_TEST:
12445 return TG3_NUM_TEST;
12446 case ETH_SS_STATS:
12447 return TG3_NUM_STATS;
12448 default:
12449 return -EOPNOTSUPP;
12450 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012451}
12452
Matt Carlson90415472011-12-16 13:33:23 +000012453static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12454 u32 *rules __always_unused)
12455{
12456 struct tg3 *tp = netdev_priv(dev);
12457
12458 if (!tg3_flag(tp, SUPPORT_MSIX))
12459 return -EOPNOTSUPP;
12460
12461 switch (info->cmd) {
12462 case ETHTOOL_GRXRINGS:
12463 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012464 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012465 else {
12466 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012467 if (info->data > TG3_RSS_MAX_NUM_QS)
12468 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012469 }
12470
12471 /* The first interrupt vector only
12472 * handles link interrupts.
12473 */
12474 info->data -= 1;
12475 return 0;
12476
12477 default:
12478 return -EOPNOTSUPP;
12479 }
12480}
12481
12482static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12483{
12484 u32 size = 0;
12485 struct tg3 *tp = netdev_priv(dev);
12486
12487 if (tg3_flag(tp, SUPPORT_MSIX))
12488 size = TG3_RSS_INDIR_TBL_SIZE;
12489
12490 return size;
12491}
12492
12493static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12494{
12495 struct tg3 *tp = netdev_priv(dev);
12496 int i;
12497
12498 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12499 indir[i] = tp->rss_ind_tbl[i];
12500
12501 return 0;
12502}
12503
12504static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12505{
12506 struct tg3 *tp = netdev_priv(dev);
12507 size_t i;
12508
12509 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12510 tp->rss_ind_tbl[i] = indir[i];
12511
12512 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12513 return 0;
12514
12515 /* It is legal to write the indirection
12516 * table while the device is running.
12517 */
12518 tg3_full_lock(tp, 0);
12519 tg3_rss_write_indir_tbl(tp);
12520 tg3_full_unlock(tp);
12521
12522 return 0;
12523}
12524
Michael Chan09681692012-09-28 07:12:42 +000012525static void tg3_get_channels(struct net_device *dev,
12526 struct ethtool_channels *channel)
12527{
12528 struct tg3 *tp = netdev_priv(dev);
12529 u32 deflt_qs = netif_get_num_default_rss_queues();
12530
12531 channel->max_rx = tp->rxq_max;
12532 channel->max_tx = tp->txq_max;
12533
12534 if (netif_running(dev)) {
12535 channel->rx_count = tp->rxq_cnt;
12536 channel->tx_count = tp->txq_cnt;
12537 } else {
12538 if (tp->rxq_req)
12539 channel->rx_count = tp->rxq_req;
12540 else
12541 channel->rx_count = min(deflt_qs, tp->rxq_max);
12542
12543 if (tp->txq_req)
12544 channel->tx_count = tp->txq_req;
12545 else
12546 channel->tx_count = min(deflt_qs, tp->txq_max);
12547 }
12548}
12549
12550static int tg3_set_channels(struct net_device *dev,
12551 struct ethtool_channels *channel)
12552{
12553 struct tg3 *tp = netdev_priv(dev);
12554
12555 if (!tg3_flag(tp, SUPPORT_MSIX))
12556 return -EOPNOTSUPP;
12557
12558 if (channel->rx_count > tp->rxq_max ||
12559 channel->tx_count > tp->txq_max)
12560 return -EINVAL;
12561
12562 tp->rxq_req = channel->rx_count;
12563 tp->txq_req = channel->tx_count;
12564
12565 if (!netif_running(dev))
12566 return 0;
12567
12568 tg3_stop(tp);
12569
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012570 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012571
Matt Carlsonbe947302012-12-03 19:36:57 +000012572 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012573
12574 return 0;
12575}
12576
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012577static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012578{
12579 switch (stringset) {
12580 case ETH_SS_STATS:
12581 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12582 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012583 case ETH_SS_TEST:
12584 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586 default:
12587 WARN_ON(1); /* we need a WARN() */
12588 break;
12589 }
12590}
12591
stephen hemminger81b87092011-04-04 08:43:50 +000012592static int tg3_set_phys_id(struct net_device *dev,
12593 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012594{
12595 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012596
12597 if (!netif_running(tp->dev))
12598 return -EAGAIN;
12599
stephen hemminger81b87092011-04-04 08:43:50 +000012600 switch (state) {
12601 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012602 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012603
stephen hemminger81b87092011-04-04 08:43:50 +000012604 case ETHTOOL_ID_ON:
12605 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12606 LED_CTRL_1000MBPS_ON |
12607 LED_CTRL_100MBPS_ON |
12608 LED_CTRL_10MBPS_ON |
12609 LED_CTRL_TRAFFIC_OVERRIDE |
12610 LED_CTRL_TRAFFIC_BLINK |
12611 LED_CTRL_TRAFFIC_LED);
12612 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012613
stephen hemminger81b87092011-04-04 08:43:50 +000012614 case ETHTOOL_ID_OFF:
12615 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12616 LED_CTRL_TRAFFIC_OVERRIDE);
12617 break;
Michael Chan4009a932005-09-05 17:52:54 -070012618
stephen hemminger81b87092011-04-04 08:43:50 +000012619 case ETHTOOL_ID_INACTIVE:
12620 tw32(MAC_LED_CTRL, tp->led_ctrl);
12621 break;
Michael Chan4009a932005-09-05 17:52:54 -070012622 }
stephen hemminger81b87092011-04-04 08:43:50 +000012623
Michael Chan4009a932005-09-05 17:52:54 -070012624 return 0;
12625}
12626
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012627static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012628 struct ethtool_stats *estats, u64 *tmp_stats)
12629{
12630 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012631
Matt Carlsonb546e462012-02-13 15:20:09 +000012632 if (tp->hw_stats)
12633 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12634 else
12635 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012636}
12637
Matt Carlson535a4902011-07-20 10:20:56 +000012638static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012639{
12640 int i;
12641 __be32 *buf;
12642 u32 offset = 0, len = 0;
12643 u32 magic, val;
12644
Joe Perches63c3a662011-04-26 08:12:10 +000012645 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012646 return NULL;
12647
12648 if (magic == TG3_EEPROM_MAGIC) {
12649 for (offset = TG3_NVM_DIR_START;
12650 offset < TG3_NVM_DIR_END;
12651 offset += TG3_NVM_DIRENT_SIZE) {
12652 if (tg3_nvram_read(tp, offset, &val))
12653 return NULL;
12654
12655 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12656 TG3_NVM_DIRTYPE_EXTVPD)
12657 break;
12658 }
12659
12660 if (offset != TG3_NVM_DIR_END) {
12661 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12662 if (tg3_nvram_read(tp, offset + 4, &offset))
12663 return NULL;
12664
12665 offset = tg3_nvram_logical_addr(tp, offset);
12666 }
12667 }
12668
12669 if (!offset || !len) {
12670 offset = TG3_NVM_VPD_OFF;
12671 len = TG3_NVM_VPD_LEN;
12672 }
12673
12674 buf = kmalloc(len, GFP_KERNEL);
12675 if (buf == NULL)
12676 return NULL;
12677
12678 if (magic == TG3_EEPROM_MAGIC) {
12679 for (i = 0; i < len; i += 4) {
12680 /* The data is in little-endian format in NVRAM.
12681 * Use the big-endian read routines to preserve
12682 * the byte order as it exists in NVRAM.
12683 */
12684 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12685 goto error;
12686 }
12687 } else {
12688 u8 *ptr;
12689 ssize_t cnt;
12690 unsigned int pos = 0;
12691
12692 ptr = (u8 *)&buf[0];
12693 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12694 cnt = pci_read_vpd(tp->pdev, pos,
12695 len - pos, ptr);
12696 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12697 cnt = 0;
12698 else if (cnt < 0)
12699 goto error;
12700 }
12701 if (pos != len)
12702 goto error;
12703 }
12704
Matt Carlson535a4902011-07-20 10:20:56 +000012705 *vpdlen = len;
12706
Matt Carlsonc3e94502011-04-13 11:05:08 +000012707 return buf;
12708
12709error:
12710 kfree(buf);
12711 return NULL;
12712}
12713
Michael Chan566f86a2005-05-29 14:56:58 -070012714#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012715#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12716#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12717#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012718#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12719#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012720#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012721#define NVRAM_SELFBOOT_HW_SIZE 0x20
12722#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012723
12724static int tg3_test_nvram(struct tg3 *tp)
12725{
Matt Carlson535a4902011-07-20 10:20:56 +000012726 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012727 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012728 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012729
Joe Perches63c3a662011-04-26 08:12:10 +000012730 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012731 return 0;
12732
Matt Carlsone4f34112009-02-25 14:25:00 +000012733 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012734 return -EIO;
12735
Michael Chan1b277772006-03-20 22:27:48 -080012736 if (magic == TG3_EEPROM_MAGIC)
12737 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012738 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012739 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12740 TG3_EEPROM_SB_FORMAT_1) {
12741 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12742 case TG3_EEPROM_SB_REVISION_0:
12743 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12744 break;
12745 case TG3_EEPROM_SB_REVISION_2:
12746 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12747 break;
12748 case TG3_EEPROM_SB_REVISION_3:
12749 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12750 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012751 case TG3_EEPROM_SB_REVISION_4:
12752 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12753 break;
12754 case TG3_EEPROM_SB_REVISION_5:
12755 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12756 break;
12757 case TG3_EEPROM_SB_REVISION_6:
12758 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12759 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012760 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012761 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012762 }
12763 } else
Michael Chan1b277772006-03-20 22:27:48 -080012764 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012765 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12766 size = NVRAM_SELFBOOT_HW_SIZE;
12767 else
Michael Chan1b277772006-03-20 22:27:48 -080012768 return -EIO;
12769
12770 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012771 if (buf == NULL)
12772 return -ENOMEM;
12773
Michael Chan1b277772006-03-20 22:27:48 -080012774 err = -EIO;
12775 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012776 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12777 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012778 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012779 }
Michael Chan1b277772006-03-20 22:27:48 -080012780 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012781 goto out;
12782
Michael Chan1b277772006-03-20 22:27:48 -080012783 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012784 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012785 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012786 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012787 u8 *buf8 = (u8 *) buf, csum8 = 0;
12788
Al Virob9fc7dc2007-12-17 22:59:57 -080012789 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012790 TG3_EEPROM_SB_REVISION_2) {
12791 /* For rev 2, the csum doesn't include the MBA. */
12792 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12793 csum8 += buf8[i];
12794 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12795 csum8 += buf8[i];
12796 } else {
12797 for (i = 0; i < size; i++)
12798 csum8 += buf8[i];
12799 }
Michael Chan1b277772006-03-20 22:27:48 -080012800
Adrian Bunkad96b482006-04-05 22:21:04 -070012801 if (csum8 == 0) {
12802 err = 0;
12803 goto out;
12804 }
12805
12806 err = -EIO;
12807 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012808 }
Michael Chan566f86a2005-05-29 14:56:58 -070012809
Al Virob9fc7dc2007-12-17 22:59:57 -080012810 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012811 TG3_EEPROM_MAGIC_HW) {
12812 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012813 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012814 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012815
12816 /* Separate the parity bits and the data bytes. */
12817 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12818 if ((i == 0) || (i == 8)) {
12819 int l;
12820 u8 msk;
12821
12822 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12823 parity[k++] = buf8[i] & msk;
12824 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012825 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012826 int l;
12827 u8 msk;
12828
12829 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12830 parity[k++] = buf8[i] & msk;
12831 i++;
12832
12833 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12834 parity[k++] = buf8[i] & msk;
12835 i++;
12836 }
12837 data[j++] = buf8[i];
12838 }
12839
12840 err = -EIO;
12841 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12842 u8 hw8 = hweight8(data[i]);
12843
12844 if ((hw8 & 0x1) && parity[i])
12845 goto out;
12846 else if (!(hw8 & 0x1) && !parity[i])
12847 goto out;
12848 }
12849 err = 0;
12850 goto out;
12851 }
12852
Matt Carlson01c3a392011-03-09 16:58:20 +000012853 err = -EIO;
12854
Michael Chan566f86a2005-05-29 14:56:58 -070012855 /* Bootstrap checksum at offset 0x10 */
12856 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012857 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012858 goto out;
12859
12860 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12861 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012862 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012863 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012864
Matt Carlsonc3e94502011-04-13 11:05:08 +000012865 kfree(buf);
12866
Matt Carlson535a4902011-07-20 10:20:56 +000012867 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012868 if (!buf)
12869 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012870
Matt Carlson535a4902011-07-20 10:20:56 +000012871 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012872 if (i > 0) {
12873 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12874 if (j < 0)
12875 goto out;
12876
Matt Carlson535a4902011-07-20 10:20:56 +000012877 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012878 goto out;
12879
12880 i += PCI_VPD_LRDT_TAG_SIZE;
12881 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12882 PCI_VPD_RO_KEYWORD_CHKSUM);
12883 if (j > 0) {
12884 u8 csum8 = 0;
12885
12886 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12887
12888 for (i = 0; i <= j; i++)
12889 csum8 += ((u8 *)buf)[i];
12890
12891 if (csum8)
12892 goto out;
12893 }
12894 }
12895
Michael Chan566f86a2005-05-29 14:56:58 -070012896 err = 0;
12897
12898out:
12899 kfree(buf);
12900 return err;
12901}
12902
Michael Chanca430072005-05-29 14:57:23 -070012903#define TG3_SERDES_TIMEOUT_SEC 2
12904#define TG3_COPPER_TIMEOUT_SEC 6
12905
12906static int tg3_test_link(struct tg3 *tp)
12907{
12908 int i, max;
12909
12910 if (!netif_running(tp->dev))
12911 return -ENODEV;
12912
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012913 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012914 max = TG3_SERDES_TIMEOUT_SEC;
12915 else
12916 max = TG3_COPPER_TIMEOUT_SEC;
12917
12918 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012919 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012920 return 0;
12921
12922 if (msleep_interruptible(1000))
12923 break;
12924 }
12925
12926 return -EIO;
12927}
12928
Michael Chana71116d2005-05-29 14:58:11 -070012929/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012930static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012931{
Michael Chanb16250e2006-09-27 16:10:14 -070012932 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012933 u32 offset, read_mask, write_mask, val, save_val, read_val;
12934 static struct {
12935 u16 offset;
12936 u16 flags;
12937#define TG3_FL_5705 0x1
12938#define TG3_FL_NOT_5705 0x2
12939#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012940#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012941 u32 read_mask;
12942 u32 write_mask;
12943 } reg_tbl[] = {
12944 /* MAC Control Registers */
12945 { MAC_MODE, TG3_FL_NOT_5705,
12946 0x00000000, 0x00ef6f8c },
12947 { MAC_MODE, TG3_FL_5705,
12948 0x00000000, 0x01ef6b8c },
12949 { MAC_STATUS, TG3_FL_NOT_5705,
12950 0x03800107, 0x00000000 },
12951 { MAC_STATUS, TG3_FL_5705,
12952 0x03800100, 0x00000000 },
12953 { MAC_ADDR_0_HIGH, 0x0000,
12954 0x00000000, 0x0000ffff },
12955 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012956 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012957 { MAC_RX_MTU_SIZE, 0x0000,
12958 0x00000000, 0x0000ffff },
12959 { MAC_TX_MODE, 0x0000,
12960 0x00000000, 0x00000070 },
12961 { MAC_TX_LENGTHS, 0x0000,
12962 0x00000000, 0x00003fff },
12963 { MAC_RX_MODE, TG3_FL_NOT_5705,
12964 0x00000000, 0x000007fc },
12965 { MAC_RX_MODE, TG3_FL_5705,
12966 0x00000000, 0x000007dc },
12967 { MAC_HASH_REG_0, 0x0000,
12968 0x00000000, 0xffffffff },
12969 { MAC_HASH_REG_1, 0x0000,
12970 0x00000000, 0xffffffff },
12971 { MAC_HASH_REG_2, 0x0000,
12972 0x00000000, 0xffffffff },
12973 { MAC_HASH_REG_3, 0x0000,
12974 0x00000000, 0xffffffff },
12975
12976 /* Receive Data and Receive BD Initiator Control Registers. */
12977 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12978 0x00000000, 0xffffffff },
12979 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12980 0x00000000, 0xffffffff },
12981 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12982 0x00000000, 0x00000003 },
12983 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12984 0x00000000, 0xffffffff },
12985 { RCVDBDI_STD_BD+0, 0x0000,
12986 0x00000000, 0xffffffff },
12987 { RCVDBDI_STD_BD+4, 0x0000,
12988 0x00000000, 0xffffffff },
12989 { RCVDBDI_STD_BD+8, 0x0000,
12990 0x00000000, 0xffff0002 },
12991 { RCVDBDI_STD_BD+0xc, 0x0000,
12992 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012993
Michael Chana71116d2005-05-29 14:58:11 -070012994 /* Receive BD Initiator Control Registers. */
12995 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12996 0x00000000, 0xffffffff },
12997 { RCVBDI_STD_THRESH, TG3_FL_5705,
12998 0x00000000, 0x000003ff },
12999 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
13000 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013001
Michael Chana71116d2005-05-29 14:58:11 -070013002 /* Host Coalescing Control Registers. */
13003 { HOSTCC_MODE, TG3_FL_NOT_5705,
13004 0x00000000, 0x00000004 },
13005 { HOSTCC_MODE, TG3_FL_5705,
13006 0x00000000, 0x000000f6 },
13007 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
13008 0x00000000, 0xffffffff },
13009 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
13010 0x00000000, 0x000003ff },
13011 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
13012 0x00000000, 0xffffffff },
13013 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
13014 0x00000000, 0x000003ff },
13015 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
13016 0x00000000, 0xffffffff },
13017 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13018 0x00000000, 0x000000ff },
13019 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
13020 0x00000000, 0xffffffff },
13021 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
13022 0x00000000, 0x000000ff },
13023 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
13024 0x00000000, 0xffffffff },
13025 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
13026 0x00000000, 0xffffffff },
13027 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13028 0x00000000, 0xffffffff },
13029 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13030 0x00000000, 0x000000ff },
13031 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
13032 0x00000000, 0xffffffff },
13033 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
13034 0x00000000, 0x000000ff },
13035 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
13036 0x00000000, 0xffffffff },
13037 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
13038 0x00000000, 0xffffffff },
13039 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
13040 0x00000000, 0xffffffff },
13041 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
13042 0x00000000, 0xffffffff },
13043 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
13044 0x00000000, 0xffffffff },
13045 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
13046 0xffffffff, 0x00000000 },
13047 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
13048 0xffffffff, 0x00000000 },
13049
13050 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070013051 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013052 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070013053 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070013054 0x00000000, 0x007fffff },
13055 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
13056 0x00000000, 0x0000003f },
13057 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
13058 0x00000000, 0x000001ff },
13059 { BUFMGR_MB_HIGH_WATER, 0x0000,
13060 0x00000000, 0x000001ff },
13061 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
13062 0xffffffff, 0x00000000 },
13063 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
13064 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013065
Michael Chana71116d2005-05-29 14:58:11 -070013066 /* Mailbox Registers */
13067 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
13068 0x00000000, 0x000001ff },
13069 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
13070 0x00000000, 0x000001ff },
13071 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
13072 0x00000000, 0x000007ff },
13073 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
13074 0x00000000, 0x000001ff },
13075
13076 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
13077 };
13078
Michael Chanb16250e2006-09-27 16:10:14 -070013079 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013080 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070013081 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000013082 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070013083 is_5750 = 1;
13084 }
Michael Chana71116d2005-05-29 14:58:11 -070013085
13086 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
13087 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
13088 continue;
13089
13090 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
13091 continue;
13092
Joe Perches63c3a662011-04-26 08:12:10 +000013093 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070013094 (reg_tbl[i].flags & TG3_FL_NOT_5788))
13095 continue;
13096
Michael Chanb16250e2006-09-27 16:10:14 -070013097 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
13098 continue;
13099
Michael Chana71116d2005-05-29 14:58:11 -070013100 offset = (u32) reg_tbl[i].offset;
13101 read_mask = reg_tbl[i].read_mask;
13102 write_mask = reg_tbl[i].write_mask;
13103
13104 /* Save the original register content */
13105 save_val = tr32(offset);
13106
13107 /* Determine the read-only value. */
13108 read_val = save_val & read_mask;
13109
13110 /* Write zero to the register, then make sure the read-only bits
13111 * are not changed and the read/write bits are all zeros.
13112 */
13113 tw32(offset, 0);
13114
13115 val = tr32(offset);
13116
13117 /* Test the read-only and read/write bits. */
13118 if (((val & read_mask) != read_val) || (val & write_mask))
13119 goto out;
13120
13121 /* Write ones to all the bits defined by RdMask and WrMask, then
13122 * make sure the read-only bits are not changed and the
13123 * read/write bits are all ones.
13124 */
13125 tw32(offset, read_mask | write_mask);
13126
13127 val = tr32(offset);
13128
13129 /* Test the read-only bits. */
13130 if ((val & read_mask) != read_val)
13131 goto out;
13132
13133 /* Test the read/write bits. */
13134 if ((val & write_mask) != write_mask)
13135 goto out;
13136
13137 tw32(offset, save_val);
13138 }
13139
13140 return 0;
13141
13142out:
Michael Chan9f88f292006-12-07 00:22:54 -080013143 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000013144 netdev_err(tp->dev,
13145 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070013146 tw32(offset, save_val);
13147 return -EIO;
13148}
13149
Michael Chan7942e1d2005-05-29 14:58:36 -070013150static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
13151{
Arjan van de Venf71e1302006-03-03 21:33:57 -050013152 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070013153 int i;
13154 u32 j;
13155
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020013156 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070013157 for (j = 0; j < len; j += 4) {
13158 u32 val;
13159
13160 tg3_write_mem(tp, offset + j, test_pattern[i]);
13161 tg3_read_mem(tp, offset + j, &val);
13162 if (val != test_pattern[i])
13163 return -EIO;
13164 }
13165 }
13166 return 0;
13167}
13168
13169static int tg3_test_memory(struct tg3 *tp)
13170{
13171 static struct mem_entry {
13172 u32 offset;
13173 u32 len;
13174 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080013175 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070013176 { 0x00002000, 0x1c000},
13177 { 0xffffffff, 0x00000}
13178 }, mem_tbl_5705[] = {
13179 { 0x00000100, 0x0000c},
13180 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070013181 { 0x00004000, 0x00800},
13182 { 0x00006000, 0x01000},
13183 { 0x00008000, 0x02000},
13184 { 0x00010000, 0x0e000},
13185 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080013186 }, mem_tbl_5755[] = {
13187 { 0x00000200, 0x00008},
13188 { 0x00004000, 0x00800},
13189 { 0x00006000, 0x00800},
13190 { 0x00008000, 0x02000},
13191 { 0x00010000, 0x0c000},
13192 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070013193 }, mem_tbl_5906[] = {
13194 { 0x00000200, 0x00008},
13195 { 0x00004000, 0x00400},
13196 { 0x00006000, 0x00400},
13197 { 0x00008000, 0x01000},
13198 { 0x00010000, 0x01000},
13199 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013200 }, mem_tbl_5717[] = {
13201 { 0x00000200, 0x00008},
13202 { 0x00010000, 0x0a000},
13203 { 0x00020000, 0x13c00},
13204 { 0xffffffff, 0x00000}
13205 }, mem_tbl_57765[] = {
13206 { 0x00000200, 0x00008},
13207 { 0x00004000, 0x00800},
13208 { 0x00006000, 0x09800},
13209 { 0x00010000, 0x0a000},
13210 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070013211 };
13212 struct mem_entry *mem_tbl;
13213 int err = 0;
13214 int i;
13215
Joe Perches63c3a662011-04-26 08:12:10 +000013216 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013217 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000013218 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000013219 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000013220 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000013221 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013222 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000013223 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080013224 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000013225 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013226 mem_tbl = mem_tbl_5705;
13227 else
Michael Chan7942e1d2005-05-29 14:58:36 -070013228 mem_tbl = mem_tbl_570x;
13229
13230 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000013231 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
13232 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070013233 break;
13234 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013235
Michael Chan7942e1d2005-05-29 14:58:36 -070013236 return err;
13237}
13238
Matt Carlsonbb158d62011-04-25 12:42:47 +000013239#define TG3_TSO_MSS 500
13240
13241#define TG3_TSO_IP_HDR_LEN 20
13242#define TG3_TSO_TCP_HDR_LEN 20
13243#define TG3_TSO_TCP_OPT_LEN 12
13244
13245static const u8 tg3_tso_header[] = {
132460x08, 0x00,
132470x45, 0x00, 0x00, 0x00,
132480x00, 0x00, 0x40, 0x00,
132490x40, 0x06, 0x00, 0x00,
132500x0a, 0x00, 0x00, 0x01,
132510x0a, 0x00, 0x00, 0x02,
132520x0d, 0x00, 0xe0, 0x00,
132530x00, 0x00, 0x01, 0x00,
132540x00, 0x00, 0x02, 0x00,
132550x80, 0x10, 0x10, 0x00,
132560x14, 0x09, 0x00, 0x00,
132570x01, 0x01, 0x08, 0x0a,
132580x11, 0x11, 0x11, 0x11,
132590x11, 0x11, 0x11, 0x11,
13260};
Michael Chan9f40dea2005-09-05 17:53:06 -070013261
Matt Carlson28a45952011-08-19 13:58:22 +000013262static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070013263{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013264 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013265 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000013266 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000013267 struct sk_buff *skb;
13268 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070013269 dma_addr_t map;
13270 int num_pkts, tx_len, rx_len, i, err;
13271 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000013272 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000013273 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070013274
Matt Carlsonc8873402010-02-12 14:47:11 +000013275 tnapi = &tp->napi[0];
13276 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013277 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000013278 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000013279 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000013280 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000013281 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000013282 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013283 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000013284
Michael Chanc76949a2005-05-29 14:58:59 -070013285 err = -EIO;
13286
Matt Carlson4852a862011-04-13 11:05:07 +000013287 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070013288 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070013289 if (!skb)
13290 return -ENOMEM;
13291
Michael Chanc76949a2005-05-29 14:58:59 -070013292 tx_data = skb_put(skb, tx_len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070013293 memcpy(tx_data, tp->dev->dev_addr, ETH_ALEN);
13294 memset(tx_data + ETH_ALEN, 0x0, 8);
Michael Chanc76949a2005-05-29 14:58:59 -070013295
Matt Carlson4852a862011-04-13 11:05:07 +000013296 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070013297
Matt Carlson28a45952011-08-19 13:58:22 +000013298 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013299 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
13300
13301 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
13302 TG3_TSO_TCP_OPT_LEN;
13303
13304 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
13305 sizeof(tg3_tso_header));
13306 mss = TG3_TSO_MSS;
13307
13308 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
13309 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
13310
13311 /* Set the total length field in the IP header */
13312 iph->tot_len = htons((u16)(mss + hdr_len));
13313
13314 base_flags = (TXD_FLAG_CPU_PRE_DMA |
13315 TXD_FLAG_CPU_POST_DMA);
13316
Joe Perches63c3a662011-04-26 08:12:10 +000013317 if (tg3_flag(tp, HW_TSO_1) ||
13318 tg3_flag(tp, HW_TSO_2) ||
13319 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013320 struct tcphdr *th;
13321 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
13322 th = (struct tcphdr *)&tx_data[val];
13323 th->check = 0;
13324 } else
13325 base_flags |= TXD_FLAG_TCPUDP_CSUM;
13326
Joe Perches63c3a662011-04-26 08:12:10 +000013327 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013328 mss |= (hdr_len & 0xc) << 12;
13329 if (hdr_len & 0x10)
13330 base_flags |= 0x00000010;
13331 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013332 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013333 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013334 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013335 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013336 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13337 } else {
13338 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13339 }
13340
13341 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13342 } else {
13343 num_pkts = 1;
13344 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013345
13346 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13347 tx_len > VLAN_ETH_FRAME_LEN)
13348 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013349 }
13350
13351 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013352 tx_data[i] = (u8) (i & 0xff);
13353
Alexander Duyckf4188d82009-12-02 16:48:38 +000013354 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13355 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013356 dev_kfree_skb(skb);
13357 return -EIO;
13358 }
Michael Chanc76949a2005-05-29 14:58:59 -070013359
Matt Carlson0d681b22011-07-27 14:20:49 +000013360 val = tnapi->tx_prod;
13361 tnapi->tx_buffers[val].skb = skb;
13362 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13363
Michael Chanc76949a2005-05-29 14:58:59 -070013364 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013365 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013366
13367 udelay(10);
13368
Matt Carlson898a56f2009-08-28 14:02:40 +000013369 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013370
Matt Carlson84b67b22011-07-27 14:20:52 +000013371 budget = tg3_tx_avail(tnapi);
13372 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013373 base_flags | TXD_FLAG_END, mss, 0)) {
13374 tnapi->tx_buffers[val].skb = NULL;
13375 dev_kfree_skb(skb);
13376 return -EIO;
13377 }
Michael Chanc76949a2005-05-29 14:58:59 -070013378
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013379 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013380
Michael Chan6541b802012-03-04 14:48:14 +000013381 /* Sync BD data before updating mailbox */
13382 wmb();
13383
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013384 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13385 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013386
13387 udelay(10);
13388
Matt Carlson303fc922009-11-02 14:27:34 +000013389 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13390 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013391 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013392 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013393
13394 udelay(10);
13395
Matt Carlson898a56f2009-08-28 14:02:40 +000013396 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13397 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013398 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013399 (rx_idx == (rx_start_idx + num_pkts)))
13400 break;
13401 }
13402
Matt Carlsonba1142e2011-11-04 09:15:00 +000013403 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013404 dev_kfree_skb(skb);
13405
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013406 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013407 goto out;
13408
13409 if (rx_idx != rx_start_idx + num_pkts)
13410 goto out;
13411
Matt Carlsonbb158d62011-04-25 12:42:47 +000013412 val = data_off;
13413 while (rx_idx != rx_start_idx) {
13414 desc = &rnapi->rx_rcb[rx_start_idx++];
13415 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13416 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013417
Matt Carlsonbb158d62011-04-25 12:42:47 +000013418 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13419 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013420 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013421
Matt Carlsonbb158d62011-04-25 12:42:47 +000013422 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13423 - ETH_FCS_LEN;
13424
Matt Carlson28a45952011-08-19 13:58:22 +000013425 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013426 if (rx_len != tx_len)
13427 goto out;
13428
13429 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13430 if (opaque_key != RXD_OPAQUE_RING_STD)
13431 goto out;
13432 } else {
13433 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13434 goto out;
13435 }
13436 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13437 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013438 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013439 goto out;
13440 }
13441
13442 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013443 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013444 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13445 mapping);
13446 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013447 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013448 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13449 mapping);
13450 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013451 goto out;
13452
Matt Carlsonbb158d62011-04-25 12:42:47 +000013453 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13454 PCI_DMA_FROMDEVICE);
13455
Eric Dumazet9205fd92011-11-18 06:47:01 +000013456 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013457 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013458 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013459 goto out;
13460 }
Matt Carlson4852a862011-04-13 11:05:07 +000013461 }
13462
Michael Chanc76949a2005-05-29 14:58:59 -070013463 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013464
Eric Dumazet9205fd92011-11-18 06:47:01 +000013465 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013466out:
13467 return err;
13468}
13469
Matt Carlson00c266b2011-04-25 12:42:46 +000013470#define TG3_STD_LOOPBACK_FAILED 1
13471#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013472#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013473#define TG3_LOOPBACK_FAILED \
13474 (TG3_STD_LOOPBACK_FAILED | \
13475 TG3_JMB_LOOPBACK_FAILED | \
13476 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013477
Matt Carlson941ec902011-08-19 13:58:23 +000013478static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013479{
Matt Carlson28a45952011-08-19 13:58:22 +000013480 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013481 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013482 u32 jmb_pkt_sz = 9000;
13483
13484 if (tp->dma_limit)
13485 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013486
Matt Carlsonab789042011-01-25 15:58:54 +000013487 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13488 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13489
Matt Carlson28a45952011-08-19 13:58:22 +000013490 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013491 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13492 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013493 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013494 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013495 goto done;
13496 }
13497
Joe Perches953c96e2013-04-09 10:18:14 +000013498 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013499 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013500 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13501 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013502 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013503 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013504 goto done;
13505 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013506
Joe Perches63c3a662011-04-26 08:12:10 +000013507 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013508 int i;
13509
13510 /* Reroute all rx packets to the 1st queue */
13511 for (i = MAC_RSS_INDIR_TBL_0;
13512 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13513 tw32(i, 0x0);
13514 }
13515
Matt Carlson6e01b202011-08-19 13:58:20 +000013516 /* HW errata - mac loopback fails in some cases on 5780.
13517 * Normal traffic and PHY loopback are not affected by
13518 * errata. Also, the MAC loopback test is deprecated for
13519 * all newer ASIC revisions.
13520 */
Joe Perches41535772013-02-16 11:20:04 +000013521 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013522 !tg3_flag(tp, CPMU_PRESENT)) {
13523 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013524
Matt Carlson28a45952011-08-19 13:58:22 +000013525 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013526 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013527
13528 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013529 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013530 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013531
13532 tg3_mac_loopback(tp, false);
13533 }
Matt Carlson4852a862011-04-13 11:05:07 +000013534
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013535 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013536 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013537 int i;
13538
Matt Carlson941ec902011-08-19 13:58:23 +000013539 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013540
13541 /* Wait for link */
13542 for (i = 0; i < 100; i++) {
13543 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13544 break;
13545 mdelay(1);
13546 }
13547
Matt Carlson28a45952011-08-19 13:58:22 +000013548 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013549 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013550 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013551 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013552 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013553 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013554 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013555 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013556
Matt Carlson941ec902011-08-19 13:58:23 +000013557 if (do_extlpbk) {
13558 tg3_phy_lpbk_set(tp, 0, true);
13559
13560 /* All link indications report up, but the hardware
13561 * isn't really ready for about 20 msec. Double it
13562 * to be sure.
13563 */
13564 mdelay(40);
13565
13566 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013567 data[TG3_EXT_LOOPB_TEST] |=
13568 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013569 if (tg3_flag(tp, TSO_CAPABLE) &&
13570 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013571 data[TG3_EXT_LOOPB_TEST] |=
13572 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013573 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013574 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013575 data[TG3_EXT_LOOPB_TEST] |=
13576 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013577 }
13578
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013579 /* Re-enable gphy autopowerdown. */
13580 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13581 tg3_phy_toggle_apd(tp, true);
13582 }
Matt Carlson6833c042008-11-21 17:18:59 -080013583
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013584 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13585 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013586
Matt Carlsonab789042011-01-25 15:58:54 +000013587done:
13588 tp->phy_flags |= eee_cap;
13589
Michael Chan9f40dea2005-09-05 17:53:06 -070013590 return err;
13591}
13592
Michael Chan4cafd3f2005-05-29 14:56:34 -070013593static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13594 u64 *data)
13595{
Michael Chan566f86a2005-05-29 14:56:58 -070013596 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013597 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013598
Nithin Sujir2e460fc2013-05-23 11:11:22 +000013599 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
13600 if (tg3_power_up(tp)) {
13601 etest->flags |= ETH_TEST_FL_FAILED;
13602 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13603 return;
13604 }
13605 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Matt Carlsonbed98292011-07-13 09:27:29 +000013606 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013607
Michael Chan566f86a2005-05-29 14:56:58 -070013608 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13609
13610 if (tg3_test_nvram(tp) != 0) {
13611 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013612 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013613 }
Matt Carlson941ec902011-08-19 13:58:23 +000013614 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013615 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013616 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013617 }
Michael Chana71116d2005-05-29 14:58:11 -070013618 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013619 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013620
Michael Chanbbe832c2005-06-24 20:20:04 -070013621 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013622 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013623 tg3_netif_stop(tp);
13624 irq_sync = 1;
13625 }
13626
13627 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013628 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013629 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013630 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013631 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013632 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013633 if (!err)
13634 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013635
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013636 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080013637 tg3_phy_reset(tp);
13638
Michael Chana71116d2005-05-29 14:58:11 -070013639 if (tg3_test_registers(tp) != 0) {
13640 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013641 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013642 }
Matt Carlson28a45952011-08-19 13:58:22 +000013643
Michael Chan7942e1d2005-05-29 14:58:36 -070013644 if (tg3_test_memory(tp) != 0) {
13645 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013646 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013647 }
Matt Carlson28a45952011-08-19 13:58:22 +000013648
Matt Carlson941ec902011-08-19 13:58:23 +000013649 if (doextlpbk)
13650 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13651
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013652 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013653 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013654
David S. Millerf47c11e2005-06-24 20:18:35 -070013655 tg3_full_unlock(tp);
13656
Michael Chand4bc3922005-05-29 14:59:20 -070013657 if (tg3_test_interrupt(tp) != 0) {
13658 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013659 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013660 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013661
13662 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013663
Michael Chana71116d2005-05-29 14:58:11 -070013664 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13665 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013666 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013667 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013668 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013669 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013670 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013671
13672 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013673
13674 if (irq_sync && !err2)
13675 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013676 }
Matt Carlson800960682010-08-02 11:26:06 +000013677 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Nithin Sujir5137a2e2013-07-29 13:58:36 -070013678 tg3_power_down_prepare(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013679
Michael Chan4cafd3f2005-05-29 14:56:34 -070013680}
13681
Ben Hutchings72608992013-11-18 22:59:43 +000013682static int tg3_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013683{
13684 struct tg3 *tp = netdev_priv(dev);
13685 struct hwtstamp_config stmpconf;
13686
13687 if (!tg3_flag(tp, PTP_CAPABLE))
Ben Hutchings72608992013-11-18 22:59:43 +000013688 return -EOPNOTSUPP;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013689
13690 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13691 return -EFAULT;
13692
13693 if (stmpconf.flags)
13694 return -EINVAL;
13695
Ben Hutchings58b187c2013-11-14 00:40:56 +000013696 if (stmpconf.tx_type != HWTSTAMP_TX_ON &&
13697 stmpconf.tx_type != HWTSTAMP_TX_OFF)
Matt Carlson0a633ac2012-12-03 19:36:59 +000013698 return -ERANGE;
Matt Carlson0a633ac2012-12-03 19:36:59 +000013699
13700 switch (stmpconf.rx_filter) {
13701 case HWTSTAMP_FILTER_NONE:
13702 tp->rxptpctl = 0;
13703 break;
13704 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13705 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13706 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13707 break;
13708 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13709 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13710 TG3_RX_PTP_CTL_SYNC_EVNT;
13711 break;
13712 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13713 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13714 TG3_RX_PTP_CTL_DELAY_REQ;
13715 break;
13716 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13717 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13718 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13719 break;
13720 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13721 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13722 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13723 break;
13724 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13725 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13726 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13727 break;
13728 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13729 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13730 TG3_RX_PTP_CTL_SYNC_EVNT;
13731 break;
13732 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13733 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13734 TG3_RX_PTP_CTL_SYNC_EVNT;
13735 break;
13736 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13737 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13738 TG3_RX_PTP_CTL_SYNC_EVNT;
13739 break;
13740 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13741 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13742 TG3_RX_PTP_CTL_DELAY_REQ;
13743 break;
13744 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13745 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13746 TG3_RX_PTP_CTL_DELAY_REQ;
13747 break;
13748 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13749 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13750 TG3_RX_PTP_CTL_DELAY_REQ;
13751 break;
13752 default:
13753 return -ERANGE;
13754 }
13755
13756 if (netif_running(dev) && tp->rxptpctl)
13757 tw32(TG3_RX_PTP_CTL,
13758 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13759
Ben Hutchings58b187c2013-11-14 00:40:56 +000013760 if (stmpconf.tx_type == HWTSTAMP_TX_ON)
13761 tg3_flag_set(tp, TX_TSTAMP_EN);
13762 else
13763 tg3_flag_clear(tp, TX_TSTAMP_EN);
13764
Matt Carlson0a633ac2012-12-03 19:36:59 +000013765 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13766 -EFAULT : 0;
13767}
13768
Ben Hutchings72608992013-11-18 22:59:43 +000013769static int tg3_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
13770{
13771 struct tg3 *tp = netdev_priv(dev);
13772 struct hwtstamp_config stmpconf;
13773
13774 if (!tg3_flag(tp, PTP_CAPABLE))
13775 return -EOPNOTSUPP;
13776
13777 stmpconf.flags = 0;
13778 stmpconf.tx_type = (tg3_flag(tp, TX_TSTAMP_EN) ?
13779 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF);
13780
13781 switch (tp->rxptpctl) {
13782 case 0:
13783 stmpconf.rx_filter = HWTSTAMP_FILTER_NONE;
13784 break;
13785 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_ALL_V1_EVENTS:
13786 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
13787 break;
13788 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13789 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
13790 break;
13791 case TG3_RX_PTP_CTL_RX_PTP_V1_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13792 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
13793 break;
13794 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13795 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13796 break;
13797 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13798 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
13799 break;
13800 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_ALL_V2_EVENTS:
13801 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
13802 break;
13803 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13804 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
13805 break;
13806 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13807 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_SYNC;
13808 break;
13809 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_SYNC_EVNT:
13810 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
13811 break;
13812 case TG3_RX_PTP_CTL_RX_PTP_V2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13813 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
13814 break;
13815 case TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13816 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ;
13817 break;
13818 case TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN | TG3_RX_PTP_CTL_DELAY_REQ:
13819 stmpconf.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
13820 break;
13821 default:
13822 WARN_ON_ONCE(1);
13823 return -ERANGE;
13824 }
13825
13826 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13827 -EFAULT : 0;
13828}
13829
Linus Torvalds1da177e2005-04-16 15:20:36 -070013830static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13831{
13832 struct mii_ioctl_data *data = if_mii(ifr);
13833 struct tg3 *tp = netdev_priv(dev);
13834 int err;
13835
Joe Perches63c3a662011-04-26 08:12:10 +000013836 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013837 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013838 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013839 return -EAGAIN;
Hauke Mehrtensead24022013-09-28 23:15:26 +020013840 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Richard Cochran28b04112010-07-17 08:48:55 +000013841 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013842 }
13843
Matt Carlson33f401a2010-04-05 10:19:27 +000013844 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013845 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013846 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013847
13848 /* fallthru */
13849 case SIOCGMIIREG: {
13850 u32 mii_regval;
13851
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013852 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013853 break; /* We have no PHY */
13854
Matt Carlson34eea5a2011-04-20 07:57:38 +000013855 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013856 return -EAGAIN;
13857
David S. Millerf47c11e2005-06-24 20:18:35 -070013858 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013859 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13860 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013861 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013862
13863 data->val_out = mii_regval;
13864
13865 return err;
13866 }
13867
13868 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013869 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013870 break; /* We have no PHY */
13871
Matt Carlson34eea5a2011-04-20 07:57:38 +000013872 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013873 return -EAGAIN;
13874
David S. Millerf47c11e2005-06-24 20:18:35 -070013875 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013876 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13877 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013878 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013879
13880 return err;
13881
Matt Carlson0a633ac2012-12-03 19:36:59 +000013882 case SIOCSHWTSTAMP:
Ben Hutchings72608992013-11-18 22:59:43 +000013883 return tg3_hwtstamp_set(dev, ifr);
13884
13885 case SIOCGHWTSTAMP:
13886 return tg3_hwtstamp_get(dev, ifr);
Matt Carlson0a633ac2012-12-03 19:36:59 +000013887
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888 default:
13889 /* do nothing */
13890 break;
13891 }
13892 return -EOPNOTSUPP;
13893}
13894
David S. Miller15f98502005-05-18 22:49:26 -070013895static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13896{
13897 struct tg3 *tp = netdev_priv(dev);
13898
13899 memcpy(ec, &tp->coal, sizeof(*ec));
13900 return 0;
13901}
13902
Michael Chand244c892005-07-05 14:42:33 -070013903static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13904{
13905 struct tg3 *tp = netdev_priv(dev);
13906 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13907 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13908
Joe Perches63c3a662011-04-26 08:12:10 +000013909 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013910 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13911 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13912 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13913 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13914 }
13915
13916 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13917 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13918 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13919 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13920 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13921 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13922 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13923 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13924 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13925 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13926 return -EINVAL;
13927
13928 /* No rx interrupts will be generated if both are zero */
13929 if ((ec->rx_coalesce_usecs == 0) &&
13930 (ec->rx_max_coalesced_frames == 0))
13931 return -EINVAL;
13932
13933 /* No tx interrupts will be generated if both are zero */
13934 if ((ec->tx_coalesce_usecs == 0) &&
13935 (ec->tx_max_coalesced_frames == 0))
13936 return -EINVAL;
13937
13938 /* Only copy relevant parameters, ignore all others. */
13939 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13940 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13941 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13942 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13943 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13944 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13945 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13946 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13947 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13948
13949 if (netif_running(dev)) {
13950 tg3_full_lock(tp, 0);
13951 __tg3_set_coalesce(tp, &tp->coal);
13952 tg3_full_unlock(tp);
13953 }
13954 return 0;
13955}
13956
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000013957static int tg3_set_eee(struct net_device *dev, struct ethtool_eee *edata)
13958{
13959 struct tg3 *tp = netdev_priv(dev);
13960
13961 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13962 netdev_warn(tp->dev, "Board does not support EEE!\n");
13963 return -EOPNOTSUPP;
13964 }
13965
13966 if (edata->advertised != tp->eee.advertised) {
13967 netdev_warn(tp->dev,
13968 "Direct manipulation of EEE advertisement is not supported\n");
13969 return -EINVAL;
13970 }
13971
13972 if (edata->tx_lpi_timer > TG3_CPMU_DBTMR1_LNKIDLE_MAX) {
13973 netdev_warn(tp->dev,
13974 "Maximal Tx Lpi timer supported is %#x(u)\n",
13975 TG3_CPMU_DBTMR1_LNKIDLE_MAX);
13976 return -EINVAL;
13977 }
13978
13979 tp->eee = *edata;
13980
13981 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
13982 tg3_warn_mgmt_link_flap(tp);
13983
13984 if (netif_running(tp->dev)) {
13985 tg3_full_lock(tp, 0);
13986 tg3_setup_eee(tp);
13987 tg3_phy_reset(tp);
13988 tg3_full_unlock(tp);
13989 }
13990
13991 return 0;
13992}
13993
13994static int tg3_get_eee(struct net_device *dev, struct ethtool_eee *edata)
13995{
13996 struct tg3 *tp = netdev_priv(dev);
13997
13998 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
13999 netdev_warn(tp->dev,
14000 "Board does not support EEE!\n");
14001 return -EOPNOTSUPP;
14002 }
14003
14004 *edata = tp->eee;
14005 return 0;
14006}
14007
Jeff Garzik7282d492006-09-13 14:30:00 -040014008static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014009 .get_settings = tg3_get_settings,
14010 .set_settings = tg3_set_settings,
14011 .get_drvinfo = tg3_get_drvinfo,
14012 .get_regs_len = tg3_get_regs_len,
14013 .get_regs = tg3_get_regs,
14014 .get_wol = tg3_get_wol,
14015 .set_wol = tg3_set_wol,
14016 .get_msglevel = tg3_get_msglevel,
14017 .set_msglevel = tg3_set_msglevel,
14018 .nway_reset = tg3_nway_reset,
14019 .get_link = ethtool_op_get_link,
14020 .get_eeprom_len = tg3_get_eeprom_len,
14021 .get_eeprom = tg3_get_eeprom,
14022 .set_eeprom = tg3_set_eeprom,
14023 .get_ringparam = tg3_get_ringparam,
14024 .set_ringparam = tg3_set_ringparam,
14025 .get_pauseparam = tg3_get_pauseparam,
14026 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070014027 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014028 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000014029 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014030 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070014031 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070014032 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070014033 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000014034 .get_rxnfc = tg3_get_rxnfc,
14035 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
14036 .get_rxfh_indir = tg3_get_rxfh_indir,
14037 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000014038 .get_channels = tg3_get_channels,
14039 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000014040 .get_ts_info = tg3_get_ts_info,
Nithin Sujir1cbf9eb2013-05-18 06:26:55 +000014041 .get_eee = tg3_get_eee,
14042 .set_eee = tg3_set_eee,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014043};
14044
David S. Millerb4017c52012-03-01 17:57:40 -050014045static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
14046 struct rtnl_link_stats64 *stats)
14047{
14048 struct tg3 *tp = netdev_priv(dev);
14049
David S. Millerb4017c52012-03-01 17:57:40 -050014050 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000014051 if (!tp->hw_stats) {
14052 spin_unlock_bh(&tp->lock);
14053 return &tp->net_stats_prev;
14054 }
14055
David S. Millerb4017c52012-03-01 17:57:40 -050014056 tg3_get_nstats(tp, stats);
14057 spin_unlock_bh(&tp->lock);
14058
14059 return stats;
14060}
14061
Matt Carlsonccd5ba92012-02-13 10:20:08 +000014062static void tg3_set_rx_mode(struct net_device *dev)
14063{
14064 struct tg3 *tp = netdev_priv(dev);
14065
14066 if (!netif_running(dev))
14067 return;
14068
14069 tg3_full_lock(tp, 0);
14070 __tg3_set_rx_mode(dev);
14071 tg3_full_unlock(tp);
14072}
14073
Matt Carlsonfaf16272012-02-13 10:20:07 +000014074static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
14075 int new_mtu)
14076{
14077 dev->mtu = new_mtu;
14078
14079 if (new_mtu > ETH_DATA_LEN) {
14080 if (tg3_flag(tp, 5780_CLASS)) {
14081 netdev_update_features(dev);
14082 tg3_flag_clear(tp, TSO_CAPABLE);
14083 } else {
14084 tg3_flag_set(tp, JUMBO_RING_ENABLE);
14085 }
14086 } else {
14087 if (tg3_flag(tp, 5780_CLASS)) {
14088 tg3_flag_set(tp, TSO_CAPABLE);
14089 netdev_update_features(dev);
14090 }
14091 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
14092 }
14093}
14094
14095static int tg3_change_mtu(struct net_device *dev, int new_mtu)
14096{
14097 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000014098 int err;
14099 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000014100
14101 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
14102 return -EINVAL;
14103
14104 if (!netif_running(dev)) {
14105 /* We'll just catch it later when the
14106 * device is up'd.
14107 */
14108 tg3_set_mtu(dev, tp, new_mtu);
14109 return 0;
14110 }
14111
14112 tg3_phy_stop(tp);
14113
14114 tg3_netif_stop(tp);
14115
Nithin Sujirc6993df2014-02-06 14:13:05 -080014116 tg3_set_mtu(dev, tp, new_mtu);
14117
Matt Carlsonfaf16272012-02-13 10:20:07 +000014118 tg3_full_lock(tp, 1);
14119
14120 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
14121
Michael Chan2fae5e32012-03-04 14:48:15 +000014122 /* Reset PHY, otherwise the read DMA engine will be in a mode that
14123 * breaks all requests to 256 bytes.
14124 */
Joe Perches41535772013-02-16 11:20:04 +000014125 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000014126 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000014127
14128 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000014129
14130 if (!err)
14131 tg3_netif_start(tp);
14132
14133 tg3_full_unlock(tp);
14134
14135 if (!err)
14136 tg3_phy_start(tp);
14137
14138 return err;
14139}
14140
14141static const struct net_device_ops tg3_netdev_ops = {
14142 .ndo_open = tg3_open,
14143 .ndo_stop = tg3_close,
14144 .ndo_start_xmit = tg3_start_xmit,
14145 .ndo_get_stats64 = tg3_get_stats64,
14146 .ndo_validate_addr = eth_validate_addr,
14147 .ndo_set_rx_mode = tg3_set_rx_mode,
14148 .ndo_set_mac_address = tg3_set_mac_addr,
14149 .ndo_do_ioctl = tg3_ioctl,
14150 .ndo_tx_timeout = tg3_tx_timeout,
14151 .ndo_change_mtu = tg3_change_mtu,
14152 .ndo_fix_features = tg3_fix_features,
14153 .ndo_set_features = tg3_set_features,
14154#ifdef CONFIG_NET_POLL_CONTROLLER
14155 .ndo_poll_controller = tg3_poll_controller,
14156#endif
14157};
14158
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014159static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160{
Michael Chan1b277772006-03-20 22:27:48 -080014161 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162
14163 tp->nvram_size = EEPROM_CHIP_SIZE;
14164
Matt Carlsone4f34112009-02-25 14:25:00 +000014165 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014166 return;
14167
Michael Chanb16250e2006-09-27 16:10:14 -070014168 if ((magic != TG3_EEPROM_MAGIC) &&
14169 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
14170 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171 return;
14172
14173 /*
14174 * Size the chip by reading offsets at increasing powers of two.
14175 * When we encounter our validation signature, we know the addressing
14176 * has wrapped around, and thus have our chip size.
14177 */
Michael Chan1b277772006-03-20 22:27:48 -080014178 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014179
14180 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014181 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014182 return;
14183
Michael Chan18201802006-03-20 22:29:15 -080014184 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014185 break;
14186
14187 cursize <<= 1;
14188 }
14189
14190 tp->nvram_size = cursize;
14191}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014192
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014193static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014194{
14195 u32 val;
14196
Joe Perches63c3a662011-04-26 08:12:10 +000014197 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080014198 return;
14199
14200 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080014201 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080014202 tg3_get_eeprom_size(tp);
14203 return;
14204 }
14205
Matt Carlson6d348f22009-02-25 14:25:52 +000014206 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000014208 /* This is confusing. We want to operate on the
14209 * 16-bit value at offset 0xf2. The tg3_nvram_read()
14210 * call will read from NVRAM and byteswap the data
14211 * according to the byteswapping settings for all
14212 * other register accesses. This ensures the data we
14213 * want will always reside in the lower 16-bits.
14214 * However, the data in NVRAM is in LE format, which
14215 * means the data from the NVRAM read will always be
14216 * opposite the endianness of the CPU. The 16-bit
14217 * byteswap then brings the data to CPU endianness.
14218 */
14219 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014220 return;
14221 }
14222 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070014223 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224}
14225
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014226static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014227{
14228 u32 nvcfg1;
14229
14230 nvcfg1 = tr32(NVRAM_CFG1);
14231 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000014232 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014233 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014234 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14235 tw32(NVRAM_CFG1, nvcfg1);
14236 }
14237
Joe Perches41535772013-02-16 11:20:04 +000014238 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014239 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014241 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
14242 tp->nvram_jedecnum = JEDEC_ATMEL;
14243 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014244 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014245 break;
14246 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
14247 tp->nvram_jedecnum = JEDEC_ATMEL;
14248 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
14249 break;
14250 case FLASH_VENDOR_ATMEL_EEPROM:
14251 tp->nvram_jedecnum = JEDEC_ATMEL;
14252 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014253 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014254 break;
14255 case FLASH_VENDOR_ST:
14256 tp->nvram_jedecnum = JEDEC_ST;
14257 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014258 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014259 break;
14260 case FLASH_VENDOR_SAIFUN:
14261 tp->nvram_jedecnum = JEDEC_SAIFUN;
14262 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
14263 break;
14264 case FLASH_VENDOR_SST_SMALL:
14265 case FLASH_VENDOR_SST_LARGE:
14266 tp->nvram_jedecnum = JEDEC_SST;
14267 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
14268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014269 }
Matt Carlson8590a602009-08-28 12:29:16 +000014270 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014271 tp->nvram_jedecnum = JEDEC_ATMEL;
14272 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000014273 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 }
14275}
14276
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014277static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014278{
14279 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
14280 case FLASH_5752PAGE_SIZE_256:
14281 tp->nvram_pagesize = 256;
14282 break;
14283 case FLASH_5752PAGE_SIZE_512:
14284 tp->nvram_pagesize = 512;
14285 break;
14286 case FLASH_5752PAGE_SIZE_1K:
14287 tp->nvram_pagesize = 1024;
14288 break;
14289 case FLASH_5752PAGE_SIZE_2K:
14290 tp->nvram_pagesize = 2048;
14291 break;
14292 case FLASH_5752PAGE_SIZE_4K:
14293 tp->nvram_pagesize = 4096;
14294 break;
14295 case FLASH_5752PAGE_SIZE_264:
14296 tp->nvram_pagesize = 264;
14297 break;
14298 case FLASH_5752PAGE_SIZE_528:
14299 tp->nvram_pagesize = 528;
14300 break;
14301 }
14302}
14303
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014304static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070014305{
14306 u32 nvcfg1;
14307
14308 nvcfg1 = tr32(NVRAM_CFG1);
14309
Michael Chane6af3012005-04-21 17:12:05 -070014310 /* NVRAM protection for TPM */
14311 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000014312 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070014313
Michael Chan361b4ac2005-04-21 17:11:21 -070014314 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014315 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
14316 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
14317 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014318 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014319 break;
14320 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14321 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014322 tg3_flag_set(tp, NVRAM_BUFFERED);
14323 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014324 break;
14325 case FLASH_5752VENDOR_ST_M45PE10:
14326 case FLASH_5752VENDOR_ST_M45PE20:
14327 case FLASH_5752VENDOR_ST_M45PE40:
14328 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014329 tg3_flag_set(tp, NVRAM_BUFFERED);
14330 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014331 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070014332 }
14333
Joe Perches63c3a662011-04-26 08:12:10 +000014334 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000014335 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000014336 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070014337 /* For eeprom, set pagesize to maximum eeprom size */
14338 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14339
14340 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14341 tw32(NVRAM_CFG1, nvcfg1);
14342 }
14343}
14344
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014345static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080014346{
Matt Carlson989a9d22007-05-05 11:51:05 -070014347 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080014348
14349 nvcfg1 = tr32(NVRAM_CFG1);
14350
14351 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070014352 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014353 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070014354 protect = 1;
14355 }
Michael Chand3c7b882006-03-23 01:28:25 -080014356
Matt Carlson989a9d22007-05-05 11:51:05 -070014357 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14358 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014359 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14360 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14361 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14362 case FLASH_5755VENDOR_ATMEL_FLASH_5:
14363 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014364 tg3_flag_set(tp, NVRAM_BUFFERED);
14365 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014366 tp->nvram_pagesize = 264;
14367 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
14368 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
14369 tp->nvram_size = (protect ? 0x3e200 :
14370 TG3_NVRAM_SIZE_512KB);
14371 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
14372 tp->nvram_size = (protect ? 0x1f200 :
14373 TG3_NVRAM_SIZE_256KB);
14374 else
14375 tp->nvram_size = (protect ? 0x1f200 :
14376 TG3_NVRAM_SIZE_128KB);
14377 break;
14378 case FLASH_5752VENDOR_ST_M45PE10:
14379 case FLASH_5752VENDOR_ST_M45PE20:
14380 case FLASH_5752VENDOR_ST_M45PE40:
14381 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014382 tg3_flag_set(tp, NVRAM_BUFFERED);
14383 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014384 tp->nvram_pagesize = 256;
14385 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
14386 tp->nvram_size = (protect ?
14387 TG3_NVRAM_SIZE_64KB :
14388 TG3_NVRAM_SIZE_128KB);
14389 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
14390 tp->nvram_size = (protect ?
14391 TG3_NVRAM_SIZE_64KB :
14392 TG3_NVRAM_SIZE_256KB);
14393 else
14394 tp->nvram_size = (protect ?
14395 TG3_NVRAM_SIZE_128KB :
14396 TG3_NVRAM_SIZE_512KB);
14397 break;
Michael Chand3c7b882006-03-23 01:28:25 -080014398 }
14399}
14400
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014401static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080014402{
14403 u32 nvcfg1;
14404
14405 nvcfg1 = tr32(NVRAM_CFG1);
14406
14407 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000014408 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
14409 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14410 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
14411 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14412 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014413 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000014414 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080014415
Matt Carlson8590a602009-08-28 12:29:16 +000014416 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14417 tw32(NVRAM_CFG1, nvcfg1);
14418 break;
14419 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14420 case FLASH_5755VENDOR_ATMEL_FLASH_1:
14421 case FLASH_5755VENDOR_ATMEL_FLASH_2:
14422 case FLASH_5755VENDOR_ATMEL_FLASH_3:
14423 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014424 tg3_flag_set(tp, NVRAM_BUFFERED);
14425 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014426 tp->nvram_pagesize = 264;
14427 break;
14428 case FLASH_5752VENDOR_ST_M45PE10:
14429 case FLASH_5752VENDOR_ST_M45PE20:
14430 case FLASH_5752VENDOR_ST_M45PE40:
14431 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014432 tg3_flag_set(tp, NVRAM_BUFFERED);
14433 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014434 tp->nvram_pagesize = 256;
14435 break;
Michael Chan1b277772006-03-20 22:27:48 -080014436 }
14437}
14438
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014439static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014440{
14441 u32 nvcfg1, protect = 0;
14442
14443 nvcfg1 = tr32(NVRAM_CFG1);
14444
14445 /* NVRAM protection for TPM */
14446 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014447 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014448 protect = 1;
14449 }
14450
14451 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14452 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014453 case FLASH_5761VENDOR_ATMEL_ADB021D:
14454 case FLASH_5761VENDOR_ATMEL_ADB041D:
14455 case FLASH_5761VENDOR_ATMEL_ADB081D:
14456 case FLASH_5761VENDOR_ATMEL_ADB161D:
14457 case FLASH_5761VENDOR_ATMEL_MDB021D:
14458 case FLASH_5761VENDOR_ATMEL_MDB041D:
14459 case FLASH_5761VENDOR_ATMEL_MDB081D:
14460 case FLASH_5761VENDOR_ATMEL_MDB161D:
14461 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014462 tg3_flag_set(tp, NVRAM_BUFFERED);
14463 tg3_flag_set(tp, FLASH);
14464 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014465 tp->nvram_pagesize = 256;
14466 break;
14467 case FLASH_5761VENDOR_ST_A_M45PE20:
14468 case FLASH_5761VENDOR_ST_A_M45PE40:
14469 case FLASH_5761VENDOR_ST_A_M45PE80:
14470 case FLASH_5761VENDOR_ST_A_M45PE16:
14471 case FLASH_5761VENDOR_ST_M_M45PE20:
14472 case FLASH_5761VENDOR_ST_M_M45PE40:
14473 case FLASH_5761VENDOR_ST_M_M45PE80:
14474 case FLASH_5761VENDOR_ST_M_M45PE16:
14475 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014476 tg3_flag_set(tp, NVRAM_BUFFERED);
14477 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014478 tp->nvram_pagesize = 256;
14479 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014480 }
14481
14482 if (protect) {
14483 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14484 } else {
14485 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014486 case FLASH_5761VENDOR_ATMEL_ADB161D:
14487 case FLASH_5761VENDOR_ATMEL_MDB161D:
14488 case FLASH_5761VENDOR_ST_A_M45PE16:
14489 case FLASH_5761VENDOR_ST_M_M45PE16:
14490 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14491 break;
14492 case FLASH_5761VENDOR_ATMEL_ADB081D:
14493 case FLASH_5761VENDOR_ATMEL_MDB081D:
14494 case FLASH_5761VENDOR_ST_A_M45PE80:
14495 case FLASH_5761VENDOR_ST_M_M45PE80:
14496 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14497 break;
14498 case FLASH_5761VENDOR_ATMEL_ADB041D:
14499 case FLASH_5761VENDOR_ATMEL_MDB041D:
14500 case FLASH_5761VENDOR_ST_A_M45PE40:
14501 case FLASH_5761VENDOR_ST_M_M45PE40:
14502 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14503 break;
14504 case FLASH_5761VENDOR_ATMEL_ADB021D:
14505 case FLASH_5761VENDOR_ATMEL_MDB021D:
14506 case FLASH_5761VENDOR_ST_A_M45PE20:
14507 case FLASH_5761VENDOR_ST_M_M45PE20:
14508 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14509 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014510 }
14511 }
14512}
14513
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014514static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014515{
14516 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014517 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014518 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14519}
14520
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014521static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014522{
14523 u32 nvcfg1;
14524
14525 nvcfg1 = tr32(NVRAM_CFG1);
14526
14527 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14528 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14529 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14530 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014531 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014532 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14533
14534 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14535 tw32(NVRAM_CFG1, nvcfg1);
14536 return;
14537 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14538 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14539 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14540 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14541 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14542 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14543 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14544 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014545 tg3_flag_set(tp, NVRAM_BUFFERED);
14546 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014547
14548 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14549 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14550 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14551 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14552 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14553 break;
14554 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14555 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14556 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14557 break;
14558 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14559 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14560 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14561 break;
14562 }
14563 break;
14564 case FLASH_5752VENDOR_ST_M45PE10:
14565 case FLASH_5752VENDOR_ST_M45PE20:
14566 case FLASH_5752VENDOR_ST_M45PE40:
14567 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014568 tg3_flag_set(tp, NVRAM_BUFFERED);
14569 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014570
14571 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14572 case FLASH_5752VENDOR_ST_M45PE10:
14573 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14574 break;
14575 case FLASH_5752VENDOR_ST_M45PE20:
14576 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14577 break;
14578 case FLASH_5752VENDOR_ST_M45PE40:
14579 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14580 break;
14581 }
14582 break;
14583 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014584 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014585 return;
14586 }
14587
Matt Carlsona1b950d2009-09-01 13:20:17 +000014588 tg3_nvram_get_pagesize(tp, nvcfg1);
14589 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014590 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014591}
14592
14593
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014594static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014595{
14596 u32 nvcfg1;
14597
14598 nvcfg1 = tr32(NVRAM_CFG1);
14599
14600 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14601 case FLASH_5717VENDOR_ATMEL_EEPROM:
14602 case FLASH_5717VENDOR_MICRO_EEPROM:
14603 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014604 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014605 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14606
14607 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14608 tw32(NVRAM_CFG1, nvcfg1);
14609 return;
14610 case FLASH_5717VENDOR_ATMEL_MDB011D:
14611 case FLASH_5717VENDOR_ATMEL_ADB011B:
14612 case FLASH_5717VENDOR_ATMEL_ADB011D:
14613 case FLASH_5717VENDOR_ATMEL_MDB021D:
14614 case FLASH_5717VENDOR_ATMEL_ADB021B:
14615 case FLASH_5717VENDOR_ATMEL_ADB021D:
14616 case FLASH_5717VENDOR_ATMEL_45USPT:
14617 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014618 tg3_flag_set(tp, NVRAM_BUFFERED);
14619 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014620
14621 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14622 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014623 /* Detect size with tg3_nvram_get_size() */
14624 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014625 case FLASH_5717VENDOR_ATMEL_ADB021B:
14626 case FLASH_5717VENDOR_ATMEL_ADB021D:
14627 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14628 break;
14629 default:
14630 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14631 break;
14632 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014633 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014634 case FLASH_5717VENDOR_ST_M_M25PE10:
14635 case FLASH_5717VENDOR_ST_A_M25PE10:
14636 case FLASH_5717VENDOR_ST_M_M45PE10:
14637 case FLASH_5717VENDOR_ST_A_M45PE10:
14638 case FLASH_5717VENDOR_ST_M_M25PE20:
14639 case FLASH_5717VENDOR_ST_A_M25PE20:
14640 case FLASH_5717VENDOR_ST_M_M45PE20:
14641 case FLASH_5717VENDOR_ST_A_M45PE20:
14642 case FLASH_5717VENDOR_ST_25USPT:
14643 case FLASH_5717VENDOR_ST_45USPT:
14644 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014645 tg3_flag_set(tp, NVRAM_BUFFERED);
14646 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014647
14648 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14649 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014650 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014651 /* Detect size with tg3_nvram_get_size() */
14652 break;
14653 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014654 case FLASH_5717VENDOR_ST_A_M45PE20:
14655 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14656 break;
14657 default:
14658 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14659 break;
14660 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014661 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014662 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014663 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014664 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014665 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014666
14667 tg3_nvram_get_pagesize(tp, nvcfg1);
14668 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014669 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014670}
14671
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014672static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014673{
14674 u32 nvcfg1, nvmpinstrp;
14675
14676 nvcfg1 = tr32(NVRAM_CFG1);
14677 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14678
Joe Perches41535772013-02-16 11:20:04 +000014679 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014680 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14681 tg3_flag_set(tp, NO_NVRAM);
14682 return;
14683 }
14684
14685 switch (nvmpinstrp) {
14686 case FLASH_5762_EEPROM_HD:
14687 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014688 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014689 case FLASH_5762_EEPROM_LD:
14690 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014691 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014692 case FLASH_5720VENDOR_M_ST_M45PE20:
14693 /* This pinstrap supports multiple sizes, so force it
14694 * to read the actual size from location 0xf0.
14695 */
14696 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14697 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014698 }
14699 }
14700
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014701 switch (nvmpinstrp) {
14702 case FLASH_5720_EEPROM_HD:
14703 case FLASH_5720_EEPROM_LD:
14704 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014705 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014706
14707 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14708 tw32(NVRAM_CFG1, nvcfg1);
14709 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14710 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14711 else
14712 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14713 return;
14714 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14715 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14716 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14717 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14718 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14719 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14720 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14721 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14722 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14723 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14724 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14725 case FLASH_5720VENDOR_ATMEL_45USPT:
14726 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014727 tg3_flag_set(tp, NVRAM_BUFFERED);
14728 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014729
14730 switch (nvmpinstrp) {
14731 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14732 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14733 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14734 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14735 break;
14736 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14737 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14738 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14739 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14740 break;
14741 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14742 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14743 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14744 break;
14745 default:
Joe Perches41535772013-02-16 11:20:04 +000014746 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014747 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014748 break;
14749 }
14750 break;
14751 case FLASH_5720VENDOR_M_ST_M25PE10:
14752 case FLASH_5720VENDOR_M_ST_M45PE10:
14753 case FLASH_5720VENDOR_A_ST_M25PE10:
14754 case FLASH_5720VENDOR_A_ST_M45PE10:
14755 case FLASH_5720VENDOR_M_ST_M25PE20:
14756 case FLASH_5720VENDOR_M_ST_M45PE20:
14757 case FLASH_5720VENDOR_A_ST_M25PE20:
14758 case FLASH_5720VENDOR_A_ST_M45PE20:
14759 case FLASH_5720VENDOR_M_ST_M25PE40:
14760 case FLASH_5720VENDOR_M_ST_M45PE40:
14761 case FLASH_5720VENDOR_A_ST_M25PE40:
14762 case FLASH_5720VENDOR_A_ST_M45PE40:
14763 case FLASH_5720VENDOR_M_ST_M25PE80:
14764 case FLASH_5720VENDOR_M_ST_M45PE80:
14765 case FLASH_5720VENDOR_A_ST_M25PE80:
14766 case FLASH_5720VENDOR_A_ST_M45PE80:
14767 case FLASH_5720VENDOR_ST_25USPT:
14768 case FLASH_5720VENDOR_ST_45USPT:
14769 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014770 tg3_flag_set(tp, NVRAM_BUFFERED);
14771 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014772
14773 switch (nvmpinstrp) {
14774 case FLASH_5720VENDOR_M_ST_M25PE20:
14775 case FLASH_5720VENDOR_M_ST_M45PE20:
14776 case FLASH_5720VENDOR_A_ST_M25PE20:
14777 case FLASH_5720VENDOR_A_ST_M45PE20:
14778 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14779 break;
14780 case FLASH_5720VENDOR_M_ST_M25PE40:
14781 case FLASH_5720VENDOR_M_ST_M45PE40:
14782 case FLASH_5720VENDOR_A_ST_M25PE40:
14783 case FLASH_5720VENDOR_A_ST_M45PE40:
14784 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14785 break;
14786 case FLASH_5720VENDOR_M_ST_M25PE80:
14787 case FLASH_5720VENDOR_M_ST_M45PE80:
14788 case FLASH_5720VENDOR_A_ST_M25PE80:
14789 case FLASH_5720VENDOR_A_ST_M45PE80:
14790 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14791 break;
14792 default:
Joe Perches41535772013-02-16 11:20:04 +000014793 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014794 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014795 break;
14796 }
14797 break;
14798 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014799 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014800 return;
14801 }
14802
14803 tg3_nvram_get_pagesize(tp, nvcfg1);
14804 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014805 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014806
Joe Perches41535772013-02-16 11:20:04 +000014807 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014808 u32 val;
14809
14810 if (tg3_nvram_read(tp, 0, &val))
14811 return;
14812
14813 if (val != TG3_EEPROM_MAGIC &&
14814 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14815 tg3_flag_set(tp, NO_NVRAM);
14816 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014817}
14818
Linus Torvalds1da177e2005-04-16 15:20:36 -070014819/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014820static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014821{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014822 if (tg3_flag(tp, IS_SSB_CORE)) {
14823 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14824 tg3_flag_clear(tp, NVRAM);
14825 tg3_flag_clear(tp, NVRAM_BUFFERED);
14826 tg3_flag_set(tp, NO_NVRAM);
14827 return;
14828 }
14829
Linus Torvalds1da177e2005-04-16 15:20:36 -070014830 tw32_f(GRC_EEPROM_ADDR,
14831 (EEPROM_ADDR_FSM_RESET |
14832 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14833 EEPROM_ADDR_CLKPERD_SHIFT)));
14834
Michael Chan9d57f012006-12-07 00:23:25 -080014835 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836
14837 /* Enable seeprom accesses. */
14838 tw32_f(GRC_LOCAL_CTRL,
14839 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14840 udelay(100);
14841
Joe Perches41535772013-02-16 11:20:04 +000014842 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14843 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014844 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014845
Michael Chanec41c7d2006-01-17 02:40:55 -080014846 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014847 netdev_warn(tp->dev,
14848 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014849 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014850 return;
14851 }
Michael Chane6af3012005-04-21 17:12:05 -070014852 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014853
Matt Carlson989a9d22007-05-05 11:51:05 -070014854 tp->nvram_size = 0;
14855
Joe Perches41535772013-02-16 11:20:04 +000014856 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014857 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014858 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014859 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014860 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14861 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14862 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014863 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014864 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014865 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014866 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014867 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014868 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014869 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014870 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014871 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14872 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014873 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014874 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14875 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014876 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014877 else
14878 tg3_get_nvram_info(tp);
14879
Matt Carlson989a9d22007-05-05 11:51:05 -070014880 if (tp->nvram_size == 0)
14881 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882
Michael Chane6af3012005-04-21 17:12:05 -070014883 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014884 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885
14886 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014887 tg3_flag_clear(tp, NVRAM);
14888 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014889
14890 tg3_get_eeprom_size(tp);
14891 }
14892}
14893
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894struct subsys_tbl_ent {
14895 u16 subsys_vendor, subsys_devid;
14896 u32 phy_id;
14897};
14898
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014899static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014900 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014902 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014903 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014904 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014905 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014906 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014907 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14908 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14909 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014910 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014911 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014912 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014913 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14914 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14915 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014916 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014917 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014918 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014919 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014920 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014921 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014922 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014923
14924 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014925 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014926 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014927 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014928 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014929 { TG3PCI_SUBVENDOR_ID_3COM,
14930 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14931 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014932 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014933 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014934 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935
14936 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014937 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014938 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014939 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014940 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014941 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014942 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014943 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014944 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014945
14946 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014947 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014948 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014949 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014950 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014951 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14952 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14953 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014954 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014955 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014956 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957
14958 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014959 { TG3PCI_SUBVENDOR_ID_IBM,
14960 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014961};
14962
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014963static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014964{
14965 int i;
14966
14967 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14968 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14969 tp->pdev->subsystem_vendor) &&
14970 (subsys_id_to_phy_id[i].subsys_devid ==
14971 tp->pdev->subsystem_device))
14972 return &subsys_id_to_phy_id[i];
14973 }
14974 return NULL;
14975}
14976
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014977static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014978{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014980
Matt Carlson79eb6902010-02-17 15:17:03 +000014981 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014982 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14983
Gary Zambranoa85feb82007-05-05 11:52:19 -070014984 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014985 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14986 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014987
Joe Perches41535772013-02-16 11:20:04 +000014988 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014989 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014990 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14991 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014992 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014993 val = tr32(VCPU_CFGSHDW);
14994 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014995 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014996 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014997 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014998 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014999 device_set_wakeup_enable(&tp->pdev->dev, true);
15000 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015001 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070015002 }
15003
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
15005 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
15006 u32 nic_cfg, led_cfg;
Nithin Sujir7c786062013-12-06 09:53:17 -080015007 u32 cfg2 = 0, cfg4 = 0, cfg5 = 0;
15008 u32 nic_phy_id, ver, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070015009 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010
15011 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
15012 tp->nic_sram_data_cfg = nic_cfg;
15013
15014 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
15015 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015016 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15017 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15018 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 (ver > 0) && (ver < 0x100))
15020 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
15021
Joe Perches41535772013-02-16 11:20:04 +000015022 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070015023 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
15024
Nithin Sujir7c786062013-12-06 09:53:17 -080015025 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15026 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15027 tg3_asic_rev(tp) == ASIC_REV_5720)
15028 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_5, &cfg5);
15029
Linus Torvalds1da177e2005-04-16 15:20:36 -070015030 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
15031 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
15032 eeprom_phy_serdes = 1;
15033
15034 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
15035 if (nic_phy_id != 0) {
15036 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
15037 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
15038
15039 eeprom_phy_id = (id1 >> 16) << 10;
15040 eeprom_phy_id |= (id2 & 0xfc00) << 16;
15041 eeprom_phy_id |= (id2 & 0x03ff) << 0;
15042 } else
15043 eeprom_phy_id = 0;
15044
Michael Chan7d0c41e2005-04-21 17:06:20 -070015045 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070015046 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000015047 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015048 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000015049 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015050 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070015051 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070015052
Joe Perches63c3a662011-04-26 08:12:10 +000015053 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
15055 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070015056 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015057 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
15058
15059 switch (led_cfg) {
15060 default:
15061 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
15062 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15063 break;
15064
15065 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
15066 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15067 break;
15068
15069 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
15070 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070015071
15072 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
15073 * read on some older 5700/5701 bootcode.
15074 */
Joe Perches41535772013-02-16 11:20:04 +000015075 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15076 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070015077 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
15078
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079 break;
15080
15081 case SHASTA_EXT_LED_SHARED:
15082 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000015083 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
15084 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15086 LED_CTRL_MODE_PHY_2);
Nithin Sujir89f67972013-09-20 16:46:57 -070015087
15088 if (tg3_flag(tp, 5717_PLUS) ||
15089 tg3_asic_rev(tp) == ASIC_REV_5762)
15090 tp->led_ctrl |= LED_CTRL_BLINK_RATE_OVERRIDE |
15091 LED_CTRL_BLINK_RATE_MASK;
15092
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093 break;
15094
15095 case SHASTA_EXT_LED_MAC:
15096 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
15097 break;
15098
15099 case SHASTA_EXT_LED_COMBO:
15100 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000015101 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
15103 LED_CTRL_MODE_PHY_2);
15104 break;
15105
Stephen Hemminger855e1112008-04-16 16:37:28 -070015106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107
Joe Perches41535772013-02-16 11:20:04 +000015108 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
15109 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015110 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
15111 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
15112
Joe Perches41535772013-02-16 11:20:04 +000015113 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015114 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080015115
Michael Chan9d26e212006-12-07 00:21:14 -080015116 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000015117 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015118 if ((tp->pdev->subsystem_vendor ==
15119 PCI_VENDOR_ID_ARIMA) &&
15120 (tp->pdev->subsystem_device == 0x205a ||
15121 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000015122 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080015123 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015124 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
15125 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080015126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127
15128 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000015129 tg3_flag_set(tp, ENABLE_ASF);
15130 if (tg3_flag(tp, 5750_PLUS))
15131 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015133
15134 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015135 tg3_flag(tp, 5750_PLUS))
15136 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080015137
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015138 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070015139 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000015140 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015141
Joe Perches63c3a662011-04-26 08:12:10 +000015142 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015143 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015144 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000015145 device_set_wakeup_enable(&tp->pdev->dev, true);
15146 }
Matt Carlson0527ba32007-10-10 18:03:30 -070015147
Linus Torvalds1da177e2005-04-16 15:20:36 -070015148 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015149 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150
15151 /* serdes signal pre-emphasis in register 0x590 set by */
15152 /* bootcode if bit 18 is set */
15153 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015154 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015155
Joe Perches63c3a662011-04-26 08:12:10 +000015156 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015157 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15158 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080015159 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015160 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080015161
Nithin Sujir942d1af2013-04-09 08:48:07 +000015162 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070015163 u32 cfg3;
15164
15165 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015166 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
15167 !tg3_flag(tp, 57765_PLUS) &&
15168 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000015169 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000015170 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
15171 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
15172 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
15173 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070015174 }
Matt Carlsona9daf362008-05-25 23:49:44 -070015175
Matt Carlson14417062010-02-17 15:16:59 +000015176 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000015177 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070015178 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015179 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070015180 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000015181 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Nithin Sujir7c786062013-12-06 09:53:17 -080015182
15183 if (cfg5 & NIC_SRAM_DISABLE_1G_HALF_ADV)
15184 tp->phy_flags |= TG3_PHYFLG_DISABLE_1G_HD_ADV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015185 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080015186done:
Joe Perches63c3a662011-04-26 08:12:10 +000015187 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015188 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000015189 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000015190 else
15191 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070015192}
15193
Michael Chanc86a8562013-01-06 12:51:08 +000015194static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
15195{
15196 int i, err;
15197 u32 val2, off = offset * 8;
15198
15199 err = tg3_nvram_lock(tp);
15200 if (err)
15201 return err;
15202
15203 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
15204 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
15205 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
15206 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
15207 udelay(10);
15208
15209 for (i = 0; i < 100; i++) {
15210 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
15211 if (val2 & APE_OTP_STATUS_CMD_DONE) {
15212 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
15213 break;
15214 }
15215 udelay(10);
15216 }
15217
15218 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
15219
15220 tg3_nvram_unlock(tp);
15221 if (val2 & APE_OTP_STATUS_CMD_DONE)
15222 return 0;
15223
15224 return -EBUSY;
15225}
15226
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015227static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015228{
15229 int i;
15230 u32 val;
15231
15232 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
15233 tw32(OTP_CTRL, cmd);
15234
15235 /* Wait for up to 1 ms for command to execute. */
15236 for (i = 0; i < 100; i++) {
15237 val = tr32(OTP_STATUS);
15238 if (val & OTP_STATUS_CMD_DONE)
15239 break;
15240 udelay(10);
15241 }
15242
15243 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
15244}
15245
15246/* Read the gphy configuration from the OTP region of the chip. The gphy
15247 * configuration is a 32-bit value that straddles the alignment boundary.
15248 * We do two 32-bit reads and then shift and merge the results.
15249 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015250static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015251{
15252 u32 bhalf_otp, thalf_otp;
15253
15254 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
15255
15256 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
15257 return 0;
15258
15259 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
15260
15261 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15262 return 0;
15263
15264 thalf_otp = tr32(OTP_READ_DATA);
15265
15266 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
15267
15268 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
15269 return 0;
15270
15271 bhalf_otp = tr32(OTP_READ_DATA);
15272
15273 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
15274}
15275
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015276static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000015277{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000015278 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015279
Nithin Sujir7c786062013-12-06 09:53:17 -080015280 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
15281 if (!(tp->phy_flags & TG3_PHYFLG_DISABLE_1G_HD_ADV))
15282 adv |= ADVERTISED_1000baseT_Half;
15283 adv |= ADVERTISED_1000baseT_Full;
15284 }
Matt Carlsone256f8a2011-03-09 16:58:24 +000015285
15286 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15287 adv |= ADVERTISED_100baseT_Half |
15288 ADVERTISED_100baseT_Full |
15289 ADVERTISED_10baseT_Half |
15290 ADVERTISED_10baseT_Full |
15291 ADVERTISED_TP;
15292 else
15293 adv |= ADVERTISED_FIBRE;
15294
15295 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000015296 tp->link_config.speed = SPEED_UNKNOWN;
15297 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015298 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000015299 tp->link_config.active_speed = SPEED_UNKNOWN;
15300 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000015301
15302 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000015303}
15304
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015305static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070015306{
15307 u32 hw_phy_id_1, hw_phy_id_2;
15308 u32 hw_phy_id, hw_phy_id_masked;
15309 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015310
Matt Carlsone256f8a2011-03-09 16:58:24 +000015311 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000015312 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000015313 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
15314
Michael Chan8151ad52012-07-29 19:15:41 +000015315 if (tg3_flag(tp, ENABLE_APE)) {
15316 switch (tp->pci_fn) {
15317 case 0:
15318 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
15319 break;
15320 case 1:
15321 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
15322 break;
15323 case 2:
15324 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
15325 break;
15326 case 3:
15327 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
15328 break;
15329 }
15330 }
15331
Nithin Sujir942d1af2013-04-09 08:48:07 +000015332 if (!tg3_flag(tp, ENABLE_ASF) &&
15333 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
15334 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
15335 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
15336 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
15337
Joe Perches63c3a662011-04-26 08:12:10 +000015338 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015339 return tg3_phy_init(tp);
15340
Linus Torvalds1da177e2005-04-16 15:20:36 -070015341 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010015342 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 */
15344 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000015345 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000015346 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015347 } else {
15348 /* Now read the physical PHY_ID from the chip and verify
15349 * that it is sane. If it doesn't look good, we fall back
15350 * to either the hard-coded table based PHY_ID and failing
15351 * that the value found in the eeprom area.
15352 */
15353 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
15354 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
15355
15356 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
15357 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
15358 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
15359
Matt Carlson79eb6902010-02-17 15:17:03 +000015360 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361 }
15362
Matt Carlson79eb6902010-02-17 15:17:03 +000015363 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000015365 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015366 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070015367 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015368 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000015370 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070015371 /* Do nothing, phy ID already set up in
15372 * tg3_get_eeprom_hw_cfg().
15373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015374 } else {
15375 struct subsys_tbl_ent *p;
15376
15377 /* No eeprom signature? Try the hardcoded
15378 * subsys device table.
15379 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000015380 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015381 if (p) {
15382 tp->phy_id = p->phy_id;
15383 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
15384 /* For now we saw the IDs 0xbc050cd0,
15385 * 0xbc050f80 and 0xbc050c30 on devices
15386 * connected to an BCM4785 and there are
15387 * probably more. Just assume that the phy is
15388 * supported when it is connected to a SSB core
15389 * for now.
15390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015391 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015393
Linus Torvalds1da177e2005-04-16 15:20:36 -070015394 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000015395 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015396 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397 }
15398 }
15399
Matt Carlsona6b68da2010-12-06 08:28:52 +000015400 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000015401 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15402 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000015403 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000015404 tg3_asic_rev(tp) == ASIC_REV_5762 ||
15405 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
15406 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
15407 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015408 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0))) {
Matt Carlson52b02d02010-10-14 10:37:41 +000015409 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
15410
Nithin Sujir9e2ecbe2013-05-18 06:26:52 +000015411 tp->eee.supported = SUPPORTED_100baseT_Full |
15412 SUPPORTED_1000baseT_Full;
15413 tp->eee.advertised = ADVERTISED_100baseT_Full |
15414 ADVERTISED_1000baseT_Full;
15415 tp->eee.eee_enabled = 1;
15416 tp->eee.tx_lpi_enabled = 1;
15417 tp->eee.tx_lpi_timer = TG3_CPMU_DBTMR1_LNKIDLE_2047US;
15418 }
15419
Matt Carlsone256f8a2011-03-09 16:58:24 +000015420 tg3_phy_init_link_config(tp);
15421
Nithin Sujir942d1af2013-04-09 08:48:07 +000015422 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
15423 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015424 !tg3_flag(tp, ENABLE_APE) &&
15425 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015426 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015427
15428 tg3_readphy(tp, MII_BMSR, &bmsr);
15429 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
15430 (bmsr & BMSR_LSTATUS))
15431 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015432
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433 err = tg3_phy_reset(tp);
15434 if (err)
15435 return err;
15436
Matt Carlson42b64a42011-05-19 12:12:49 +000015437 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438
Matt Carlsone2bf73e2011-12-08 14:40:15 +000015439 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000015440 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
15441 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442
15443 tg3_writephy(tp, MII_BMCR,
15444 BMCR_ANENABLE | BMCR_ANRESTART);
15445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446 }
15447
15448skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000015449 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015450 err = tg3_init_5401phy_dsp(tp);
15451 if (err)
15452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 err = tg3_init_5401phy_dsp(tp);
15455 }
15456
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457 return err;
15458}
15459
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015460static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015461{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015462 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015463 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000015464 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000015465 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015466
Matt Carlson535a4902011-07-20 10:20:56 +000015467 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015468 if (!vpd_data)
15469 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470
Matt Carlson535a4902011-07-20 10:20:56 +000015471 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015472 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015473 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015474
15475 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15476 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15477 i += PCI_VPD_LRDT_TAG_SIZE;
15478
Matt Carlson535a4902011-07-20 10:20:56 +000015479 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015480 goto out_not_found;
15481
Matt Carlson184b8902010-04-05 10:19:25 +000015482 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15483 PCI_VPD_RO_KEYWORD_MFR_ID);
15484 if (j > 0) {
15485 len = pci_vpd_info_field_size(&vpd_data[j]);
15486
15487 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15488 if (j + len > block_end || len != 4 ||
15489 memcmp(&vpd_data[j], "1028", 4))
15490 goto partno;
15491
15492 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15493 PCI_VPD_RO_KEYWORD_VENDOR0);
15494 if (j < 0)
15495 goto partno;
15496
15497 len = pci_vpd_info_field_size(&vpd_data[j]);
15498
15499 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15500 if (j + len > block_end)
15501 goto partno;
15502
Kees Cook715230a2013-03-27 06:40:50 +000015503 if (len >= sizeof(tp->fw_ver))
15504 len = sizeof(tp->fw_ver) - 1;
15505 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15506 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15507 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015508 }
15509
15510partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015511 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15512 PCI_VPD_RO_KEYWORD_PARTNO);
15513 if (i < 0)
15514 goto out_not_found;
15515
15516 len = pci_vpd_info_field_size(&vpd_data[i]);
15517
15518 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15519 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015520 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015521 goto out_not_found;
15522
15523 memcpy(tp->board_part_number, &vpd_data[i], len);
15524
Linus Torvalds1da177e2005-04-16 15:20:36 -070015525out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015526 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015527 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015528 return;
15529
15530out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015531 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015532 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15533 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015534 strcpy(tp->board_part_number, "BCM5717");
15535 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15536 strcpy(tp->board_part_number, "BCM5718");
15537 else
15538 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015539 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015540 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15541 strcpy(tp->board_part_number, "BCM57780");
15542 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15543 strcpy(tp->board_part_number, "BCM57760");
15544 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15545 strcpy(tp->board_part_number, "BCM57790");
15546 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15547 strcpy(tp->board_part_number, "BCM57788");
15548 else
15549 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015550 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015551 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15552 strcpy(tp->board_part_number, "BCM57761");
15553 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15554 strcpy(tp->board_part_number, "BCM57765");
15555 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15556 strcpy(tp->board_part_number, "BCM57781");
15557 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15558 strcpy(tp->board_part_number, "BCM57785");
15559 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15560 strcpy(tp->board_part_number, "BCM57791");
15561 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15562 strcpy(tp->board_part_number, "BCM57795");
15563 else
15564 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015565 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015566 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15567 strcpy(tp->board_part_number, "BCM57762");
15568 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15569 strcpy(tp->board_part_number, "BCM57766");
15570 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15571 strcpy(tp->board_part_number, "BCM57782");
15572 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15573 strcpy(tp->board_part_number, "BCM57786");
15574 else
15575 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015576 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015577 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015578 } else {
15579nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015580 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015582}
15583
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015584static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015585{
15586 u32 val;
15587
Matt Carlsone4f34112009-02-25 14:25:00 +000015588 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015589 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015590 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015591 val != 0)
15592 return 0;
15593
15594 return 1;
15595}
15596
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015597static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015598{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015599 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015600 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015601 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015602
15603 if (tg3_nvram_read(tp, 0xc, &offset) ||
15604 tg3_nvram_read(tp, 0x4, &start))
15605 return;
15606
15607 offset = tg3_nvram_logical_addr(tp, offset);
15608
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015609 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015610 return;
15611
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015612 if ((val & 0xfc000000) == 0x0c000000) {
15613 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015614 return;
15615
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015616 if (val == 0)
15617 newver = true;
15618 }
15619
Matt Carlson75f99362010-04-05 10:19:24 +000015620 dst_off = strlen(tp->fw_ver);
15621
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015622 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015623 if (TG3_VER_SIZE - dst_off < 16 ||
15624 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015625 return;
15626
15627 offset = offset + ver_offset - start;
15628 for (i = 0; i < 16; i += 4) {
15629 __be32 v;
15630 if (tg3_nvram_read_be32(tp, offset + i, &v))
15631 return;
15632
Matt Carlson75f99362010-04-05 10:19:24 +000015633 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015634 }
15635 } else {
15636 u32 major, minor;
15637
15638 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15639 return;
15640
15641 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15642 TG3_NVM_BCVER_MAJSFT;
15643 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015644 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15645 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015646 }
15647}
15648
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015649static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015650{
15651 u32 val, major, minor;
15652
15653 /* Use native endian representation */
15654 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15655 return;
15656
15657 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15658 TG3_NVM_HWSB_CFG1_MAJSFT;
15659 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15660 TG3_NVM_HWSB_CFG1_MINSFT;
15661
15662 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15663}
15664
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015665static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015666{
15667 u32 offset, major, minor, build;
15668
Matt Carlson75f99362010-04-05 10:19:24 +000015669 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015670
15671 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15672 return;
15673
15674 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15675 case TG3_EEPROM_SB_REVISION_0:
15676 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15677 break;
15678 case TG3_EEPROM_SB_REVISION_2:
15679 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15680 break;
15681 case TG3_EEPROM_SB_REVISION_3:
15682 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15683 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015684 case TG3_EEPROM_SB_REVISION_4:
15685 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15686 break;
15687 case TG3_EEPROM_SB_REVISION_5:
15688 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15689 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015690 case TG3_EEPROM_SB_REVISION_6:
15691 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15692 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015693 default:
15694 return;
15695 }
15696
Matt Carlsone4f34112009-02-25 14:25:00 +000015697 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015698 return;
15699
15700 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15701 TG3_EEPROM_SB_EDH_BLD_SHFT;
15702 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15703 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15704 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15705
15706 if (minor > 99 || build > 26)
15707 return;
15708
Matt Carlson75f99362010-04-05 10:19:24 +000015709 offset = strlen(tp->fw_ver);
15710 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15711 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015712
15713 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015714 offset = strlen(tp->fw_ver);
15715 if (offset < TG3_VER_SIZE - 1)
15716 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015717 }
15718}
15719
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015720static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015721{
15722 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015723 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015724
15725 for (offset = TG3_NVM_DIR_START;
15726 offset < TG3_NVM_DIR_END;
15727 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015728 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015729 return;
15730
15731 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15732 break;
15733 }
15734
15735 if (offset == TG3_NVM_DIR_END)
15736 return;
15737
Joe Perches63c3a662011-04-26 08:12:10 +000015738 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015739 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015740 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015741 return;
15742
Matt Carlsone4f34112009-02-25 14:25:00 +000015743 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015744 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015745 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015746 return;
15747
15748 offset += val - start;
15749
Matt Carlsonacd9c112009-02-25 14:26:33 +000015750 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015751
Matt Carlsonacd9c112009-02-25 14:26:33 +000015752 tp->fw_ver[vlen++] = ',';
15753 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015754
15755 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015756 __be32 v;
15757 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015758 return;
15759
Al Virob9fc7dc2007-12-17 22:59:57 -080015760 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015761
Matt Carlsonacd9c112009-02-25 14:26:33 +000015762 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15763 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015764 break;
15765 }
15766
Matt Carlsonacd9c112009-02-25 14:26:33 +000015767 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15768 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015769 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015770}
15771
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015772static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015773{
Matt Carlson7fd76442009-02-25 14:27:20 +000015774 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015775
15776 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15777 if (apedata != APE_SEG_SIG_MAGIC)
15778 return;
15779
15780 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15781 if (!(apedata & APE_FW_STATUS_READY))
15782 return;
15783
Michael Chan165f4d12012-07-16 16:23:59 +000015784 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15785 tg3_flag_set(tp, APE_HAS_NCSI);
15786}
15787
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015788static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015789{
15790 int vlen;
15791 u32 apedata;
15792 char *fwtype;
15793
Matt Carlson7fd76442009-02-25 14:27:20 +000015794 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15795
Michael Chan165f4d12012-07-16 16:23:59 +000015796 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015797 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015798 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15799 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015800 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015801 fwtype = "DASH";
15802
Matt Carlson7fd76442009-02-25 14:27:20 +000015803 vlen = strlen(tp->fw_ver);
15804
Matt Carlsonecc79642010-08-02 11:26:01 +000015805 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15806 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015807 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15808 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15809 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15810 (apedata & APE_FW_VERSION_BLDMSK));
15811}
15812
Michael Chanc86a8562013-01-06 12:51:08 +000015813static void tg3_read_otp_ver(struct tg3 *tp)
15814{
15815 u32 val, val2;
15816
Joe Perches41535772013-02-16 11:20:04 +000015817 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015818 return;
15819
15820 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15821 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15822 TG3_OTP_MAGIC0_VALID(val)) {
15823 u64 val64 = (u64) val << 32 | val2;
15824 u32 ver = 0;
15825 int i, vlen;
15826
15827 for (i = 0; i < 7; i++) {
15828 if ((val64 & 0xff) == 0)
15829 break;
15830 ver = val64 & 0xff;
15831 val64 >>= 8;
15832 }
15833 vlen = strlen(tp->fw_ver);
15834 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15835 }
15836}
15837
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015838static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015839{
15840 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015841 bool vpd_vers = false;
15842
15843 if (tp->fw_ver[0] != 0)
15844 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015845
Joe Perches63c3a662011-04-26 08:12:10 +000015846 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015847 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015848 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015849 return;
15850 }
15851
Matt Carlsonacd9c112009-02-25 14:26:33 +000015852 if (tg3_nvram_read(tp, 0, &val))
15853 return;
15854
15855 if (val == TG3_EEPROM_MAGIC)
15856 tg3_read_bc_ver(tp);
15857 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15858 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015859 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15860 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015861
Michael Chan165f4d12012-07-16 16:23:59 +000015862 if (tg3_flag(tp, ENABLE_ASF)) {
15863 if (tg3_flag(tp, ENABLE_APE)) {
15864 tg3_probe_ncsi(tp);
15865 if (!vpd_vers)
15866 tg3_read_dash_ver(tp);
15867 } else if (!vpd_vers) {
15868 tg3_read_mgmtfw_ver(tp);
15869 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015870 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015871
15872 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015873}
15874
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015875static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15876{
Joe Perches63c3a662011-04-26 08:12:10 +000015877 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015878 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015879 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015880 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015881 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015882 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015883}
15884
Matt Carlson41434702011-03-09 16:58:22 +000015885static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015886 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15887 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15888 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15889 { },
15890};
15891
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015892static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015893{
15894 struct pci_dev *peer;
15895 unsigned int func, devnr = tp->pdev->devfn & ~7;
15896
15897 for (func = 0; func < 8; func++) {
15898 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15899 if (peer && peer != tp->pdev)
15900 break;
15901 pci_dev_put(peer);
15902 }
15903 /* 5704 can be configured in single-port mode, set peer to
15904 * tp->pdev in that case.
15905 */
15906 if (!peer) {
15907 peer = tp->pdev;
15908 return peer;
15909 }
15910
15911 /*
15912 * We don't need to keep the refcount elevated; there's no way
15913 * to remove one half of this device without removing the other
15914 */
15915 pci_dev_put(peer);
15916
15917 return peer;
15918}
15919
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015920static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015921{
15922 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015923 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015924 u32 reg;
15925
15926 /* All devices that use the alternate
15927 * ASIC REV location have a CPMU.
15928 */
15929 tg3_flag_set(tp, CPMU_PRESENT);
15930
15931 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015932 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015933 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15934 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015935 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015936 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
15937 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015938 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15939 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070015940 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
15941 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787)
Matt Carlson42b123b2012-02-13 15:20:13 +000015942 reg = TG3PCI_GEN2_PRODID_ASICREV;
15943 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15944 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15945 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15946 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15947 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15948 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15949 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15950 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15951 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15952 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15953 reg = TG3PCI_GEN15_PRODID_ASICREV;
15954 else
15955 reg = TG3PCI_PRODID_ASICREV;
15956
15957 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15958 }
15959
15960 /* Wrong chip ID in 5752 A0. This code can be removed later
15961 * as A0 is not in production.
15962 */
Joe Perches41535772013-02-16 11:20:04 +000015963 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015964 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15965
Joe Perches41535772013-02-16 11:20:04 +000015966 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015967 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15968
Joe Perches41535772013-02-16 11:20:04 +000015969 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15970 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15971 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015972 tg3_flag_set(tp, 5717_PLUS);
15973
Joe Perches41535772013-02-16 11:20:04 +000015974 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15975 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015976 tg3_flag_set(tp, 57765_CLASS);
15977
Michael Chanc65a17f2013-01-06 12:51:07 +000015978 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015979 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015980 tg3_flag_set(tp, 57765_PLUS);
15981
15982 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015983 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15984 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15985 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15986 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15987 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15988 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015989 tg3_flag(tp, 57765_PLUS))
15990 tg3_flag_set(tp, 5755_PLUS);
15991
Joe Perches41535772013-02-16 11:20:04 +000015992 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15993 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015994 tg3_flag_set(tp, 5780_CLASS);
15995
Joe Perches41535772013-02-16 11:20:04 +000015996 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15997 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15998 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015999 tg3_flag(tp, 5755_PLUS) ||
16000 tg3_flag(tp, 5780_CLASS))
16001 tg3_flag_set(tp, 5750_PLUS);
16002
Joe Perches41535772013-02-16 11:20:04 +000016003 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000016004 tg3_flag(tp, 5750_PLUS))
16005 tg3_flag_set(tp, 5705_PLUS);
16006}
16007
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016008static bool tg3_10_100_only_device(struct tg3 *tp,
16009 const struct pci_device_id *ent)
16010{
16011 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
16012
Joe Perches41535772013-02-16 11:20:04 +000016013 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
16014 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016015 (tp->phy_flags & TG3_PHYFLG_IS_FET))
16016 return true;
16017
16018 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000016019 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016020 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
16021 return true;
16022 } else {
16023 return true;
16024 }
16025 }
16026
16027 return false;
16028}
16029
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000016030static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016031{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016032 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016033 u32 pci_state_reg, grc_misc_cfg;
16034 u32 val;
16035 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016036 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016037
Linus Torvalds1da177e2005-04-16 15:20:36 -070016038 /* Force memory write invalidate off. If we leave it on,
16039 * then on 5700_BX chips we have to enable a workaround.
16040 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
16041 * to match the cacheline size. The Broadcom driver have this
16042 * workaround but turns MWI off all the times so never uses
16043 * it. This seems to suggest that the workaround is insufficient.
16044 */
16045 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16046 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
16047 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16048
Matt Carlson16821282011-07-13 09:27:28 +000016049 /* Important! -- Make sure register accesses are byteswapped
16050 * correctly. Also, for those chips that require it, make
16051 * sure that indirect register accesses are enabled before
16052 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016053 */
16054 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16055 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000016056 tp->misc_host_ctrl |= (misc_ctrl_reg &
16057 MISC_HOST_CTRL_CHIPREV);
16058 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16059 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016060
Matt Carlson42b123b2012-02-13 15:20:13 +000016061 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070016062
Michael Chan68929142005-08-09 20:17:14 -070016063 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
16064 * we need to disable memory and use config. cycles
16065 * only to access all registers. The 5702/03 chips
16066 * can mistakenly decode the special cycles from the
16067 * ICH chipsets as memory write cycles, causing corruption
16068 * of register and memory space. Only certain ICH bridges
16069 * will drive special cycles with non-zero data during the
16070 * address phase which can fall within the 5703's address
16071 * range. This is not an ICH bug as the PCI spec allows
16072 * non-zero address during special cycles. However, only
16073 * these ICH bridges are known to drive non-zero addresses
16074 * during special cycles.
16075 *
16076 * Since special cycles do not cross PCI bridges, we only
16077 * enable this workaround if the 5703 is on the secondary
16078 * bus of these ICH bridges.
16079 */
Joe Perches41535772013-02-16 11:20:04 +000016080 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
16081 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070016082 static struct tg3_dev_id {
16083 u32 vendor;
16084 u32 device;
16085 u32 rev;
16086 } ich_chipsets[] = {
16087 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
16088 PCI_ANY_ID },
16089 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
16090 PCI_ANY_ID },
16091 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
16092 0xa },
16093 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
16094 PCI_ANY_ID },
16095 { },
16096 };
16097 struct tg3_dev_id *pci_id = &ich_chipsets[0];
16098 struct pci_dev *bridge = NULL;
16099
16100 while (pci_id->vendor != 0) {
16101 bridge = pci_get_device(pci_id->vendor, pci_id->device,
16102 bridge);
16103 if (!bridge) {
16104 pci_id++;
16105 continue;
16106 }
16107 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070016108 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070016109 continue;
16110 }
16111 if (bridge->subordinate &&
16112 (bridge->subordinate->number ==
16113 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016114 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070016115 pci_dev_put(bridge);
16116 break;
16117 }
16118 }
16119 }
16120
Joe Perches41535772013-02-16 11:20:04 +000016121 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070016122 static struct tg3_dev_id {
16123 u32 vendor;
16124 u32 device;
16125 } bridge_chipsets[] = {
16126 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
16127 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
16128 { },
16129 };
16130 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
16131 struct pci_dev *bridge = NULL;
16132
16133 while (pci_id->vendor != 0) {
16134 bridge = pci_get_device(pci_id->vendor,
16135 pci_id->device,
16136 bridge);
16137 if (!bridge) {
16138 pci_id++;
16139 continue;
16140 }
16141 if (bridge->subordinate &&
16142 (bridge->subordinate->number <=
16143 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016144 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070016145 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016146 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070016147 pci_dev_put(bridge);
16148 break;
16149 }
16150 }
16151 }
16152
Michael Chan4a29cc22006-03-19 13:21:12 -080016153 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
16154 * DMA addresses > 40-bit. This bridge may have other additional
16155 * 57xx devices behind it in some 4-port NIC designs for example.
16156 * Any tg3 device found behind the bridge will also need the 40-bit
16157 * DMA workaround.
16158 */
Matt Carlson42b123b2012-02-13 15:20:13 +000016159 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016160 tg3_flag_set(tp, 40BIT_DMA_BUG);
Yijing Wang0f847582013-08-08 21:03:12 +080016161 tp->msi_cap = tp->pdev->msi_cap;
Matt Carlson859a588792010-04-05 10:19:28 +000016162 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080016163 struct pci_dev *bridge = NULL;
16164
16165 do {
16166 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
16167 PCI_DEVICE_ID_SERVERWORKS_EPB,
16168 bridge);
16169 if (bridge && bridge->subordinate &&
16170 (bridge->subordinate->number <=
16171 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070016172 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080016173 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016174 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080016175 pci_dev_put(bridge);
16176 break;
16177 }
16178 } while (bridge);
16179 }
Michael Chan4cf78e42005-07-25 12:29:19 -070016180
Joe Perches41535772013-02-16 11:20:04 +000016181 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
16182 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070016183 tp->pdev_peer = tg3_find_peer(tp);
16184
Matt Carlson507399f2009-11-13 13:03:37 +000016185 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000016186 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000016187 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000016188 else if (tg3_flag(tp, 57765_PLUS))
16189 tg3_flag_set(tp, HW_TSO_3);
16190 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016191 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016192 tg3_flag_set(tp, HW_TSO_2);
16193 else if (tg3_flag(tp, 5750_PLUS)) {
16194 tg3_flag_set(tp, HW_TSO_1);
16195 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016196 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
16197 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000016198 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016199 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16200 tg3_asic_rev(tp) != ASIC_REV_5701 &&
16201 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000016202 tg3_flag_set(tp, FW_TSO);
16203 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016204 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000016205 tp->fw_needed = FIRMWARE_TG3TSO5;
16206 else
16207 tp->fw_needed = FIRMWARE_TG3TSO;
16208 }
16209
Matt Carlsondabc5c62011-05-19 12:12:52 +000016210 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000016211 if (tg3_flag(tp, HW_TSO_1) ||
16212 tg3_flag(tp, HW_TSO_2) ||
16213 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000016214 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016215 /* For firmware TSO, assume ASF is disabled.
16216 * We'll disable TSO later if we discover ASF
16217 * is enabled in tg3_get_eeprom_hw_cfg().
16218 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000016219 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016220 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000016221 tg3_flag_clear(tp, TSO_CAPABLE);
16222 tg3_flag_clear(tp, TSO_BUG);
16223 tp->fw_needed = NULL;
16224 }
16225
Joe Perches41535772013-02-16 11:20:04 +000016226 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000016227 tp->fw_needed = FIRMWARE_TG3;
16228
Nithin Sujirc4dab502013-03-06 17:02:34 +000016229 if (tg3_asic_rev(tp) == ASIC_REV_57766)
16230 tp->fw_needed = FIRMWARE_TG357766;
16231
Matt Carlson507399f2009-11-13 13:03:37 +000016232 tp->irq_max = 1;
16233
Joe Perches63c3a662011-04-26 08:12:10 +000016234 if (tg3_flag(tp, 5750_PLUS)) {
16235 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000016236 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
16237 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
16238 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
16239 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070016240 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000016241 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070016242
Joe Perches63c3a662011-04-26 08:12:10 +000016243 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016244 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016245 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070016246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016247
Joe Perches63c3a662011-04-26 08:12:10 +000016248 if (tg3_flag(tp, 57765_PLUS)) {
16249 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000016250 tp->irq_max = TG3_IRQ_MAX_VECS;
16251 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016252 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000016253
Michael Chan91024262012-09-28 07:12:38 +000016254 tp->txq_max = 1;
16255 tp->rxq_max = 1;
16256 if (tp->irq_max > 1) {
16257 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
16258 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
16259
Joe Perches41535772013-02-16 11:20:04 +000016260 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16261 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000016262 tp->txq_max = tp->irq_max - 1;
16263 }
16264
Matt Carlsonb7abee62012-06-07 12:56:54 +000016265 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000016266 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000016267 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000016268
Joe Perches41535772013-02-16 11:20:04 +000016269 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000016270 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000016271
Joe Perches41535772013-02-16 11:20:04 +000016272 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16273 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16274 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16275 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000016276 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000016277
Joe Perches63c3a662011-04-26 08:12:10 +000016278 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000016279 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000016280 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000016281
Joe Perches63c3a662011-04-26 08:12:10 +000016282 if (!tg3_flag(tp, 5705_PLUS) ||
16283 tg3_flag(tp, 5780_CLASS) ||
16284 tg3_flag(tp, USE_JUMBO_BDFLAG))
16285 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070016286
Matt Carlson52f44902008-11-21 17:17:04 -080016287 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16288 &pci_state_reg);
16289
Jon Mason708ebb32011-06-27 12:56:50 +000016290 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016291 u16 lnkctl;
16292
Joe Perches63c3a662011-04-26 08:12:10 +000016293 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080016294
Jiang Liu0f49bfb2012-08-20 13:28:20 -060016295 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080016296 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000016297 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000016298 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000016299 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000016300 }
Joe Perches41535772013-02-16 11:20:04 +000016301 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
16302 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16303 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
16304 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000016305 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000016306 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000016307 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080016308 }
Joe Perches41535772013-02-16 11:20:04 +000016309 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000016310 /* BCM5785 devices are effectively PCIe devices, and should
16311 * follow PCIe codepaths, but do not have a PCIe capabilities
16312 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000016313 */
Joe Perches63c3a662011-04-26 08:12:10 +000016314 tg3_flag_set(tp, PCI_EXPRESS);
16315 } else if (!tg3_flag(tp, 5705_PLUS) ||
16316 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080016317 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
16318 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000016319 dev_err(&tp->pdev->dev,
16320 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080016321 return -EIO;
16322 }
16323
16324 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000016325 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080016326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016327
Michael Chan399de502005-10-03 14:02:39 -070016328 /* If we have an AMD 762 or VIA K8T800 chipset, write
16329 * reordering to the mailbox registers done by the host
16330 * controller can cause major troubles. We read back from
16331 * every mailbox register write to force the writes to be
16332 * posted to the chip in order.
16333 */
Matt Carlson41434702011-03-09 16:58:22 +000016334 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000016335 !tg3_flag(tp, PCI_EXPRESS))
16336 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070016337
Matt Carlson69fc4052008-12-21 20:19:57 -080016338 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
16339 &tp->pci_cacheline_sz);
16340 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16341 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000016342 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016343 tp->pci_lat_timer < 64) {
16344 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080016345 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
16346 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016347 }
16348
Matt Carlson16821282011-07-13 09:27:28 +000016349 /* Important! -- It is critical that the PCI-X hw workaround
16350 * situation is decided before the first MMIO register access.
16351 */
Joe Perches41535772013-02-16 11:20:04 +000016352 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080016353 /* 5700 BX chips need to have their TX producer index
16354 * mailboxes written twice to workaround a bug.
16355 */
Joe Perches63c3a662011-04-26 08:12:10 +000016356 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070016357
Matt Carlson52f44902008-11-21 17:17:04 -080016358 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016359 *
16360 * The workaround is to use indirect register accesses
16361 * for all chip writes not to mailbox registers.
16362 */
Joe Perches63c3a662011-04-26 08:12:10 +000016363 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016364 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016365
Joe Perches63c3a662011-04-26 08:12:10 +000016366 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016367
16368 /* The chip can have it's power management PCI config
16369 * space registers clobbered due to this bug.
16370 * So explicitly force the chip into D0 here.
16371 */
Matt Carlson9974a352007-10-07 23:27:28 -070016372 pci_read_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016373 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016374 &pm_reg);
16375 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
16376 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070016377 pci_write_config_dword(tp->pdev,
Jon Mason0319f302013-09-11 11:22:40 -070016378 tp->pdev->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016379 pm_reg);
16380
16381 /* Also, force SERR#/PERR# in PCI command. */
16382 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16383 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
16384 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16385 }
16386 }
16387
Linus Torvalds1da177e2005-04-16 15:20:36 -070016388 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016389 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016390 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000016391 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016392
16393 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000016394 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016395 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
16396 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
16397 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
16398 }
16399
Michael Chan1ee582d2005-08-09 20:16:46 -070016400 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070016401 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016402 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070016403 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070016404 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070016405 tp->write32_tx_mbox = tg3_write32;
16406 tp->write32_rx_mbox = tg3_write32;
16407
16408 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000016409 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070016410 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000016411 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016412 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000016413 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070016414 /*
16415 * Back to back register writes can cause problems on these
16416 * chips, the workaround is to read back all reg writes
16417 * except those to mailbox regs.
16418 *
16419 * See tg3_write_indirect_reg32().
16420 */
Michael Chan1ee582d2005-08-09 20:16:46 -070016421 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070016422 }
16423
Joe Perches63c3a662011-04-26 08:12:10 +000016424 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070016425 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000016426 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070016427 tp->write32_rx_mbox = tg3_write_flush_reg32;
16428 }
Michael Chan20094932005-08-09 20:16:32 -070016429
Joe Perches63c3a662011-04-26 08:12:10 +000016430 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070016431 tp->read32 = tg3_read_indirect_reg32;
16432 tp->write32 = tg3_write_indirect_reg32;
16433 tp->read32_mbox = tg3_read_indirect_mbox;
16434 tp->write32_mbox = tg3_write_indirect_mbox;
16435 tp->write32_tx_mbox = tg3_write_indirect_mbox;
16436 tp->write32_rx_mbox = tg3_write_indirect_mbox;
16437
16438 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016439 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016440
16441 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
16442 pci_cmd &= ~PCI_COMMAND_MEMORY;
16443 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
16444 }
Joe Perches41535772013-02-16 11:20:04 +000016445 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016446 tp->read32_mbox = tg3_read32_mbox_5906;
16447 tp->write32_mbox = tg3_write32_mbox_5906;
16448 tp->write32_tx_mbox = tg3_write32_mbox_5906;
16449 tp->write32_rx_mbox = tg3_write32_mbox_5906;
16450 }
Michael Chan68929142005-08-09 20:17:14 -070016451
Michael Chanbbadf502006-04-06 21:46:34 -070016452 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016453 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000016454 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16455 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000016456 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070016457
Matt Carlson16821282011-07-13 09:27:28 +000016458 /* The memory arbiter has to be enabled in order for SRAM accesses
16459 * to succeed. Normally on powerup the tg3 chip firmware will make
16460 * sure it is enabled, but other entities such as system netboot
16461 * code might disable it.
16462 */
16463 val = tr32(MEMARB_MODE);
16464 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
16465
Matt Carlson9dc5e342011-11-04 09:15:02 +000016466 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000016467 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000016468 tg3_flag(tp, 5780_CLASS)) {
16469 if (tg3_flag(tp, PCIX_MODE)) {
16470 pci_read_config_dword(tp->pdev,
16471 tp->pcix_cap + PCI_X_STATUS,
16472 &val);
16473 tp->pci_fn = val & 0x7;
16474 }
Joe Perches41535772013-02-16 11:20:04 +000016475 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16476 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16477 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016478 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016479 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16480 val = tr32(TG3_CPMU_STATUS);
16481
Joe Perches41535772013-02-16 11:20:04 +000016482 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016483 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16484 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016485 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16486 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016487 }
16488
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016489 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16490 tp->write32_tx_mbox = tg3_write_flush_reg32;
16491 tp->write32_rx_mbox = tg3_write_flush_reg32;
16492 }
16493
Michael Chan7d0c41e2005-04-21 17:06:20 -070016494 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016495 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016496 * determined before calling tg3_set_power_state() so that
16497 * we know whether or not to switch out of Vaux power.
16498 * When the flag is set, it means that GPIO1 is used for eeprom
16499 * write protect and also implies that it is a LOM where GPIOs
16500 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016501 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016502 tg3_get_eeprom_hw_cfg(tp);
16503
Matt Carlson1caf13e2013-03-06 17:02:29 +000016504 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016505 tg3_flag_clear(tp, TSO_CAPABLE);
16506 tg3_flag_clear(tp, TSO_BUG);
16507 tp->fw_needed = NULL;
16508 }
16509
Joe Perches63c3a662011-04-26 08:12:10 +000016510 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016511 /* Allow reads and writes to the
16512 * APE register and memory space.
16513 */
16514 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016515 PCISTATE_ALLOW_APE_SHMEM_WR |
16516 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016517 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16518 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016519
16520 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016521 }
16522
Matt Carlson16821282011-07-13 09:27:28 +000016523 /* Set up tp->grc_local_ctrl before calling
16524 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16525 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016526 * It is also used as eeprom write protect on LOMs.
16527 */
16528 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016529 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016530 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016531 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16532 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016533 /* Unused GPIO3 must be driven as output on 5752 because there
16534 * are no pull-up resistors on unused GPIO pins.
16535 */
Joe Perches41535772013-02-16 11:20:04 +000016536 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016537 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016538
Joe Perches41535772013-02-16 11:20:04 +000016539 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16540 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016541 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016542 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16543
Matt Carlson8d519ab2009-04-20 06:58:01 +000016544 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16545 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016546 /* Turn off the debug UART. */
16547 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016548 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016549 /* Keep VMain power. */
16550 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16551 GRC_LCLCTRL_GPIO_OUTPUT0;
16552 }
16553
Joe Perches41535772013-02-16 11:20:04 +000016554 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016555 tp->grc_local_ctrl |=
16556 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16557
Matt Carlson16821282011-07-13 09:27:28 +000016558 /* Switch out of Vaux if it is a NIC */
16559 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016560
Linus Torvalds1da177e2005-04-16 15:20:36 -070016561 /* Derive initial jumbo mode from MTU assigned in
16562 * ether_setup() via the alloc_etherdev() call
16563 */
Joe Perches63c3a662011-04-26 08:12:10 +000016564 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16565 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016566
16567 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016568 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16569 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16570 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16571 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016572 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016573 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016574 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016575 }
16576
Joe Perches41535772013-02-16 11:20:04 +000016577 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016578 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016579
Linus Torvalds1da177e2005-04-16 15:20:36 -070016580 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016581 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16582 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16583 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16584 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016585 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16586 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16587 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016588
Joe Perches41535772013-02-16 11:20:04 +000016589 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16590 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016591 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016592 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016593 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016594
Joe Perches63c3a662011-04-26 08:12:10 +000016595 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016596 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016597 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16598 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016599 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016600 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16601 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16602 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16603 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016604 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16605 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016606 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016607 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016608 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016609 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016610 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016612
Joe Perches41535772013-02-16 11:20:04 +000016613 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16614 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016615 tp->phy_otp = tg3_read_otp_phycfg(tp);
16616 if (tp->phy_otp == 0)
16617 tp->phy_otp = TG3_OTP_DEFAULT;
16618 }
16619
Joe Perches63c3a662011-04-26 08:12:10 +000016620 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016621 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16622 else
16623 tp->mi_mode = MAC_MI_MODE_BASE;
16624
Linus Torvalds1da177e2005-04-16 15:20:36 -070016625 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016626 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16627 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016628 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16629
Matt Carlson4d958472011-04-20 07:57:35 +000016630 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016631 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Nithin Sujir94962f72013-12-06 09:53:19 -080016632 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Joe Perches41535772013-02-16 11:20:04 +000016633 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16634 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016635 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16636 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16637 }
16638
Joe Perches41535772013-02-16 11:20:04 +000016639 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16640 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016641 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016642
Matt Carlson158d7ab2008-05-29 01:37:54 -070016643 err = tg3_mdio_init(tp);
16644 if (err)
16645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016646
16647 /* Initialize data/descriptor byte/word swapping. */
16648 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016649 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16650 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016651 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16652 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16653 GRC_MODE_B2HRX_ENABLE |
16654 GRC_MODE_HTX2B_ENABLE |
16655 GRC_MODE_HOST_STACKUP);
16656 else
16657 val &= GRC_MODE_HOST_STACKUP;
16658
Linus Torvalds1da177e2005-04-16 15:20:36 -070016659 tw32(GRC_MODE, val | tp->grc_mode);
16660
16661 tg3_switch_clocks(tp);
16662
16663 /* Clear this out for sanity. */
16664 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16665
Nat Gurumoorthy388d3332013-12-09 10:43:21 -080016666 /* Clear TG3PCI_REG_BASE_ADDR to prevent hangs. */
16667 tw32(TG3PCI_REG_BASE_ADDR, 0);
16668
Linus Torvalds1da177e2005-04-16 15:20:36 -070016669 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16670 &pci_state_reg);
16671 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016672 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016673 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16674 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16675 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16676 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016677 void __iomem *sram_base;
16678
16679 /* Write some dummy words into the SRAM status block
16680 * area, see if it reads back correctly. If the return
16681 * value is bad, force enable the PCIX workaround.
16682 */
16683 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16684
16685 writel(0x00000000, sram_base);
16686 writel(0x00000000, sram_base + 4);
16687 writel(0xffffffff, sram_base + 4);
16688 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016689 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016690 }
16691 }
16692
16693 udelay(50);
16694 tg3_nvram_init(tp);
16695
Nithin Sujirc4dab502013-03-06 17:02:34 +000016696 /* If the device has an NVRAM, no need to load patch firmware */
16697 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16698 !tg3_flag(tp, NO_NVRAM))
16699 tp->fw_needed = NULL;
16700
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701 grc_misc_cfg = tr32(GRC_MISC_CFG);
16702 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16703
Joe Perches41535772013-02-16 11:20:04 +000016704 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016705 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16706 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016707 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016708
Joe Perches63c3a662011-04-26 08:12:10 +000016709 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016710 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016711 tg3_flag_set(tp, TAGGED_STATUS);
16712 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016713 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16714 HOSTCC_MODE_CLRTICK_TXBD);
16715
16716 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16717 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16718 tp->misc_host_ctrl);
16719 }
16720
Matt Carlson3bda1252008-08-15 14:08:22 -070016721 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016722 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016723 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016724 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016725 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016726
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016727 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016728 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016729
16730 err = tg3_phy_probe(tp);
16731 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016732 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016733 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016734 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016735 }
16736
Matt Carlson184b8902010-04-05 10:19:25 +000016737 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016738 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016739
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016740 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16741 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016742 } else {
Joe Perches41535772013-02-16 11:20:04 +000016743 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016744 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016745 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016746 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747 }
16748
16749 /* 5700 {AX,BX} chips have a broken status block link
16750 * change bit implementation, so we must use the
16751 * status register in those cases.
16752 */
Joe Perches41535772013-02-16 11:20:04 +000016753 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016754 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016755 else
Joe Perches63c3a662011-04-26 08:12:10 +000016756 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016757
16758 /* The led_ctrl is set during tg3_phy_probe, here we might
16759 * have to force the link status polling mechanism based
16760 * upon subsystem IDs.
16761 */
16762 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016763 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016764 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16765 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016766 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016767 }
16768
16769 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016770 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016771 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016772 else
Joe Perches63c3a662011-04-26 08:12:10 +000016773 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016774
Nithin Sujir1743b832014-01-03 10:09:14 -080016775 if (tg3_flag(tp, ENABLE_APE) && tg3_flag(tp, ENABLE_ASF))
16776 tg3_flag_set(tp, POLL_CPMU_LINK);
16777
Eric Dumazet9205fd92011-11-18 06:47:01 +000016778 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016779 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016780 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016781 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016782 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016783#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016784 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016785#endif
16786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016787
Matt Carlson2c49a442010-09-30 10:34:35 +000016788 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16789 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016790 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16791
Matt Carlson2c49a442010-09-30 10:34:35 +000016792 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016793
16794 /* Increment the rx prod index on the rx std ring by at most
16795 * 8 for these chips to workaround hw errata.
16796 */
Joe Perches41535772013-02-16 11:20:04 +000016797 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16798 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16799 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016800 tp->rx_std_max_post = 8;
16801
Joe Perches63c3a662011-04-26 08:12:10 +000016802 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016803 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16804 PCIE_PWR_MGMT_L1_THRESH_MSK;
16805
Linus Torvalds1da177e2005-04-16 15:20:36 -070016806 return err;
16807}
16808
David S. Miller49b6e95f2007-03-29 01:38:42 -070016809#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016810static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016811{
16812 struct net_device *dev = tp->dev;
16813 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016814 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016815 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016816 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016817
David S. Miller49b6e95f2007-03-29 01:38:42 -070016818 addr = of_get_property(dp, "local-mac-address", &len);
Joe Perchesd458cdf2013-10-01 19:04:40 -070016819 if (addr && len == ETH_ALEN) {
16820 memcpy(dev->dev_addr, addr, ETH_ALEN);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016822 }
16823 return -ENODEV;
16824}
16825
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016826static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016827{
16828 struct net_device *dev = tp->dev;
16829
Joe Perchesd458cdf2013-10-01 19:04:40 -070016830 memcpy(dev->dev_addr, idprom->id_ethaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016831 return 0;
16832}
16833#endif
16834
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016835static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016836{
16837 struct net_device *dev = tp->dev;
16838 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016839 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016840 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016841
David S. Miller49b6e95f2007-03-29 01:38:42 -070016842#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843 if (!tg3_get_macaddr_sparc(tp))
16844 return 0;
16845#endif
16846
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016847 if (tg3_flag(tp, IS_SSB_CORE)) {
16848 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16849 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16850 return 0;
16851 }
16852
Linus Torvalds1da177e2005-04-16 15:20:36 -070016853 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016854 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016855 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016856 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16857 mac_offset = 0xcc;
16858 if (tg3_nvram_lock(tp))
16859 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16860 else
16861 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016862 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016863 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016864 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016865 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016866 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016867 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016868 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016869
16870 /* First try to get it from MAC address mailbox. */
16871 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16872 if ((hi >> 16) == 0x484b) {
16873 dev->dev_addr[0] = (hi >> 8) & 0xff;
16874 dev->dev_addr[1] = (hi >> 0) & 0xff;
16875
16876 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16877 dev->dev_addr[2] = (lo >> 24) & 0xff;
16878 dev->dev_addr[3] = (lo >> 16) & 0xff;
16879 dev->dev_addr[4] = (lo >> 8) & 0xff;
16880 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016881
Michael Chan008652b2006-03-27 23:14:53 -080016882 /* Some old bootcode may report a 0 MAC address in SRAM */
16883 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16884 }
16885 if (!addr_ok) {
16886 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016887 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016888 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016889 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016890 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16891 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016892 }
16893 /* Finally just fetch it out of the MAC control regs. */
16894 else {
16895 hi = tr32(MAC_ADDR_0_HIGH);
16896 lo = tr32(MAC_ADDR_0_LOW);
16897
16898 dev->dev_addr[5] = lo & 0xff;
16899 dev->dev_addr[4] = (lo >> 8) & 0xff;
16900 dev->dev_addr[3] = (lo >> 16) & 0xff;
16901 dev->dev_addr[2] = (lo >> 24) & 0xff;
16902 dev->dev_addr[1] = hi & 0xff;
16903 dev->dev_addr[0] = (hi >> 8) & 0xff;
16904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016905 }
16906
16907 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016908#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016909 if (!tg3_get_default_macaddr_sparc(tp))
16910 return 0;
16911#endif
16912 return -EINVAL;
16913 }
16914 return 0;
16915}
16916
David S. Miller59e6b432005-05-18 22:50:10 -070016917#define BOUNDARY_SINGLE_CACHELINE 1
16918#define BOUNDARY_MULTI_CACHELINE 2
16919
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016920static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016921{
16922 int cacheline_size;
16923 u8 byte;
16924 int goal;
16925
16926 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16927 if (byte == 0)
16928 cacheline_size = 1024;
16929 else
16930 cacheline_size = (int) byte * 4;
16931
16932 /* On 5703 and later chips, the boundary bits have no
16933 * effect.
16934 */
Joe Perches41535772013-02-16 11:20:04 +000016935 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16936 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016937 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016938 goto out;
16939
16940#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16941 goal = BOUNDARY_MULTI_CACHELINE;
16942#else
16943#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16944 goal = BOUNDARY_SINGLE_CACHELINE;
16945#else
16946 goal = 0;
16947#endif
16948#endif
16949
Joe Perches63c3a662011-04-26 08:12:10 +000016950 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016951 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16952 goto out;
16953 }
16954
David S. Miller59e6b432005-05-18 22:50:10 -070016955 if (!goal)
16956 goto out;
16957
16958 /* PCI controllers on most RISC systems tend to disconnect
16959 * when a device tries to burst across a cache-line boundary.
16960 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16961 *
16962 * Unfortunately, for PCI-E there are only limited
16963 * write-side controls for this, and thus for reads
16964 * we will still get the disconnects. We'll also waste
16965 * these PCI cycles for both read and write for chips
16966 * other than 5700 and 5701 which do not implement the
16967 * boundary bits.
16968 */
Joe Perches63c3a662011-04-26 08:12:10 +000016969 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016970 switch (cacheline_size) {
16971 case 16:
16972 case 32:
16973 case 64:
16974 case 128:
16975 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16976 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16977 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16978 } else {
16979 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16980 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16981 }
16982 break;
16983
16984 case 256:
16985 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16986 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16987 break;
16988
16989 default:
16990 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16991 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16992 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016993 }
Joe Perches63c3a662011-04-26 08:12:10 +000016994 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016995 switch (cacheline_size) {
16996 case 16:
16997 case 32:
16998 case 64:
16999 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17000 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17001 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
17002 break;
17003 }
17004 /* fallthrough */
17005 case 128:
17006 default:
17007 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
17008 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
17009 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017010 }
David S. Miller59e6b432005-05-18 22:50:10 -070017011 } else {
17012 switch (cacheline_size) {
17013 case 16:
17014 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17015 val |= (DMA_RWCTRL_READ_BNDRY_16 |
17016 DMA_RWCTRL_WRITE_BNDRY_16);
17017 break;
17018 }
17019 /* fallthrough */
17020 case 32:
17021 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17022 val |= (DMA_RWCTRL_READ_BNDRY_32 |
17023 DMA_RWCTRL_WRITE_BNDRY_32);
17024 break;
17025 }
17026 /* fallthrough */
17027 case 64:
17028 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17029 val |= (DMA_RWCTRL_READ_BNDRY_64 |
17030 DMA_RWCTRL_WRITE_BNDRY_64);
17031 break;
17032 }
17033 /* fallthrough */
17034 case 128:
17035 if (goal == BOUNDARY_SINGLE_CACHELINE) {
17036 val |= (DMA_RWCTRL_READ_BNDRY_128 |
17037 DMA_RWCTRL_WRITE_BNDRY_128);
17038 break;
17039 }
17040 /* fallthrough */
17041 case 256:
17042 val |= (DMA_RWCTRL_READ_BNDRY_256 |
17043 DMA_RWCTRL_WRITE_BNDRY_256);
17044 break;
17045 case 512:
17046 val |= (DMA_RWCTRL_READ_BNDRY_512 |
17047 DMA_RWCTRL_WRITE_BNDRY_512);
17048 break;
17049 case 1024:
17050 default:
17051 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
17052 DMA_RWCTRL_WRITE_BNDRY_1024);
17053 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070017054 }
David S. Miller59e6b432005-05-18 22:50:10 -070017055 }
17056
17057out:
17058 return val;
17059}
17060
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017061static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000017062 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017063{
17064 struct tg3_internal_buffer_desc test_desc;
17065 u32 sram_dma_descs;
17066 int i, ret;
17067
17068 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
17069
17070 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
17071 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
17072 tw32(RDMAC_STATUS, 0);
17073 tw32(WDMAC_STATUS, 0);
17074
17075 tw32(BUFMGR_MODE, 0);
17076 tw32(FTQ_RESET, 0);
17077
17078 test_desc.addr_hi = ((u64) buf_dma) >> 32;
17079 test_desc.addr_lo = buf_dma & 0xffffffff;
17080 test_desc.nic_mbuf = 0x00002100;
17081 test_desc.len = size;
17082
17083 /*
17084 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
17085 * the *second* time the tg3 driver was getting loaded after an
17086 * initial scan.
17087 *
17088 * Broadcom tells me:
17089 * ...the DMA engine is connected to the GRC block and a DMA
17090 * reset may affect the GRC block in some unpredictable way...
17091 * The behavior of resets to individual blocks has not been tested.
17092 *
17093 * Broadcom noted the GRC reset will also reset all sub-components.
17094 */
17095 if (to_device) {
17096 test_desc.cqid_sqid = (13 << 8) | 2;
17097
17098 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
17099 udelay(40);
17100 } else {
17101 test_desc.cqid_sqid = (16 << 8) | 7;
17102
17103 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
17104 udelay(40);
17105 }
17106 test_desc.flags = 0x00000005;
17107
17108 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
17109 u32 val;
17110
17111 val = *(((u32 *)&test_desc) + i);
17112 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
17113 sram_dma_descs + (i * sizeof(u32)));
17114 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
17115 }
17116 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
17117
Matt Carlson859a588792010-04-05 10:19:28 +000017118 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017119 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000017120 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070017121 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017122
17123 ret = -ENODEV;
17124 for (i = 0; i < 40; i++) {
17125 u32 val;
17126
17127 if (to_device)
17128 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
17129 else
17130 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
17131 if ((val & 0xffff) == sram_dma_descs) {
17132 ret = 0;
17133 break;
17134 }
17135
17136 udelay(100);
17137 }
17138
17139 return ret;
17140}
17141
David S. Millerded73402005-05-23 13:59:47 -070017142#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070017143
Matt Carlson41434702011-03-09 16:58:22 +000017144static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080017145 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
17146 { },
17147};
17148
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017149static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017150{
17151 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070017152 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017153 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017154
Matt Carlson4bae65c2010-11-24 08:31:52 +000017155 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
17156 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017157 if (!buf) {
17158 ret = -ENOMEM;
17159 goto out_nofree;
17160 }
17161
17162 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
17163 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
17164
David S. Miller59e6b432005-05-18 22:50:10 -070017165 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017166
Joe Perches63c3a662011-04-26 08:12:10 +000017167 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000017168 goto out;
17169
Joe Perches63c3a662011-04-26 08:12:10 +000017170 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017171 /* DMA read watermark not used on PCIE */
17172 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000017173 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000017174 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
17175 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017176 tp->dma_rwctrl |= 0x003f0000;
17177 else
17178 tp->dma_rwctrl |= 0x003f000f;
17179 } else {
Joe Perches41535772013-02-16 11:20:04 +000017180 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17181 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017182 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080017183 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017184
Michael Chan4a29cc22006-03-19 13:21:12 -080017185 /* If the 5704 is behind the EPB bridge, we can
17186 * do the less restrictive ONE_DMA workaround for
17187 * better performance.
17188 */
Joe Perches63c3a662011-04-26 08:12:10 +000017189 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000017190 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080017191 tp->dma_rwctrl |= 0x8000;
17192 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017193 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
17194
Joe Perches41535772013-02-16 11:20:04 +000017195 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080017196 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070017197 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080017198 tp->dma_rwctrl |=
17199 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
17200 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
17201 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000017202 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070017203 /* 5780 always in PCIX mode */
17204 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000017205 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070017206 /* 5714 always in PCIX mode */
17207 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017208 } else {
17209 tp->dma_rwctrl |= 0x001b000f;
17210 }
17211 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017212 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
17213 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017214
Joe Perches41535772013-02-16 11:20:04 +000017215 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
17216 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017217 tp->dma_rwctrl &= 0xfffffff0;
17218
Joe Perches41535772013-02-16 11:20:04 +000017219 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
17220 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017221 /* Remove this if it causes problems for some boards. */
17222 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
17223
17224 /* On 5700/5701 chips, we need to set this bit.
17225 * Otherwise the chip will issue cacheline transactions
17226 * to streamable DMA memory with not all the byte
17227 * enables turned on. This is an error on several
17228 * RISC PCI controllers, in particular sparc64.
17229 *
17230 * On 5703/5704 chips, this bit has been reassigned
17231 * a different meaning. In particular, it is used
17232 * on those chips to enable a PCI-X workaround.
17233 */
17234 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
17235 }
17236
17237 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17238
Linus Torvalds1da177e2005-04-16 15:20:36 -070017239
Joe Perches41535772013-02-16 11:20:04 +000017240 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
17241 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017242 goto out;
17243
David S. Miller59e6b432005-05-18 22:50:10 -070017244 /* It is best to perform DMA test with maximum write burst size
17245 * to expose the 5700/5701 write DMA bug.
17246 */
17247 saved_dma_rwctrl = tp->dma_rwctrl;
17248 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17249 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17250
Linus Torvalds1da177e2005-04-16 15:20:36 -070017251 while (1) {
17252 u32 *p = buf, i;
17253
17254 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
17255 p[i] = i;
17256
17257 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000017258 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017259 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000017260 dev_err(&tp->pdev->dev,
17261 "%s: Buffer write failed. err = %d\n",
17262 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017263 break;
17264 }
17265
Linus Torvalds1da177e2005-04-16 15:20:36 -070017266 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000017267 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017268 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000017269 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
17270 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017271 break;
17272 }
17273
17274 /* Verify it. */
17275 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
17276 if (p[i] == i)
17277 continue;
17278
David S. Miller59e6b432005-05-18 22:50:10 -070017279 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17280 DMA_RWCTRL_WRITE_BNDRY_16) {
17281 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017282 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
17283 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17284 break;
17285 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000017286 dev_err(&tp->pdev->dev,
17287 "%s: Buffer corrupted on read back! "
17288 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017289 ret = -ENODEV;
17290 goto out;
17291 }
17292 }
17293
17294 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
17295 /* Success. */
17296 ret = 0;
17297 break;
17298 }
17299 }
David S. Miller59e6b432005-05-18 22:50:10 -070017300 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
17301 DMA_RWCTRL_WRITE_BNDRY_16) {
17302 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070017303 * now look for chipsets that are known to expose the
17304 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070017305 */
Matt Carlson41434702011-03-09 16:58:22 +000017306 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017307 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
17308 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000017309 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070017310 /* Safe to use the calculated DMA boundary. */
17311 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000017312 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070017313
David S. Miller59e6b432005-05-18 22:50:10 -070017314 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
17315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017316
17317out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000017318 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017319out_nofree:
17320 return ret;
17321}
17322
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017323static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017324{
Joe Perches63c3a662011-04-26 08:12:10 +000017325 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000017326 tp->bufmgr_config.mbuf_read_dma_low_water =
17327 DEFAULT_MB_RDMA_LOW_WATER_5705;
17328 tp->bufmgr_config.mbuf_mac_rx_low_water =
17329 DEFAULT_MB_MACRX_LOW_WATER_57765;
17330 tp->bufmgr_config.mbuf_high_water =
17331 DEFAULT_MB_HIGH_WATER_57765;
17332
17333 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17334 DEFAULT_MB_RDMA_LOW_WATER_5705;
17335 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17336 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
17337 tp->bufmgr_config.mbuf_high_water_jumbo =
17338 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000017339 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070017340 tp->bufmgr_config.mbuf_read_dma_low_water =
17341 DEFAULT_MB_RDMA_LOW_WATER_5705;
17342 tp->bufmgr_config.mbuf_mac_rx_low_water =
17343 DEFAULT_MB_MACRX_LOW_WATER_5705;
17344 tp->bufmgr_config.mbuf_high_water =
17345 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000017346 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070017347 tp->bufmgr_config.mbuf_mac_rx_low_water =
17348 DEFAULT_MB_MACRX_LOW_WATER_5906;
17349 tp->bufmgr_config.mbuf_high_water =
17350 DEFAULT_MB_HIGH_WATER_5906;
17351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017352
Michael Chanfdfec1722005-07-25 12:31:48 -070017353 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17354 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
17355 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17356 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
17357 tp->bufmgr_config.mbuf_high_water_jumbo =
17358 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
17359 } else {
17360 tp->bufmgr_config.mbuf_read_dma_low_water =
17361 DEFAULT_MB_RDMA_LOW_WATER;
17362 tp->bufmgr_config.mbuf_mac_rx_low_water =
17363 DEFAULT_MB_MACRX_LOW_WATER;
17364 tp->bufmgr_config.mbuf_high_water =
17365 DEFAULT_MB_HIGH_WATER;
17366
17367 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
17368 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
17369 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
17370 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
17371 tp->bufmgr_config.mbuf_high_water_jumbo =
17372 DEFAULT_MB_HIGH_WATER_JUMBO;
17373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017374
17375 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
17376 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
17377}
17378
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017379static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017380{
Matt Carlson79eb6902010-02-17 15:17:03 +000017381 switch (tp->phy_id & TG3_PHY_ID_MASK) {
17382 case TG3_PHY_ID_BCM5400: return "5400";
17383 case TG3_PHY_ID_BCM5401: return "5401";
17384 case TG3_PHY_ID_BCM5411: return "5411";
17385 case TG3_PHY_ID_BCM5701: return "5701";
17386 case TG3_PHY_ID_BCM5703: return "5703";
17387 case TG3_PHY_ID_BCM5704: return "5704";
17388 case TG3_PHY_ID_BCM5705: return "5705";
17389 case TG3_PHY_ID_BCM5750: return "5750";
17390 case TG3_PHY_ID_BCM5752: return "5752";
17391 case TG3_PHY_ID_BCM5714: return "5714";
17392 case TG3_PHY_ID_BCM5780: return "5780";
17393 case TG3_PHY_ID_BCM5755: return "5755";
17394 case TG3_PHY_ID_BCM5787: return "5787";
17395 case TG3_PHY_ID_BCM5784: return "5784";
17396 case TG3_PHY_ID_BCM5756: return "5722/5756";
17397 case TG3_PHY_ID_BCM5906: return "5906";
17398 case TG3_PHY_ID_BCM5761: return "5761";
17399 case TG3_PHY_ID_BCM5718C: return "5718C";
17400 case TG3_PHY_ID_BCM5718S: return "5718S";
17401 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000017402 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000017403 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000017404 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000017405 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070017406 case 0: return "serdes";
17407 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070017408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017409}
17410
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017411static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070017412{
Joe Perches63c3a662011-04-26 08:12:10 +000017413 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017414 strcpy(str, "PCI Express");
17415 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000017416 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070017417 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
17418
17419 strcpy(str, "PCIX:");
17420
17421 if ((clock_ctrl == 7) ||
17422 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
17423 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
17424 strcat(str, "133MHz");
17425 else if (clock_ctrl == 0)
17426 strcat(str, "33MHz");
17427 else if (clock_ctrl == 2)
17428 strcat(str, "50MHz");
17429 else if (clock_ctrl == 4)
17430 strcat(str, "66MHz");
17431 else if (clock_ctrl == 6)
17432 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070017433 } else {
17434 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000017435 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070017436 strcat(str, "66MHz");
17437 else
17438 strcat(str, "33MHz");
17439 }
Joe Perches63c3a662011-04-26 08:12:10 +000017440 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070017441 strcat(str, ":32-bit");
17442 else
17443 strcat(str, ":64-bit");
17444 return str;
17445}
17446
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017447static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070017448{
17449 struct ethtool_coalesce *ec = &tp->coal;
17450
17451 memset(ec, 0, sizeof(*ec));
17452 ec->cmd = ETHTOOL_GCOALESCE;
17453 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
17454 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
17455 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
17456 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
17457 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
17458 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
17459 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
17460 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
17461 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
17462
17463 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17464 HOSTCC_MODE_CLRTICK_TXBD)) {
17465 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17466 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17467 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17468 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17469 }
Michael Chand244c892005-07-05 14:42:33 -070017470
Joe Perches63c3a662011-04-26 08:12:10 +000017471 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017472 ec->rx_coalesce_usecs_irq = 0;
17473 ec->tx_coalesce_usecs_irq = 0;
17474 ec->stats_block_coalesce_usecs = 0;
17475 }
David S. Miller15f98502005-05-18 22:49:26 -070017476}
17477
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017478static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017479 const struct pci_device_id *ent)
17480{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017481 struct net_device *dev;
17482 struct tg3 *tp;
Yijing Wang5865fc12013-06-02 21:36:21 +000017483 int i, err;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017484 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017485 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017486 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017487 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017488
Joe Perches05dbe002010-02-17 19:44:19 +000017489 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017490
17491 err = pci_enable_device(pdev);
17492 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017493 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017494 return err;
17495 }
17496
Linus Torvalds1da177e2005-04-16 15:20:36 -070017497 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17498 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017499 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017500 goto err_out_disable_pdev;
17501 }
17502
17503 pci_set_master(pdev);
17504
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017505 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017506 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 err = -ENOMEM;
Yijing Wang5865fc12013-06-02 21:36:21 +000017508 goto err_out_free_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017509 }
17510
Linus Torvalds1da177e2005-04-16 15:20:36 -070017511 SET_NETDEV_DEV(dev, &pdev->dev);
17512
Linus Torvalds1da177e2005-04-16 15:20:36 -070017513 tp = netdev_priv(dev);
17514 tp->pdev = pdev;
17515 tp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017516 tp->rx_mode = TG3_DEF_RX_MODE;
17517 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017518 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017519
Linus Torvalds1da177e2005-04-16 15:20:36 -070017520 if (tg3_debug > 0)
17521 tp->msg_enable = tg3_debug;
17522 else
17523 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17524
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017525 if (pdev_is_ssb_gige_core(pdev)) {
17526 tg3_flag_set(tp, IS_SSB_CORE);
17527 if (ssb_gige_must_flush_posted_writes(pdev))
17528 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17529 if (ssb_gige_one_dma_at_once(pdev))
17530 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017531 if (ssb_gige_have_roboswitch(pdev)) {
17532 tg3_flag_set(tp, USE_PHYLIB);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017533 tg3_flag_set(tp, ROBOSWITCH);
Hauke Mehrtensee002b62013-09-28 23:15:28 +020017534 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017535 if (ssb_gige_is_rgmii(pdev))
17536 tg3_flag_set(tp, RGMII_MODE);
17537 }
17538
Linus Torvalds1da177e2005-04-16 15:20:36 -070017539 /* The word/byte swap controls here control register access byte
17540 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17541 * setting below.
17542 */
17543 tp->misc_host_ctrl =
17544 MISC_HOST_CTRL_MASK_PCI_INT |
17545 MISC_HOST_CTRL_WORD_SWAP |
17546 MISC_HOST_CTRL_INDIR_ACCESS |
17547 MISC_HOST_CTRL_PCISTATE_RW;
17548
17549 /* The NONFRM (non-frame) byte/word swap controls take effect
17550 * on descriptor entries, anything which isn't packet data.
17551 *
17552 * The StrongARM chips on the board (one for tx, one for rx)
17553 * are running in big-endian mode.
17554 */
17555 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17556 GRC_MODE_WSWAP_NONFRM_DATA);
17557#ifdef __BIG_ENDIAN
17558 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17559#endif
17560 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017561 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017562 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017563
Matt Carlsond5fe4882008-11-21 17:20:32 -080017564 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017565 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017566 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017567 err = -ENOMEM;
17568 goto err_out_free_dev;
17569 }
17570
Matt Carlsonc9cab242011-07-13 09:27:27 +000017571 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17572 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17573 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17574 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17575 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017576 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017577 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17578 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017579 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017580 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57767 ||
17581 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57764 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017582 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17583 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
Nithin Sujir68273712013-09-20 16:46:56 -070017584 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727 ||
17585 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57787) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017586 tg3_flag_set(tp, ENABLE_APE);
17587 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17588 if (!tp->aperegs) {
17589 dev_err(&pdev->dev,
17590 "Cannot map APE registers, aborting\n");
17591 err = -ENOMEM;
17592 goto err_out_iounmap;
17593 }
17594 }
17595
Linus Torvalds1da177e2005-04-16 15:20:36 -070017596 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17597 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017598
Linus Torvalds1da177e2005-04-16 15:20:36 -070017599 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017600 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017601 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017602 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017603
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017604 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017605 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017606 dev_err(&pdev->dev,
17607 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017608 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017609 }
17610
Michael Chan4a29cc22006-03-19 13:21:12 -080017611 /* The EPB bridge inside 5714, 5715, and 5780 and any
17612 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017613 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17614 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17615 * do DMA address check in tg3_start_xmit().
17616 */
Joe Perches63c3a662011-04-26 08:12:10 +000017617 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017618 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017619 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017620 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017621#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017622 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017623#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017624 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017625 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017626
17627 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017628 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017629 err = pci_set_dma_mask(pdev, dma_mask);
17630 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017631 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017632 err = pci_set_consistent_dma_mask(pdev,
17633 persist_dma_mask);
17634 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017635 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17636 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017637 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017638 }
17639 }
17640 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017641 if (err || dma_mask == DMA_BIT_MASK(32)) {
17642 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017643 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017644 dev_err(&pdev->dev,
17645 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017646 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017647 }
17648 }
17649
Michael Chanfdfec1722005-07-25 12:31:48 -070017650 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017651
Patrick McHardyf6469682013-04-19 02:04:27 +000017652 features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Matt Carlson0da06062011-05-19 12:12:53 +000017653
17654 /* 5700 B0 chips do not support checksumming correctly due
17655 * to hardware bugs.
17656 */
Joe Perches41535772013-02-16 11:20:04 +000017657 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017658 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17659
17660 if (tg3_flag(tp, 5755_PLUS))
17661 features |= NETIF_F_IPV6_CSUM;
17662 }
17663
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017664 /* TSO is on by default on chips that support hardware TSO.
17665 * Firmware TSO on older chips gives lower performance, so it
17666 * is off by default, but can be enabled using ethtool.
17667 */
Joe Perches63c3a662011-04-26 08:12:10 +000017668 if ((tg3_flag(tp, HW_TSO_1) ||
17669 tg3_flag(tp, HW_TSO_2) ||
17670 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017671 (features & NETIF_F_IP_CSUM))
17672 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017673 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017674 if (features & NETIF_F_IPV6_CSUM)
17675 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017676 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017677 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17678 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17679 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17680 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17681 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017682 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017684
Matt Carlsond542fe22011-05-19 16:02:43 +000017685 dev->features |= features;
17686 dev->vlan_features |= features;
17687
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017688 /*
17689 * Add loopback capability only for a subset of devices that support
17690 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17691 * loopback for the remaining devices.
17692 */
Joe Perches41535772013-02-16 11:20:04 +000017693 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017694 !tg3_flag(tp, CPMU_PRESENT))
17695 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017696 features |= NETIF_F_LOOPBACK;
17697
Matt Carlson0da06062011-05-19 12:12:53 +000017698 dev->hw_features |= features;
Michael Chane565eec2014-01-03 10:09:12 -080017699 dev->priv_flags |= IFF_UNICAST_FLT;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017700
Joe Perches41535772013-02-16 11:20:04 +000017701 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017702 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017703 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017704 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017705 tp->rx_pending = 63;
17706 }
17707
Linus Torvalds1da177e2005-04-16 15:20:36 -070017708 err = tg3_get_device_address(tp);
17709 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017710 dev_err(&pdev->dev,
17711 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017712 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017713 }
17714
Matt Carlsonc88864d2007-11-12 21:07:01 -080017715 /*
17716 * Reset chip in case UNDI or EFI driver did not shutdown
17717 * DMA self test will enable WDMAC and we'll see (spurious)
17718 * pending DMA on the PCI bus at that point.
17719 */
17720 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17721 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17722 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17723 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17724 }
17725
17726 err = tg3_test_dma(tp);
17727 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017728 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017729 goto err_out_apeunmap;
17730 }
17731
Matt Carlson78f90dc2009-11-13 13:03:42 +000017732 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17733 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17734 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017735 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017736 struct tg3_napi *tnapi = &tp->napi[i];
17737
17738 tnapi->tp = tp;
17739 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17740
17741 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017742 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017743 intmbx += 0x8;
17744 else
17745 intmbx += 0x4;
17746
17747 tnapi->consmbox = rcvmbx;
17748 tnapi->prodmbox = sndmbx;
17749
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017750 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017751 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017752 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017753 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017754
Joe Perches63c3a662011-04-26 08:12:10 +000017755 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017756 break;
17757
17758 /*
17759 * If we support MSIX, we'll be using RSS. If we're using
17760 * RSS, the first vector only handles link interrupts and the
17761 * remaining vectors handle rx and tx interrupts. Reuse the
17762 * mailbox values for the next iteration. The values we setup
17763 * above are still useful for the single vectored mode.
17764 */
17765 if (!i)
17766 continue;
17767
17768 rcvmbx += 0x8;
17769
17770 if (sndmbx & 0x4)
17771 sndmbx -= 0x4;
17772 else
17773 sndmbx += 0xc;
17774 }
17775
Matt Carlsonc88864d2007-11-12 21:07:01 -080017776 tg3_init_coal(tp);
17777
Michael Chanc49a1562006-12-17 17:07:29 -080017778 pci_set_drvdata(pdev, dev);
17779
Joe Perches41535772013-02-16 11:20:04 +000017780 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17781 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17782 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017783 tg3_flag_set(tp, PTP_CAPABLE);
17784
Matt Carlson21f76382012-02-22 12:35:21 +000017785 tg3_timer_init(tp);
17786
Michael Chan402e1392013-02-14 12:13:41 +000017787 tg3_carrier_off(tp);
17788
Linus Torvalds1da177e2005-04-16 15:20:36 -070017789 err = register_netdev(dev);
17790 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017791 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017792 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017793 }
17794
Joe Perches05dbe002010-02-17 19:44:19 +000017795 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17796 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017797 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017798 tg3_bus_string(tp, str),
17799 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017800
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017801 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017802 struct phy_device *phydev;
Hauke Mehrtensead24022013-09-28 23:15:26 +020017803 phydev = tp->mdio_bus->phy_map[tp->phy_addr];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017804 netdev_info(dev,
17805 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017806 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017807 } else {
17808 char *ethtype;
17809
17810 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17811 ethtype = "10/100Base-TX";
17812 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17813 ethtype = "1000Base-SX";
17814 else
17815 ethtype = "10/100/1000Base-T";
17816
Matt Carlson5129c3a2010-04-05 10:19:23 +000017817 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017818 "(WireSpeed[%d], EEE[%d])\n",
17819 tg3_phy_string(tp), ethtype,
17820 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17821 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017822 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017823
Joe Perches05dbe002010-02-17 19:44:19 +000017824 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017825 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017826 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017827 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017828 tg3_flag(tp, ENABLE_ASF) != 0,
17829 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017830 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17831 tp->dma_rwctrl,
17832 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17833 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017834
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017835 pci_save_state(pdev);
17836
Linus Torvalds1da177e2005-04-16 15:20:36 -070017837 return 0;
17838
Matt Carlson0d3031d2007-10-10 18:02:43 -070017839err_out_apeunmap:
17840 if (tp->aperegs) {
17841 iounmap(tp->aperegs);
17842 tp->aperegs = NULL;
17843 }
17844
Linus Torvalds1da177e2005-04-16 15:20:36 -070017845err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017846 if (tp->regs) {
17847 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017848 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017850
17851err_out_free_dev:
17852 free_netdev(dev);
17853
17854err_out_free_res:
17855 pci_release_regions(pdev);
17856
17857err_out_disable_pdev:
Gavin Shanc80dc132013-07-24 17:25:09 +080017858 if (pci_is_enabled(pdev))
17859 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017860 return err;
17861}
17862
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017863static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017864{
17865 struct net_device *dev = pci_get_drvdata(pdev);
17866
17867 if (dev) {
17868 struct tg3 *tp = netdev_priv(dev);
17869
Jesper Juhle3c55302012-04-09 22:50:15 +020017870 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017871
Matt Carlsondb219972011-11-04 09:15:03 +000017872 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017873
David S. Miller1805b2f2011-10-24 18:18:09 -040017874 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017875 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017876 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017877 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017878
Linus Torvalds1da177e2005-04-16 15:20:36 -070017879 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017880 if (tp->aperegs) {
17881 iounmap(tp->aperegs);
17882 tp->aperegs = NULL;
17883 }
Michael Chan68929142005-08-09 20:17:14 -070017884 if (tp->regs) {
17885 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017886 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017888 free_netdev(dev);
17889 pci_release_regions(pdev);
17890 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017891 }
17892}
17893
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017894#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017895static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017896{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017897 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017898 struct net_device *dev = pci_get_drvdata(pdev);
17899 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017900 int err = 0;
17901
17902 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017903
17904 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017905 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017906
Matt Carlsondb219972011-11-04 09:15:03 +000017907 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017908 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017909 tg3_netif_stop(tp);
17910
Matt Carlson21f76382012-02-22 12:35:21 +000017911 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017912
David S. Millerf47c11e2005-06-24 20:18:35 -070017913 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017914 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017915 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017916
17917 netif_device_detach(dev);
17918
David S. Millerf47c11e2005-06-24 20:18:35 -070017919 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017920 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017921 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017922 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017923
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017924 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017925 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017926 int err2;
17927
David S. Millerf47c11e2005-06-24 20:18:35 -070017928 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017929
Joe Perches63c3a662011-04-26 08:12:10 +000017930 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017931 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017932 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017934
Matt Carlson21f76382012-02-22 12:35:21 +000017935 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017936
17937 netif_device_attach(dev);
17938 tg3_netif_start(tp);
17939
Michael Chanb9ec6c12006-07-25 16:37:27 -070017940out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017941 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017942
17943 if (!err2)
17944 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017945 }
17946
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017947unlock:
17948 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017949 return err;
17950}
17951
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017952static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017953{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017954 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017955 struct net_device *dev = pci_get_drvdata(pdev);
17956 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017957 int err = 0;
17958
17959 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -070017960
17961 if (!netif_running(dev))
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017962 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017963
Linus Torvalds1da177e2005-04-16 15:20:36 -070017964 netif_device_attach(dev);
17965
David S. Millerf47c11e2005-06-24 20:18:35 -070017966 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017967
Nithin Sujir2e460fc2013-05-23 11:11:22 +000017968 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
17969
Joe Perches63c3a662011-04-26 08:12:10 +000017970 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017971 err = tg3_restart_hw(tp,
17972 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017973 if (err)
17974 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017975
Matt Carlson21f76382012-02-22 12:35:21 +000017976 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017977
Linus Torvalds1da177e2005-04-16 15:20:36 -070017978 tg3_netif_start(tp);
17979
Michael Chanb9ec6c12006-07-25 16:37:27 -070017980out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017981 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017982
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017983 if (!err)
17984 tg3_phy_start(tp);
17985
Rafael J. Wysocki8496e852013-12-01 02:34:37 +010017986unlock:
17987 rtnl_unlock();
Michael Chanb9ec6c12006-07-25 16:37:27 -070017988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017989}
Fabio Estevam42df36a2013-04-16 09:28:29 +000017990#endif /* CONFIG_PM_SLEEP */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017991
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017992static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
17993
Nithin Sujir4c305fa2013-07-29 13:58:37 -070017994static void tg3_shutdown(struct pci_dev *pdev)
17995{
17996 struct net_device *dev = pci_get_drvdata(pdev);
17997 struct tg3 *tp = netdev_priv(dev);
17998
17999 rtnl_lock();
18000 netif_device_detach(dev);
18001
18002 if (netif_running(dev))
18003 dev_close(dev);
18004
18005 if (system_state == SYSTEM_POWER_OFF)
18006 tg3_power_down(tp);
18007
18008 rtnl_unlock();
18009}
18010
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018011/**
18012 * tg3_io_error_detected - called when PCI error is detected
18013 * @pdev: Pointer to PCI device
18014 * @state: The current pci connection state
18015 *
18016 * This function is called after a PCI bus error affecting
18017 * this device has been detected.
18018 */
18019static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
18020 pci_channel_state_t state)
18021{
18022 struct net_device *netdev = pci_get_drvdata(pdev);
18023 struct tg3 *tp = netdev_priv(netdev);
18024 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
18025
18026 netdev_info(netdev, "PCI I/O error detected\n");
18027
18028 rtnl_lock();
18029
Gavin Shand8af4df2013-07-24 17:25:08 +080018030 /* We probably don't have netdev yet */
18031 if (!netdev || !netif_running(netdev))
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018032 goto done;
18033
18034 tg3_phy_stop(tp);
18035
18036 tg3_netif_stop(tp);
18037
Matt Carlson21f76382012-02-22 12:35:21 +000018038 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018039
18040 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000018041 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018042
18043 netif_device_detach(netdev);
18044
18045 /* Clean up software state, even if MMIO is blocked */
18046 tg3_full_lock(tp, 0);
18047 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
18048 tg3_full_unlock(tp);
18049
18050done:
Michael Chan72bb72b2013-06-17 13:47:25 -070018051 if (state == pci_channel_io_perm_failure) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018052 if (netdev) {
18053 tg3_napi_enable(tp);
18054 dev_close(netdev);
18055 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018056 err = PCI_ERS_RESULT_DISCONNECT;
Michael Chan72bb72b2013-06-17 13:47:25 -070018057 } else {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018058 pci_disable_device(pdev);
Michael Chan72bb72b2013-06-17 13:47:25 -070018059 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018060
18061 rtnl_unlock();
18062
18063 return err;
18064}
18065
18066/**
18067 * tg3_io_slot_reset - called after the pci bus has been reset.
18068 * @pdev: Pointer to PCI device
18069 *
18070 * Restart the card from scratch, as if from a cold-boot.
18071 * At this point, the card has exprienced a hard reset,
18072 * followed by fixups by BIOS, and has its config space
18073 * set up identically to what it was at cold boot.
18074 */
18075static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
18076{
18077 struct net_device *netdev = pci_get_drvdata(pdev);
18078 struct tg3 *tp = netdev_priv(netdev);
18079 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
18080 int err;
18081
18082 rtnl_lock();
18083
18084 if (pci_enable_device(pdev)) {
Daniel Borkmann68293092013-08-13 11:45:13 -070018085 dev_err(&pdev->dev,
18086 "Cannot re-enable PCI device after reset.\n");
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018087 goto done;
18088 }
18089
18090 pci_set_master(pdev);
18091 pci_restore_state(pdev);
18092 pci_save_state(pdev);
18093
Daniel Borkmann68293092013-08-13 11:45:13 -070018094 if (!netdev || !netif_running(netdev)) {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018095 rc = PCI_ERS_RESULT_RECOVERED;
18096 goto done;
18097 }
18098
18099 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000018100 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018101 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018102
18103 rc = PCI_ERS_RESULT_RECOVERED;
18104
18105done:
Daniel Borkmann68293092013-08-13 11:45:13 -070018106 if (rc != PCI_ERS_RESULT_RECOVERED && netdev && netif_running(netdev)) {
Michael Chan72bb72b2013-06-17 13:47:25 -070018107 tg3_napi_enable(tp);
18108 dev_close(netdev);
18109 }
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018110 rtnl_unlock();
18111
18112 return rc;
18113}
18114
18115/**
18116 * tg3_io_resume - called when traffic can start flowing again.
18117 * @pdev: Pointer to PCI device
18118 *
18119 * This callback is called when the error recovery driver tells
18120 * us that its OK to resume normal operation.
18121 */
18122static void tg3_io_resume(struct pci_dev *pdev)
18123{
18124 struct net_device *netdev = pci_get_drvdata(pdev);
18125 struct tg3 *tp = netdev_priv(netdev);
18126 int err;
18127
18128 rtnl_lock();
18129
18130 if (!netif_running(netdev))
18131 goto done;
18132
18133 tg3_full_lock(tp, 0);
Nithin Sujir2e460fc2013-05-23 11:11:22 +000018134 tg3_ape_driver_state_change(tp, RESET_KIND_INIT);
Joe Perches63c3a662011-04-26 08:12:10 +000018135 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000018136 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018137 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018138 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018139 netdev_err(netdev, "Cannot restart hardware after reset.\n");
18140 goto done;
18141 }
18142
18143 netif_device_attach(netdev);
18144
Matt Carlson21f76382012-02-22 12:35:21 +000018145 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018146
18147 tg3_netif_start(tp);
18148
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000018149 tg3_full_unlock(tp);
18150
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018151 tg3_phy_start(tp);
18152
18153done:
18154 rtnl_unlock();
18155}
18156
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070018157static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018158 .error_detected = tg3_io_error_detected,
18159 .slot_reset = tg3_io_slot_reset,
18160 .resume = tg3_io_resume
18161};
18162
Linus Torvalds1da177e2005-04-16 15:20:36 -070018163static struct pci_driver tg3_driver = {
18164 .name = DRV_MODULE_NAME,
18165 .id_table = tg3_pci_tbl,
18166 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050018167 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000018168 .err_handler = &tg3_err_handler,
Fabio Estevam42df36a2013-04-16 09:28:29 +000018169 .driver.pm = &tg3_pm_ops,
Nithin Sujir4c305fa2013-07-29 13:58:37 -070018170 .shutdown = tg3_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018171};
18172
Peter Hüwe8dbb0dc2013-05-21 12:58:06 +000018173module_pci_driver(tg3_driver);