blob: d044adf2718b970b23db28b1d34b28d010d4a5e7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Michael Chand8871992013-02-14 12:13:42 +000097#define TG3_MIN_NUM 130
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chand8871992013-02-14 12:13:42 +0000100#define DRV_MODULE_RELDATE "February 14, 2013"
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
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
Nithin Sujirc4dab502013-03-06 17:02:34 +0000215#define FIRMWARE_TG357766 "tigon/tg357766.bin"
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800216#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
217#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
218
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500219static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000220 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
223MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
224MODULE_LICENSE("GPL");
225MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800226MODULE_FIRMWARE(FIRMWARE_TG3);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO);
228MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
231module_param(tg3_debug, int, 0);
232MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
233
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000234#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
235#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
236
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000237static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
257 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
258 TG3_DRV_DATA_FLAG_5705_10_100},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
260 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
261 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
264 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
265 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
272 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
278 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000286 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
287 PCI_VENDOR_ID_LENOVO,
288 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
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_5714)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
310 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
314 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
315 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
316 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
320 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000329 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
330 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
332 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
339 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700340 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
341 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
342 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
343 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
344 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
345 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
346 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000347 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700348 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
351MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
352
Andreas Mohr50da8592006-08-14 23:54:30 -0700353static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000355} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 { "rx_octets" },
357 { "rx_fragments" },
358 { "rx_ucast_packets" },
359 { "rx_mcast_packets" },
360 { "rx_bcast_packets" },
361 { "rx_fcs_errors" },
362 { "rx_align_errors" },
363 { "rx_xon_pause_rcvd" },
364 { "rx_xoff_pause_rcvd" },
365 { "rx_mac_ctrl_rcvd" },
366 { "rx_xoff_entered" },
367 { "rx_frame_too_long_errors" },
368 { "rx_jabbers" },
369 { "rx_undersize_packets" },
370 { "rx_in_length_errors" },
371 { "rx_out_length_errors" },
372 { "rx_64_or_less_octet_packets" },
373 { "rx_65_to_127_octet_packets" },
374 { "rx_128_to_255_octet_packets" },
375 { "rx_256_to_511_octet_packets" },
376 { "rx_512_to_1023_octet_packets" },
377 { "rx_1024_to_1522_octet_packets" },
378 { "rx_1523_to_2047_octet_packets" },
379 { "rx_2048_to_4095_octet_packets" },
380 { "rx_4096_to_8191_octet_packets" },
381 { "rx_8192_to_9022_octet_packets" },
382
383 { "tx_octets" },
384 { "tx_collisions" },
385
386 { "tx_xon_sent" },
387 { "tx_xoff_sent" },
388 { "tx_flow_control" },
389 { "tx_mac_errors" },
390 { "tx_single_collisions" },
391 { "tx_mult_collisions" },
392 { "tx_deferred" },
393 { "tx_excessive_collisions" },
394 { "tx_late_collisions" },
395 { "tx_collide_2times" },
396 { "tx_collide_3times" },
397 { "tx_collide_4times" },
398 { "tx_collide_5times" },
399 { "tx_collide_6times" },
400 { "tx_collide_7times" },
401 { "tx_collide_8times" },
402 { "tx_collide_9times" },
403 { "tx_collide_10times" },
404 { "tx_collide_11times" },
405 { "tx_collide_12times" },
406 { "tx_collide_13times" },
407 { "tx_collide_14times" },
408 { "tx_collide_15times" },
409 { "tx_ucast_packets" },
410 { "tx_mcast_packets" },
411 { "tx_bcast_packets" },
412 { "tx_carrier_sense_errors" },
413 { "tx_discards" },
414 { "tx_errors" },
415
416 { "dma_writeq_full" },
417 { "dma_write_prioq_full" },
418 { "rxbds_empty" },
419 { "rx_discards" },
420 { "rx_errors" },
421 { "rx_threshold_hit" },
422
423 { "dma_readq_full" },
424 { "dma_read_prioq_full" },
425 { "tx_comp_queue_full" },
426
427 { "ring_set_send_prod_index" },
428 { "ring_status_update" },
429 { "nic_irqs" },
430 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000431 { "nic_tx_threshold_hit" },
432
433 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
Matt Carlson48fa55a2011-04-13 11:05:06 +0000436#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000437#define TG3_NVRAM_TEST 0
438#define TG3_LINK_TEST 1
439#define TG3_REGISTER_TEST 2
440#define TG3_MEMORY_TEST 3
441#define TG3_MAC_LOOPB_TEST 4
442#define TG3_PHY_LOOPB_TEST 5
443#define TG3_EXT_LOOPB_TEST 6
444#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000445
446
Andreas Mohr50da8592006-08-14 23:54:30 -0700447static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700448 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000449} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000450 [TG3_NVRAM_TEST] = { "nvram test (online) " },
451 [TG3_LINK_TEST] = { "link test (online) " },
452 [TG3_REGISTER_TEST] = { "register test (offline)" },
453 [TG3_MEMORY_TEST] = { "memory test (offline)" },
454 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
455 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
456 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
457 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700458};
459
Matt Carlson48fa55a2011-04-13 11:05:06 +0000460#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
461
462
Michael Chanb401e9e2005-12-19 16:27:04 -0800463static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
464{
465 writel(val, tp->regs + off);
466}
467
468static u32 tg3_read32(struct tg3 *tp, u32 off)
469{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000470 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800471}
472
Matt Carlson0d3031d2007-10-10 18:02:43 -0700473static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
474{
475 writel(val, tp->aperegs + off);
476}
477
478static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
479{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000480 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
484{
Michael Chan68929142005-08-09 20:17:14 -0700485 unsigned long flags;
486
487 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700488 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
489 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700490 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700491}
492
493static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
494{
495 writel(val, tp->regs + off);
496 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Michael Chan68929142005-08-09 20:17:14 -0700499static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
500{
501 unsigned long flags;
502 u32 val;
503
504 spin_lock_irqsave(&tp->indirect_lock, flags);
505 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
506 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
507 spin_unlock_irqrestore(&tp->indirect_lock, flags);
508 return val;
509}
510
511static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
512{
513 unsigned long flags;
514
515 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
516 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
517 TG3_64BIT_REG_LOW, val);
518 return;
519 }
Matt Carlson66711e62009-11-13 13:03:49 +0000520 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700521 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
522 TG3_64BIT_REG_LOW, val);
523 return;
524 }
525
526 spin_lock_irqsave(&tp->indirect_lock, flags);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
528 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
529 spin_unlock_irqrestore(&tp->indirect_lock, flags);
530
531 /* In indirect mode when disabling interrupts, we also need
532 * to clear the interrupt bit in the GRC local ctrl register.
533 */
534 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
535 (val == 0x1)) {
536 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
537 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
538 }
539}
540
541static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
542{
543 unsigned long flags;
544 u32 val;
545
546 spin_lock_irqsave(&tp->indirect_lock, flags);
547 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
548 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
549 spin_unlock_irqrestore(&tp->indirect_lock, flags);
550 return val;
551}
552
Michael Chanb401e9e2005-12-19 16:27:04 -0800553/* usec_wait specifies the wait time in usec when writing to certain registers
554 * where it is unsafe to read back the register without some delay.
555 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
556 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
557 */
558static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Joe Perches63c3a662011-04-26 08:12:10 +0000560 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800561 /* Non-posted methods */
562 tp->write32(tp, off, val);
563 else {
564 /* Posted method */
565 tg3_write32(tp, off, val);
566 if (usec_wait)
567 udelay(usec_wait);
568 tp->read32(tp, off);
569 }
570 /* Wait again after the read for the posted method to guarantee that
571 * the wait time is met.
572 */
573 if (usec_wait)
574 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Michael Chan09ee9292005-08-09 20:17:00 -0700577static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
578{
579 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000580 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
581 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
582 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700583 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700584}
585
Michael Chan20094932005-08-09 20:16:32 -0700586static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 void __iomem *mbox = tp->regs + off;
589 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000590 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000592 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
593 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 readl(mbox);
595}
596
Michael Chanb5d37722006-09-27 16:06:21 -0700597static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
598{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000599 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700600}
601
602static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
603{
604 writel(val, tp->regs + off + GRCMBOX_BASE);
605}
606
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000607#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700608#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000609#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
610#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
611#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700612
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000613#define tw32(reg, val) tp->write32(tp, reg, val)
614#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
615#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
616#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
619{
Michael Chan68929142005-08-09 20:17:14 -0700620 unsigned long flags;
621
Joe Perches41535772013-02-16 11:20:04 +0000622 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700623 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
624 return;
625
Michael Chan68929142005-08-09 20:17:14 -0700626 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000627 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
629 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Michael Chanbbadf502006-04-06 21:46:34 -0700631 /* Always leave this as zero. */
632 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
633 } else {
634 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
635 tw32_f(TG3PCI_MEM_WIN_DATA, val);
636
637 /* Always leave this as zero. */
638 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
639 }
Michael Chan68929142005-08-09 20:17:14 -0700640 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
644{
Michael Chan68929142005-08-09 20:17:14 -0700645 unsigned long flags;
646
Joe Perches41535772013-02-16 11:20:04 +0000647 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700648 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
649 *val = 0;
650 return;
651 }
652
Michael Chan68929142005-08-09 20:17:14 -0700653 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000654 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700655 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
656 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Michael Chanbbadf502006-04-06 21:46:34 -0700658 /* Always leave this as zero. */
659 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
660 } else {
661 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
662 *val = tr32(TG3PCI_MEM_WIN_DATA);
663
664 /* Always leave this as zero. */
665 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
666 }
Michael Chan68929142005-08-09 20:17:14 -0700667 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670static void tg3_ape_lock_init(struct tg3 *tp)
671{
672 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000673 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674
Joe Perches41535772013-02-16 11:20:04 +0000675 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000676 regbase = TG3_APE_LOCK_GRANT;
677 else
678 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700679
680 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000681 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
682 switch (i) {
683 case TG3_APE_LOCK_PHY0:
684 case TG3_APE_LOCK_PHY1:
685 case TG3_APE_LOCK_PHY2:
686 case TG3_APE_LOCK_PHY3:
687 bit = APE_LOCK_GRANT_DRIVER;
688 break;
689 default:
690 if (!tp->pci_fn)
691 bit = APE_LOCK_GRANT_DRIVER;
692 else
693 bit = 1 << tp->pci_fn;
694 }
695 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 }
697
Matt Carlson0d3031d2007-10-10 18:02:43 -0700698}
699
700static int tg3_ape_lock(struct tg3 *tp, int locknum)
701{
702 int i, off;
703 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700705
Joe Perches63c3a662011-04-26 08:12:10 +0000706 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700707 return 0;
708
709 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000710 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000711 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000712 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000713 case TG3_APE_LOCK_GRC:
714 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000715 if (!tp->pci_fn)
716 bit = APE_LOCK_REQ_DRIVER;
717 else
718 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000719 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000720 case TG3_APE_LOCK_PHY0:
721 case TG3_APE_LOCK_PHY1:
722 case TG3_APE_LOCK_PHY2:
723 case TG3_APE_LOCK_PHY3:
724 bit = APE_LOCK_REQ_DRIVER;
725 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000726 default:
727 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 }
729
Joe Perches41535772013-02-16 11:20:04 +0000730 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000731 req = TG3_APE_LOCK_REQ;
732 gnt = TG3_APE_LOCK_GRANT;
733 } else {
734 req = TG3_APE_PER_LOCK_REQ;
735 gnt = TG3_APE_PER_LOCK_GRANT;
736 }
737
Matt Carlson0d3031d2007-10-10 18:02:43 -0700738 off = 4 * locknum;
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741
742 /* Wait for up to 1 millisecond to acquire lock. */
743 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000744 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000745 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700746 break;
747 udelay(10);
748 }
749
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000750 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700751 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000752 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700753 ret = -EBUSY;
754 }
755
756 return ret;
757}
758
759static void tg3_ape_unlock(struct tg3 *tp, int locknum)
760{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000761 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700762
Joe Perches63c3a662011-04-26 08:12:10 +0000763 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700764 return;
765
766 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000767 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000768 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000769 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000770 case TG3_APE_LOCK_GRC:
771 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000772 if (!tp->pci_fn)
773 bit = APE_LOCK_GRANT_DRIVER;
774 else
775 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000776 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000777 case TG3_APE_LOCK_PHY0:
778 case TG3_APE_LOCK_PHY1:
779 case TG3_APE_LOCK_PHY2:
780 case TG3_APE_LOCK_PHY3:
781 bit = APE_LOCK_GRANT_DRIVER;
782 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000783 default:
784 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700785 }
786
Joe Perches41535772013-02-16 11:20:04 +0000787 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000788 gnt = TG3_APE_LOCK_GRANT;
789 else
790 gnt = TG3_APE_PER_LOCK_GRANT;
791
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000792 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700793}
794
Matt Carlsonb65a3722012-07-16 16:24:00 +0000795static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000796{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000797 u32 apedata;
798
Matt Carlsonb65a3722012-07-16 16:24:00 +0000799 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000800 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000801 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000802
803 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000804 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
805 break;
806
Matt Carlsonb65a3722012-07-16 16:24:00 +0000807 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
808
809 udelay(10);
810 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000811 }
812
Matt Carlsonb65a3722012-07-16 16:24:00 +0000813 return timeout_us ? 0 : -EBUSY;
814}
815
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000816static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
817{
818 u32 i, apedata;
819
820 for (i = 0; i < timeout_us / 10; i++) {
821 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
822
823 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
824 break;
825
826 udelay(10);
827 }
828
829 return i == timeout_us / 10;
830}
831
Michael Chan86449942012-10-02 20:31:14 -0700832static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
833 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000834{
835 int err;
836 u32 i, bufoff, msgoff, maxlen, apedata;
837
838 if (!tg3_flag(tp, APE_HAS_NCSI))
839 return 0;
840
841 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
842 if (apedata != APE_SEG_SIG_MAGIC)
843 return -ENODEV;
844
845 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
846 if (!(apedata & APE_FW_STATUS_READY))
847 return -EAGAIN;
848
849 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
850 TG3_APE_SHMEM_BASE;
851 msgoff = bufoff + 2 * sizeof(u32);
852 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
853
854 while (len) {
855 u32 length;
856
857 /* Cap xfer sizes to scratchpad limits. */
858 length = (len > maxlen) ? maxlen : len;
859 len -= length;
860
861 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
862 if (!(apedata & APE_FW_STATUS_READY))
863 return -EAGAIN;
864
865 /* Wait for up to 1 msec for APE to service previous event. */
866 err = tg3_ape_event_lock(tp, 1000);
867 if (err)
868 return err;
869
870 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
871 APE_EVENT_STATUS_SCRTCHPD_READ |
872 APE_EVENT_STATUS_EVENT_PENDING;
873 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
874
875 tg3_ape_write32(tp, bufoff, base_off);
876 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
877
878 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
879 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
880
881 base_off += length;
882
883 if (tg3_ape_wait_for_event(tp, 30000))
884 return -EAGAIN;
885
886 for (i = 0; length; i += 4, length -= 4) {
887 u32 val = tg3_ape_read32(tp, msgoff + i);
888 memcpy(data, &val, sizeof(u32));
889 data++;
890 }
891 }
892
893 return 0;
894}
895
Matt Carlsonb65a3722012-07-16 16:24:00 +0000896static int tg3_ape_send_event(struct tg3 *tp, u32 event)
897{
898 int err;
899 u32 apedata;
900
901 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
902 if (apedata != APE_SEG_SIG_MAGIC)
903 return -EAGAIN;
904
905 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
906 if (!(apedata & APE_FW_STATUS_READY))
907 return -EAGAIN;
908
909 /* Wait for up to 1 millisecond for APE to service previous event. */
910 err = tg3_ape_event_lock(tp, 1000);
911 if (err)
912 return err;
913
914 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
915 event | APE_EVENT_STATUS_EVENT_PENDING);
916
917 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
918 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
919
920 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000921}
922
923static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
924{
925 u32 event;
926 u32 apedata;
927
928 if (!tg3_flag(tp, ENABLE_APE))
929 return;
930
931 switch (kind) {
932 case RESET_KIND_INIT:
933 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
934 APE_HOST_SEG_SIG_MAGIC);
935 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
936 APE_HOST_SEG_LEN_MAGIC);
937 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
938 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
939 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
940 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
941 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
942 APE_HOST_BEHAV_NO_PHYLOCK);
943 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
944 TG3_APE_HOST_DRVR_STATE_START);
945
946 event = APE_EVENT_STATUS_STATE_START;
947 break;
948 case RESET_KIND_SHUTDOWN:
949 /* With the interface we are currently using,
950 * APE does not track driver state. Wiping
951 * out the HOST SEGMENT SIGNATURE forces
952 * the APE to assume OS absent status.
953 */
954 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
955
956 if (device_may_wakeup(&tp->pdev->dev) &&
957 tg3_flag(tp, WOL_ENABLE)) {
958 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
959 TG3_APE_HOST_WOL_SPEED_AUTO);
960 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
961 } else
962 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
963
964 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
965
966 event = APE_EVENT_STATUS_STATE_UNLOAD;
967 break;
968 case RESET_KIND_SUSPEND:
969 event = APE_EVENT_STATUS_STATE_SUSPEND;
970 break;
971 default:
972 return;
973 }
974
975 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
976
977 tg3_ape_send_event(tp, event);
978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static void tg3_disable_ints(struct tg3 *tp)
981{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000982 int i;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 tw32(TG3PCI_MISC_HOST_CTRL,
985 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000986 for (i = 0; i < tp->irq_max; i++)
987 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990static void tg3_enable_ints(struct tg3 *tp)
991{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000992 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000993
Michael Chanbbe832c2005-06-24 20:20:04 -0700994 tp->irq_sync = 0;
995 wmb();
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 tw32(TG3PCI_MISC_HOST_CTRL,
998 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000999
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001000 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001001 for (i = 0; i < tp->irq_cnt; i++) {
1002 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001003
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001004 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001005 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001006 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1007
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001008 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001009 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001010
1011 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001012 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001013 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1014 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1015 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001016 tw32(HOSTCC_MODE, tp->coal_now);
1017
1018 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Matt Carlson17375d22009-08-28 14:02:18 +00001021static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001022{
Matt Carlson17375d22009-08-28 14:02:18 +00001023 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001024 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001025 unsigned int work_exists = 0;
1026
1027 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001028 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001029 if (sblk->status & SD_STATUS_LINK_CHG)
1030 work_exists = 1;
1031 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001032
1033 /* check for TX work to do */
1034 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1035 work_exists = 1;
1036
1037 /* check for RX work to do */
1038 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001039 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001040 work_exists = 1;
1041
1042 return work_exists;
1043}
1044
Matt Carlson17375d22009-08-28 14:02:18 +00001045/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001046 * similar to tg3_enable_ints, but it accurately determines whether there
1047 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001048 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Matt Carlson17375d22009-08-28 14:02:18 +00001050static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Matt Carlson17375d22009-08-28 14:02:18 +00001052 struct tg3 *tp = tnapi->tp;
1053
Matt Carlson898a56f2009-08-28 14:02:40 +00001054 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 mmiowb();
1056
David S. Millerfac9b832005-05-18 22:46:34 -07001057 /* When doing tagged status, this work check is unnecessary.
1058 * The last_tag we write above tells the chip which piece of
1059 * work we've completed.
1060 */
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001062 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001063 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066static void tg3_switch_clocks(struct tg3 *tp)
1067{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001068 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 u32 orig_clock_ctrl;
1070
Joe Perches63c3a662011-04-26 08:12:10 +00001071 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001072 return;
1073
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001074 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 orig_clock_ctrl = clock_ctrl;
1077 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1078 CLOCK_CTRL_CLKRUN_OENABLE |
1079 0x1f);
1080 tp->pci_clock_ctrl = clock_ctrl;
1081
Joe Perches63c3a662011-04-26 08:12:10 +00001082 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001084 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1085 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001088 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1089 clock_ctrl |
1090 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1091 40);
1092 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1093 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1094 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001096 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099#define PHY_BUSY_LOOPS 5000
1100
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001101static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1102 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 u32 frame_val;
1105 unsigned int loops;
1106 int ret;
1107
1108 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1109 tw32_f(MAC_MI_MODE,
1110 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1111 udelay(80);
1112 }
1113
Michael Chan8151ad52012-07-29 19:15:41 +00001114 tg3_ape_lock(tp, tp->phy_ape_lock);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *val = 0x0;
1117
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001118 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 MI_COM_PHY_ADDR_MASK);
1120 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1121 MI_COM_REG_ADDR_MASK);
1122 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 tw32_f(MAC_MI_COM, frame_val);
1125
1126 loops = PHY_BUSY_LOOPS;
1127 while (loops != 0) {
1128 udelay(10);
1129 frame_val = tr32(MAC_MI_COM);
1130
1131 if ((frame_val & MI_COM_BUSY) == 0) {
1132 udelay(5);
1133 frame_val = tr32(MAC_MI_COM);
1134 break;
1135 }
1136 loops -= 1;
1137 }
1138
1139 ret = -EBUSY;
1140 if (loops != 0) {
1141 *val = frame_val & MI_COM_DATA_MASK;
1142 ret = 0;
1143 }
1144
1145 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1146 tw32_f(MAC_MI_MODE, tp->mi_mode);
1147 udelay(80);
1148 }
1149
Michael Chan8151ad52012-07-29 19:15:41 +00001150 tg3_ape_unlock(tp, tp->phy_ape_lock);
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return ret;
1153}
1154
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001155static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1156{
1157 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1158}
1159
1160static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1161 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 u32 frame_val;
1164 unsigned int loops;
1165 int ret;
1166
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001167 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001168 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001169 return 0;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1172 tw32_f(MAC_MI_MODE,
1173 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1174 udelay(80);
1175 }
1176
Michael Chan8151ad52012-07-29 19:15:41 +00001177 tg3_ape_lock(tp, tp->phy_ape_lock);
1178
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001179 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 MI_COM_PHY_ADDR_MASK);
1181 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1182 MI_COM_REG_ADDR_MASK);
1183 frame_val |= (val & MI_COM_DATA_MASK);
1184 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 tw32_f(MAC_MI_COM, frame_val);
1187
1188 loops = PHY_BUSY_LOOPS;
1189 while (loops != 0) {
1190 udelay(10);
1191 frame_val = tr32(MAC_MI_COM);
1192 if ((frame_val & MI_COM_BUSY) == 0) {
1193 udelay(5);
1194 frame_val = tr32(MAC_MI_COM);
1195 break;
1196 }
1197 loops -= 1;
1198 }
1199
1200 ret = -EBUSY;
1201 if (loops != 0)
1202 ret = 0;
1203
1204 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1205 tw32_f(MAC_MI_MODE, tp->mi_mode);
1206 udelay(80);
1207 }
1208
Michael Chan8151ad52012-07-29 19:15:41 +00001209 tg3_ape_unlock(tp, tp->phy_ape_lock);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return ret;
1212}
1213
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001214static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1215{
1216 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1217}
1218
Matt Carlsonb0988c12011-04-20 07:57:39 +00001219static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1220{
1221 int err;
1222
1223 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1224 if (err)
1225 goto done;
1226
1227 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1228 if (err)
1229 goto done;
1230
1231 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1232 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1233 if (err)
1234 goto done;
1235
1236 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1237
1238done:
1239 return err;
1240}
1241
1242static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1243{
1244 int err;
1245
1246 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1247 if (err)
1248 goto done;
1249
1250 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1251 if (err)
1252 goto done;
1253
1254 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1255 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1256 if (err)
1257 goto done;
1258
1259 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1260
1261done:
1262 return err;
1263}
1264
1265static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1270 if (!err)
1271 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1272
1273 return err;
1274}
1275
1276static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1277{
1278 int err;
1279
1280 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1281 if (!err)
1282 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1283
1284 return err;
1285}
1286
Matt Carlson15ee95c2011-04-20 07:57:40 +00001287static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1288{
1289 int err;
1290
1291 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1292 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1293 MII_TG3_AUXCTL_SHDWSEL_MISC);
1294 if (!err)
1295 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1296
1297 return err;
1298}
1299
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001300static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1301{
1302 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1303 set |= MII_TG3_AUXCTL_MISC_WREN;
1304
1305 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1306}
1307
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001308static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1309{
1310 u32 val;
1311 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001312
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001313 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1314
1315 if (err)
1316 return err;
1317 if (enable)
1318
1319 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1320 else
1321 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1322
1323 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1324 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1325
1326 return err;
1327}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001328
Matt Carlson95e28692008-05-25 23:44:14 -07001329static int tg3_bmcr_reset(struct tg3 *tp)
1330{
1331 u32 phy_control;
1332 int limit, err;
1333
1334 /* OK, reset it, and poll the BMCR_RESET bit until it
1335 * clears or we time out.
1336 */
1337 phy_control = BMCR_RESET;
1338 err = tg3_writephy(tp, MII_BMCR, phy_control);
1339 if (err != 0)
1340 return -EBUSY;
1341
1342 limit = 5000;
1343 while (limit--) {
1344 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1345 if (err != 0)
1346 return -EBUSY;
1347
1348 if ((phy_control & BMCR_RESET) == 0) {
1349 udelay(40);
1350 break;
1351 }
1352 udelay(10);
1353 }
Roel Kluind4675b52009-02-12 16:33:27 -08001354 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001355 return -EBUSY;
1356
1357 return 0;
1358}
1359
Matt Carlson158d7ab2008-05-29 01:37:54 -07001360static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1361{
Francois Romieu3d165432009-01-19 16:56:50 -08001362 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001363 u32 val;
1364
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001365 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001366
1367 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001368 val = -EIO;
1369
1370 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001371
1372 return val;
1373}
1374
1375static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1376{
Francois Romieu3d165432009-01-19 16:56:50 -08001377 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001378 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001379
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001380 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001381
1382 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001383 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001384
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001385 spin_unlock_bh(&tp->lock);
1386
1387 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001388}
1389
1390static int tg3_mdio_reset(struct mii_bus *bp)
1391{
1392 return 0;
1393}
1394
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001395static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001396{
1397 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001399
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001400 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001402 case PHY_ID_BCM50610:
1403 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 val = MAC_PHYCFG2_50610_LED_MODES;
1405 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001406 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001407 val = MAC_PHYCFG2_AC131_LED_MODES;
1408 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001409 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1411 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001412 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001413 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1414 break;
1415 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001416 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001417 }
1418
1419 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1420 tw32(MAC_PHYCFG2, val);
1421
1422 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001423 val &= ~(MAC_PHYCFG1_RGMII_INT |
1424 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1425 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001426 tw32(MAC_PHYCFG1, val);
1427
1428 return;
1429 }
1430
Joe Perches63c3a662011-04-26 08:12:10 +00001431 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001432 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1433 MAC_PHYCFG2_FMODE_MASK_MASK |
1434 MAC_PHYCFG2_GMODE_MASK_MASK |
1435 MAC_PHYCFG2_ACT_MASK_MASK |
1436 MAC_PHYCFG2_QUAL_MASK_MASK |
1437 MAC_PHYCFG2_INBAND_ENABLE;
1438
1439 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001440
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001441 val = tr32(MAC_PHYCFG1);
1442 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1443 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001444 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1445 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001446 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001447 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001448 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1449 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001450 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1451 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1452 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001453
Matt Carlsona9daf362008-05-25 23:49:44 -07001454 val = tr32(MAC_EXT_RGMII_MODE);
1455 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1456 MAC_RGMII_MODE_RX_QUALITY |
1457 MAC_RGMII_MODE_RX_ACTIVITY |
1458 MAC_RGMII_MODE_RX_ENG_DET |
1459 MAC_RGMII_MODE_TX_ENABLE |
1460 MAC_RGMII_MODE_TX_LOWPWR |
1461 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001462 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1463 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001464 val |= MAC_RGMII_MODE_RX_INT_B |
1465 MAC_RGMII_MODE_RX_QUALITY |
1466 MAC_RGMII_MODE_RX_ACTIVITY |
1467 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001468 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001469 val |= MAC_RGMII_MODE_TX_ENABLE |
1470 MAC_RGMII_MODE_TX_LOWPWR |
1471 MAC_RGMII_MODE_TX_RESET;
1472 }
1473 tw32(MAC_EXT_RGMII_MODE, val);
1474}
1475
Matt Carlson158d7ab2008-05-29 01:37:54 -07001476static void tg3_mdio_start(struct tg3 *tp)
1477{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001478 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1479 tw32_f(MAC_MI_MODE, tp->mi_mode);
1480 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001481
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001483 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001484 tg3_mdio_config_5785(tp);
1485}
1486
1487static int tg3_mdio_init(struct tg3 *tp)
1488{
1489 int i;
1490 u32 reg;
1491 struct phy_device *phydev;
1492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001494 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001495
Matt Carlson69f11c92011-07-13 09:27:30 +00001496 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001497
Joe Perches41535772013-02-16 11:20:04 +00001498 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001499 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1500 else
1501 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1502 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001503 if (is_serdes)
1504 tp->phy_addr += 7;
1505 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001506 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001507
Matt Carlson158d7ab2008-05-29 01:37:54 -07001508 tg3_mdio_start(tp);
1509
Joe Perches63c3a662011-04-26 08:12:10 +00001510 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001511 return 0;
1512
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001513 tp->mdio_bus = mdiobus_alloc();
1514 if (tp->mdio_bus == NULL)
1515 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001516
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001517 tp->mdio_bus->name = "tg3 mdio bus";
1518 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001519 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001520 tp->mdio_bus->priv = tp;
1521 tp->mdio_bus->parent = &tp->pdev->dev;
1522 tp->mdio_bus->read = &tg3_mdio_read;
1523 tp->mdio_bus->write = &tg3_mdio_write;
1524 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001525 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001526 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001527
1528 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001529 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001530
1531 /* The bus registration will look for all the PHYs on the mdio bus.
1532 * Unfortunately, it does not ensure the PHY is powered up before
1533 * accessing the PHY ID registers. A chip reset is the
1534 * quickest way to bring the device back to an operational state..
1535 */
1536 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1537 tg3_bmcr_reset(tp);
1538
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001539 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001540 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001541 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001542 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001543 return i;
1544 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001545
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001546 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001547
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001548 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001549 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001550 mdiobus_unregister(tp->mdio_bus);
1551 mdiobus_free(tp->mdio_bus);
1552 return -ENODEV;
1553 }
1554
1555 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001556 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001557 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001558 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001559 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001560 case PHY_ID_BCM50610:
1561 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001562 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001563 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001564 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001565 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001566 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001567 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001568 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001569 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001570 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001571 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001572 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001573 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001574 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001575 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001576 case PHY_ID_RTL8201E:
1577 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001578 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001579 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001580 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001581 break;
1582 }
1583
Joe Perches63c3a662011-04-26 08:12:10 +00001584 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001585
Joe Perches41535772013-02-16 11:20:04 +00001586 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001587 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001588
1589 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001590}
1591
1592static void tg3_mdio_fini(struct tg3 *tp)
1593{
Joe Perches63c3a662011-04-26 08:12:10 +00001594 if (tg3_flag(tp, MDIOBUS_INITED)) {
1595 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001596 mdiobus_unregister(tp->mdio_bus);
1597 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001598 }
1599}
1600
Matt Carlson95e28692008-05-25 23:44:14 -07001601/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001602static inline void tg3_generate_fw_event(struct tg3 *tp)
1603{
1604 u32 val;
1605
1606 val = tr32(GRC_RX_CPU_EVENT);
1607 val |= GRC_RX_CPU_DRIVER_EVENT;
1608 tw32_f(GRC_RX_CPU_EVENT, val);
1609
1610 tp->last_event_jiffies = jiffies;
1611}
1612
1613#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1614
1615/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001616static void tg3_wait_for_event_ack(struct tg3 *tp)
1617{
1618 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001619 unsigned int delay_cnt;
1620 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001621
Matt Carlson4ba526c2008-08-15 14:10:04 -07001622 /* If enough time has passed, no wait is necessary. */
1623 time_remain = (long)(tp->last_event_jiffies + 1 +
1624 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1625 (long)jiffies;
1626 if (time_remain < 0)
1627 return;
1628
1629 /* Check if we can shorten the wait time. */
1630 delay_cnt = jiffies_to_usecs(time_remain);
1631 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1632 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1633 delay_cnt = (delay_cnt >> 3) + 1;
1634
1635 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001636 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1637 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001638 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001639 }
1640}
1641
1642/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001643static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001644{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001645 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001646
1647 val = 0;
1648 if (!tg3_readphy(tp, MII_BMCR, &reg))
1649 val = reg << 16;
1650 if (!tg3_readphy(tp, MII_BMSR, &reg))
1651 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001652 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001653
1654 val = 0;
1655 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1656 val = reg << 16;
1657 if (!tg3_readphy(tp, MII_LPA, &reg))
1658 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001659 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001660
1661 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001662 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001663 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1664 val = reg << 16;
1665 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1666 val |= (reg & 0xffff);
1667 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001668 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001669
1670 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1671 val = reg << 16;
1672 else
1673 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001674 *data++ = val;
1675}
1676
1677/* tp->lock is held. */
1678static void tg3_ump_link_report(struct tg3 *tp)
1679{
1680 u32 data[4];
1681
1682 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1683 return;
1684
1685 tg3_phy_gather_ump_data(tp, data);
1686
1687 tg3_wait_for_event_ack(tp);
1688
1689 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1691 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1694 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001695
Matt Carlson4ba526c2008-08-15 14:10:04 -07001696 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001697}
1698
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001699/* tp->lock is held. */
1700static void tg3_stop_fw(struct tg3 *tp)
1701{
1702 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1703 /* Wait for RX cpu to ACK the previous event. */
1704 tg3_wait_for_event_ack(tp);
1705
1706 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1707
1708 tg3_generate_fw_event(tp);
1709
1710 /* Wait for RX cpu to ACK this event. */
1711 tg3_wait_for_event_ack(tp);
1712 }
1713}
1714
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001715/* tp->lock is held. */
1716static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1717{
1718 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1719 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1720
1721 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1722 switch (kind) {
1723 case RESET_KIND_INIT:
1724 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1725 DRV_STATE_START);
1726 break;
1727
1728 case RESET_KIND_SHUTDOWN:
1729 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1730 DRV_STATE_UNLOAD);
1731 break;
1732
1733 case RESET_KIND_SUSPEND:
1734 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1735 DRV_STATE_SUSPEND);
1736 break;
1737
1738 default:
1739 break;
1740 }
1741 }
1742
1743 if (kind == RESET_KIND_INIT ||
1744 kind == RESET_KIND_SUSPEND)
1745 tg3_ape_driver_state_change(tp, kind);
1746}
1747
1748/* tp->lock is held. */
1749static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1750{
1751 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1752 switch (kind) {
1753 case RESET_KIND_INIT:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_START_DONE);
1756 break;
1757
1758 case RESET_KIND_SHUTDOWN:
1759 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1760 DRV_STATE_UNLOAD_DONE);
1761 break;
1762
1763 default:
1764 break;
1765 }
1766 }
1767
1768 if (kind == RESET_KIND_SHUTDOWN)
1769 tg3_ape_driver_state_change(tp, kind);
1770}
1771
1772/* tp->lock is held. */
1773static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1774{
1775 if (tg3_flag(tp, ENABLE_ASF)) {
1776 switch (kind) {
1777 case RESET_KIND_INIT:
1778 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1779 DRV_STATE_START);
1780 break;
1781
1782 case RESET_KIND_SHUTDOWN:
1783 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1784 DRV_STATE_UNLOAD);
1785 break;
1786
1787 case RESET_KIND_SUSPEND:
1788 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1789 DRV_STATE_SUSPEND);
1790 break;
1791
1792 default:
1793 break;
1794 }
1795 }
1796}
1797
1798static int tg3_poll_fw(struct tg3 *tp)
1799{
1800 int i;
1801 u32 val;
1802
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001803 if (tg3_flag(tp, IS_SSB_CORE)) {
1804 /* We don't use firmware. */
1805 return 0;
1806 }
1807
Joe Perches41535772013-02-16 11:20:04 +00001808 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001809 /* Wait up to 20ms for init done. */
1810 for (i = 0; i < 200; i++) {
1811 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1812 return 0;
1813 udelay(100);
1814 }
1815 return -ENODEV;
1816 }
1817
1818 /* Wait for firmware initialization to complete. */
1819 for (i = 0; i < 100000; i++) {
1820 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1821 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1822 break;
1823 udelay(10);
1824 }
1825
1826 /* Chip might not be fitted with firmware. Some Sun onboard
1827 * parts are configured like that. So don't signal the timeout
1828 * of the above loop as an error, but do report the lack of
1829 * running firmware once.
1830 */
1831 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1832 tg3_flag_set(tp, NO_FWARE_REPORTED);
1833
1834 netdev_info(tp->dev, "No firmware running\n");
1835 }
1836
Joe Perches41535772013-02-16 11:20:04 +00001837 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001838 /* The 57765 A0 needs a little more
1839 * time to do some important work.
1840 */
1841 mdelay(10);
1842 }
1843
1844 return 0;
1845}
1846
Matt Carlson95e28692008-05-25 23:44:14 -07001847static void tg3_link_report(struct tg3 *tp)
1848{
1849 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001850 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001851 tg3_ump_link_report(tp);
1852 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001853 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1854 (tp->link_config.active_speed == SPEED_1000 ?
1855 1000 :
1856 (tp->link_config.active_speed == SPEED_100 ?
1857 100 : 10)),
1858 (tp->link_config.active_duplex == DUPLEX_FULL ?
1859 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001860
Joe Perches05dbe002010-02-17 19:44:19 +00001861 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1862 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1863 "on" : "off",
1864 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1865 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001866
1867 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1868 netdev_info(tp->dev, "EEE is %s\n",
1869 tp->setlpicnt ? "enabled" : "disabled");
1870
Matt Carlson95e28692008-05-25 23:44:14 -07001871 tg3_ump_link_report(tp);
1872 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001873
1874 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001875}
1876
Matt Carlson95e28692008-05-25 23:44:14 -07001877static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1878{
1879 u16 miireg;
1880
Steve Glendinninge18ce342008-12-16 02:00:00 -08001881 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001882 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001883 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001884 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001885 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001886 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1887 else
1888 miireg = 0;
1889
1890 return miireg;
1891}
1892
Matt Carlson95e28692008-05-25 23:44:14 -07001893static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1894{
1895 u8 cap = 0;
1896
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001897 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1898 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1899 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1900 if (lcladv & ADVERTISE_1000XPAUSE)
1901 cap = FLOW_CTRL_RX;
1902 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001903 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001904 }
1905
1906 return cap;
1907}
1908
Matt Carlsonf51f3562008-05-25 23:45:08 -07001909static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001910{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001911 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001912 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001913 u32 old_rx_mode = tp->rx_mode;
1914 u32 old_tx_mode = tp->tx_mode;
1915
Joe Perches63c3a662011-04-26 08:12:10 +00001916 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001917 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001918 else
1919 autoneg = tp->link_config.autoneg;
1920
Joe Perches63c3a662011-04-26 08:12:10 +00001921 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001922 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001923 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001924 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001925 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001926 } else
1927 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001928
Matt Carlsonf51f3562008-05-25 23:45:08 -07001929 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001930
Steve Glendinninge18ce342008-12-16 02:00:00 -08001931 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001932 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1933 else
1934 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1935
Matt Carlsonf51f3562008-05-25 23:45:08 -07001936 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001937 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001938
Steve Glendinninge18ce342008-12-16 02:00:00 -08001939 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001940 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1941 else
1942 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1943
Matt Carlsonf51f3562008-05-25 23:45:08 -07001944 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001945 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001946}
1947
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001948static void tg3_adjust_link(struct net_device *dev)
1949{
1950 u8 oldflowctrl, linkmesg = 0;
1951 u32 mac_mode, lcl_adv, rmt_adv;
1952 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001953 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001954
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001955 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001956
1957 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1958 MAC_MODE_HALF_DUPLEX);
1959
1960 oldflowctrl = tp->link_config.active_flowctrl;
1961
1962 if (phydev->link) {
1963 lcl_adv = 0;
1964 rmt_adv = 0;
1965
1966 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1967 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001968 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001969 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001970 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001971 else
1972 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001973
1974 if (phydev->duplex == DUPLEX_HALF)
1975 mac_mode |= MAC_MODE_HALF_DUPLEX;
1976 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001977 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001978 tp->link_config.flowctrl);
1979
1980 if (phydev->pause)
1981 rmt_adv = LPA_PAUSE_CAP;
1982 if (phydev->asym_pause)
1983 rmt_adv |= LPA_PAUSE_ASYM;
1984 }
1985
1986 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1987 } else
1988 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1989
1990 if (mac_mode != tp->mac_mode) {
1991 tp->mac_mode = mac_mode;
1992 tw32_f(MAC_MODE, tp->mac_mode);
1993 udelay(40);
1994 }
1995
Joe Perches41535772013-02-16 11:20:04 +00001996 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001997 if (phydev->speed == SPEED_10)
1998 tw32(MAC_MI_STAT,
1999 MAC_MI_STAT_10MBPS_MODE |
2000 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2001 else
2002 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2003 }
2004
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002005 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2006 tw32(MAC_TX_LENGTHS,
2007 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2008 (6 << TX_LENGTHS_IPG_SHIFT) |
2009 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2010 else
2011 tw32(MAC_TX_LENGTHS,
2012 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2013 (6 << TX_LENGTHS_IPG_SHIFT) |
2014 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2015
Matt Carlson34655ad2012-02-22 12:35:18 +00002016 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002017 phydev->speed != tp->link_config.active_speed ||
2018 phydev->duplex != tp->link_config.active_duplex ||
2019 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002020 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002021
Matt Carlson34655ad2012-02-22 12:35:18 +00002022 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002023 tp->link_config.active_speed = phydev->speed;
2024 tp->link_config.active_duplex = phydev->duplex;
2025
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002026 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002027
2028 if (linkmesg)
2029 tg3_link_report(tp);
2030}
2031
2032static int tg3_phy_init(struct tg3 *tp)
2033{
2034 struct phy_device *phydev;
2035
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002036 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002037 return 0;
2038
2039 /* Bring the PHY back to a known state. */
2040 tg3_bmcr_reset(tp);
2041
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002042 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002043
2044 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002045 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2046 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002047 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002048 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002049 return PTR_ERR(phydev);
2050 }
2051
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002052 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002053 switch (phydev->interface) {
2054 case PHY_INTERFACE_MODE_GMII:
2055 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002056 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002057 phydev->supported &= (PHY_GBIT_FEATURES |
2058 SUPPORTED_Pause |
2059 SUPPORTED_Asym_Pause);
2060 break;
2061 }
2062 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002063 case PHY_INTERFACE_MODE_MII:
2064 phydev->supported &= (PHY_BASIC_FEATURES |
2065 SUPPORTED_Pause |
2066 SUPPORTED_Asym_Pause);
2067 break;
2068 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002069 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002070 return -EINVAL;
2071 }
2072
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002073 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002074
2075 phydev->advertising = phydev->supported;
2076
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 return 0;
2078}
2079
2080static void tg3_phy_start(struct tg3 *tp)
2081{
2082 struct phy_device *phydev;
2083
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002084 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002085 return;
2086
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002087 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002088
Matt Carlson80096062010-08-02 11:26:06 +00002089 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2090 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002091 phydev->speed = tp->link_config.speed;
2092 phydev->duplex = tp->link_config.duplex;
2093 phydev->autoneg = tp->link_config.autoneg;
2094 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002095 }
2096
2097 phy_start(phydev);
2098
2099 phy_start_aneg(phydev);
2100}
2101
2102static void tg3_phy_stop(struct tg3 *tp)
2103{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002104 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002105 return;
2106
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002107 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002108}
2109
2110static void tg3_phy_fini(struct tg3 *tp)
2111{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002112 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002113 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002114 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002115 }
2116}
2117
Matt Carlson941ec902011-08-19 13:58:23 +00002118static int tg3_phy_set_extloopbk(struct tg3 *tp)
2119{
2120 int err;
2121 u32 val;
2122
2123 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2124 return 0;
2125
2126 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2127 /* Cannot do read-modify-write on 5401 */
2128 err = tg3_phy_auxctl_write(tp,
2129 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2130 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2131 0x4c20);
2132 goto done;
2133 }
2134
2135 err = tg3_phy_auxctl_read(tp,
2136 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2137 if (err)
2138 return err;
2139
2140 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2141 err = tg3_phy_auxctl_write(tp,
2142 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2143
2144done:
2145 return err;
2146}
2147
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002148static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2149{
2150 u32 phytest;
2151
2152 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2153 u32 phy;
2154
2155 tg3_writephy(tp, MII_TG3_FET_TEST,
2156 phytest | MII_TG3_FET_SHADOW_EN);
2157 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2158 if (enable)
2159 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2160 else
2161 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2162 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2163 }
2164 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2165 }
2166}
2167
Matt Carlson6833c042008-11-21 17:18:59 -08002168static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2169{
2170 u32 reg;
2171
Joe Perches63c3a662011-04-26 08:12:10 +00002172 if (!tg3_flag(tp, 5705_PLUS) ||
2173 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002174 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002175 return;
2176
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002177 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002178 tg3_phy_fet_toggle_apd(tp, enable);
2179 return;
2180 }
2181
Matt Carlson6833c042008-11-21 17:18:59 -08002182 reg = MII_TG3_MISC_SHDW_WREN |
2183 MII_TG3_MISC_SHDW_SCR5_SEL |
2184 MII_TG3_MISC_SHDW_SCR5_LPED |
2185 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2186 MII_TG3_MISC_SHDW_SCR5_SDTL |
2187 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002188 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002189 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2190
2191 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2192
2193
2194 reg = MII_TG3_MISC_SHDW_WREN |
2195 MII_TG3_MISC_SHDW_APD_SEL |
2196 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2197 if (enable)
2198 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2199
2200 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2201}
2202
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002203static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2204{
2205 u32 phy;
2206
Joe Perches63c3a662011-04-26 08:12:10 +00002207 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002208 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002209 return;
2210
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002211 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002212 u32 ephy;
2213
Matt Carlson535ef6e2009-08-25 10:09:36 +00002214 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2215 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2216
2217 tg3_writephy(tp, MII_TG3_FET_TEST,
2218 ephy | MII_TG3_FET_SHADOW_EN);
2219 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002220 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002221 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002222 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002223 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2224 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002225 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002226 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002227 }
2228 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002229 int ret;
2230
2231 ret = tg3_phy_auxctl_read(tp,
2232 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2233 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002234 if (enable)
2235 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2236 else
2237 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002238 tg3_phy_auxctl_write(tp,
2239 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002240 }
2241 }
2242}
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244static void tg3_phy_set_wirespeed(struct tg3 *tp)
2245{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002246 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 u32 val;
2248
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002249 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return;
2251
Matt Carlson15ee95c2011-04-20 07:57:40 +00002252 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2253 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002254 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2255 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002258static void tg3_phy_apply_otp(struct tg3 *tp)
2259{
2260 u32 otp, phy;
2261
2262 if (!tp->phy_otp)
2263 return;
2264
2265 otp = tp->phy_otp;
2266
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002267 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002268 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002269
2270 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2271 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2272 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2273
2274 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2275 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2276 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2277
2278 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2279 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2280 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2281
2282 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2283 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2284
2285 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2286 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2287
2288 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2289 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2290 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2291
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002292 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002293}
2294
Matt Carlson52b02d02010-10-14 10:37:41 +00002295static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2296{
2297 u32 val;
2298
2299 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2300 return;
2301
2302 tp->setlpicnt = 0;
2303
2304 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2305 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002306 tp->link_config.active_duplex == DUPLEX_FULL &&
2307 (tp->link_config.active_speed == SPEED_100 ||
2308 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002309 u32 eeectl;
2310
2311 if (tp->link_config.active_speed == SPEED_1000)
2312 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2313 else
2314 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2315
2316 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2317
Matt Carlson3110f5f52010-12-06 08:28:50 +00002318 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2319 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002320
Matt Carlsonb0c59432011-05-19 12:12:48 +00002321 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2322 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002323 tp->setlpicnt = 2;
2324 }
2325
2326 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002327 if (current_link_up == 1 &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002328 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002329 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002330 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002331 }
2332
Matt Carlson52b02d02010-10-14 10:37:41 +00002333 val = tr32(TG3_CPMU_EEE_MODE);
2334 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2335 }
2336}
2337
Matt Carlsonb0c59432011-05-19 12:12:48 +00002338static void tg3_phy_eee_enable(struct tg3 *tp)
2339{
2340 u32 val;
2341
2342 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002343 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2344 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002345 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002346 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002347 val = MII_TG3_DSP_TAP26_ALNOKO |
2348 MII_TG3_DSP_TAP26_RMRXSTO;
2349 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002350 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002351 }
2352
2353 val = tr32(TG3_CPMU_EEE_MODE);
2354 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static int tg3_wait_macro_done(struct tg3 *tp)
2358{
2359 int limit = 100;
2360
2361 while (limit--) {
2362 u32 tmp32;
2363
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002364 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if ((tmp32 & 0x1000) == 0)
2366 break;
2367 }
2368 }
Roel Kluind4675b52009-02-12 16:33:27 -08002369 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return -EBUSY;
2371
2372 return 0;
2373}
2374
2375static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2376{
2377 static const u32 test_pat[4][6] = {
2378 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2379 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2380 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2381 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2382 };
2383 int chan;
2384
2385 for (chan = 0; chan < 4; chan++) {
2386 int i;
2387
2388 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2389 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002390 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 for (i = 0; i < 6; i++)
2393 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2394 test_pat[chan][i]);
2395
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002396 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (tg3_wait_macro_done(tp)) {
2398 *resetp = 1;
2399 return -EBUSY;
2400 }
2401
2402 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2403 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002404 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 if (tg3_wait_macro_done(tp)) {
2406 *resetp = 1;
2407 return -EBUSY;
2408 }
2409
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002410 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (tg3_wait_macro_done(tp)) {
2412 *resetp = 1;
2413 return -EBUSY;
2414 }
2415
2416 for (i = 0; i < 6; i += 2) {
2417 u32 low, high;
2418
2419 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2420 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2421 tg3_wait_macro_done(tp)) {
2422 *resetp = 1;
2423 return -EBUSY;
2424 }
2425 low &= 0x7fff;
2426 high &= 0x000f;
2427 if (low != test_pat[chan][i] ||
2428 high != test_pat[chan][i+1]) {
2429 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2430 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2431 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2432
2433 return -EBUSY;
2434 }
2435 }
2436 }
2437
2438 return 0;
2439}
2440
2441static int tg3_phy_reset_chanpat(struct tg3 *tp)
2442{
2443 int chan;
2444
2445 for (chan = 0; chan < 4; chan++) {
2446 int i;
2447
2448 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2449 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002450 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 for (i = 0; i < 6; i++)
2452 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002453 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (tg3_wait_macro_done(tp))
2455 return -EBUSY;
2456 }
2457
2458 return 0;
2459}
2460
2461static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2462{
2463 u32 reg32, phy9_orig;
2464 int retries, do_phy_reset, err;
2465
2466 retries = 10;
2467 do_phy_reset = 1;
2468 do {
2469 if (do_phy_reset) {
2470 err = tg3_bmcr_reset(tp);
2471 if (err)
2472 return err;
2473 do_phy_reset = 0;
2474 }
2475
2476 /* Disable transmitter and interrupt. */
2477 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2478 continue;
2479
2480 reg32 |= 0x3000;
2481 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2482
2483 /* Set full-duplex, 1000 mbps. */
2484 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002485 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002488 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 continue;
2490
Matt Carlson221c5632011-06-13 13:39:01 +00002491 tg3_writephy(tp, MII_CTRL1000,
2492 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002494 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002495 if (err)
2496 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002499 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2502 if (!err)
2503 break;
2504 } while (--retries);
2505
2506 err = tg3_phy_reset_chanpat(tp);
2507 if (err)
2508 return err;
2509
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002510 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002513 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002515 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Matt Carlson221c5632011-06-13 13:39:01 +00002517 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2520 reg32 &= ~0x3000;
2521 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2522 } else if (!err)
2523 err = -EBUSY;
2524
2525 return err;
2526}
2527
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002528static void tg3_carrier_off(struct tg3 *tp)
2529{
2530 netif_carrier_off(tp->dev);
2531 tp->link_up = false;
2532}
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534/* This will reset the tigon3 PHY if there is no valid
2535 * link unless the FORCE argument is non-zero.
2536 */
2537static int tg3_phy_reset(struct tg3 *tp)
2538{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002539 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 int err;
2541
Joe Perches41535772013-02-16 11:20:04 +00002542 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002543 val = tr32(GRC_MISC_CFG);
2544 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2545 udelay(40);
2546 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002547 err = tg3_readphy(tp, MII_BMSR, &val);
2548 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (err != 0)
2550 return -EBUSY;
2551
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002552 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002553 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002554 tg3_link_report(tp);
2555 }
2556
Joe Perches41535772013-02-16 11:20:04 +00002557 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2558 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2559 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 err = tg3_phy_reset_5703_4_5(tp);
2561 if (err)
2562 return err;
2563 goto out;
2564 }
2565
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002566 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002567 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2568 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002569 cpmuctrl = tr32(TG3_CPMU_CTRL);
2570 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2571 tw32(TG3_CPMU_CTRL,
2572 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2573 }
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 err = tg3_bmcr_reset(tp);
2576 if (err)
2577 return err;
2578
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002579 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002580 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2581 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002582
2583 tw32(TG3_CPMU_CTRL, cpmuctrl);
2584 }
2585
Joe Perches41535772013-02-16 11:20:04 +00002586 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2587 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002588 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2589 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2590 CPMU_LSPD_1000MB_MACCLK_12_5) {
2591 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2592 udelay(40);
2593 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2594 }
2595 }
2596
Joe Perches63c3a662011-04-26 08:12:10 +00002597 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002598 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002599 return 0;
2600
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002601 tg3_phy_apply_otp(tp);
2602
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002603 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002604 tg3_phy_toggle_apd(tp, true);
2605 else
2606 tg3_phy_toggle_apd(tp, false);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002609 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002610 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002611 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2612 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002613 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002615
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002616 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002617 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2618 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002620
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002621 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002622 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002623 tg3_phydsp_write(tp, 0x000a, 0x310b);
2624 tg3_phydsp_write(tp, 0x201f, 0x9506);
2625 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002626 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002627 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002628 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002629 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002630 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2631 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2632 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2633 tg3_writephy(tp, MII_TG3_TEST1,
2634 MII_TG3_TEST1_TRIM_EN | 0x4);
2635 } else
2636 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2637
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002638 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002639 }
Michael Chanc424cb22006-04-29 18:56:34 -07002640 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /* Set Extended packet length bit (bit 14) on all chips that */
2643 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002644 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002646 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002647 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002649 err = tg3_phy_auxctl_read(tp,
2650 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2651 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002652 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2653 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655
2656 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2657 * jumbo frames transmission.
2658 */
Joe Perches63c3a662011-04-26 08:12:10 +00002659 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002660 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002661 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002662 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664
Joe Perches41535772013-02-16 11:20:04 +00002665 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002666 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002667 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002668 }
2669
Joe Perches41535772013-02-16 11:20:04 +00002670 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002671 tg3_phydsp_write(tp, 0xffb, 0x4000);
2672
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002673 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 tg3_phy_set_wirespeed(tp);
2675 return 0;
2676}
2677
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002678#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2679#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2680#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2681 TG3_GPIO_MSG_NEED_VAUX)
2682#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2683 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2684 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2685 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2686 (TG3_GPIO_MSG_DRVR_PRES << 12))
2687
2688#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2689 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2690 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2691 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2692 (TG3_GPIO_MSG_NEED_VAUX << 12))
2693
2694static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2695{
2696 u32 status, shift;
2697
Joe Perches41535772013-02-16 11:20:04 +00002698 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2699 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002700 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2701 else
2702 status = tr32(TG3_CPMU_DRV_STATUS);
2703
2704 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2705 status &= ~(TG3_GPIO_MSG_MASK << shift);
2706 status |= (newstat << shift);
2707
Joe Perches41535772013-02-16 11:20:04 +00002708 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2709 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002710 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2711 else
2712 tw32(TG3_CPMU_DRV_STATUS, status);
2713
2714 return status >> TG3_APE_GPIO_MSG_SHIFT;
2715}
2716
Matt Carlson520b2752011-06-13 13:39:02 +00002717static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2718{
2719 if (!tg3_flag(tp, IS_NIC))
2720 return 0;
2721
Joe Perches41535772013-02-16 11:20:04 +00002722 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2723 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2724 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002725 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2726 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002727
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002728 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2729
2730 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2731 TG3_GRC_LCLCTL_PWRSW_DELAY);
2732
2733 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2734 } else {
2735 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2736 TG3_GRC_LCLCTL_PWRSW_DELAY);
2737 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002738
Matt Carlson520b2752011-06-13 13:39:02 +00002739 return 0;
2740}
2741
2742static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2743{
2744 u32 grc_local_ctrl;
2745
2746 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002747 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2748 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002749 return;
2750
2751 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2752
2753 tw32_wait_f(GRC_LOCAL_CTRL,
2754 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2755 TG3_GRC_LCLCTL_PWRSW_DELAY);
2756
2757 tw32_wait_f(GRC_LOCAL_CTRL,
2758 grc_local_ctrl,
2759 TG3_GRC_LCLCTL_PWRSW_DELAY);
2760
2761 tw32_wait_f(GRC_LOCAL_CTRL,
2762 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2763 TG3_GRC_LCLCTL_PWRSW_DELAY);
2764}
2765
2766static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2767{
2768 if (!tg3_flag(tp, IS_NIC))
2769 return;
2770
Joe Perches41535772013-02-16 11:20:04 +00002771 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2772 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002773 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2774 (GRC_LCLCTRL_GPIO_OE0 |
2775 GRC_LCLCTRL_GPIO_OE1 |
2776 GRC_LCLCTRL_GPIO_OE2 |
2777 GRC_LCLCTRL_GPIO_OUTPUT0 |
2778 GRC_LCLCTRL_GPIO_OUTPUT1),
2779 TG3_GRC_LCLCTL_PWRSW_DELAY);
2780 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2781 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2782 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2783 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2784 GRC_LCLCTRL_GPIO_OE1 |
2785 GRC_LCLCTRL_GPIO_OE2 |
2786 GRC_LCLCTRL_GPIO_OUTPUT0 |
2787 GRC_LCLCTRL_GPIO_OUTPUT1 |
2788 tp->grc_local_ctrl;
2789 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2790 TG3_GRC_LCLCTL_PWRSW_DELAY);
2791
2792 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2793 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2797 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2798 TG3_GRC_LCLCTL_PWRSW_DELAY);
2799 } else {
2800 u32 no_gpio2;
2801 u32 grc_local_ctrl = 0;
2802
2803 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002804 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002805 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2806 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2807 grc_local_ctrl,
2808 TG3_GRC_LCLCTL_PWRSW_DELAY);
2809 }
2810
2811 /* On 5753 and variants, GPIO2 cannot be used. */
2812 no_gpio2 = tp->nic_sram_data_cfg &
2813 NIC_SRAM_DATA_CFG_NO_GPIO2;
2814
2815 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2816 GRC_LCLCTRL_GPIO_OE1 |
2817 GRC_LCLCTRL_GPIO_OE2 |
2818 GRC_LCLCTRL_GPIO_OUTPUT1 |
2819 GRC_LCLCTRL_GPIO_OUTPUT2;
2820 if (no_gpio2) {
2821 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2822 GRC_LCLCTRL_GPIO_OUTPUT2);
2823 }
2824 tw32_wait_f(GRC_LOCAL_CTRL,
2825 tp->grc_local_ctrl | grc_local_ctrl,
2826 TG3_GRC_LCLCTL_PWRSW_DELAY);
2827
2828 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2829
2830 tw32_wait_f(GRC_LOCAL_CTRL,
2831 tp->grc_local_ctrl | grc_local_ctrl,
2832 TG3_GRC_LCLCTL_PWRSW_DELAY);
2833
2834 if (!no_gpio2) {
2835 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2836 tw32_wait_f(GRC_LOCAL_CTRL,
2837 tp->grc_local_ctrl | grc_local_ctrl,
2838 TG3_GRC_LCLCTL_PWRSW_DELAY);
2839 }
2840 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002841}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002842
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002843static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002844{
2845 u32 msg = 0;
2846
2847 /* Serialize power state transitions */
2848 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2849 return;
2850
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002851 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002852 msg = TG3_GPIO_MSG_NEED_VAUX;
2853
2854 msg = tg3_set_function_status(tp, msg);
2855
2856 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2857 goto done;
2858
2859 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2860 tg3_pwrsrc_switch_to_vaux(tp);
2861 else
2862 tg3_pwrsrc_die_with_vmain(tp);
2863
2864done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002865 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002866}
2867
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002868static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Matt Carlson683644b2011-03-09 16:58:23 +00002870 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Matt Carlson334355a2010-01-20 16:58:10 +00002872 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002873 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return;
2875
Joe Perches41535772013-02-16 11:20:04 +00002876 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2877 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2878 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002879 tg3_frob_aux_power_5717(tp, include_wol ?
2880 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002881 return;
2882 }
2883
2884 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002885 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002887 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002888
Michael Chanbc1c7562006-03-20 17:48:03 -08002889 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002890 if (dev_peer) {
2891 struct tg3 *tp_peer = netdev_priv(dev_peer);
2892
Joe Perches63c3a662011-04-26 08:12:10 +00002893 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002894 return;
2895
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002896 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002897 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002898 need_vaux = true;
2899 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002902 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2903 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002904 need_vaux = true;
2905
Matt Carlson520b2752011-06-13 13:39:02 +00002906 if (need_vaux)
2907 tg3_pwrsrc_switch_to_vaux(tp);
2908 else
2909 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002912static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2913{
2914 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2915 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002916 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002917 if (speed != SPEED_10)
2918 return 1;
2919 } else if (speed == SPEED_10)
2920 return 1;
2921
2922 return 0;
2923}
2924
Matt Carlson0a459aa2008-11-03 16:54:15 -08002925static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002926{
Matt Carlsonce057f02007-11-12 21:08:03 -08002927 u32 val;
2928
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002929 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00002930 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08002931 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2932 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2933
2934 sg_dig_ctrl |=
2935 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2936 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2937 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2938 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002939 return;
Michael Chan51297242007-02-13 12:17:57 -08002940 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002941
Joe Perches41535772013-02-16 11:20:04 +00002942 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002943 tg3_bmcr_reset(tp);
2944 val = tr32(GRC_MISC_CFG);
2945 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2946 udelay(40);
2947 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002948 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002949 u32 phytest;
2950 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2951 u32 phy;
2952
2953 tg3_writephy(tp, MII_ADVERTISE, 0);
2954 tg3_writephy(tp, MII_BMCR,
2955 BMCR_ANENABLE | BMCR_ANRESTART);
2956
2957 tg3_writephy(tp, MII_TG3_FET_TEST,
2958 phytest | MII_TG3_FET_SHADOW_EN);
2959 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2960 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2961 tg3_writephy(tp,
2962 MII_TG3_FET_SHDW_AUXMODE4,
2963 phy);
2964 }
2965 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2966 }
2967 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002968 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002969 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2970 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002971
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002972 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2973 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2974 MII_TG3_AUXCTL_PCTL_VREG_11V;
2975 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002976 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002977
Michael Chan15c3b692006-03-22 01:06:52 -08002978 /* The PHY should not be powered down on some chips because
2979 * of bugs.
2980 */
Joe Perches41535772013-02-16 11:20:04 +00002981 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2982 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2983 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002984 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00002985 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002986 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002987 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002988
Joe Perches41535772013-02-16 11:20:04 +00002989 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2990 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002991 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2992 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2993 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2994 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2995 }
2996
Michael Chan15c3b692006-03-22 01:06:52 -08002997 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2998}
2999
Matt Carlson3f007892008-11-03 16:51:36 -08003000/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003001static int tg3_nvram_lock(struct tg3 *tp)
3002{
Joe Perches63c3a662011-04-26 08:12:10 +00003003 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003004 int i;
3005
3006 if (tp->nvram_lock_cnt == 0) {
3007 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3008 for (i = 0; i < 8000; i++) {
3009 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3010 break;
3011 udelay(20);
3012 }
3013 if (i == 8000) {
3014 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3015 return -ENODEV;
3016 }
3017 }
3018 tp->nvram_lock_cnt++;
3019 }
3020 return 0;
3021}
3022
3023/* tp->lock is held. */
3024static void tg3_nvram_unlock(struct tg3 *tp)
3025{
Joe Perches63c3a662011-04-26 08:12:10 +00003026 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003027 if (tp->nvram_lock_cnt > 0)
3028 tp->nvram_lock_cnt--;
3029 if (tp->nvram_lock_cnt == 0)
3030 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3031 }
3032}
3033
3034/* tp->lock is held. */
3035static void tg3_enable_nvram_access(struct tg3 *tp)
3036{
Joe Perches63c3a662011-04-26 08:12:10 +00003037 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003038 u32 nvaccess = tr32(NVRAM_ACCESS);
3039
3040 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3041 }
3042}
3043
3044/* tp->lock is held. */
3045static void tg3_disable_nvram_access(struct tg3 *tp)
3046{
Joe Perches63c3a662011-04-26 08:12:10 +00003047 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003048 u32 nvaccess = tr32(NVRAM_ACCESS);
3049
3050 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3051 }
3052}
3053
3054static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3055 u32 offset, u32 *val)
3056{
3057 u32 tmp;
3058 int i;
3059
3060 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3061 return -EINVAL;
3062
3063 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3064 EEPROM_ADDR_DEVID_MASK |
3065 EEPROM_ADDR_READ);
3066 tw32(GRC_EEPROM_ADDR,
3067 tmp |
3068 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3069 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3070 EEPROM_ADDR_ADDR_MASK) |
3071 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3072
3073 for (i = 0; i < 1000; i++) {
3074 tmp = tr32(GRC_EEPROM_ADDR);
3075
3076 if (tmp & EEPROM_ADDR_COMPLETE)
3077 break;
3078 msleep(1);
3079 }
3080 if (!(tmp & EEPROM_ADDR_COMPLETE))
3081 return -EBUSY;
3082
Matt Carlson62cedd12009-04-20 14:52:29 -07003083 tmp = tr32(GRC_EEPROM_DATA);
3084
3085 /*
3086 * The data will always be opposite the native endian
3087 * format. Perform a blind byteswap to compensate.
3088 */
3089 *val = swab32(tmp);
3090
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003091 return 0;
3092}
3093
3094#define NVRAM_CMD_TIMEOUT 10000
3095
3096static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3097{
3098 int i;
3099
3100 tw32(NVRAM_CMD, nvram_cmd);
3101 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3102 udelay(10);
3103 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3104 udelay(10);
3105 break;
3106 }
3107 }
3108
3109 if (i == NVRAM_CMD_TIMEOUT)
3110 return -EBUSY;
3111
3112 return 0;
3113}
3114
3115static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3116{
Joe Perches63c3a662011-04-26 08:12:10 +00003117 if (tg3_flag(tp, NVRAM) &&
3118 tg3_flag(tp, NVRAM_BUFFERED) &&
3119 tg3_flag(tp, FLASH) &&
3120 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003121 (tp->nvram_jedecnum == JEDEC_ATMEL))
3122
3123 addr = ((addr / tp->nvram_pagesize) <<
3124 ATMEL_AT45DB0X1B_PAGE_POS) +
3125 (addr % tp->nvram_pagesize);
3126
3127 return addr;
3128}
3129
3130static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3131{
Joe Perches63c3a662011-04-26 08:12:10 +00003132 if (tg3_flag(tp, NVRAM) &&
3133 tg3_flag(tp, NVRAM_BUFFERED) &&
3134 tg3_flag(tp, FLASH) &&
3135 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003136 (tp->nvram_jedecnum == JEDEC_ATMEL))
3137
3138 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3139 tp->nvram_pagesize) +
3140 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3141
3142 return addr;
3143}
3144
Matt Carlsone4f34112009-02-25 14:25:00 +00003145/* NOTE: Data read in from NVRAM is byteswapped according to
3146 * the byteswapping settings for all other register accesses.
3147 * tg3 devices are BE devices, so on a BE machine, the data
3148 * returned will be exactly as it is seen in NVRAM. On a LE
3149 * machine, the 32-bit value will be byteswapped.
3150 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003151static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3152{
3153 int ret;
3154
Joe Perches63c3a662011-04-26 08:12:10 +00003155 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003156 return tg3_nvram_read_using_eeprom(tp, offset, val);
3157
3158 offset = tg3_nvram_phys_addr(tp, offset);
3159
3160 if (offset > NVRAM_ADDR_MSK)
3161 return -EINVAL;
3162
3163 ret = tg3_nvram_lock(tp);
3164 if (ret)
3165 return ret;
3166
3167 tg3_enable_nvram_access(tp);
3168
3169 tw32(NVRAM_ADDR, offset);
3170 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3171 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3172
3173 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003174 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003175
3176 tg3_disable_nvram_access(tp);
3177
3178 tg3_nvram_unlock(tp);
3179
3180 return ret;
3181}
3182
Matt Carlsona9dc5292009-02-25 14:25:30 +00003183/* Ensures NVRAM data is in bytestream format. */
3184static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003185{
3186 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003187 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003188 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003189 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003190 return res;
3191}
3192
Matt Carlsondbe9b922012-02-13 10:20:09 +00003193static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3194 u32 offset, u32 len, u8 *buf)
3195{
3196 int i, j, rc = 0;
3197 u32 val;
3198
3199 for (i = 0; i < len; i += 4) {
3200 u32 addr;
3201 __be32 data;
3202
3203 addr = offset + i;
3204
3205 memcpy(&data, buf + i, 4);
3206
3207 /*
3208 * The SEEPROM interface expects the data to always be opposite
3209 * the native endian format. We accomplish this by reversing
3210 * all the operations that would have been performed on the
3211 * data from a call to tg3_nvram_read_be32().
3212 */
3213 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3214
3215 val = tr32(GRC_EEPROM_ADDR);
3216 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3217
3218 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3219 EEPROM_ADDR_READ);
3220 tw32(GRC_EEPROM_ADDR, val |
3221 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3222 (addr & EEPROM_ADDR_ADDR_MASK) |
3223 EEPROM_ADDR_START |
3224 EEPROM_ADDR_WRITE);
3225
3226 for (j = 0; j < 1000; j++) {
3227 val = tr32(GRC_EEPROM_ADDR);
3228
3229 if (val & EEPROM_ADDR_COMPLETE)
3230 break;
3231 msleep(1);
3232 }
3233 if (!(val & EEPROM_ADDR_COMPLETE)) {
3234 rc = -EBUSY;
3235 break;
3236 }
3237 }
3238
3239 return rc;
3240}
3241
3242/* offset and length are dword aligned */
3243static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3244 u8 *buf)
3245{
3246 int ret = 0;
3247 u32 pagesize = tp->nvram_pagesize;
3248 u32 pagemask = pagesize - 1;
3249 u32 nvram_cmd;
3250 u8 *tmp;
3251
3252 tmp = kmalloc(pagesize, GFP_KERNEL);
3253 if (tmp == NULL)
3254 return -ENOMEM;
3255
3256 while (len) {
3257 int j;
3258 u32 phy_addr, page_off, size;
3259
3260 phy_addr = offset & ~pagemask;
3261
3262 for (j = 0; j < pagesize; j += 4) {
3263 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3264 (__be32 *) (tmp + j));
3265 if (ret)
3266 break;
3267 }
3268 if (ret)
3269 break;
3270
3271 page_off = offset & pagemask;
3272 size = pagesize;
3273 if (len < size)
3274 size = len;
3275
3276 len -= size;
3277
3278 memcpy(tmp + page_off, buf, size);
3279
3280 offset = offset + (pagesize - page_off);
3281
3282 tg3_enable_nvram_access(tp);
3283
3284 /*
3285 * Before we can erase the flash page, we need
3286 * to issue a special "write enable" command.
3287 */
3288 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3289
3290 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3291 break;
3292
3293 /* Erase the target page */
3294 tw32(NVRAM_ADDR, phy_addr);
3295
3296 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3297 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3298
3299 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3300 break;
3301
3302 /* Issue another write enable to start the write. */
3303 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3304
3305 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3306 break;
3307
3308 for (j = 0; j < pagesize; j += 4) {
3309 __be32 data;
3310
3311 data = *((__be32 *) (tmp + j));
3312
3313 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3314
3315 tw32(NVRAM_ADDR, phy_addr + j);
3316
3317 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3318 NVRAM_CMD_WR;
3319
3320 if (j == 0)
3321 nvram_cmd |= NVRAM_CMD_FIRST;
3322 else if (j == (pagesize - 4))
3323 nvram_cmd |= NVRAM_CMD_LAST;
3324
3325 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3326 if (ret)
3327 break;
3328 }
3329 if (ret)
3330 break;
3331 }
3332
3333 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3334 tg3_nvram_exec_cmd(tp, nvram_cmd);
3335
3336 kfree(tmp);
3337
3338 return ret;
3339}
3340
3341/* offset and length are dword aligned */
3342static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3343 u8 *buf)
3344{
3345 int i, ret = 0;
3346
3347 for (i = 0; i < len; i += 4, offset += 4) {
3348 u32 page_off, phy_addr, nvram_cmd;
3349 __be32 data;
3350
3351 memcpy(&data, buf + i, 4);
3352 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3353
3354 page_off = offset % tp->nvram_pagesize;
3355
3356 phy_addr = tg3_nvram_phys_addr(tp, offset);
3357
Matt Carlsondbe9b922012-02-13 10:20:09 +00003358 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3359
3360 if (page_off == 0 || i == 0)
3361 nvram_cmd |= NVRAM_CMD_FIRST;
3362 if (page_off == (tp->nvram_pagesize - 4))
3363 nvram_cmd |= NVRAM_CMD_LAST;
3364
3365 if (i == (len - 4))
3366 nvram_cmd |= NVRAM_CMD_LAST;
3367
Matt Carlson42278222012-02-13 15:20:11 +00003368 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3369 !tg3_flag(tp, FLASH) ||
3370 !tg3_flag(tp, 57765_PLUS))
3371 tw32(NVRAM_ADDR, phy_addr);
3372
Joe Perches41535772013-02-16 11:20:04 +00003373 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003374 !tg3_flag(tp, 5755_PLUS) &&
3375 (tp->nvram_jedecnum == JEDEC_ST) &&
3376 (nvram_cmd & NVRAM_CMD_FIRST)) {
3377 u32 cmd;
3378
3379 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3380 ret = tg3_nvram_exec_cmd(tp, cmd);
3381 if (ret)
3382 break;
3383 }
3384 if (!tg3_flag(tp, FLASH)) {
3385 /* We always do complete word writes to eeprom. */
3386 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3387 }
3388
3389 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3390 if (ret)
3391 break;
3392 }
3393 return ret;
3394}
3395
3396/* offset and length are dword aligned */
3397static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3398{
3399 int ret;
3400
3401 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3402 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3403 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3404 udelay(40);
3405 }
3406
3407 if (!tg3_flag(tp, NVRAM)) {
3408 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3409 } else {
3410 u32 grc_mode;
3411
3412 ret = tg3_nvram_lock(tp);
3413 if (ret)
3414 return ret;
3415
3416 tg3_enable_nvram_access(tp);
3417 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3418 tw32(NVRAM_WRITE1, 0x406);
3419
3420 grc_mode = tr32(GRC_MODE);
3421 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3422
3423 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3424 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3425 buf);
3426 } else {
3427 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3428 buf);
3429 }
3430
3431 grc_mode = tr32(GRC_MODE);
3432 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3433
3434 tg3_disable_nvram_access(tp);
3435 tg3_nvram_unlock(tp);
3436 }
3437
3438 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3439 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3440 udelay(40);
3441 }
3442
3443 return ret;
3444}
3445
Matt Carlson997b4f12011-08-31 11:44:53 +00003446#define RX_CPU_SCRATCH_BASE 0x30000
3447#define RX_CPU_SCRATCH_SIZE 0x04000
3448#define TX_CPU_SCRATCH_BASE 0x34000
3449#define TX_CPU_SCRATCH_SIZE 0x04000
3450
3451/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003452static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003453{
3454 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003455 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003456
Nithin Sujir837c45b2013-03-06 17:02:30 +00003457 for (i = 0; i < iters; i++) {
3458 tw32(cpu_base + CPU_STATE, 0xffffffff);
3459 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3460 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3461 break;
3462 }
3463
3464 return (i == iters) ? -EBUSY : 0;
3465}
3466
3467/* tp->lock is held. */
3468static int tg3_rxcpu_pause(struct tg3 *tp)
3469{
3470 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3471
3472 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3473 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3474 udelay(10);
3475
3476 return rc;
3477}
3478
3479/* tp->lock is held. */
3480static int tg3_txcpu_pause(struct tg3 *tp)
3481{
3482 return tg3_pause_cpu(tp, TX_CPU_BASE);
3483}
3484
3485/* tp->lock is held. */
3486static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3487{
3488 tw32(cpu_base + CPU_STATE, 0xffffffff);
3489 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3490}
3491
3492/* tp->lock is held. */
3493static void tg3_rxcpu_resume(struct tg3 *tp)
3494{
3495 tg3_resume_cpu(tp, RX_CPU_BASE);
3496}
3497
3498/* tp->lock is held. */
3499static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3500{
3501 int rc;
3502
3503 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003504
Joe Perches41535772013-02-16 11:20:04 +00003505 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003506 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3507
3508 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3509 return 0;
3510 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003511 if (cpu_base == RX_CPU_BASE) {
3512 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003513 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003514 /*
3515 * There is only an Rx CPU for the 5750 derivative in the
3516 * BCM4785.
3517 */
3518 if (tg3_flag(tp, IS_SSB_CORE))
3519 return 0;
3520
Nithin Sujir837c45b2013-03-06 17:02:30 +00003521 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003522 }
3523
Nithin Sujir837c45b2013-03-06 17:02:30 +00003524 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003525 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003526 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003527 return -ENODEV;
3528 }
3529
3530 /* Clear firmware's nvram arbitration. */
3531 if (tg3_flag(tp, NVRAM))
3532 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3533 return 0;
3534}
3535
Nithin Sujir31f11a92013-03-06 17:02:33 +00003536static int tg3_fw_data_len(struct tg3 *tp,
3537 const struct tg3_firmware_hdr *fw_hdr)
3538{
3539 int fw_len;
3540
3541 /* Non fragmented firmware have one firmware header followed by a
3542 * contiguous chunk of data to be written. The length field in that
3543 * header is not the length of data to be written but the complete
3544 * length of the bss. The data length is determined based on
3545 * tp->fw->size minus headers.
3546 *
3547 * Fragmented firmware have a main header followed by multiple
3548 * fragments. Each fragment is identical to non fragmented firmware
3549 * with a firmware header followed by a contiguous chunk of data. In
3550 * the main header, the length field is unused and set to 0xffffffff.
3551 * In each fragment header the length is the entire size of that
3552 * fragment i.e. fragment data + header length. Data length is
3553 * therefore length field in the header minus TG3_FW_HDR_LEN.
3554 */
3555 if (tp->fw_len == 0xffffffff)
3556 fw_len = be32_to_cpu(fw_hdr->len);
3557 else
3558 fw_len = tp->fw->size;
3559
3560 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3561}
3562
Matt Carlson997b4f12011-08-31 11:44:53 +00003563/* tp->lock is held. */
3564static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3565 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003566 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003567{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003568 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003569 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003570 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003571
3572 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3573 netdev_err(tp->dev,
3574 "%s: Trying to load TX cpu firmware which is 5705\n",
3575 __func__);
3576 return -EINVAL;
3577 }
3578
Nithin Sujirc4dab502013-03-06 17:02:34 +00003579 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003580 write_op = tg3_write_mem;
3581 else
3582 write_op = tg3_write_indirect_reg32;
3583
Nithin Sujirc4dab502013-03-06 17:02:34 +00003584 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3585 /* It is possible that bootcode is still loading at this point.
3586 * Get the nvram lock first before halting the cpu.
3587 */
3588 int lock_err = tg3_nvram_lock(tp);
3589 err = tg3_halt_cpu(tp, cpu_base);
3590 if (!lock_err)
3591 tg3_nvram_unlock(tp);
3592 if (err)
3593 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003594
Nithin Sujirc4dab502013-03-06 17:02:34 +00003595 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3596 write_op(tp, cpu_scratch_base + i, 0);
3597 tw32(cpu_base + CPU_STATE, 0xffffffff);
3598 tw32(cpu_base + CPU_MODE,
3599 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3600 } else {
3601 /* Subtract additional main header for fragmented firmware and
3602 * advance to the first fragment
3603 */
3604 total_len -= TG3_FW_HDR_LEN;
3605 fw_hdr++;
3606 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003607
Nithin Sujir31f11a92013-03-06 17:02:33 +00003608 do {
3609 u32 *fw_data = (u32 *)(fw_hdr + 1);
3610 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3611 write_op(tp, cpu_scratch_base +
3612 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3613 (i * sizeof(u32)),
3614 be32_to_cpu(fw_data[i]));
3615
3616 total_len -= be32_to_cpu(fw_hdr->len);
3617
3618 /* Advance to next fragment */
3619 fw_hdr = (struct tg3_firmware_hdr *)
3620 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3621 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003622
3623 err = 0;
3624
3625out:
3626 return err;
3627}
3628
3629/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003630static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3631{
3632 int i;
3633 const int iters = 5;
3634
3635 tw32(cpu_base + CPU_STATE, 0xffffffff);
3636 tw32_f(cpu_base + CPU_PC, pc);
3637
3638 for (i = 0; i < iters; i++) {
3639 if (tr32(cpu_base + CPU_PC) == pc)
3640 break;
3641 tw32(cpu_base + CPU_STATE, 0xffffffff);
3642 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3643 tw32_f(cpu_base + CPU_PC, pc);
3644 udelay(1000);
3645 }
3646
3647 return (i == iters) ? -EBUSY : 0;
3648}
3649
3650/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003651static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3652{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003653 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003654 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003655
Nithin Sujir77997ea2013-03-06 17:02:32 +00003656 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003657
3658 /* Firmware blob starts with version numbers, followed by
3659 start address and length. We are setting complete length.
3660 length = end_address_of_bss - start_address_of_text.
3661 Remainder is the blob to be loaded contiguously
3662 from start address. */
3663
Matt Carlson997b4f12011-08-31 11:44:53 +00003664 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3665 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003666 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003667 if (err)
3668 return err;
3669
3670 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3671 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003672 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003673 if (err)
3674 return err;
3675
3676 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003677 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3678 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003679 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003680 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3681 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003682 tr32(RX_CPU_BASE + CPU_PC),
3683 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003684 return -ENODEV;
3685 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003686
3687 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003688
3689 return 0;
3690}
3691
Nithin Sujirc4dab502013-03-06 17:02:34 +00003692static int tg3_validate_rxcpu_state(struct tg3 *tp)
3693{
3694 const int iters = 1000;
3695 int i;
3696 u32 val;
3697
3698 /* Wait for boot code to complete initialization and enter service
3699 * loop. It is then safe to download service patches
3700 */
3701 for (i = 0; i < iters; i++) {
3702 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3703 break;
3704
3705 udelay(10);
3706 }
3707
3708 if (i == iters) {
3709 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3710 return -EBUSY;
3711 }
3712
3713 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3714 if (val & 0xff) {
3715 netdev_warn(tp->dev,
3716 "Other patches exist. Not downloading EEE patch\n");
3717 return -EEXIST;
3718 }
3719
3720 return 0;
3721}
3722
3723/* tp->lock is held. */
3724static void tg3_load_57766_firmware(struct tg3 *tp)
3725{
3726 struct tg3_firmware_hdr *fw_hdr;
3727
3728 if (!tg3_flag(tp, NO_NVRAM))
3729 return;
3730
3731 if (tg3_validate_rxcpu_state(tp))
3732 return;
3733
3734 if (!tp->fw)
3735 return;
3736
3737 /* This firmware blob has a different format than older firmware
3738 * releases as given below. The main difference is we have fragmented
3739 * data to be written to non-contiguous locations.
3740 *
3741 * In the beginning we have a firmware header identical to other
3742 * firmware which consists of version, base addr and length. The length
3743 * here is unused and set to 0xffffffff.
3744 *
3745 * This is followed by a series of firmware fragments which are
3746 * individually identical to previous firmware. i.e. they have the
3747 * firmware header and followed by data for that fragment. The version
3748 * field of the individual fragment header is unused.
3749 */
3750
3751 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3752 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3753 return;
3754
3755 if (tg3_rxcpu_pause(tp))
3756 return;
3757
3758 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3759 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3760
3761 tg3_rxcpu_resume(tp);
3762}
3763
Matt Carlson997b4f12011-08-31 11:44:53 +00003764/* tp->lock is held. */
3765static int tg3_load_tso_firmware(struct tg3 *tp)
3766{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003767 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003768 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003769 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003770
Matt Carlson1caf13e2013-03-06 17:02:29 +00003771 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003772 return 0;
3773
Nithin Sujir77997ea2013-03-06 17:02:32 +00003774 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003775
3776 /* Firmware blob starts with version numbers, followed by
3777 start address and length. We are setting complete length.
3778 length = end_address_of_bss - start_address_of_text.
3779 Remainder is the blob to be loaded contiguously
3780 from start address. */
3781
Matt Carlson997b4f12011-08-31 11:44:53 +00003782 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003783
Joe Perches41535772013-02-16 11:20:04 +00003784 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003785 cpu_base = RX_CPU_BASE;
3786 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3787 } else {
3788 cpu_base = TX_CPU_BASE;
3789 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3790 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3791 }
3792
3793 err = tg3_load_firmware_cpu(tp, cpu_base,
3794 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003795 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003796 if (err)
3797 return err;
3798
3799 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003800 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3801 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003802 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003803 netdev_err(tp->dev,
3804 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003805 __func__, tr32(cpu_base + CPU_PC),
3806 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003807 return -ENODEV;
3808 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003809
3810 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003811 return 0;
3812}
3813
3814
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003815/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003816static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3817{
3818 u32 addr_high, addr_low;
3819 int i;
3820
3821 addr_high = ((tp->dev->dev_addr[0] << 8) |
3822 tp->dev->dev_addr[1]);
3823 addr_low = ((tp->dev->dev_addr[2] << 24) |
3824 (tp->dev->dev_addr[3] << 16) |
3825 (tp->dev->dev_addr[4] << 8) |
3826 (tp->dev->dev_addr[5] << 0));
3827 for (i = 0; i < 4; i++) {
3828 if (i == 1 && skip_mac_1)
3829 continue;
3830 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3831 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3832 }
3833
Joe Perches41535772013-02-16 11:20:04 +00003834 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3835 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003836 for (i = 0; i < 12; i++) {
3837 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3838 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3839 }
3840 }
3841
3842 addr_high = (tp->dev->dev_addr[0] +
3843 tp->dev->dev_addr[1] +
3844 tp->dev->dev_addr[2] +
3845 tp->dev->dev_addr[3] +
3846 tp->dev->dev_addr[4] +
3847 tp->dev->dev_addr[5]) &
3848 TX_BACKOFF_SEED_MASK;
3849 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3850}
3851
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003852static void tg3_enable_register_access(struct tg3 *tp)
3853{
3854 /*
3855 * Make sure register accesses (indirect or otherwise) will function
3856 * correctly.
3857 */
3858 pci_write_config_dword(tp->pdev,
3859 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3860}
3861
3862static int tg3_power_up(struct tg3 *tp)
3863{
Matt Carlsonbed98292011-07-13 09:27:29 +00003864 int err;
3865
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003866 tg3_enable_register_access(tp);
3867
Matt Carlsonbed98292011-07-13 09:27:29 +00003868 err = pci_set_power_state(tp->pdev, PCI_D0);
3869 if (!err) {
3870 /* Switch out of Vaux if it is a NIC */
3871 tg3_pwrsrc_switch_to_vmain(tp);
3872 } else {
3873 netdev_err(tp->dev, "Transition to D0 failed\n");
3874 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003875
Matt Carlsonbed98292011-07-13 09:27:29 +00003876 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003877}
3878
Matt Carlson4b409522012-02-13 10:20:11 +00003879static int tg3_setup_phy(struct tg3 *, int);
3880
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003881static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
3883 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003884 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003886 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003887
3888 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003889 if (tg3_flag(tp, CLKREQ_BUG))
3890 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3891 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3894 tw32(TG3PCI_MISC_HOST_CTRL,
3895 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3896
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003897 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003898 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003899
Joe Perches63c3a662011-04-26 08:12:10 +00003900 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003901 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003902 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003903 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003904 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003905 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003906
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003907 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003908
Matt Carlson80096062010-08-02 11:26:06 +00003909 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003910
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003911 tp->link_config.speed = phydev->speed;
3912 tp->link_config.duplex = phydev->duplex;
3913 tp->link_config.autoneg = phydev->autoneg;
3914 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003915
3916 advertising = ADVERTISED_TP |
3917 ADVERTISED_Pause |
3918 ADVERTISED_Autoneg |
3919 ADVERTISED_10baseT_Half;
3920
Joe Perches63c3a662011-04-26 08:12:10 +00003921 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3922 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003923 advertising |=
3924 ADVERTISED_100baseT_Half |
3925 ADVERTISED_100baseT_Full |
3926 ADVERTISED_10baseT_Full;
3927 else
3928 advertising |= ADVERTISED_10baseT_Full;
3929 }
3930
3931 phydev->advertising = advertising;
3932
3933 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003934
3935 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003936 if (phyid != PHY_ID_BCMAC131) {
3937 phyid &= PHY_BCM_OUI_MASK;
3938 if (phyid == PHY_BCM_OUI_1 ||
3939 phyid == PHY_BCM_OUI_2 ||
3940 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003941 do_low_power = true;
3942 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003943 }
Matt Carlsondd477002008-05-25 23:45:58 -07003944 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003945 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003946
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003947 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003948 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Matt Carlson2855b9f2012-02-13 15:20:14 +00003950 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003951 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
3953
Joe Perches41535772013-02-16 11:20:04 +00003954 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003955 u32 val;
3956
3957 val = tr32(GRC_VCPU_EXT_CTRL);
3958 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003959 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003960 int i;
3961 u32 val;
3962
3963 for (i = 0; i < 200; i++) {
3964 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3965 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3966 break;
3967 msleep(1);
3968 }
3969 }
Joe Perches63c3a662011-04-26 08:12:10 +00003970 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003971 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3972 WOL_DRV_STATE_SHUTDOWN |
3973 WOL_DRV_WOL |
3974 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003975
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003976 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 u32 mac_mode;
3978
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003979 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003980 if (do_low_power &&
3981 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3982 tg3_phy_auxctl_write(tp,
3983 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3984 MII_TG3_AUXCTL_PCTL_WOL_EN |
3985 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3986 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003987 udelay(40);
3988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003990 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003991 mac_mode = MAC_MODE_PORT_MODE_GMII;
3992 else
3993 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003995 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00003996 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003997 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003998 SPEED_100 : SPEED_10;
3999 if (tg3_5700_link_polarity(tp, speed))
4000 mac_mode |= MAC_MODE_LINK_POLARITY;
4001 else
4002 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 } else {
4005 mac_mode = MAC_MODE_PORT_MODE_TBI;
4006 }
4007
Joe Perches63c3a662011-04-26 08:12:10 +00004008 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 tw32(MAC_LED_CTRL, tp->led_ctrl);
4010
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004011 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004012 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4013 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004014 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Joe Perches63c3a662011-04-26 08:12:10 +00004016 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004017 mac_mode |= MAC_MODE_APE_TX_EN |
4018 MAC_MODE_APE_RX_EN |
4019 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 tw32_f(MAC_MODE, mac_mode);
4022 udelay(100);
4023
4024 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4025 udelay(10);
4026 }
4027
Joe Perches63c3a662011-04-26 08:12:10 +00004028 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004029 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4030 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 u32 base_val;
4032
4033 base_val = tp->pci_clock_ctrl;
4034 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4035 CLOCK_CTRL_TXCLK_DISABLE);
4036
Michael Chanb401e9e2005-12-19 16:27:04 -08004037 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4038 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004039 } else if (tg3_flag(tp, 5780_CLASS) ||
4040 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004041 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004042 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004043 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 u32 newbits1, newbits2;
4045
Joe Perches41535772013-02-16 11:20:04 +00004046 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4047 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4049 CLOCK_CTRL_TXCLK_DISABLE |
4050 CLOCK_CTRL_ALTCLK);
4051 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004052 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 newbits1 = CLOCK_CTRL_625_CORE;
4054 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4055 } else {
4056 newbits1 = CLOCK_CTRL_ALTCLK;
4057 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4058 }
4059
Michael Chanb401e9e2005-12-19 16:27:04 -08004060 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4061 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Michael Chanb401e9e2005-12-19 16:27:04 -08004063 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4064 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Joe Perches63c3a662011-04-26 08:12:10 +00004066 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 u32 newbits3;
4068
Joe Perches41535772013-02-16 11:20:04 +00004069 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4070 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4072 CLOCK_CTRL_TXCLK_DISABLE |
4073 CLOCK_CTRL_44MHZ_CORE);
4074 } else {
4075 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4076 }
4077
Michael Chanb401e9e2005-12-19 16:27:04 -08004078 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4079 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
4081 }
4082
Joe Perches63c3a662011-04-26 08:12:10 +00004083 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004084 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004085
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004086 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
4088 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004089 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004090 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4091 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 u32 val = tr32(0x7d00);
4093
4094 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4095 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004096 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004097 int err;
4098
4099 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004101 if (!err)
4102 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
4105
Michael Chanbbadf502006-04-06 21:46:34 -07004106 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return 0;
4109}
4110
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004111static void tg3_power_down(struct tg3 *tp)
4112{
4113 tg3_power_down_prepare(tp);
4114
Joe Perches63c3a662011-04-26 08:12:10 +00004115 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004116 pci_set_power_state(tp->pdev, PCI_D3hot);
4117}
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4120{
4121 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4122 case MII_TG3_AUX_STAT_10HALF:
4123 *speed = SPEED_10;
4124 *duplex = DUPLEX_HALF;
4125 break;
4126
4127 case MII_TG3_AUX_STAT_10FULL:
4128 *speed = SPEED_10;
4129 *duplex = DUPLEX_FULL;
4130 break;
4131
4132 case MII_TG3_AUX_STAT_100HALF:
4133 *speed = SPEED_100;
4134 *duplex = DUPLEX_HALF;
4135 break;
4136
4137 case MII_TG3_AUX_STAT_100FULL:
4138 *speed = SPEED_100;
4139 *duplex = DUPLEX_FULL;
4140 break;
4141
4142 case MII_TG3_AUX_STAT_1000HALF:
4143 *speed = SPEED_1000;
4144 *duplex = DUPLEX_HALF;
4145 break;
4146
4147 case MII_TG3_AUX_STAT_1000FULL:
4148 *speed = SPEED_1000;
4149 *duplex = DUPLEX_FULL;
4150 break;
4151
4152 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004153 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004154 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4155 SPEED_10;
4156 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4157 DUPLEX_HALF;
4158 break;
4159 }
Matt Carlsone7405222012-02-13 15:20:16 +00004160 *speed = SPEED_UNKNOWN;
4161 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Matt Carlson42b64a42011-05-19 12:12:49 +00004166static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
Matt Carlson42b64a42011-05-19 12:12:49 +00004168 int err = 0;
4169 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Matt Carlson42b64a42011-05-19 12:12:49 +00004171 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004172 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004173 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Matt Carlson42b64a42011-05-19 12:12:49 +00004175 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4176 if (err)
4177 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Matt Carlson4f272092011-12-14 11:09:57 +00004179 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4180 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004181
Joe Perches41535772013-02-16 11:20:04 +00004182 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4183 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004184 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004185
Matt Carlson4f272092011-12-14 11:09:57 +00004186 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4187 if (err)
4188 goto done;
4189 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004190
Matt Carlson42b64a42011-05-19 12:12:49 +00004191 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4192 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Matt Carlson42b64a42011-05-19 12:12:49 +00004194 tw32(TG3_CPMU_EEE_MODE,
4195 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004196
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004197 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004198 if (!err) {
4199 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004200
Matt Carlsona6b68da2010-12-06 08:28:52 +00004201 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004202 /* Advertise 100-BaseTX EEE ability */
4203 if (advertise & ADVERTISED_100baseT_Full)
4204 val |= MDIO_AN_EEE_ADV_100TX;
4205 /* Advertise 1000-BaseT EEE ability */
4206 if (advertise & ADVERTISED_1000baseT_Full)
4207 val |= MDIO_AN_EEE_ADV_1000T;
4208 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004209 if (err)
4210 val = 0;
4211
Joe Perches41535772013-02-16 11:20:04 +00004212 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004213 case ASIC_REV_5717:
4214 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004215 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004216 case ASIC_REV_5719:
4217 /* If we advertised any eee advertisements above... */
4218 if (val)
4219 val = MII_TG3_DSP_TAP26_ALNOKO |
4220 MII_TG3_DSP_TAP26_RMRXSTO |
4221 MII_TG3_DSP_TAP26_OPCSINPT;
4222 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4223 /* Fall through */
4224 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004225 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004226 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4227 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4228 MII_TG3_DSP_CH34TP2_HIBW01);
4229 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004230
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004231 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004232 if (!err)
4233 err = err2;
4234 }
4235
4236done:
4237 return err;
4238}
4239
4240static void tg3_phy_copper_begin(struct tg3 *tp)
4241{
Matt Carlsond13ba512012-02-22 12:35:19 +00004242 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4243 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4244 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004245
Matt Carlsond13ba512012-02-22 12:35:19 +00004246 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4247 adv = ADVERTISED_10baseT_Half |
4248 ADVERTISED_10baseT_Full;
4249 if (tg3_flag(tp, WOL_SPEED_100MB))
4250 adv |= ADVERTISED_100baseT_Half |
4251 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004252
Matt Carlsond13ba512012-02-22 12:35:19 +00004253 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004254 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004255 adv = tp->link_config.advertising;
4256 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4257 adv &= ~(ADVERTISED_1000baseT_Half |
4258 ADVERTISED_1000baseT_Full);
4259
4260 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004261 }
4262
Matt Carlsond13ba512012-02-22 12:35:19 +00004263 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004264
Matt Carlsond13ba512012-02-22 12:35:19 +00004265 tg3_writephy(tp, MII_BMCR,
4266 BMCR_ANENABLE | BMCR_ANRESTART);
4267 } else {
4268 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 u32 bmcr, orig_bmcr;
4270
4271 tp->link_config.active_speed = tp->link_config.speed;
4272 tp->link_config.active_duplex = tp->link_config.duplex;
4273
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004274 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4275 /* With autoneg disabled, 5715 only links up when the
4276 * advertisement register has the configured speed
4277 * enabled.
4278 */
4279 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4280 }
4281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 bmcr = 0;
4283 switch (tp->link_config.speed) {
4284 default:
4285 case SPEED_10:
4286 break;
4287
4288 case SPEED_100:
4289 bmcr |= BMCR_SPEED100;
4290 break;
4291
4292 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004293 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 if (tp->link_config.duplex == DUPLEX_FULL)
4298 bmcr |= BMCR_FULLDPLX;
4299
4300 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4301 (bmcr != orig_bmcr)) {
4302 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4303 for (i = 0; i < 1500; i++) {
4304 u32 tmp;
4305
4306 udelay(10);
4307 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4308 tg3_readphy(tp, MII_BMSR, &tmp))
4309 continue;
4310 if (!(tmp & BMSR_LSTATUS)) {
4311 udelay(40);
4312 break;
4313 }
4314 }
4315 tg3_writephy(tp, MII_BMCR, bmcr);
4316 udelay(40);
4317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 }
4319}
4320
4321static int tg3_init_5401phy_dsp(struct tg3 *tp)
4322{
4323 int err;
4324
4325 /* Turn off tap power management. */
4326 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004327 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004329 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4330 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4331 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4332 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4333 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 udelay(40);
4336
4337 return err;
4338}
4339
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004340static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004342 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004343
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004344 advertising = tp->link_config.advertising;
4345 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004347 advmsk = ADVERTISE_ALL;
4348 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004349 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004350 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004353 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4354 return false;
4355
4356 if ((*lcladv & advmsk) != tgtadv)
4357 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004358
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004359 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 u32 tg3_ctrl;
4361
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004362 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004363
Matt Carlson221c5632011-06-13 13:39:01 +00004364 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004365 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Matt Carlson3198e072012-02-13 15:20:10 +00004367 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004368 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4369 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004370 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4371 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4372 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4373 } else {
4374 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4375 }
4376
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004377 if (tg3_ctrl != tgtadv)
4378 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004380
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004381 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004382}
4383
Matt Carlson859edb22011-12-08 14:40:16 +00004384static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4385{
4386 u32 lpeth = 0;
4387
4388 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4389 u32 val;
4390
4391 if (tg3_readphy(tp, MII_STAT1000, &val))
4392 return false;
4393
4394 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4395 }
4396
4397 if (tg3_readphy(tp, MII_LPA, rmtadv))
4398 return false;
4399
4400 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4401 tp->link_config.rmt_adv = lpeth;
4402
4403 return true;
4404}
4405
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004406static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4407{
4408 if (curr_link_up != tp->link_up) {
4409 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004410 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004411 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004412 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004413 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4414 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4415 }
4416
4417 tg3_link_report(tp);
4418 return true;
4419 }
4420
4421 return false;
4422}
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4425{
4426 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004427 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004428 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 u16 current_speed;
4430 u8 current_duplex;
4431 int i, err;
4432
4433 tw32(MAC_EVENT, 0);
4434
4435 tw32_f(MAC_STATUS,
4436 (MAC_STATUS_SYNC_CHANGED |
4437 MAC_STATUS_CFG_CHANGED |
4438 MAC_STATUS_MI_COMPLETION |
4439 MAC_STATUS_LNKSTATE_CHANGED));
4440 udelay(40);
4441
Matt Carlson8ef21422008-05-02 16:47:53 -07004442 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4443 tw32_f(MAC_MI_MODE,
4444 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4445 udelay(80);
4446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004448 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450 /* Some third-party PHYs need to be reset on link going
4451 * down.
4452 */
Joe Perches41535772013-02-16 11:20:04 +00004453 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4454 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4455 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004456 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 tg3_readphy(tp, MII_BMSR, &bmsr);
4458 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4459 !(bmsr & BMSR_LSTATUS))
4460 force_reset = 1;
4461 }
4462 if (force_reset)
4463 tg3_phy_reset(tp);
4464
Matt Carlson79eb6902010-02-17 15:17:03 +00004465 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 tg3_readphy(tp, MII_BMSR, &bmsr);
4467 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004468 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 bmsr = 0;
4470
4471 if (!(bmsr & BMSR_LSTATUS)) {
4472 err = tg3_init_5401phy_dsp(tp);
4473 if (err)
4474 return err;
4475
4476 tg3_readphy(tp, MII_BMSR, &bmsr);
4477 for (i = 0; i < 1000; i++) {
4478 udelay(10);
4479 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4480 (bmsr & BMSR_LSTATUS)) {
4481 udelay(40);
4482 break;
4483 }
4484 }
4485
Matt Carlson79eb6902010-02-17 15:17:03 +00004486 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4487 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 !(bmsr & BMSR_LSTATUS) &&
4489 tp->link_config.active_speed == SPEED_1000) {
4490 err = tg3_phy_reset(tp);
4491 if (!err)
4492 err = tg3_init_5401phy_dsp(tp);
4493 if (err)
4494 return err;
4495 }
4496 }
Joe Perches41535772013-02-16 11:20:04 +00004497 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4498 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 /* 5701 {A0,B0} CRC bug workaround */
4500 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004501 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4502 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4503 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
4505
4506 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004507 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4508 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004510 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004512 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4514
Joe Perches41535772013-02-16 11:20:04 +00004515 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4516 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4518 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4519 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4520 else
4521 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4522 }
4523
4524 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004525 current_speed = SPEED_UNKNOWN;
4526 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004527 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004528 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004530 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004531 err = tg3_phy_auxctl_read(tp,
4532 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4533 &val);
4534 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004535 tg3_phy_auxctl_write(tp,
4536 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4537 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 goto relink;
4539 }
4540 }
4541
4542 bmsr = 0;
4543 for (i = 0; i < 100; i++) {
4544 tg3_readphy(tp, MII_BMSR, &bmsr);
4545 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4546 (bmsr & BMSR_LSTATUS))
4547 break;
4548 udelay(40);
4549 }
4550
4551 if (bmsr & BMSR_LSTATUS) {
4552 u32 aux_stat, bmcr;
4553
4554 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4555 for (i = 0; i < 2000; i++) {
4556 udelay(10);
4557 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4558 aux_stat)
4559 break;
4560 }
4561
4562 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4563 &current_speed,
4564 &current_duplex);
4565
4566 bmcr = 0;
4567 for (i = 0; i < 200; i++) {
4568 tg3_readphy(tp, MII_BMCR, &bmcr);
4569 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4570 continue;
4571 if (bmcr && bmcr != 0x7fff)
4572 break;
4573 udelay(10);
4574 }
4575
Matt Carlsonef167e22007-12-20 20:10:01 -08004576 lcl_adv = 0;
4577 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Matt Carlsonef167e22007-12-20 20:10:01 -08004579 tp->link_config.active_speed = current_speed;
4580 tp->link_config.active_duplex = current_duplex;
4581
4582 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4583 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004584 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004585 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004586 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 } else {
4588 if (!(bmcr & BMCR_ANENABLE) &&
4589 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004590 tp->link_config.duplex == current_duplex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
4593 }
4594
Matt Carlsonef167e22007-12-20 20:10:01 -08004595 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004596 tp->link_config.active_duplex == DUPLEX_FULL) {
4597 u32 reg, bit;
4598
4599 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4600 reg = MII_TG3_FET_GEN_STAT;
4601 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4602 } else {
4603 reg = MII_TG3_EXT_STAT;
4604 bit = MII_TG3_EXT_STAT_MDIX;
4605 }
4606
4607 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4608 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4609
Matt Carlsonef167e22007-12-20 20:10:01 -08004610 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614relink:
Matt Carlson80096062010-08-02 11:26:06 +00004615 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 tg3_phy_copper_begin(tp);
4617
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004618 if (tg3_flag(tp, ROBOSWITCH)) {
4619 current_link_up = 1;
4620 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4621 current_speed = SPEED_1000;
4622 current_duplex = DUPLEX_FULL;
4623 tp->link_config.active_speed = current_speed;
4624 tp->link_config.active_duplex = current_duplex;
4625 }
4626
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004627 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004628 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4629 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 current_link_up = 1;
4631 }
4632
4633 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4634 if (current_link_up == 1) {
4635 if (tp->link_config.active_speed == SPEED_100 ||
4636 tp->link_config.active_speed == SPEED_10)
4637 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4638 else
4639 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004640 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004641 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4642 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4644
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004645 /* In order for the 5750 core in BCM4785 chip to work properly
4646 * in RGMII mode, the Led Control Register must be set up.
4647 */
4648 if (tg3_flag(tp, RGMII_MODE)) {
4649 u32 led_ctrl = tr32(MAC_LED_CTRL);
4650 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4651
4652 if (tp->link_config.active_speed == SPEED_10)
4653 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4654 else if (tp->link_config.active_speed == SPEED_100)
4655 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4656 LED_CTRL_100MBPS_ON);
4657 else if (tp->link_config.active_speed == SPEED_1000)
4658 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4659 LED_CTRL_1000MBPS_ON);
4660
4661 tw32(MAC_LED_CTRL, led_ctrl);
4662 udelay(40);
4663 }
4664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4666 if (tp->link_config.active_duplex == DUPLEX_HALF)
4667 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4668
Joe Perches41535772013-02-16 11:20:04 +00004669 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004670 if (current_link_up == 1 &&
4671 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004673 else
4674 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 }
4676
4677 /* ??? Without this setting Netgear GA302T PHY does not
4678 * ??? send/receive packets...
4679 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004680 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004681 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4683 tw32_f(MAC_MI_MODE, tp->mi_mode);
4684 udelay(80);
4685 }
4686
4687 tw32_f(MAC_MODE, tp->mac_mode);
4688 udelay(40);
4689
Matt Carlson52b02d02010-10-14 10:37:41 +00004690 tg3_phy_eee_adjust(tp, current_link_up);
4691
Joe Perches63c3a662011-04-26 08:12:10 +00004692 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 /* Polled via timer. */
4694 tw32_f(MAC_EVENT, 0);
4695 } else {
4696 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4697 }
4698 udelay(40);
4699
Joe Perches41535772013-02-16 11:20:04 +00004700 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 current_link_up == 1 &&
4702 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004703 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 udelay(120);
4705 tw32_f(MAC_STATUS,
4706 (MAC_STATUS_SYNC_CHANGED |
4707 MAC_STATUS_CFG_CHANGED));
4708 udelay(40);
4709 tg3_write_mem(tp,
4710 NIC_SRAM_FIRMWARE_MBOX,
4711 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4712 }
4713
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004714 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004715 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004716 if (tp->link_config.active_speed == SPEED_100 ||
4717 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004718 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4719 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004720 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004721 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4722 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004723 }
4724
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004725 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 return 0;
4728}
4729
4730struct tg3_fiber_aneginfo {
4731 int state;
4732#define ANEG_STATE_UNKNOWN 0
4733#define ANEG_STATE_AN_ENABLE 1
4734#define ANEG_STATE_RESTART_INIT 2
4735#define ANEG_STATE_RESTART 3
4736#define ANEG_STATE_DISABLE_LINK_OK 4
4737#define ANEG_STATE_ABILITY_DETECT_INIT 5
4738#define ANEG_STATE_ABILITY_DETECT 6
4739#define ANEG_STATE_ACK_DETECT_INIT 7
4740#define ANEG_STATE_ACK_DETECT 8
4741#define ANEG_STATE_COMPLETE_ACK_INIT 9
4742#define ANEG_STATE_COMPLETE_ACK 10
4743#define ANEG_STATE_IDLE_DETECT_INIT 11
4744#define ANEG_STATE_IDLE_DETECT 12
4745#define ANEG_STATE_LINK_OK 13
4746#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4747#define ANEG_STATE_NEXT_PAGE_WAIT 15
4748
4749 u32 flags;
4750#define MR_AN_ENABLE 0x00000001
4751#define MR_RESTART_AN 0x00000002
4752#define MR_AN_COMPLETE 0x00000004
4753#define MR_PAGE_RX 0x00000008
4754#define MR_NP_LOADED 0x00000010
4755#define MR_TOGGLE_TX 0x00000020
4756#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4757#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4758#define MR_LP_ADV_SYM_PAUSE 0x00000100
4759#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4760#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4761#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4762#define MR_LP_ADV_NEXT_PAGE 0x00001000
4763#define MR_TOGGLE_RX 0x00002000
4764#define MR_NP_RX 0x00004000
4765
4766#define MR_LINK_OK 0x80000000
4767
4768 unsigned long link_time, cur_time;
4769
4770 u32 ability_match_cfg;
4771 int ability_match_count;
4772
4773 char ability_match, idle_match, ack_match;
4774
4775 u32 txconfig, rxconfig;
4776#define ANEG_CFG_NP 0x00000080
4777#define ANEG_CFG_ACK 0x00000040
4778#define ANEG_CFG_RF2 0x00000020
4779#define ANEG_CFG_RF1 0x00000010
4780#define ANEG_CFG_PS2 0x00000001
4781#define ANEG_CFG_PS1 0x00008000
4782#define ANEG_CFG_HD 0x00004000
4783#define ANEG_CFG_FD 0x00002000
4784#define ANEG_CFG_INVAL 0x00001f06
4785
4786};
4787#define ANEG_OK 0
4788#define ANEG_DONE 1
4789#define ANEG_TIMER_ENAB 2
4790#define ANEG_FAILED -1
4791
4792#define ANEG_STATE_SETTLE_TIME 10000
4793
4794static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4795 struct tg3_fiber_aneginfo *ap)
4796{
Matt Carlson5be73b42007-12-20 20:09:29 -08004797 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 unsigned long delta;
4799 u32 rx_cfg_reg;
4800 int ret;
4801
4802 if (ap->state == ANEG_STATE_UNKNOWN) {
4803 ap->rxconfig = 0;
4804 ap->link_time = 0;
4805 ap->cur_time = 0;
4806 ap->ability_match_cfg = 0;
4807 ap->ability_match_count = 0;
4808 ap->ability_match = 0;
4809 ap->idle_match = 0;
4810 ap->ack_match = 0;
4811 }
4812 ap->cur_time++;
4813
4814 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4815 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4816
4817 if (rx_cfg_reg != ap->ability_match_cfg) {
4818 ap->ability_match_cfg = rx_cfg_reg;
4819 ap->ability_match = 0;
4820 ap->ability_match_count = 0;
4821 } else {
4822 if (++ap->ability_match_count > 1) {
4823 ap->ability_match = 1;
4824 ap->ability_match_cfg = rx_cfg_reg;
4825 }
4826 }
4827 if (rx_cfg_reg & ANEG_CFG_ACK)
4828 ap->ack_match = 1;
4829 else
4830 ap->ack_match = 0;
4831
4832 ap->idle_match = 0;
4833 } else {
4834 ap->idle_match = 1;
4835 ap->ability_match_cfg = 0;
4836 ap->ability_match_count = 0;
4837 ap->ability_match = 0;
4838 ap->ack_match = 0;
4839
4840 rx_cfg_reg = 0;
4841 }
4842
4843 ap->rxconfig = rx_cfg_reg;
4844 ret = ANEG_OK;
4845
Matt Carlson33f401a2010-04-05 10:19:27 +00004846 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 case ANEG_STATE_UNKNOWN:
4848 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4849 ap->state = ANEG_STATE_AN_ENABLE;
4850
4851 /* fallthru */
4852 case ANEG_STATE_AN_ENABLE:
4853 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4854 if (ap->flags & MR_AN_ENABLE) {
4855 ap->link_time = 0;
4856 ap->cur_time = 0;
4857 ap->ability_match_cfg = 0;
4858 ap->ability_match_count = 0;
4859 ap->ability_match = 0;
4860 ap->idle_match = 0;
4861 ap->ack_match = 0;
4862
4863 ap->state = ANEG_STATE_RESTART_INIT;
4864 } else {
4865 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4866 }
4867 break;
4868
4869 case ANEG_STATE_RESTART_INIT:
4870 ap->link_time = ap->cur_time;
4871 ap->flags &= ~(MR_NP_LOADED);
4872 ap->txconfig = 0;
4873 tw32(MAC_TX_AUTO_NEG, 0);
4874 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4875 tw32_f(MAC_MODE, tp->mac_mode);
4876 udelay(40);
4877
4878 ret = ANEG_TIMER_ENAB;
4879 ap->state = ANEG_STATE_RESTART;
4880
4881 /* fallthru */
4882 case ANEG_STATE_RESTART:
4883 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004884 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004886 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 break;
4889
4890 case ANEG_STATE_DISABLE_LINK_OK:
4891 ret = ANEG_DONE;
4892 break;
4893
4894 case ANEG_STATE_ABILITY_DETECT_INIT:
4895 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004896 ap->txconfig = ANEG_CFG_FD;
4897 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4898 if (flowctrl & ADVERTISE_1000XPAUSE)
4899 ap->txconfig |= ANEG_CFG_PS1;
4900 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4901 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4903 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4904 tw32_f(MAC_MODE, tp->mac_mode);
4905 udelay(40);
4906
4907 ap->state = ANEG_STATE_ABILITY_DETECT;
4908 break;
4909
4910 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004911 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 break;
4914
4915 case ANEG_STATE_ACK_DETECT_INIT:
4916 ap->txconfig |= ANEG_CFG_ACK;
4917 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4918 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4919 tw32_f(MAC_MODE, tp->mac_mode);
4920 udelay(40);
4921
4922 ap->state = ANEG_STATE_ACK_DETECT;
4923
4924 /* fallthru */
4925 case ANEG_STATE_ACK_DETECT:
4926 if (ap->ack_match != 0) {
4927 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4928 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4929 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4930 } else {
4931 ap->state = ANEG_STATE_AN_ENABLE;
4932 }
4933 } else if (ap->ability_match != 0 &&
4934 ap->rxconfig == 0) {
4935 ap->state = ANEG_STATE_AN_ENABLE;
4936 }
4937 break;
4938
4939 case ANEG_STATE_COMPLETE_ACK_INIT:
4940 if (ap->rxconfig & ANEG_CFG_INVAL) {
4941 ret = ANEG_FAILED;
4942 break;
4943 }
4944 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4945 MR_LP_ADV_HALF_DUPLEX |
4946 MR_LP_ADV_SYM_PAUSE |
4947 MR_LP_ADV_ASYM_PAUSE |
4948 MR_LP_ADV_REMOTE_FAULT1 |
4949 MR_LP_ADV_REMOTE_FAULT2 |
4950 MR_LP_ADV_NEXT_PAGE |
4951 MR_TOGGLE_RX |
4952 MR_NP_RX);
4953 if (ap->rxconfig & ANEG_CFG_FD)
4954 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4955 if (ap->rxconfig & ANEG_CFG_HD)
4956 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4957 if (ap->rxconfig & ANEG_CFG_PS1)
4958 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4959 if (ap->rxconfig & ANEG_CFG_PS2)
4960 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4961 if (ap->rxconfig & ANEG_CFG_RF1)
4962 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4963 if (ap->rxconfig & ANEG_CFG_RF2)
4964 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4965 if (ap->rxconfig & ANEG_CFG_NP)
4966 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4967
4968 ap->link_time = ap->cur_time;
4969
4970 ap->flags ^= (MR_TOGGLE_TX);
4971 if (ap->rxconfig & 0x0008)
4972 ap->flags |= MR_TOGGLE_RX;
4973 if (ap->rxconfig & ANEG_CFG_NP)
4974 ap->flags |= MR_NP_RX;
4975 ap->flags |= MR_PAGE_RX;
4976
4977 ap->state = ANEG_STATE_COMPLETE_ACK;
4978 ret = ANEG_TIMER_ENAB;
4979 break;
4980
4981 case ANEG_STATE_COMPLETE_ACK:
4982 if (ap->ability_match != 0 &&
4983 ap->rxconfig == 0) {
4984 ap->state = ANEG_STATE_AN_ENABLE;
4985 break;
4986 }
4987 delta = ap->cur_time - ap->link_time;
4988 if (delta > ANEG_STATE_SETTLE_TIME) {
4989 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4990 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4991 } else {
4992 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4993 !(ap->flags & MR_NP_RX)) {
4994 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4995 } else {
4996 ret = ANEG_FAILED;
4997 }
4998 }
4999 }
5000 break;
5001
5002 case ANEG_STATE_IDLE_DETECT_INIT:
5003 ap->link_time = ap->cur_time;
5004 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5005 tw32_f(MAC_MODE, tp->mac_mode);
5006 udelay(40);
5007
5008 ap->state = ANEG_STATE_IDLE_DETECT;
5009 ret = ANEG_TIMER_ENAB;
5010 break;
5011
5012 case ANEG_STATE_IDLE_DETECT:
5013 if (ap->ability_match != 0 &&
5014 ap->rxconfig == 0) {
5015 ap->state = ANEG_STATE_AN_ENABLE;
5016 break;
5017 }
5018 delta = ap->cur_time - ap->link_time;
5019 if (delta > ANEG_STATE_SETTLE_TIME) {
5020 /* XXX another gem from the Broadcom driver :( */
5021 ap->state = ANEG_STATE_LINK_OK;
5022 }
5023 break;
5024
5025 case ANEG_STATE_LINK_OK:
5026 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5027 ret = ANEG_DONE;
5028 break;
5029
5030 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5031 /* ??? unimplemented */
5032 break;
5033
5034 case ANEG_STATE_NEXT_PAGE_WAIT:
5035 /* ??? unimplemented */
5036 break;
5037
5038 default:
5039 ret = ANEG_FAILED;
5040 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
5043 return ret;
5044}
5045
Matt Carlson5be73b42007-12-20 20:09:29 -08005046static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
5048 int res = 0;
5049 struct tg3_fiber_aneginfo aninfo;
5050 int status = ANEG_FAILED;
5051 unsigned int tick;
5052 u32 tmp;
5053
5054 tw32_f(MAC_TX_AUTO_NEG, 0);
5055
5056 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5057 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5058 udelay(40);
5059
5060 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5061 udelay(40);
5062
5063 memset(&aninfo, 0, sizeof(aninfo));
5064 aninfo.flags |= MR_AN_ENABLE;
5065 aninfo.state = ANEG_STATE_UNKNOWN;
5066 aninfo.cur_time = 0;
5067 tick = 0;
5068 while (++tick < 195000) {
5069 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5070 if (status == ANEG_DONE || status == ANEG_FAILED)
5071 break;
5072
5073 udelay(1);
5074 }
5075
5076 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5077 tw32_f(MAC_MODE, tp->mac_mode);
5078 udelay(40);
5079
Matt Carlson5be73b42007-12-20 20:09:29 -08005080 *txflags = aninfo.txconfig;
5081 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
5083 if (status == ANEG_DONE &&
5084 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5085 MR_LP_ADV_FULL_DUPLEX)))
5086 res = 1;
5087
5088 return res;
5089}
5090
5091static void tg3_init_bcm8002(struct tg3 *tp)
5092{
5093 u32 mac_status = tr32(MAC_STATUS);
5094 int i;
5095
5096 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005097 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 !(mac_status & MAC_STATUS_PCS_SYNCED))
5099 return;
5100
5101 /* Set PLL lock range. */
5102 tg3_writephy(tp, 0x16, 0x8007);
5103
5104 /* SW reset */
5105 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5106
5107 /* Wait for reset to complete. */
5108 /* XXX schedule_timeout() ... */
5109 for (i = 0; i < 500; i++)
5110 udelay(10);
5111
5112 /* Config mode; select PMA/Ch 1 regs. */
5113 tg3_writephy(tp, 0x10, 0x8411);
5114
5115 /* Enable auto-lock and comdet, select txclk for tx. */
5116 tg3_writephy(tp, 0x11, 0x0a10);
5117
5118 tg3_writephy(tp, 0x18, 0x00a0);
5119 tg3_writephy(tp, 0x16, 0x41ff);
5120
5121 /* Assert and deassert POR. */
5122 tg3_writephy(tp, 0x13, 0x0400);
5123 udelay(40);
5124 tg3_writephy(tp, 0x13, 0x0000);
5125
5126 tg3_writephy(tp, 0x11, 0x0a50);
5127 udelay(40);
5128 tg3_writephy(tp, 0x11, 0x0a10);
5129
5130 /* Wait for signal to stabilize */
5131 /* XXX schedule_timeout() ... */
5132 for (i = 0; i < 15000; i++)
5133 udelay(10);
5134
5135 /* Deselect the channel register so we can read the PHYID
5136 * later.
5137 */
5138 tg3_writephy(tp, 0x10, 0x8011);
5139}
5140
5141static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
5142{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005143 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 u32 sg_dig_ctrl, sg_dig_status;
5145 u32 serdes_cfg, expected_sg_dig_ctrl;
5146 int workaround, port_a;
5147 int current_link_up;
5148
5149 serdes_cfg = 0;
5150 expected_sg_dig_ctrl = 0;
5151 workaround = 0;
5152 port_a = 1;
5153 current_link_up = 0;
5154
Joe Perches41535772013-02-16 11:20:04 +00005155 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5156 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 workaround = 1;
5158 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5159 port_a = 0;
5160
5161 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5162 /* preserve bits 20-23 for voltage regulator */
5163 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5164 }
5165
5166 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5167
5168 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005169 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (workaround) {
5171 u32 val = serdes_cfg;
5172
5173 if (port_a)
5174 val |= 0xc010000;
5175 else
5176 val |= 0x4010000;
5177 tw32_f(MAC_SERDES_CFG, val);
5178 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005179
5180 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
5182 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5183 tg3_setup_flow_control(tp, 0, 0);
5184 current_link_up = 1;
5185 }
5186 goto out;
5187 }
5188
5189 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005190 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
Matt Carlson82cd3d12007-12-20 20:09:00 -08005192 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5193 if (flowctrl & ADVERTISE_1000XPAUSE)
5194 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5195 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5196 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
5198 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005199 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005200 tp->serdes_counter &&
5201 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5202 MAC_STATUS_RCVD_CFG)) ==
5203 MAC_STATUS_PCS_SYNCED)) {
5204 tp->serdes_counter--;
5205 current_link_up = 1;
5206 goto out;
5207 }
5208restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 if (workaround)
5210 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005211 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 udelay(5);
5213 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5214
Michael Chan3d3ebe72006-09-27 15:59:15 -07005215 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005216 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5218 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005219 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 mac_status = tr32(MAC_STATUS);
5221
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005222 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005224 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Matt Carlson82cd3d12007-12-20 20:09:00 -08005226 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5227 local_adv |= ADVERTISE_1000XPAUSE;
5228 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5229 local_adv |= ADVERTISE_1000XPSE_ASYM;
5230
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005231 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005232 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005233 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005234 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Matt Carlson859edb22011-12-08 14:40:16 +00005236 tp->link_config.rmt_adv =
5237 mii_adv_to_ethtool_adv_x(remote_adv);
5238
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 tg3_setup_flow_control(tp, local_adv, remote_adv);
5240 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005241 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005242 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005243 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005244 if (tp->serdes_counter)
5245 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 else {
5247 if (workaround) {
5248 u32 val = serdes_cfg;
5249
5250 if (port_a)
5251 val |= 0xc010000;
5252 else
5253 val |= 0x4010000;
5254
5255 tw32_f(MAC_SERDES_CFG, val);
5256 }
5257
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005258 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 udelay(40);
5260
5261 /* Link parallel detection - link is up */
5262 /* only if we have PCS_SYNC and not */
5263 /* receiving config code words */
5264 mac_status = tr32(MAC_STATUS);
5265 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5266 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5267 tg3_setup_flow_control(tp, 0, 0);
5268 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005269 tp->phy_flags |=
5270 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005271 tp->serdes_counter =
5272 SERDES_PARALLEL_DET_TIMEOUT;
5273 } else
5274 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 }
5276 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005277 } else {
5278 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005279 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 }
5281
5282out:
5283 return current_link_up;
5284}
5285
5286static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5287{
5288 int current_link_up = 0;
5289
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005290 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005294 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005296
Matt Carlson5be73b42007-12-20 20:09:29 -08005297 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5298 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Matt Carlson5be73b42007-12-20 20:09:29 -08005300 if (txflags & ANEG_CFG_PS1)
5301 local_adv |= ADVERTISE_1000XPAUSE;
5302 if (txflags & ANEG_CFG_PS2)
5303 local_adv |= ADVERTISE_1000XPSE_ASYM;
5304
5305 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5306 remote_adv |= LPA_1000XPAUSE;
5307 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5308 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Matt Carlson859edb22011-12-08 14:40:16 +00005310 tp->link_config.rmt_adv =
5311 mii_adv_to_ethtool_adv_x(remote_adv);
5312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 tg3_setup_flow_control(tp, local_adv, remote_adv);
5314
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 current_link_up = 1;
5316 }
5317 for (i = 0; i < 30; i++) {
5318 udelay(20);
5319 tw32_f(MAC_STATUS,
5320 (MAC_STATUS_SYNC_CHANGED |
5321 MAC_STATUS_CFG_CHANGED));
5322 udelay(40);
5323 if ((tr32(MAC_STATUS) &
5324 (MAC_STATUS_SYNC_CHANGED |
5325 MAC_STATUS_CFG_CHANGED)) == 0)
5326 break;
5327 }
5328
5329 mac_status = tr32(MAC_STATUS);
5330 if (current_link_up == 0 &&
5331 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5332 !(mac_status & MAC_STATUS_RCVD_CFG))
5333 current_link_up = 1;
5334 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005335 tg3_setup_flow_control(tp, 0, 0);
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 /* Forcing 1000FD link up. */
5338 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
5340 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5341 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005342
5343 tw32_f(MAC_MODE, tp->mac_mode);
5344 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 }
5346
5347out:
5348 return current_link_up;
5349}
5350
5351static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5352{
5353 u32 orig_pause_cfg;
5354 u16 orig_active_speed;
5355 u8 orig_active_duplex;
5356 u32 mac_status;
5357 int current_link_up;
5358 int i;
5359
Matt Carlson8d018622007-12-20 20:05:44 -08005360 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 orig_active_speed = tp->link_config.active_speed;
5362 orig_active_duplex = tp->link_config.active_duplex;
5363
Joe Perches63c3a662011-04-26 08:12:10 +00005364 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005365 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005366 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 mac_status = tr32(MAC_STATUS);
5368 mac_status &= (MAC_STATUS_PCS_SYNCED |
5369 MAC_STATUS_SIGNAL_DET |
5370 MAC_STATUS_CFG_CHANGED |
5371 MAC_STATUS_RCVD_CFG);
5372 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5373 MAC_STATUS_SIGNAL_DET)) {
5374 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5375 MAC_STATUS_CFG_CHANGED));
5376 return 0;
5377 }
5378 }
5379
5380 tw32_f(MAC_TX_AUTO_NEG, 0);
5381
5382 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5383 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5384 tw32_f(MAC_MODE, tp->mac_mode);
5385 udelay(40);
5386
Matt Carlson79eb6902010-02-17 15:17:03 +00005387 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 tg3_init_bcm8002(tp);
5389
5390 /* Enable link change event even when serdes polling. */
5391 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5392 udelay(40);
5393
5394 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005395 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 mac_status = tr32(MAC_STATUS);
5397
Joe Perches63c3a662011-04-26 08:12:10 +00005398 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5400 else
5401 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5402
Matt Carlson898a56f2009-08-28 14:02:40 +00005403 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005405 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 for (i = 0; i < 100; i++) {
5408 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5409 MAC_STATUS_CFG_CHANGED));
5410 udelay(5);
5411 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005412 MAC_STATUS_CFG_CHANGED |
5413 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 break;
5415 }
5416
5417 mac_status = tr32(MAC_STATUS);
5418 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5419 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005420 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5421 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 tw32_f(MAC_MODE, (tp->mac_mode |
5423 MAC_MODE_SEND_CONFIGS));
5424 udelay(1);
5425 tw32_f(MAC_MODE, tp->mac_mode);
5426 }
5427 }
5428
5429 if (current_link_up == 1) {
5430 tp->link_config.active_speed = SPEED_1000;
5431 tp->link_config.active_duplex = DUPLEX_FULL;
5432 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5433 LED_CTRL_LNKLED_OVERRIDE |
5434 LED_CTRL_1000MBPS_ON));
5435 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005436 tp->link_config.active_speed = SPEED_UNKNOWN;
5437 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5439 LED_CTRL_LNKLED_OVERRIDE |
5440 LED_CTRL_TRAFFIC_OVERRIDE));
5441 }
5442
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005443 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005444 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 if (orig_pause_cfg != now_pause_cfg ||
5446 orig_active_speed != tp->link_config.active_speed ||
5447 orig_active_duplex != tp->link_config.active_duplex)
5448 tg3_link_report(tp);
5449 }
5450
5451 return 0;
5452}
5453
Michael Chan747e8f82005-07-25 12:33:22 -07005454static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5455{
5456 int current_link_up, err = 0;
5457 u32 bmsr, bmcr;
5458 u16 current_speed;
5459 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005460 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005461
5462 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5463 tw32_f(MAC_MODE, tp->mac_mode);
5464 udelay(40);
5465
5466 tw32(MAC_EVENT, 0);
5467
5468 tw32_f(MAC_STATUS,
5469 (MAC_STATUS_SYNC_CHANGED |
5470 MAC_STATUS_CFG_CHANGED |
5471 MAC_STATUS_MI_COMPLETION |
5472 MAC_STATUS_LNKSTATE_CHANGED));
5473 udelay(40);
5474
5475 if (force_reset)
5476 tg3_phy_reset(tp);
5477
5478 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005479 current_speed = SPEED_UNKNOWN;
5480 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005481 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005482
5483 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5484 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005485 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005486 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5487 bmsr |= BMSR_LSTATUS;
5488 else
5489 bmsr &= ~BMSR_LSTATUS;
5490 }
Michael Chan747e8f82005-07-25 12:33:22 -07005491
5492 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5493
5494 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005495 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005496 /* do nothing, just check for link up at the end */
5497 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005498 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005499
5500 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005501 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5502 ADVERTISE_1000XPAUSE |
5503 ADVERTISE_1000XPSE_ASYM |
5504 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005505
Matt Carlson28011cf2011-11-16 18:36:59 -05005506 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005507 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005508
Matt Carlson28011cf2011-11-16 18:36:59 -05005509 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5510 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005511 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5512 tg3_writephy(tp, MII_BMCR, bmcr);
5513
5514 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005515 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005516 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005517
5518 return err;
5519 }
5520 } else {
5521 u32 new_bmcr;
5522
5523 bmcr &= ~BMCR_SPEED1000;
5524 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5525
5526 if (tp->link_config.duplex == DUPLEX_FULL)
5527 new_bmcr |= BMCR_FULLDPLX;
5528
5529 if (new_bmcr != bmcr) {
5530 /* BMCR_SPEED1000 is a reserved bit that needs
5531 * to be set on write.
5532 */
5533 new_bmcr |= BMCR_SPEED1000;
5534
5535 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005536 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005537 u32 adv;
5538
5539 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5540 adv &= ~(ADVERTISE_1000XFULL |
5541 ADVERTISE_1000XHALF |
5542 ADVERTISE_SLCT);
5543 tg3_writephy(tp, MII_ADVERTISE, adv);
5544 tg3_writephy(tp, MII_BMCR, bmcr |
5545 BMCR_ANRESTART |
5546 BMCR_ANENABLE);
5547 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005548 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005549 }
5550 tg3_writephy(tp, MII_BMCR, new_bmcr);
5551 bmcr = new_bmcr;
5552 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5553 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005554 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005555 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5556 bmsr |= BMSR_LSTATUS;
5557 else
5558 bmsr &= ~BMSR_LSTATUS;
5559 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005560 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005561 }
5562 }
5563
5564 if (bmsr & BMSR_LSTATUS) {
5565 current_speed = SPEED_1000;
5566 current_link_up = 1;
5567 if (bmcr & BMCR_FULLDPLX)
5568 current_duplex = DUPLEX_FULL;
5569 else
5570 current_duplex = DUPLEX_HALF;
5571
Matt Carlsonef167e22007-12-20 20:10:01 -08005572 local_adv = 0;
5573 remote_adv = 0;
5574
Michael Chan747e8f82005-07-25 12:33:22 -07005575 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005576 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005577
5578 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5579 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5580 common = local_adv & remote_adv;
5581 if (common & (ADVERTISE_1000XHALF |
5582 ADVERTISE_1000XFULL)) {
5583 if (common & ADVERTISE_1000XFULL)
5584 current_duplex = DUPLEX_FULL;
5585 else
5586 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005587
5588 tp->link_config.rmt_adv =
5589 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005590 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005591 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005592 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005593 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005594 }
Michael Chan747e8f82005-07-25 12:33:22 -07005595 }
5596 }
5597
Matt Carlsonef167e22007-12-20 20:10:01 -08005598 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5599 tg3_setup_flow_control(tp, local_adv, remote_adv);
5600
Michael Chan747e8f82005-07-25 12:33:22 -07005601 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5602 if (tp->link_config.active_duplex == DUPLEX_HALF)
5603 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5604
5605 tw32_f(MAC_MODE, tp->mac_mode);
5606 udelay(40);
5607
5608 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5609
5610 tp->link_config.active_speed = current_speed;
5611 tp->link_config.active_duplex = current_duplex;
5612
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005613 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005614 return err;
5615}
5616
5617static void tg3_serdes_parallel_detect(struct tg3 *tp)
5618{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005619 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005620 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005621 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005622 return;
5623 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005624
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005625 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005626 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5627 u32 bmcr;
5628
5629 tg3_readphy(tp, MII_BMCR, &bmcr);
5630 if (bmcr & BMCR_ANENABLE) {
5631 u32 phy1, phy2;
5632
5633 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005634 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5635 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005636
5637 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005638 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5639 MII_TG3_DSP_EXP1_INT_STAT);
5640 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5641 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005642
5643 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5644 /* We have signal detect and not receiving
5645 * config code words, link is up by parallel
5646 * detection.
5647 */
5648
5649 bmcr &= ~BMCR_ANENABLE;
5650 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5651 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005652 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005653 }
5654 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005655 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005656 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005657 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005658 u32 phy2;
5659
5660 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005661 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5662 MII_TG3_DSP_EXP1_INT_STAT);
5663 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005664 if (phy2 & 0x20) {
5665 u32 bmcr;
5666
5667 /* Config code words received, turn on autoneg. */
5668 tg3_readphy(tp, MII_BMCR, &bmcr);
5669 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5670
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005671 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005672
5673 }
5674 }
5675}
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5678{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005679 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 int err;
5681
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005682 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005684 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005685 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005686 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Joe Perches41535772013-02-16 11:20:04 +00005689 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005690 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005691
5692 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5693 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5694 scale = 65;
5695 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5696 scale = 6;
5697 else
5698 scale = 12;
5699
5700 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5701 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5702 tw32(GRC_MISC_CFG, val);
5703 }
5704
Matt Carlsonf2096f92011-04-05 14:22:48 +00005705 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5706 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005707 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5708 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005709 val |= tr32(MAC_TX_LENGTHS) &
5710 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5711 TX_LENGTHS_CNT_DWN_VAL_MSK);
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 if (tp->link_config.active_speed == SPEED_1000 &&
5714 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005715 tw32(MAC_TX_LENGTHS, val |
5716 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005718 tw32(MAC_TX_LENGTHS, val |
5719 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Joe Perches63c3a662011-04-26 08:12:10 +00005721 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005722 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005724 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 } else {
5726 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5727 }
5728 }
5729
Joe Perches63c3a662011-04-26 08:12:10 +00005730 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005731 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005732 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005733 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5734 tp->pwrmgmt_thresh;
5735 else
5736 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5737 tw32(PCIE_PWR_MGMT_THRESH, val);
5738 }
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 return err;
5741}
5742
Matt Carlsonbe947302012-12-03 19:36:57 +00005743/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005744static u64 tg3_refclk_read(struct tg3 *tp)
5745{
5746 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5747 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5748}
5749
5750/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005751static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5752{
5753 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5754 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5755 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5756 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5757}
5758
Matt Carlson7d41e492012-12-03 19:36:58 +00005759static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5760static inline void tg3_full_unlock(struct tg3 *tp);
5761static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5762{
5763 struct tg3 *tp = netdev_priv(dev);
5764
5765 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5766 SOF_TIMESTAMPING_RX_SOFTWARE |
5767 SOF_TIMESTAMPING_SOFTWARE |
5768 SOF_TIMESTAMPING_TX_HARDWARE |
5769 SOF_TIMESTAMPING_RX_HARDWARE |
5770 SOF_TIMESTAMPING_RAW_HARDWARE;
5771
5772 if (tp->ptp_clock)
5773 info->phc_index = ptp_clock_index(tp->ptp_clock);
5774 else
5775 info->phc_index = -1;
5776
5777 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5778
5779 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5780 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5781 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5782 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5783 return 0;
5784}
5785
5786static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5787{
5788 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5789 bool neg_adj = false;
5790 u32 correction = 0;
5791
5792 if (ppb < 0) {
5793 neg_adj = true;
5794 ppb = -ppb;
5795 }
5796
5797 /* Frequency adjustment is performed using hardware with a 24 bit
5798 * accumulator and a programmable correction value. On each clk, the
5799 * correction value gets added to the accumulator and when it
5800 * overflows, the time counter is incremented/decremented.
5801 *
5802 * So conversion from ppb to correction value is
5803 * ppb * (1 << 24) / 1000000000
5804 */
5805 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5806 TG3_EAV_REF_CLK_CORRECT_MASK;
5807
5808 tg3_full_lock(tp, 0);
5809
5810 if (correction)
5811 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5812 TG3_EAV_REF_CLK_CORRECT_EN |
5813 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5814 else
5815 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5816
5817 tg3_full_unlock(tp);
5818
5819 return 0;
5820}
5821
5822static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5823{
5824 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5825
5826 tg3_full_lock(tp, 0);
5827 tp->ptp_adjust += delta;
5828 tg3_full_unlock(tp);
5829
5830 return 0;
5831}
5832
5833static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5834{
5835 u64 ns;
5836 u32 remainder;
5837 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5838
5839 tg3_full_lock(tp, 0);
5840 ns = tg3_refclk_read(tp);
5841 ns += tp->ptp_adjust;
5842 tg3_full_unlock(tp);
5843
5844 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5845 ts->tv_nsec = remainder;
5846
5847 return 0;
5848}
5849
5850static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5851 const struct timespec *ts)
5852{
5853 u64 ns;
5854 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5855
5856 ns = timespec_to_ns(ts);
5857
5858 tg3_full_lock(tp, 0);
5859 tg3_refclk_write(tp, ns);
5860 tp->ptp_adjust = 0;
5861 tg3_full_unlock(tp);
5862
5863 return 0;
5864}
5865
5866static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5867 struct ptp_clock_request *rq, int on)
5868{
5869 return -EOPNOTSUPP;
5870}
5871
5872static const struct ptp_clock_info tg3_ptp_caps = {
5873 .owner = THIS_MODULE,
5874 .name = "tg3 clock",
5875 .max_adj = 250000000,
5876 .n_alarm = 0,
5877 .n_ext_ts = 0,
5878 .n_per_out = 0,
5879 .pps = 0,
5880 .adjfreq = tg3_ptp_adjfreq,
5881 .adjtime = tg3_ptp_adjtime,
5882 .gettime = tg3_ptp_gettime,
5883 .settime = tg3_ptp_settime,
5884 .enable = tg3_ptp_enable,
5885};
5886
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005887static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5888 struct skb_shared_hwtstamps *timestamp)
5889{
5890 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5891 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5892 tp->ptp_adjust);
5893}
5894
Matt Carlsonbe947302012-12-03 19:36:57 +00005895/* tp->lock must be held */
5896static void tg3_ptp_init(struct tg3 *tp)
5897{
5898 if (!tg3_flag(tp, PTP_CAPABLE))
5899 return;
5900
5901 /* Initialize the hardware clock to the system time. */
5902 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5903 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005904 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005905}
5906
5907/* tp->lock must be held */
5908static void tg3_ptp_resume(struct tg3 *tp)
5909{
5910 if (!tg3_flag(tp, PTP_CAPABLE))
5911 return;
5912
5913 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5914 tp->ptp_adjust = 0;
5915}
5916
5917static void tg3_ptp_fini(struct tg3 *tp)
5918{
5919 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5920 return;
5921
Matt Carlson7d41e492012-12-03 19:36:58 +00005922 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005923 tp->ptp_clock = NULL;
5924 tp->ptp_adjust = 0;
5925}
5926
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005927static inline int tg3_irq_sync(struct tg3 *tp)
5928{
5929 return tp->irq_sync;
5930}
5931
Matt Carlson97bd8e42011-04-13 11:05:04 +00005932static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5933{
5934 int i;
5935
5936 dst = (u32 *)((u8 *)dst + off);
5937 for (i = 0; i < len; i += sizeof(u32))
5938 *dst++ = tr32(off + i);
5939}
5940
5941static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5942{
5943 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5944 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5945 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5946 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5947 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5948 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5949 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5950 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5951 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5952 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5953 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5954 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5955 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5956 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5957 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5958 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5959 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5960 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5961 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5962
Joe Perches63c3a662011-04-26 08:12:10 +00005963 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005964 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5965
5966 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5967 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5968 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5969 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5970 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5971 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5972 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5973 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5974
Joe Perches63c3a662011-04-26 08:12:10 +00005975 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005976 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5977 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5978 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5979 }
5980
5981 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5982 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5983 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5984 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5985 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5986
Joe Perches63c3a662011-04-26 08:12:10 +00005987 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005988 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5989}
5990
5991static void tg3_dump_state(struct tg3 *tp)
5992{
5993 int i;
5994 u32 *regs;
5995
5996 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00005997 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00005998 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00005999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006001 /* Read up to but not including private PCI registers */
6002 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6003 regs[i / sizeof(u32)] = tr32(i);
6004 } else
6005 tg3_dump_legacy_regs(tp, regs);
6006
6007 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6008 if (!regs[i + 0] && !regs[i + 1] &&
6009 !regs[i + 2] && !regs[i + 3])
6010 continue;
6011
6012 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6013 i * 4,
6014 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6015 }
6016
6017 kfree(regs);
6018
6019 for (i = 0; i < tp->irq_cnt; i++) {
6020 struct tg3_napi *tnapi = &tp->napi[i];
6021
6022 /* SW status block */
6023 netdev_err(tp->dev,
6024 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6025 i,
6026 tnapi->hw_status->status,
6027 tnapi->hw_status->status_tag,
6028 tnapi->hw_status->rx_jumbo_consumer,
6029 tnapi->hw_status->rx_consumer,
6030 tnapi->hw_status->rx_mini_consumer,
6031 tnapi->hw_status->idx[0].rx_producer,
6032 tnapi->hw_status->idx[0].tx_consumer);
6033
6034 netdev_err(tp->dev,
6035 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6036 i,
6037 tnapi->last_tag, tnapi->last_irq_tag,
6038 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6039 tnapi->rx_rcb_ptr,
6040 tnapi->prodring.rx_std_prod_idx,
6041 tnapi->prodring.rx_std_cons_idx,
6042 tnapi->prodring.rx_jmb_prod_idx,
6043 tnapi->prodring.rx_jmb_cons_idx);
6044 }
6045}
6046
Michael Chandf3e6542006-05-26 17:48:07 -07006047/* This is called whenever we suspect that the system chipset is re-
6048 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6049 * is bogus tx completions. We try to recover by setting the
6050 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6051 * in the workqueue.
6052 */
6053static void tg3_tx_recover(struct tg3 *tp)
6054{
Joe Perches63c3a662011-04-26 08:12:10 +00006055 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006056 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6057
Matt Carlson5129c3a2010-04-05 10:19:23 +00006058 netdev_warn(tp->dev,
6059 "The system may be re-ordering memory-mapped I/O "
6060 "cycles to the network device, attempting to recover. "
6061 "Please report the problem to the driver maintainer "
6062 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006063
6064 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006065 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006066 spin_unlock(&tp->lock);
6067}
6068
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006070{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006071 /* Tell compiler to fetch tx indices from memory. */
6072 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006073 return tnapi->tx_pending -
6074 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006075}
6076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077/* Tigon3 never reports partial packet sends. So we do not
6078 * need special logic to handle SKBs that have not had all
6079 * of their frags sent yet, like SunGEM does.
6080 */
Matt Carlson17375d22009-08-28 14:02:18 +00006081static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082{
Matt Carlson17375d22009-08-28 14:02:18 +00006083 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006084 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006085 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006086 struct netdev_queue *txq;
6087 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006088 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006089
Joe Perches63c3a662011-04-26 08:12:10 +00006090 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006091 index--;
6092
6093 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
6095 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006096 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006098 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Michael Chandf3e6542006-05-26 17:48:07 -07006100 if (unlikely(skb == NULL)) {
6101 tg3_tx_recover(tp);
6102 return;
6103 }
6104
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006105 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6106 struct skb_shared_hwtstamps timestamp;
6107 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6108 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6109
6110 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6111
6112 skb_tstamp_tx(skb, &timestamp);
6113 }
6114
Alexander Duyckf4188d82009-12-02 16:48:38 +00006115 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006116 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006117 skb_headlen(skb),
6118 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
6120 ri->skb = NULL;
6121
Matt Carlsone01ee142011-07-27 14:20:50 +00006122 while (ri->fragmented) {
6123 ri->fragmented = false;
6124 sw_idx = NEXT_TX(sw_idx);
6125 ri = &tnapi->tx_buffers[sw_idx];
6126 }
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 sw_idx = NEXT_TX(sw_idx);
6129
6130 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006131 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006132 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6133 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006134
6135 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006136 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006137 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006138 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006139
6140 while (ri->fragmented) {
6141 ri->fragmented = false;
6142 sw_idx = NEXT_TX(sw_idx);
6143 ri = &tnapi->tx_buffers[sw_idx];
6144 }
6145
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 sw_idx = NEXT_TX(sw_idx);
6147 }
6148
Tom Herbert298376d2011-11-28 16:33:30 +00006149 pkts_compl++;
6150 bytes_compl += skb->len;
6151
David S. Millerf47c11e2005-06-24 20:18:35 -07006152 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006153
6154 if (unlikely(tx_bug)) {
6155 tg3_tx_recover(tp);
6156 return;
6157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 }
6159
Tom Herbert5cb917b2012-03-05 19:53:50 +00006160 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006161
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006162 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
Michael Chan1b2a7202006-08-07 21:46:02 -07006164 /* Need to make the tx_cons update visible to tg3_start_xmit()
6165 * before checking for netif_queue_stopped(). Without the
6166 * memory barrier, there is a small possibility that tg3_start_xmit()
6167 * will miss it and cause the queue to be stopped forever.
6168 */
6169 smp_mb();
6170
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006171 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006172 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006173 __netif_tx_lock(txq, smp_processor_id());
6174 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006175 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006176 netif_tx_wake_queue(txq);
6177 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179}
6180
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006181static void tg3_frag_free(bool is_frag, void *data)
6182{
6183 if (is_frag)
6184 put_page(virt_to_head_page(data));
6185 else
6186 kfree(data);
6187}
6188
Eric Dumazet9205fd92011-11-18 06:47:01 +00006189static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006190{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006191 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6192 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6193
Eric Dumazet9205fd92011-11-18 06:47:01 +00006194 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006195 return;
6196
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006197 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006198 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006199 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006200 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006201}
6202
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006203
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204/* Returns size of skb allocated or < 0 on error.
6205 *
6206 * We only need to fill in the address because the other members
6207 * of the RX descriptor are invariant, see tg3_init_rings.
6208 *
6209 * Note the purposeful assymetry of cpu vs. chip accesses. For
6210 * posting buffers we only dirty the first cache line of the RX
6211 * descriptor (containing the address). Whereas for the RX status
6212 * buffers the cpu only reads the last cacheline of the RX descriptor
6213 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6214 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006215static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006216 u32 opaque_key, u32 dest_idx_unmasked,
6217 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218{
6219 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006220 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006221 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006223 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 switch (opaque_key) {
6226 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006227 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006228 desc = &tpr->rx_std[dest_idx];
6229 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006230 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 break;
6232
6233 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006234 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006235 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006236 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006237 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 break;
6239
6240 default:
6241 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 /* Do not overwrite any of the map or rp information
6245 * until we are sure we can commit to a new buffer.
6246 *
6247 * Callers depend upon this behavior and assume that
6248 * we leave everything unchanged if we fail.
6249 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006250 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6251 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006252 if (skb_size <= PAGE_SIZE) {
6253 data = netdev_alloc_frag(skb_size);
6254 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006255 } else {
6256 data = kmalloc(skb_size, GFP_ATOMIC);
6257 *frag_size = 0;
6258 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006259 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 return -ENOMEM;
6261
Eric Dumazet9205fd92011-11-18 06:47:01 +00006262 mapping = pci_map_single(tp->pdev,
6263 data + TG3_RX_OFFSET(tp),
6264 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006266 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006267 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006268 return -EIO;
6269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Eric Dumazet9205fd92011-11-18 06:47:01 +00006271 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006272 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 desc->addr_hi = ((u64)mapping >> 32);
6275 desc->addr_lo = ((u64)mapping & 0xffffffff);
6276
Eric Dumazet9205fd92011-11-18 06:47:01 +00006277 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278}
6279
6280/* We only need to move over in the address because the other
6281 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006282 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 */
Matt Carlsona3896162009-11-13 13:03:44 +00006284static void tg3_recycle_rx(struct tg3_napi *tnapi,
6285 struct tg3_rx_prodring_set *dpr,
6286 u32 opaque_key, int src_idx,
6287 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Matt Carlson17375d22009-08-28 14:02:18 +00006289 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6291 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006292 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006293 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
6295 switch (opaque_key) {
6296 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006298 dest_desc = &dpr->rx_std[dest_idx];
6299 dest_map = &dpr->rx_std_buffers[dest_idx];
6300 src_desc = &spr->rx_std[src_idx];
6301 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 break;
6303
6304 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006305 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006306 dest_desc = &dpr->rx_jmb[dest_idx].std;
6307 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6308 src_desc = &spr->rx_jmb[src_idx].std;
6309 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 break;
6311
6312 default:
6313 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
Eric Dumazet9205fd92011-11-18 06:47:01 +00006316 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006317 dma_unmap_addr_set(dest_map, mapping,
6318 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 dest_desc->addr_hi = src_desc->addr_hi;
6320 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006321
6322 /* Ensure that the update to the skb happens after the physical
6323 * addresses have been transferred to the new BD location.
6324 */
6325 smp_wmb();
6326
Eric Dumazet9205fd92011-11-18 06:47:01 +00006327 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328}
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330/* The RX ring scheme is composed of multiple rings which post fresh
6331 * buffers to the chip, and one special ring the chip uses to report
6332 * status back to the host.
6333 *
6334 * The special ring reports the status of received packets to the
6335 * host. The chip does not write into the original descriptor the
6336 * RX buffer was obtained from. The chip simply takes the original
6337 * descriptor as provided by the host, updates the status and length
6338 * field, then writes this into the next status ring entry.
6339 *
6340 * Each ring the host uses to post buffers to the chip is described
6341 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6342 * it is first placed into the on-chip ram. When the packet's length
6343 * is known, it walks down the TG3_BDINFO entries to select the ring.
6344 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6345 * which is within the range of the new packet's length is chosen.
6346 *
6347 * The "separate ring for rx status" scheme may sound queer, but it makes
6348 * sense from a cache coherency perspective. If only the host writes
6349 * to the buffer post rings, and only the chip writes to the rx status
6350 * rings, then cache lines never move beyond shared-modified state.
6351 * If both the host and chip were to write into the same ring, cache line
6352 * eviction could occur since both entities want it in an exclusive state.
6353 */
Matt Carlson17375d22009-08-28 14:02:18 +00006354static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355{
Matt Carlson17375d22009-08-28 14:02:18 +00006356 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006357 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006358 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006359 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006360 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006362 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006364 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 /*
6366 * We need to order the read of hw_idx and the read of
6367 * the opaque cookie.
6368 */
6369 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 work_mask = 0;
6371 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006372 std_prod_idx = tpr->rx_std_prod_idx;
6373 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006375 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006376 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 unsigned int len;
6378 struct sk_buff *skb;
6379 dma_addr_t dma_addr;
6380 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006381 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006382 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6385 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6386 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006387 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006388 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006389 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006390 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006391 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006393 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006394 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006395 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006396 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006397 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
6400 work_mask |= opaque_key;
6401
6402 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6403 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6404 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006405 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 desc_idx, *post_ptr);
6407 drop_it_no_recycle:
6408 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006409 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 goto next_pkt;
6411 }
6412
Eric Dumazet9205fd92011-11-18 06:47:01 +00006413 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006414 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6415 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006417 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6418 RXD_FLAG_PTPSTAT_PTPV1 ||
6419 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6420 RXD_FLAG_PTPSTAT_PTPV2) {
6421 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6422 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6423 }
6424
Matt Carlsond2757fc2010-04-12 06:58:27 +00006425 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006427 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
Eric Dumazet9205fd92011-11-18 06:47:01 +00006429 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006430 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 if (skb_size < 0)
6432 goto drop_it;
6433
Matt Carlson287be122009-08-28 13:58:46 +00006434 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 PCI_DMA_FROMDEVICE);
6436
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006437 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006438 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006439 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006440 goto drop_it_no_recycle;
6441 }
6442 skb_reserve(skb, TG3_RX_OFFSET(tp));
6443 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006444 * after the usage of the old DMA mapping.
6445 */
6446 smp_wmb();
6447
Eric Dumazet9205fd92011-11-18 06:47:01 +00006448 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006451 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 desc_idx, *post_ptr);
6453
Eric Dumazet9205fd92011-11-18 06:47:01 +00006454 skb = netdev_alloc_skb(tp->dev,
6455 len + TG3_RAW_IP_ALIGN);
6456 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 goto drop_it_no_recycle;
6458
Eric Dumazet9205fd92011-11-18 06:47:01 +00006459 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006461 memcpy(skb->data,
6462 data + TG3_RX_OFFSET(tp),
6463 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 }
6466
Eric Dumazet9205fd92011-11-18 06:47:01 +00006467 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006468 if (tstamp)
6469 tg3_hwclock_to_timestamp(tp, tstamp,
6470 skb_hwtstamps(skb));
6471
Michał Mirosławdc668912011-04-07 03:35:07 +00006472 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6474 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6475 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6476 skb->ip_summed = CHECKSUM_UNNECESSARY;
6477 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006478 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
6480 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006481
6482 if (len > (tp->dev->mtu + ETH_HLEN) &&
6483 skb->protocol != htons(ETH_P_8021Q)) {
6484 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006485 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006486 }
6487
Matt Carlson9dc7a112010-04-12 06:58:28 +00006488 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006489 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6490 __vlan_hwaccel_put_tag(skb,
6491 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006492
Matt Carlsonbf933c82011-01-25 15:58:49 +00006493 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 received++;
6496 budget--;
6497
6498next_pkt:
6499 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006500
6501 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006502 tpr->rx_std_prod_idx = std_prod_idx &
6503 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006504 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6505 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006506 work_mask &= ~RXD_OPAQUE_RING_STD;
6507 rx_std_posted = 0;
6508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006510 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006511 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006512
6513 /* Refresh hw_idx to see if there is new work */
6514 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006515 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006516 rmb();
6517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 }
6519
6520 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006521 tnapi->rx_rcb_ptr = sw_idx;
6522 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
6524 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006525 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006526 /* Sync BD data before updating mailbox */
6527 wmb();
6528
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006529 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006530 tpr->rx_std_prod_idx = std_prod_idx &
6531 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006532 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6533 tpr->rx_std_prod_idx);
6534 }
6535 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006536 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6537 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006538 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6539 tpr->rx_jmb_prod_idx);
6540 }
6541 mmiowb();
6542 } else if (work_mask) {
6543 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6544 * updated before the producer indices can be updated.
6545 */
6546 smp_wmb();
6547
Matt Carlson2c49a442010-09-30 10:34:35 +00006548 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6549 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006550
Michael Chan7ae52892012-03-21 15:38:33 +00006551 if (tnapi != &tp->napi[1]) {
6552 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006553 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
6557 return received;
6558}
6559
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006560static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006563 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006564 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 if (sblk->status & SD_STATUS_LINK_CHG) {
6567 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006568 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006569 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006570 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006571 tw32_f(MAC_STATUS,
6572 (MAC_STATUS_SYNC_CHANGED |
6573 MAC_STATUS_CFG_CHANGED |
6574 MAC_STATUS_MI_COMPLETION |
6575 MAC_STATUS_LNKSTATE_CHANGED));
6576 udelay(40);
6577 } else
6578 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006579 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 }
6581 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006582}
6583
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006584static int tg3_rx_prodring_xfer(struct tg3 *tp,
6585 struct tg3_rx_prodring_set *dpr,
6586 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006587{
6588 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006589 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006590
6591 while (1) {
6592 src_prod_idx = spr->rx_std_prod_idx;
6593
6594 /* Make sure updates to the rx_std_buffers[] entries and the
6595 * standard producer index are seen in the correct order.
6596 */
6597 smp_rmb();
6598
6599 if (spr->rx_std_cons_idx == src_prod_idx)
6600 break;
6601
6602 if (spr->rx_std_cons_idx < src_prod_idx)
6603 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6604 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006605 cpycnt = tp->rx_std_ring_mask + 1 -
6606 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006607
Matt Carlson2c49a442010-09-30 10:34:35 +00006608 cpycnt = min(cpycnt,
6609 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006610
6611 si = spr->rx_std_cons_idx;
6612 di = dpr->rx_std_prod_idx;
6613
Matt Carlsone92967b2010-02-12 14:47:06 +00006614 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006615 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006616 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006617 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006618 break;
6619 }
6620 }
6621
6622 if (!cpycnt)
6623 break;
6624
6625 /* Ensure that updates to the rx_std_buffers ring and the
6626 * shadowed hardware producer ring from tg3_recycle_skb() are
6627 * ordered correctly WRT the skb check above.
6628 */
6629 smp_rmb();
6630
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006631 memcpy(&dpr->rx_std_buffers[di],
6632 &spr->rx_std_buffers[si],
6633 cpycnt * sizeof(struct ring_info));
6634
6635 for (i = 0; i < cpycnt; i++, di++, si++) {
6636 struct tg3_rx_buffer_desc *sbd, *dbd;
6637 sbd = &spr->rx_std[si];
6638 dbd = &dpr->rx_std[di];
6639 dbd->addr_hi = sbd->addr_hi;
6640 dbd->addr_lo = sbd->addr_lo;
6641 }
6642
Matt Carlson2c49a442010-09-30 10:34:35 +00006643 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6644 tp->rx_std_ring_mask;
6645 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6646 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006647 }
6648
6649 while (1) {
6650 src_prod_idx = spr->rx_jmb_prod_idx;
6651
6652 /* Make sure updates to the rx_jmb_buffers[] entries and
6653 * the jumbo producer index are seen in the correct order.
6654 */
6655 smp_rmb();
6656
6657 if (spr->rx_jmb_cons_idx == src_prod_idx)
6658 break;
6659
6660 if (spr->rx_jmb_cons_idx < src_prod_idx)
6661 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6662 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006663 cpycnt = tp->rx_jmb_ring_mask + 1 -
6664 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006665
6666 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006667 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006668
6669 si = spr->rx_jmb_cons_idx;
6670 di = dpr->rx_jmb_prod_idx;
6671
Matt Carlsone92967b2010-02-12 14:47:06 +00006672 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006673 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006674 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006675 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006676 break;
6677 }
6678 }
6679
6680 if (!cpycnt)
6681 break;
6682
6683 /* Ensure that updates to the rx_jmb_buffers ring and the
6684 * shadowed hardware producer ring from tg3_recycle_skb() are
6685 * ordered correctly WRT the skb check above.
6686 */
6687 smp_rmb();
6688
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006689 memcpy(&dpr->rx_jmb_buffers[di],
6690 &spr->rx_jmb_buffers[si],
6691 cpycnt * sizeof(struct ring_info));
6692
6693 for (i = 0; i < cpycnt; i++, di++, si++) {
6694 struct tg3_rx_buffer_desc *sbd, *dbd;
6695 sbd = &spr->rx_jmb[si].std;
6696 dbd = &dpr->rx_jmb[di].std;
6697 dbd->addr_hi = sbd->addr_hi;
6698 dbd->addr_lo = sbd->addr_lo;
6699 }
6700
Matt Carlson2c49a442010-09-30 10:34:35 +00006701 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6702 tp->rx_jmb_ring_mask;
6703 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6704 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006705 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006706
6707 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006708}
6709
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006710static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6711{
6712 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
6714 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006715 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006716 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006717 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006718 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 }
6720
Matt Carlsonf891ea12012-04-24 13:37:01 +00006721 if (!tnapi->rx_rcb_prod_idx)
6722 return work_done;
6723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 /* run RX thread, within the bounds set by NAPI.
6725 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006726 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006728 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006729 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Joe Perches63c3a662011-04-26 08:12:10 +00006731 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006732 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006733 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006734 u32 std_prod_idx = dpr->rx_std_prod_idx;
6735 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006736
Michael Chan7ae52892012-03-21 15:38:33 +00006737 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006738 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006739 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006740 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006741
6742 wmb();
6743
Matt Carlsone4af1af2010-02-12 14:47:05 +00006744 if (std_prod_idx != dpr->rx_std_prod_idx)
6745 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6746 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006747
Matt Carlsone4af1af2010-02-12 14:47:05 +00006748 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6749 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6750 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006751
6752 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006753
6754 if (err)
6755 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006756 }
6757
David S. Miller6f535762007-10-11 18:08:29 -07006758 return work_done;
6759}
David S. Millerf7383c22005-05-18 22:50:53 -07006760
Matt Carlsondb219972011-11-04 09:15:03 +00006761static inline void tg3_reset_task_schedule(struct tg3 *tp)
6762{
6763 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6764 schedule_work(&tp->reset_task);
6765}
6766
6767static inline void tg3_reset_task_cancel(struct tg3 *tp)
6768{
6769 cancel_work_sync(&tp->reset_task);
6770 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006771 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006772}
6773
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006774static int tg3_poll_msix(struct napi_struct *napi, int budget)
6775{
6776 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6777 struct tg3 *tp = tnapi->tp;
6778 int work_done = 0;
6779 struct tg3_hw_status *sblk = tnapi->hw_status;
6780
6781 while (1) {
6782 work_done = tg3_poll_work(tnapi, work_done, budget);
6783
Joe Perches63c3a662011-04-26 08:12:10 +00006784 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006785 goto tx_recovery;
6786
6787 if (unlikely(work_done >= budget))
6788 break;
6789
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006790 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006791 * to tell the hw how much work has been processed,
6792 * so we must read it before checking for more work.
6793 */
6794 tnapi->last_tag = sblk->status_tag;
6795 tnapi->last_irq_tag = tnapi->last_tag;
6796 rmb();
6797
6798 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006799 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6800 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006801
6802 /* This test here is not race free, but will reduce
6803 * the number of interrupts by looping again.
6804 */
6805 if (tnapi == &tp->napi[1] && tp->rx_refill)
6806 continue;
6807
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006808 napi_complete(napi);
6809 /* Reenable interrupts. */
6810 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006811
6812 /* This test here is synchronized by napi_schedule()
6813 * and napi_complete() to close the race condition.
6814 */
6815 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6816 tw32(HOSTCC_MODE, tp->coalesce_mode |
6817 HOSTCC_MODE_ENABLE |
6818 tnapi->coal_now);
6819 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006820 mmiowb();
6821 break;
6822 }
6823 }
6824
6825 return work_done;
6826
6827tx_recovery:
6828 /* work_done is guaranteed to be less than budget. */
6829 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006830 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006831 return work_done;
6832}
6833
Matt Carlsone64de4e2011-04-13 11:05:05 +00006834static void tg3_process_error(struct tg3 *tp)
6835{
6836 u32 val;
6837 bool real_error = false;
6838
Joe Perches63c3a662011-04-26 08:12:10 +00006839 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006840 return;
6841
6842 /* Check Flow Attention register */
6843 val = tr32(HOSTCC_FLOW_ATTN);
6844 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6845 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6846 real_error = true;
6847 }
6848
6849 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6850 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6851 real_error = true;
6852 }
6853
6854 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6855 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6856 real_error = true;
6857 }
6858
6859 if (!real_error)
6860 return;
6861
6862 tg3_dump_state(tp);
6863
Joe Perches63c3a662011-04-26 08:12:10 +00006864 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006865 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006866}
6867
David S. Miller6f535762007-10-11 18:08:29 -07006868static int tg3_poll(struct napi_struct *napi, int budget)
6869{
Matt Carlson8ef04422009-08-28 14:01:37 +00006870 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6871 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006872 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006873 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006874
6875 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006876 if (sblk->status & SD_STATUS_ERROR)
6877 tg3_process_error(tp);
6878
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006879 tg3_poll_link(tp);
6880
Matt Carlson17375d22009-08-28 14:02:18 +00006881 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006882
Joe Perches63c3a662011-04-26 08:12:10 +00006883 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006884 goto tx_recovery;
6885
6886 if (unlikely(work_done >= budget))
6887 break;
6888
Joe Perches63c3a662011-04-26 08:12:10 +00006889 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006890 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006891 * to tell the hw how much work has been processed,
6892 * so we must read it before checking for more work.
6893 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006894 tnapi->last_tag = sblk->status_tag;
6895 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006896 rmb();
6897 } else
6898 sblk->status &= ~SD_STATUS_UPDATED;
6899
Matt Carlson17375d22009-08-28 14:02:18 +00006900 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006901 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006902 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006903 break;
6904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 }
6906
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006907 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006908
6909tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006910 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006911 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006912 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006913 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914}
6915
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006916static void tg3_napi_disable(struct tg3 *tp)
6917{
6918 int i;
6919
6920 for (i = tp->irq_cnt - 1; i >= 0; i--)
6921 napi_disable(&tp->napi[i].napi);
6922}
6923
6924static void tg3_napi_enable(struct tg3 *tp)
6925{
6926 int i;
6927
6928 for (i = 0; i < tp->irq_cnt; i++)
6929 napi_enable(&tp->napi[i].napi);
6930}
6931
6932static void tg3_napi_init(struct tg3 *tp)
6933{
6934 int i;
6935
6936 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6937 for (i = 1; i < tp->irq_cnt; i++)
6938 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6939}
6940
6941static void tg3_napi_fini(struct tg3 *tp)
6942{
6943 int i;
6944
6945 for (i = 0; i < tp->irq_cnt; i++)
6946 netif_napi_del(&tp->napi[i].napi);
6947}
6948
6949static inline void tg3_netif_stop(struct tg3 *tp)
6950{
6951 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6952 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006953 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006954 netif_tx_disable(tp->dev);
6955}
6956
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006957/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006958static inline void tg3_netif_start(struct tg3 *tp)
6959{
Matt Carlsonbe947302012-12-03 19:36:57 +00006960 tg3_ptp_resume(tp);
6961
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006962 /* NOTE: unconditional netif_tx_wake_all_queues is only
6963 * appropriate so long as all callers are assured to
6964 * have free tx slots (such as after tg3_init_hw)
6965 */
6966 netif_tx_wake_all_queues(tp->dev);
6967
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006968 if (tp->link_up)
6969 netif_carrier_on(tp->dev);
6970
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006971 tg3_napi_enable(tp);
6972 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6973 tg3_enable_ints(tp);
6974}
6975
David S. Millerf47c11e2005-06-24 20:18:35 -07006976static void tg3_irq_quiesce(struct tg3 *tp)
6977{
Matt Carlson4f125f42009-09-01 12:55:02 +00006978 int i;
6979
David S. Millerf47c11e2005-06-24 20:18:35 -07006980 BUG_ON(tp->irq_sync);
6981
6982 tp->irq_sync = 1;
6983 smp_mb();
6984
Matt Carlson4f125f42009-09-01 12:55:02 +00006985 for (i = 0; i < tp->irq_cnt; i++)
6986 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006987}
6988
David S. Millerf47c11e2005-06-24 20:18:35 -07006989/* Fully shutdown all tg3 driver activity elsewhere in the system.
6990 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6991 * with as well. Most of the time, this is not necessary except when
6992 * shutting down the device.
6993 */
6994static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6995{
Michael Chan46966542007-07-11 19:47:19 -07006996 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006997 if (irq_sync)
6998 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006999}
7000
7001static inline void tg3_full_unlock(struct tg3 *tp)
7002{
David S. Millerf47c11e2005-06-24 20:18:35 -07007003 spin_unlock_bh(&tp->lock);
7004}
7005
Michael Chanfcfa0a32006-03-20 22:28:41 -08007006/* One-shot MSI handler - Chip automatically disables interrupt
7007 * after sending MSI so driver doesn't have to do it.
7008 */
David Howells7d12e782006-10-05 14:55:46 +01007009static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007010{
Matt Carlson09943a12009-08-28 14:01:57 +00007011 struct tg3_napi *tnapi = dev_id;
7012 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007013
Matt Carlson898a56f2009-08-28 14:02:40 +00007014 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007015 if (tnapi->rx_rcb)
7016 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007017
7018 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007019 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007020
7021 return IRQ_HANDLED;
7022}
7023
Michael Chan88b06bc22005-04-21 17:13:25 -07007024/* MSI ISR - No need to check for interrupt sharing and no need to
7025 * flush status block and interrupt mailbox. PCI ordering rules
7026 * guarantee that MSI will arrive after the status block.
7027 */
David Howells7d12e782006-10-05 14:55:46 +01007028static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007029{
Matt Carlson09943a12009-08-28 14:01:57 +00007030 struct tg3_napi *tnapi = dev_id;
7031 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007032
Matt Carlson898a56f2009-08-28 14:02:40 +00007033 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007034 if (tnapi->rx_rcb)
7035 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007036 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007037 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007038 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007039 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007040 * NIC to stop sending us irqs, engaging "in-intr-handler"
7041 * event coalescing.
7042 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007043 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007044 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007045 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007046
Michael Chan88b06bc22005-04-21 17:13:25 -07007047 return IRQ_RETVAL(1);
7048}
7049
David Howells7d12e782006-10-05 14:55:46 +01007050static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051{
Matt Carlson09943a12009-08-28 14:01:57 +00007052 struct tg3_napi *tnapi = dev_id;
7053 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007054 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 unsigned int handled = 1;
7056
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 /* In INTx mode, it is possible for the interrupt to arrive at
7058 * the CPU before the status block posted prior to the interrupt.
7059 * Reading the PCI State register will confirm whether the
7060 * interrupt is ours and will flush the status block.
7061 */
Michael Chand18edcb2007-03-24 20:57:11 -07007062 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007063 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007064 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7065 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007066 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007067 }
Michael Chand18edcb2007-03-24 20:57:11 -07007068 }
7069
7070 /*
7071 * Writing any value to intr-mbox-0 clears PCI INTA# and
7072 * chip-internal interrupt pending events.
7073 * Writing non-zero to intr-mbox-0 additional tells the
7074 * NIC to stop sending us irqs, engaging "in-intr-handler"
7075 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007076 *
7077 * Flush the mailbox to de-assert the IRQ immediately to prevent
7078 * spurious interrupts. The flush impacts performance but
7079 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007080 */
Michael Chanc04cb342007-05-07 00:26:15 -07007081 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007082 if (tg3_irq_sync(tp))
7083 goto out;
7084 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007085 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007086 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007087 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007088 } else {
7089 /* No work, shared interrupt perhaps? re-enable
7090 * interrupts, and flush that PCI write
7091 */
7092 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7093 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007094 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007095out:
David S. Millerfac9b832005-05-18 22:46:34 -07007096 return IRQ_RETVAL(handled);
7097}
7098
David Howells7d12e782006-10-05 14:55:46 +01007099static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007100{
Matt Carlson09943a12009-08-28 14:01:57 +00007101 struct tg3_napi *tnapi = dev_id;
7102 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007103 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007104 unsigned int handled = 1;
7105
David S. Millerfac9b832005-05-18 22:46:34 -07007106 /* In INTx mode, it is possible for the interrupt to arrive at
7107 * the CPU before the status block posted prior to the interrupt.
7108 * Reading the PCI State register will confirm whether the
7109 * interrupt is ours and will flush the status block.
7110 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007111 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007112 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007113 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7114 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 }
Michael Chand18edcb2007-03-24 20:57:11 -07007117 }
7118
7119 /*
7120 * writing any value to intr-mbox-0 clears PCI INTA# and
7121 * chip-internal interrupt pending events.
7122 * writing non-zero to intr-mbox-0 additional tells the
7123 * NIC to stop sending us irqs, engaging "in-intr-handler"
7124 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007125 *
7126 * Flush the mailbox to de-assert the IRQ immediately to prevent
7127 * spurious interrupts. The flush impacts performance but
7128 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007129 */
Michael Chanc04cb342007-05-07 00:26:15 -07007130 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007131
7132 /*
7133 * In a shared interrupt configuration, sometimes other devices'
7134 * interrupts will scream. We record the current status tag here
7135 * so that the above check can report that the screaming interrupts
7136 * are unhandled. Eventually they will be silenced.
7137 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007138 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007139
Michael Chand18edcb2007-03-24 20:57:11 -07007140 if (tg3_irq_sync(tp))
7141 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007142
Matt Carlson72334482009-08-28 14:03:01 +00007143 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007144
Matt Carlson09943a12009-08-28 14:01:57 +00007145 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007146
David S. Millerf47c11e2005-06-24 20:18:35 -07007147out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 return IRQ_RETVAL(handled);
7149}
7150
Michael Chan79381092005-04-21 17:13:59 -07007151/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007152static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007153{
Matt Carlson09943a12009-08-28 14:01:57 +00007154 struct tg3_napi *tnapi = dev_id;
7155 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007156 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007157
Michael Chanf9804dd2005-09-27 12:13:10 -07007158 if ((sblk->status & SD_STATUS_UPDATED) ||
7159 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007160 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007161 return IRQ_RETVAL(1);
7162 }
7163 return IRQ_RETVAL(0);
7164}
7165
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166#ifdef CONFIG_NET_POLL_CONTROLLER
7167static void tg3_poll_controller(struct net_device *dev)
7168{
Matt Carlson4f125f42009-09-01 12:55:02 +00007169 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007170 struct tg3 *tp = netdev_priv(dev);
7171
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007172 if (tg3_irq_sync(tp))
7173 return;
7174
Matt Carlson4f125f42009-09-01 12:55:02 +00007175 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007176 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177}
7178#endif
7179
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180static void tg3_tx_timeout(struct net_device *dev)
7181{
7182 struct tg3 *tp = netdev_priv(dev);
7183
Michael Chanb0408752007-02-13 12:18:30 -08007184 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007185 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007186 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188
Matt Carlsondb219972011-11-04 09:15:03 +00007189 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190}
7191
Michael Chanc58ec932005-09-17 00:46:27 -07007192/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7193static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7194{
7195 u32 base = (u32) mapping & 0xffffffff;
7196
Eric Dumazet807540b2010-09-23 05:40:09 +00007197 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007198}
7199
Michael Chan72f2afb2006-03-06 19:28:35 -08007200/* Test for DMA addresses > 40-bit */
7201static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7202 int len)
7203{
7204#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007205 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007206 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007207 return 0;
7208#else
7209 return 0;
7210#endif
7211}
7212
Matt Carlsond1a3b732011-07-27 14:20:51 +00007213static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007214 dma_addr_t mapping, u32 len, u32 flags,
7215 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007216{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007217 txbd->addr_hi = ((u64) mapping >> 32);
7218 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7219 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7220 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
Matt Carlson84b67b22011-07-27 14:20:52 +00007223static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007224 dma_addr_t map, u32 len, u32 flags,
7225 u32 mss, u32 vlan)
7226{
7227 struct tg3 *tp = tnapi->tp;
7228 bool hwbug = false;
7229
7230 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007231 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007232
7233 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007234 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007235
7236 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007237 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007238
Matt Carlsona4cb4282011-12-14 11:09:58 +00007239 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007240 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007241 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007242 while (len > tp->dma_limit && *budget) {
7243 u32 frag_len = tp->dma_limit;
7244 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007245
Matt Carlsonb9e45482011-11-04 09:14:59 +00007246 /* Avoid the 8byte DMA problem */
7247 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007248 len += tp->dma_limit / 2;
7249 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007250 }
7251
Matt Carlsonb9e45482011-11-04 09:14:59 +00007252 tnapi->tx_buffers[*entry].fragmented = true;
7253
7254 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7255 frag_len, tmp_flag, mss, vlan);
7256 *budget -= 1;
7257 prvidx = *entry;
7258 *entry = NEXT_TX(*entry);
7259
Matt Carlsone31aa982011-07-27 14:20:53 +00007260 map += frag_len;
7261 }
7262
7263 if (len) {
7264 if (*budget) {
7265 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7266 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007267 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007268 *entry = NEXT_TX(*entry);
7269 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007270 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007271 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007272 }
7273 }
7274 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007275 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7276 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007277 *entry = NEXT_TX(*entry);
7278 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007279
7280 return hwbug;
7281}
7282
Matt Carlson0d681b22011-07-27 14:20:49 +00007283static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007284{
7285 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007286 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007287 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007288
Matt Carlson0d681b22011-07-27 14:20:49 +00007289 skb = txb->skb;
7290 txb->skb = NULL;
7291
Matt Carlson432aa7e2011-05-19 12:12:45 +00007292 pci_unmap_single(tnapi->tp->pdev,
7293 dma_unmap_addr(txb, mapping),
7294 skb_headlen(skb),
7295 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007296
7297 while (txb->fragmented) {
7298 txb->fragmented = false;
7299 entry = NEXT_TX(entry);
7300 txb = &tnapi->tx_buffers[entry];
7301 }
7302
Matt Carlsonba1142e2011-11-04 09:15:00 +00007303 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007304 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007305
7306 entry = NEXT_TX(entry);
7307 txb = &tnapi->tx_buffers[entry];
7308
7309 pci_unmap_page(tnapi->tp->pdev,
7310 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007311 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007312
7313 while (txb->fragmented) {
7314 txb->fragmented = false;
7315 entry = NEXT_TX(entry);
7316 txb = &tnapi->tx_buffers[entry];
7317 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007318 }
7319}
7320
Michael Chan72f2afb2006-03-06 19:28:35 -08007321/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007322static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007323 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007324 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007325 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007327 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007328 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007329 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007330 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331
Joe Perches41535772013-02-16 11:20:04 +00007332 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007333 new_skb = skb_copy(skb, GFP_ATOMIC);
7334 else {
7335 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7336
7337 new_skb = skb_copy_expand(skb,
7338 skb_headroom(skb) + more_headroom,
7339 skb_tailroom(skb), GFP_ATOMIC);
7340 }
7341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007343 ret = -1;
7344 } else {
7345 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007346 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7347 PCI_DMA_TODEVICE);
7348 /* Make sure the mapping succeeded */
7349 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007350 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007351 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007352 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007353 u32 save_entry = *entry;
7354
Matt Carlson92cd3a12011-07-27 14:20:47 +00007355 base_flags |= TXD_FLAG_END;
7356
Matt Carlson84b67b22011-07-27 14:20:52 +00007357 tnapi->tx_buffers[*entry].skb = new_skb;
7358 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007359 mapping, new_addr);
7360
Matt Carlson84b67b22011-07-27 14:20:52 +00007361 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007362 new_skb->len, base_flags,
7363 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007364 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007365 dev_kfree_skb(new_skb);
7366 ret = -1;
7367 }
Michael Chanc58ec932005-09-17 00:46:27 -07007368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 }
7370
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007372 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374}
7375
Matt Carlson2ffcc982011-05-19 12:12:44 +00007376static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007377
7378/* Use GSO to workaround a rare TSO bug that may be triggered when the
7379 * TSO header is greater than 80 bytes.
7380 */
7381static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7382{
7383 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007384 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007385
7386 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007387 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007388 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007389
7390 /* netif_tx_stop_queue() must be done before checking
7391 * checking tx index in tg3_tx_avail() below, because in
7392 * tg3_tx(), we update tx index before checking for
7393 * netif_tx_queue_stopped().
7394 */
7395 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007396 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007397 return NETDEV_TX_BUSY;
7398
7399 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007400 }
7401
7402 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007403 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007404 goto tg3_tso_bug_end;
7405
7406 do {
7407 nskb = segs;
7408 segs = segs->next;
7409 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007410 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007411 } while (segs);
7412
7413tg3_tso_bug_end:
7414 dev_kfree_skb(skb);
7415
7416 return NETDEV_TX_OK;
7417}
Michael Chan52c0fd82006-06-29 20:15:54 -07007418
Michael Chan5a6f3072006-03-20 22:28:05 -08007419/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007420 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007421 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007422static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007423{
7424 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007425 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007426 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007427 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007428 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007429 struct tg3_napi *tnapi;
7430 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007431 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007432
Matt Carlson24f4efd2009-11-13 13:03:35 +00007433 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7434 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007435 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007436 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
Matt Carlson84b67b22011-07-27 14:20:52 +00007438 budget = tg3_tx_avail(tnapi);
7439
Michael Chan00b70502006-06-17 21:58:45 -07007440 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007441 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007442 * interrupt. Furthermore, IRQ processing runs lockless so we have
7443 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007445 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007446 if (!netif_tx_queue_stopped(txq)) {
7447 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007448
7449 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007450 netdev_err(dev,
7451 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 return NETDEV_TX_BUSY;
7454 }
7455
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007456 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007458 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007460
Matt Carlsonbe98da62010-07-11 09:31:46 +00007461 mss = skb_shinfo(skb)->gso_size;
7462 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007463 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007464 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
7466 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007467 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7468 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
Matt Carlson34195c32010-07-11 09:31:42 +00007470 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007471 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472
Eric Dumazeta5a11952012-01-23 01:22:09 +00007473 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007474
Eric Dumazeta5a11952012-01-23 01:22:09 +00007475 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007476 iph->check = 0;
7477 iph->tot_len = htons(mss + hdr_len);
7478 }
7479
Michael Chan52c0fd82006-06-29 20:15:54 -07007480 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007481 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007482 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7485 TXD_FLAG_CPU_POST_DMA);
7486
Joe Perches63c3a662011-04-26 08:12:10 +00007487 if (tg3_flag(tp, HW_TSO_1) ||
7488 tg3_flag(tp, HW_TSO_2) ||
7489 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007490 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007492 } else
7493 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7494 iph->daddr, 0,
7495 IPPROTO_TCP,
7496 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
Joe Perches63c3a662011-04-26 08:12:10 +00007498 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007499 mss |= (hdr_len & 0xc) << 12;
7500 if (hdr_len & 0x10)
7501 base_flags |= 0x00000010;
7502 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007503 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007504 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007505 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007506 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007507 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 int tsflags;
7509
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007510 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 mss |= (tsflags << 11);
7512 }
7513 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007514 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 int tsflags;
7516
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007517 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 base_flags |= tsflags << 12;
7519 }
7520 }
7521 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007522
Matt Carlson93a700a2011-08-31 11:44:54 +00007523 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7524 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7525 base_flags |= TXD_FLAG_JMB_PKT;
7526
Matt Carlson92cd3a12011-07-27 14:20:47 +00007527 if (vlan_tx_tag_present(skb)) {
7528 base_flags |= TXD_FLAG_VLAN;
7529 vlan = vlan_tx_tag_get(skb);
7530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007532 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7533 tg3_flag(tp, TX_TSTAMP_EN)) {
7534 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7535 base_flags |= TXD_FLAG_HWTSTAMP;
7536 }
7537
Alexander Duyckf4188d82009-12-02 16:48:38 +00007538 len = skb_headlen(skb);
7539
7540 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007541 if (pci_dma_mapping_error(tp->pdev, mapping))
7542 goto drop;
7543
David S. Miller90079ce2008-09-11 04:52:51 -07007544
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007545 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007546 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547
7548 would_hit_hwbug = 0;
7549
Joe Perches63c3a662011-04-26 08:12:10 +00007550 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007551 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
Matt Carlson84b67b22011-07-27 14:20:52 +00007553 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007554 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007555 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007556 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007557 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007558 u32 tmp_mss = mss;
7559
7560 if (!tg3_flag(tp, HW_TSO_1) &&
7561 !tg3_flag(tp, HW_TSO_2) &&
7562 !tg3_flag(tp, HW_TSO_3))
7563 tmp_mss = 0;
7564
Matt Carlsonc5665a52012-02-13 10:20:12 +00007565 /* Now loop through additional data
7566 * fragments, and queue them.
7567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 last = skb_shinfo(skb)->nr_frags - 1;
7569 for (i = 0; i <= last; i++) {
7570 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7571
Eric Dumazet9e903e02011-10-18 21:00:24 +00007572 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007573 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007574 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007576 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007577 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007578 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007579 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007580 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
Matt Carlsonb9e45482011-11-04 09:14:59 +00007582 if (!budget ||
7583 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007584 len, base_flags |
7585 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007586 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007587 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007588 break;
7589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 }
7591 }
7592
7593 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007594 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595
7596 /* If the workaround fails due to memory/mapping
7597 * failure, silently drop this packet.
7598 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007599 entry = tnapi->tx_prod;
7600 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007601 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007602 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007603 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 }
7605
Richard Cochrand515b452011-06-19 03:31:41 +00007606 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007607 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007608
Michael Chan6541b802012-03-04 14:48:14 +00007609 /* Sync BD data before updating mailbox */
7610 wmb();
7611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007613 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007615 tnapi->tx_prod = entry;
7616 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007617 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007618
7619 /* netif_tx_stop_queue() must be done before checking
7620 * checking tx index in tg3_tx_avail() below, because in
7621 * tg3_tx(), we update tx index before checking for
7622 * netif_tx_queue_stopped().
7623 */
7624 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007625 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007626 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007629 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007631
7632dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007633 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007634 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007635drop:
7636 dev_kfree_skb(skb);
7637drop_nofree:
7638 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007639 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640}
7641
Matt Carlson6e01b202011-08-19 13:58:20 +00007642static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7643{
7644 if (enable) {
7645 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7646 MAC_MODE_PORT_MODE_MASK);
7647
7648 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7649
7650 if (!tg3_flag(tp, 5705_PLUS))
7651 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7652
7653 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7654 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7655 else
7656 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7657 } else {
7658 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7659
7660 if (tg3_flag(tp, 5705_PLUS) ||
7661 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007662 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007663 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7664 }
7665
7666 tw32(MAC_MODE, tp->mac_mode);
7667 udelay(40);
7668}
7669
Matt Carlson941ec902011-08-19 13:58:23 +00007670static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007671{
Matt Carlson941ec902011-08-19 13:58:23 +00007672 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007673
7674 tg3_phy_toggle_apd(tp, false);
7675 tg3_phy_toggle_automdix(tp, 0);
7676
Matt Carlson941ec902011-08-19 13:58:23 +00007677 if (extlpbk && tg3_phy_set_extloopbk(tp))
7678 return -EIO;
7679
7680 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007681 switch (speed) {
7682 case SPEED_10:
7683 break;
7684 case SPEED_100:
7685 bmcr |= BMCR_SPEED100;
7686 break;
7687 case SPEED_1000:
7688 default:
7689 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7690 speed = SPEED_100;
7691 bmcr |= BMCR_SPEED100;
7692 } else {
7693 speed = SPEED_1000;
7694 bmcr |= BMCR_SPEED1000;
7695 }
7696 }
7697
Matt Carlson941ec902011-08-19 13:58:23 +00007698 if (extlpbk) {
7699 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7700 tg3_readphy(tp, MII_CTRL1000, &val);
7701 val |= CTL1000_AS_MASTER |
7702 CTL1000_ENABLE_MASTER;
7703 tg3_writephy(tp, MII_CTRL1000, val);
7704 } else {
7705 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7706 MII_TG3_FET_PTEST_TRIM_2;
7707 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7708 }
7709 } else
7710 bmcr |= BMCR_LOOPBACK;
7711
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007712 tg3_writephy(tp, MII_BMCR, bmcr);
7713
7714 /* The write needs to be flushed for the FETs */
7715 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7716 tg3_readphy(tp, MII_BMCR, &bmcr);
7717
7718 udelay(40);
7719
7720 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007721 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007722 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007723 MII_TG3_FET_PTEST_FRC_TX_LINK |
7724 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7725
7726 /* The write needs to be flushed for the AC131 */
7727 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7728 }
7729
7730 /* Reset to prevent losing 1st rx packet intermittently */
7731 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7732 tg3_flag(tp, 5780_CLASS)) {
7733 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7734 udelay(10);
7735 tw32_f(MAC_RX_MODE, tp->rx_mode);
7736 }
7737
7738 mac_mode = tp->mac_mode &
7739 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7740 if (speed == SPEED_1000)
7741 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7742 else
7743 mac_mode |= MAC_MODE_PORT_MODE_MII;
7744
Joe Perches41535772013-02-16 11:20:04 +00007745 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007746 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7747
7748 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7749 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7750 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7751 mac_mode |= MAC_MODE_LINK_POLARITY;
7752
7753 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7754 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7755 }
7756
7757 tw32(MAC_MODE, mac_mode);
7758 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007759
7760 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007761}
7762
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007763static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007764{
7765 struct tg3 *tp = netdev_priv(dev);
7766
7767 if (features & NETIF_F_LOOPBACK) {
7768 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7769 return;
7770
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007771 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007772 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007773 netif_carrier_on(tp->dev);
7774 spin_unlock_bh(&tp->lock);
7775 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7776 } else {
7777 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7778 return;
7779
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007780 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007781 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007782 /* Force link status check */
7783 tg3_setup_phy(tp, 1);
7784 spin_unlock_bh(&tp->lock);
7785 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7786 }
7787}
7788
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007789static netdev_features_t tg3_fix_features(struct net_device *dev,
7790 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007791{
7792 struct tg3 *tp = netdev_priv(dev);
7793
Joe Perches63c3a662011-04-26 08:12:10 +00007794 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007795 features &= ~NETIF_F_ALL_TSO;
7796
7797 return features;
7798}
7799
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007800static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007801{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007802 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007803
7804 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7805 tg3_set_loopback(dev, features);
7806
7807 return 0;
7808}
7809
Matt Carlson21f581a2009-08-28 14:00:25 +00007810static void tg3_rx_prodring_free(struct tg3 *tp,
7811 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813 int i;
7814
Matt Carlson8fea32b2010-09-15 08:59:58 +00007815 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007816 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007817 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007818 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007819 tp->rx_pkt_map_sz);
7820
Joe Perches63c3a662011-04-26 08:12:10 +00007821 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007822 for (i = tpr->rx_jmb_cons_idx;
7823 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007824 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007825 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007826 TG3_RX_JMB_MAP_SZ);
7827 }
7828 }
7829
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007830 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
Matt Carlson2c49a442010-09-30 10:34:35 +00007833 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007834 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007835 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
Joe Perches63c3a662011-04-26 08:12:10 +00007837 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007838 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007839 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007840 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841 }
7842}
7843
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007844/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007845 *
7846 * The chip has been shut down and the driver detached from
7847 * the networking, so no interrupts or new tx packets will
7848 * end up in the driver. tp->{tx,}lock are held and thus
7849 * we may not sleep.
7850 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007851static int tg3_rx_prodring_alloc(struct tg3 *tp,
7852 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853{
Matt Carlson287be122009-08-28 13:58:46 +00007854 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007856 tpr->rx_std_cons_idx = 0;
7857 tpr->rx_std_prod_idx = 0;
7858 tpr->rx_jmb_cons_idx = 0;
7859 tpr->rx_jmb_prod_idx = 0;
7860
Matt Carlson8fea32b2010-09-15 08:59:58 +00007861 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007862 memset(&tpr->rx_std_buffers[0], 0,
7863 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007864 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007865 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007866 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007867 goto done;
7868 }
7869
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007871 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872
Matt Carlson287be122009-08-28 13:58:46 +00007873 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007874 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007875 tp->dev->mtu > ETH_DATA_LEN)
7876 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7877 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007878
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 /* Initialize invariants of the rings, we only set this
7880 * stuff once. This works because the card does not
7881 * write into the rx buffer posting rings.
7882 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007883 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884 struct tg3_rx_buffer_desc *rxd;
7885
Matt Carlson21f581a2009-08-28 14:00:25 +00007886 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007887 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7889 rxd->opaque = (RXD_OPAQUE_RING_STD |
7890 (i << RXD_OPAQUE_INDEX_SHIFT));
7891 }
7892
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007893 /* Now allocate fresh SKBs for each rx ring. */
7894 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007895 unsigned int frag_size;
7896
7897 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7898 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007899 netdev_warn(tp->dev,
7900 "Using a smaller RX standard ring. Only "
7901 "%d out of %d buffers were allocated "
7902 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007903 if (i == 0)
7904 goto initfail;
7905 tp->rx_pending = i;
7906 break;
7907 }
7908 }
7909
Joe Perches63c3a662011-04-26 08:12:10 +00007910 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007911 goto done;
7912
Matt Carlson2c49a442010-09-30 10:34:35 +00007913 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007914
Joe Perches63c3a662011-04-26 08:12:10 +00007915 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007916 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917
Matt Carlson2c49a442010-09-30 10:34:35 +00007918 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007919 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920
Matt Carlson0d86df82010-02-17 15:17:00 +00007921 rxd = &tpr->rx_jmb[i].std;
7922 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7923 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7924 RXD_FLAG_JUMBO;
7925 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7926 (i << RXD_OPAQUE_INDEX_SHIFT));
7927 }
7928
7929 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007930 unsigned int frag_size;
7931
7932 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7933 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007934 netdev_warn(tp->dev,
7935 "Using a smaller RX jumbo ring. Only %d "
7936 "out of %d buffers were allocated "
7937 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007938 if (i == 0)
7939 goto initfail;
7940 tp->rx_jumbo_pending = i;
7941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 }
7943 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007944
7945done:
Michael Chan32d8c572006-07-25 16:38:29 -07007946 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007947
7948initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007949 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007950 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951}
7952
Matt Carlson21f581a2009-08-28 14:00:25 +00007953static void tg3_rx_prodring_fini(struct tg3 *tp,
7954 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955{
Matt Carlson21f581a2009-08-28 14:00:25 +00007956 kfree(tpr->rx_std_buffers);
7957 tpr->rx_std_buffers = NULL;
7958 kfree(tpr->rx_jmb_buffers);
7959 tpr->rx_jmb_buffers = NULL;
7960 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007961 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7962 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007963 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007965 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007966 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7967 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007968 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007969 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007970}
7971
Matt Carlson21f581a2009-08-28 14:00:25 +00007972static int tg3_rx_prodring_init(struct tg3 *tp,
7973 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007974{
Matt Carlson2c49a442010-09-30 10:34:35 +00007975 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7976 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007977 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007978 return -ENOMEM;
7979
Matt Carlson4bae65c2010-11-24 08:31:52 +00007980 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7981 TG3_RX_STD_RING_BYTES(tp),
7982 &tpr->rx_std_mapping,
7983 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007984 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007985 goto err_out;
7986
Joe Perches63c3a662011-04-26 08:12:10 +00007987 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007988 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007989 GFP_KERNEL);
7990 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007991 goto err_out;
7992
Matt Carlson4bae65c2010-11-24 08:31:52 +00007993 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7994 TG3_RX_JMB_RING_BYTES(tp),
7995 &tpr->rx_jmb_mapping,
7996 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007997 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007998 goto err_out;
7999 }
8000
8001 return 0;
8002
8003err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008004 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008005 return -ENOMEM;
8006}
8007
8008/* Free up pending packets in all rx/tx rings.
8009 *
8010 * The chip has been shut down and the driver detached from
8011 * the networking, so no interrupts or new tx packets will
8012 * end up in the driver. tp->{tx,}lock is not held and we are not
8013 * in an interrupt context and thus may sleep.
8014 */
8015static void tg3_free_rings(struct tg3 *tp)
8016{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008017 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008018
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008019 for (j = 0; j < tp->irq_cnt; j++) {
8020 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008021
Matt Carlson8fea32b2010-09-15 08:59:58 +00008022 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008023
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008024 if (!tnapi->tx_buffers)
8025 continue;
8026
Matt Carlson0d681b22011-07-27 14:20:49 +00008027 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8028 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008029
Matt Carlson0d681b22011-07-27 14:20:49 +00008030 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008031 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008032
Matt Carlsonba1142e2011-11-04 09:15:00 +00008033 tg3_tx_skb_unmap(tnapi, i,
8034 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008035
8036 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008037 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008038 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008039 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008040}
8041
8042/* Initialize tx/rx rings for packet processing.
8043 *
8044 * The chip has been shut down and the driver detached from
8045 * the networking, so no interrupts or new tx packets will
8046 * end up in the driver. tp->{tx,}lock are held and thus
8047 * we may not sleep.
8048 */
8049static int tg3_init_rings(struct tg3 *tp)
8050{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008051 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008052
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008053 /* Free up all the SKBs. */
8054 tg3_free_rings(tp);
8055
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008056 for (i = 0; i < tp->irq_cnt; i++) {
8057 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008058
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008059 tnapi->last_tag = 0;
8060 tnapi->last_irq_tag = 0;
8061 tnapi->hw_status->status = 0;
8062 tnapi->hw_status->status_tag = 0;
8063 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8064
8065 tnapi->tx_prod = 0;
8066 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008067 if (tnapi->tx_ring)
8068 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008069
8070 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008071 if (tnapi->rx_rcb)
8072 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008073
Matt Carlson8fea32b2010-09-15 08:59:58 +00008074 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008075 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008076 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008077 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008078 }
Matt Carlson72334482009-08-28 14:03:01 +00008079
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008080 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008081}
8082
Michael Chan49a359e2012-09-28 07:12:37 +00008083static void tg3_mem_tx_release(struct tg3 *tp)
8084{
8085 int i;
8086
8087 for (i = 0; i < tp->irq_max; i++) {
8088 struct tg3_napi *tnapi = &tp->napi[i];
8089
8090 if (tnapi->tx_ring) {
8091 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8092 tnapi->tx_ring, tnapi->tx_desc_mapping);
8093 tnapi->tx_ring = NULL;
8094 }
8095
8096 kfree(tnapi->tx_buffers);
8097 tnapi->tx_buffers = NULL;
8098 }
8099}
8100
8101static int tg3_mem_tx_acquire(struct tg3 *tp)
8102{
8103 int i;
8104 struct tg3_napi *tnapi = &tp->napi[0];
8105
8106 /* If multivector TSS is enabled, vector 0 does not handle
8107 * tx interrupts. Don't allocate any resources for it.
8108 */
8109 if (tg3_flag(tp, ENABLE_TSS))
8110 tnapi++;
8111
8112 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8113 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8114 TG3_TX_RING_SIZE, GFP_KERNEL);
8115 if (!tnapi->tx_buffers)
8116 goto err_out;
8117
8118 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8119 TG3_TX_RING_BYTES,
8120 &tnapi->tx_desc_mapping,
8121 GFP_KERNEL);
8122 if (!tnapi->tx_ring)
8123 goto err_out;
8124 }
8125
8126 return 0;
8127
8128err_out:
8129 tg3_mem_tx_release(tp);
8130 return -ENOMEM;
8131}
8132
8133static void tg3_mem_rx_release(struct tg3 *tp)
8134{
8135 int i;
8136
8137 for (i = 0; i < tp->irq_max; i++) {
8138 struct tg3_napi *tnapi = &tp->napi[i];
8139
8140 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8141
8142 if (!tnapi->rx_rcb)
8143 continue;
8144
8145 dma_free_coherent(&tp->pdev->dev,
8146 TG3_RX_RCB_RING_BYTES(tp),
8147 tnapi->rx_rcb,
8148 tnapi->rx_rcb_mapping);
8149 tnapi->rx_rcb = NULL;
8150 }
8151}
8152
8153static int tg3_mem_rx_acquire(struct tg3 *tp)
8154{
8155 unsigned int i, limit;
8156
8157 limit = tp->rxq_cnt;
8158
8159 /* If RSS is enabled, we need a (dummy) producer ring
8160 * set on vector zero. This is the true hw prodring.
8161 */
8162 if (tg3_flag(tp, ENABLE_RSS))
8163 limit++;
8164
8165 for (i = 0; i < limit; i++) {
8166 struct tg3_napi *tnapi = &tp->napi[i];
8167
8168 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8169 goto err_out;
8170
8171 /* If multivector RSS is enabled, vector 0
8172 * does not handle rx or tx interrupts.
8173 * Don't allocate any resources for it.
8174 */
8175 if (!i && tg3_flag(tp, ENABLE_RSS))
8176 continue;
8177
8178 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8179 TG3_RX_RCB_RING_BYTES(tp),
8180 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008181 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008182 if (!tnapi->rx_rcb)
8183 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008184 }
8185
8186 return 0;
8187
8188err_out:
8189 tg3_mem_rx_release(tp);
8190 return -ENOMEM;
8191}
8192
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008193/*
8194 * Must not be invoked with interrupt sources disabled and
8195 * the hardware shutdown down.
8196 */
8197static void tg3_free_consistent(struct tg3 *tp)
8198{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008199 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008200
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008201 for (i = 0; i < tp->irq_cnt; i++) {
8202 struct tg3_napi *tnapi = &tp->napi[i];
8203
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008204 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008205 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8206 tnapi->hw_status,
8207 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008208 tnapi->hw_status = NULL;
8209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008211
Michael Chan49a359e2012-09-28 07:12:37 +00008212 tg3_mem_rx_release(tp);
8213 tg3_mem_tx_release(tp);
8214
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008216 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8217 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218 tp->hw_stats = NULL;
8219 }
8220}
8221
8222/*
8223 * Must not be invoked with interrupt sources disabled and
8224 * the hardware shutdown down. Can sleep.
8225 */
8226static int tg3_alloc_consistent(struct tg3 *tp)
8227{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008228 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008229
Matt Carlson4bae65c2010-11-24 08:31:52 +00008230 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8231 sizeof(struct tg3_hw_stats),
8232 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008233 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 if (!tp->hw_stats)
8235 goto err_out;
8236
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008237 for (i = 0; i < tp->irq_cnt; i++) {
8238 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008239 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008240
Matt Carlson4bae65c2010-11-24 08:31:52 +00008241 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8242 TG3_HW_STATUS_SIZE,
8243 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008244 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008245 if (!tnapi->hw_status)
8246 goto err_out;
8247
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008248 sblk = tnapi->hw_status;
8249
Michael Chan49a359e2012-09-28 07:12:37 +00008250 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008251 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008252
Michael Chan49a359e2012-09-28 07:12:37 +00008253 /*
8254 * When RSS is enabled, the status block format changes
8255 * slightly. The "rx_jumbo_consumer", "reserved",
8256 * and "rx_mini_consumer" members get mapped to the
8257 * other three rx return ring producer indexes.
8258 */
8259 switch (i) {
8260 case 1:
8261 prodptr = &sblk->idx[0].rx_producer;
8262 break;
8263 case 2:
8264 prodptr = &sblk->rx_jumbo_consumer;
8265 break;
8266 case 3:
8267 prodptr = &sblk->reserved;
8268 break;
8269 case 4:
8270 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008271 break;
8272 }
Michael Chan49a359e2012-09-28 07:12:37 +00008273 tnapi->rx_rcb_prod_idx = prodptr;
8274 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008275 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008276 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008277 }
8278
Michael Chan49a359e2012-09-28 07:12:37 +00008279 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8280 goto err_out;
8281
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 return 0;
8283
8284err_out:
8285 tg3_free_consistent(tp);
8286 return -ENOMEM;
8287}
8288
8289#define MAX_WAIT_CNT 1000
8290
8291/* To stop a block, clear the enable bit and poll till it
8292 * clears. tp->lock is held.
8293 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008294static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295{
8296 unsigned int i;
8297 u32 val;
8298
Joe Perches63c3a662011-04-26 08:12:10 +00008299 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008300 switch (ofs) {
8301 case RCVLSC_MODE:
8302 case DMAC_MODE:
8303 case MBFREE_MODE:
8304 case BUFMGR_MODE:
8305 case MEMARB_MODE:
8306 /* We can't enable/disable these bits of the
8307 * 5705/5750, just say success.
8308 */
8309 return 0;
8310
8311 default:
8312 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008314 }
8315
8316 val = tr32(ofs);
8317 val &= ~enable_bit;
8318 tw32_f(ofs, val);
8319
8320 for (i = 0; i < MAX_WAIT_CNT; i++) {
8321 udelay(100);
8322 val = tr32(ofs);
8323 if ((val & enable_bit) == 0)
8324 break;
8325 }
8326
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008327 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008328 dev_err(&tp->pdev->dev,
8329 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8330 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 return -ENODEV;
8332 }
8333
8334 return 0;
8335}
8336
8337/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008338static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339{
8340 int i, err;
8341
8342 tg3_disable_ints(tp);
8343
8344 tp->rx_mode &= ~RX_MODE_ENABLE;
8345 tw32_f(MAC_RX_MODE, tp->rx_mode);
8346 udelay(10);
8347
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008348 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8349 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8350 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8351 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8352 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8353 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008355 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8356 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8357 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8358 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8359 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8360 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8361 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
8363 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8364 tw32_f(MAC_MODE, tp->mac_mode);
8365 udelay(40);
8366
8367 tp->tx_mode &= ~TX_MODE_ENABLE;
8368 tw32_f(MAC_TX_MODE, tp->tx_mode);
8369
8370 for (i = 0; i < MAX_WAIT_CNT; i++) {
8371 udelay(100);
8372 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8373 break;
8374 }
8375 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008376 dev_err(&tp->pdev->dev,
8377 "%s timed out, TX_MODE_ENABLE will not clear "
8378 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008379 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 }
8381
Michael Chane6de8ad2005-05-05 14:42:41 -07008382 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008383 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8384 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385
8386 tw32(FTQ_RESET, 0xffffffff);
8387 tw32(FTQ_RESET, 0x00000000);
8388
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008389 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8390 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008392 for (i = 0; i < tp->irq_cnt; i++) {
8393 struct tg3_napi *tnapi = &tp->napi[i];
8394 if (tnapi->hw_status)
8395 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397
Linus Torvalds1da177e2005-04-16 15:20:36 -07008398 return err;
8399}
8400
Michael Chanee6a99b2007-07-18 21:49:10 -07008401/* Save PCI command register before chip reset */
8402static void tg3_save_pci_state(struct tg3 *tp)
8403{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008404 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008405}
8406
8407/* Restore PCI state after chip reset */
8408static void tg3_restore_pci_state(struct tg3 *tp)
8409{
8410 u32 val;
8411
8412 /* Re-enable indirect register accesses. */
8413 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8414 tp->misc_host_ctrl);
8415
8416 /* Set MAX PCI retry to zero. */
8417 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008418 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008419 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008420 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008421 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008422 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008423 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008424 PCISTATE_ALLOW_APE_SHMEM_WR |
8425 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008426 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8427
Matt Carlson8a6eac92007-10-21 16:17:55 -07008428 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008429
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008430 if (!tg3_flag(tp, PCI_EXPRESS)) {
8431 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8432 tp->pci_cacheline_sz);
8433 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8434 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008435 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008436
Michael Chanee6a99b2007-07-18 21:49:10 -07008437 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008438 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008439 u16 pcix_cmd;
8440
8441 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8442 &pcix_cmd);
8443 pcix_cmd &= ~PCI_X_CMD_ERO;
8444 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8445 pcix_cmd);
8446 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008447
Joe Perches63c3a662011-04-26 08:12:10 +00008448 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008449
8450 /* Chip reset on 5780 will reset MSI enable bit,
8451 * so need to restore it.
8452 */
Joe Perches63c3a662011-04-26 08:12:10 +00008453 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008454 u16 ctrl;
8455
8456 pci_read_config_word(tp->pdev,
8457 tp->msi_cap + PCI_MSI_FLAGS,
8458 &ctrl);
8459 pci_write_config_word(tp->pdev,
8460 tp->msi_cap + PCI_MSI_FLAGS,
8461 ctrl | PCI_MSI_FLAGS_ENABLE);
8462 val = tr32(MSGINT_MODE);
8463 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8464 }
8465 }
8466}
8467
Linus Torvalds1da177e2005-04-16 15:20:36 -07008468/* tp->lock is held. */
8469static int tg3_chip_reset(struct tg3 *tp)
8470{
8471 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008472 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008473 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474
David S. Millerf49639e2006-06-09 11:58:36 -07008475 tg3_nvram_lock(tp);
8476
Matt Carlson77b483f2008-08-15 14:07:24 -07008477 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8478
David S. Millerf49639e2006-06-09 11:58:36 -07008479 /* No matching tg3_nvram_unlock() after this because
8480 * chip reset below will undo the nvram lock.
8481 */
8482 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
Michael Chanee6a99b2007-07-18 21:49:10 -07008484 /* GRC_MISC_CFG core clock reset will clear the memory
8485 * enable bit in PCI register 4 and the MSI enable bit
8486 * on some chips, so we save relevant registers here.
8487 */
8488 tg3_save_pci_state(tp);
8489
Joe Perches41535772013-02-16 11:20:04 +00008490 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008491 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008492 tw32(GRC_FASTBOOT_PC, 0);
8493
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 /*
8495 * We must avoid the readl() that normally takes place.
8496 * It locks machines, causes machine checks, and other
8497 * fun things. So, temporarily disable the 5701
8498 * hardware workaround, while we do the reset.
8499 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008500 write_op = tp->write32;
8501 if (write_op == tg3_write_flush_reg32)
8502 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008503
Michael Chand18edcb2007-03-24 20:57:11 -07008504 /* Prevent the irq handler from reading or writing PCI registers
8505 * during chip reset when the memory enable bit in the PCI command
8506 * register may be cleared. The chip does not generate interrupt
8507 * at this time, but the irq handler may still be called due to irq
8508 * sharing or irqpoll.
8509 */
Joe Perches63c3a662011-04-26 08:12:10 +00008510 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008511 for (i = 0; i < tp->irq_cnt; i++) {
8512 struct tg3_napi *tnapi = &tp->napi[i];
8513 if (tnapi->hw_status) {
8514 tnapi->hw_status->status = 0;
8515 tnapi->hw_status->status_tag = 0;
8516 }
8517 tnapi->last_tag = 0;
8518 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008519 }
Michael Chand18edcb2007-03-24 20:57:11 -07008520 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008521
8522 for (i = 0; i < tp->irq_cnt; i++)
8523 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008524
Joe Perches41535772013-02-16 11:20:04 +00008525 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008526 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8527 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8528 }
8529
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 /* do the reset */
8531 val = GRC_MISC_CFG_CORECLK_RESET;
8532
Joe Perches63c3a662011-04-26 08:12:10 +00008533 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008534 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008535 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008536 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008537 tr32(TG3_PCIE_PHY_TSTCTL) ==
8538 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8539 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8540
Joe Perches41535772013-02-16 11:20:04 +00008541 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 tw32(GRC_MISC_CFG, (1 << 29));
8543 val |= (1 << 29);
8544 }
8545 }
8546
Joe Perches41535772013-02-16 11:20:04 +00008547 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008548 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8549 tw32(GRC_VCPU_EXT_CTRL,
8550 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8551 }
8552
Matt Carlsonf37500d2010-08-02 11:25:59 +00008553 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008554 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008556
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 tw32(GRC_MISC_CFG, val);
8558
Michael Chan1ee582d2005-08-09 20:16:46 -07008559 /* restore 5701 hardware bug workaround write method */
8560 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561
8562 /* Unfortunately, we have to delay before the PCI read back.
8563 * Some 575X chips even will not respond to a PCI cfg access
8564 * when the reset command is given to the chip.
8565 *
8566 * How do these hardware designers expect things to work
8567 * properly if the PCI write is posted for a long period
8568 * of time? It is always necessary to have some method by
8569 * which a register read back can occur to push the write
8570 * out which does the reset.
8571 *
8572 * For most tg3 variants the trick below was working.
8573 * Ho hum...
8574 */
8575 udelay(120);
8576
8577 /* Flush PCI posted writes. The normal MMIO registers
8578 * are inaccessible at this time so this is the only
8579 * way to make this reliably (actually, this is no longer
8580 * the case, see above). I tried to use indirect
8581 * register read/write but this upset some 5701 variants.
8582 */
8583 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8584
8585 udelay(120);
8586
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008587 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008588 u16 val16;
8589
Joe Perches41535772013-02-16 11:20:04 +00008590 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008591 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592 u32 cfg_val;
8593
8594 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008595 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596 udelay(100);
8597
8598 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8599 pci_write_config_dword(tp->pdev, 0xc4,
8600 cfg_val | (1 << 15));
8601 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008602
Matt Carlsone7126992009-08-25 10:08:16 +00008603 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008604 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008605 /*
8606 * Older PCIe devices only support the 128 byte
8607 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008608 */
Joe Perches63c3a662011-04-26 08:12:10 +00008609 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008610 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8611 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008612
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008613 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008614 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008615 PCI_EXP_DEVSTA_CED |
8616 PCI_EXP_DEVSTA_NFED |
8617 PCI_EXP_DEVSTA_FED |
8618 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 }
8620
Michael Chanee6a99b2007-07-18 21:49:10 -07008621 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622
Joe Perches63c3a662011-04-26 08:12:10 +00008623 tg3_flag_clear(tp, CHIP_RESETTING);
8624 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008625
Michael Chanee6a99b2007-07-18 21:49:10 -07008626 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008627 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008628 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008629 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630
Joe Perches41535772013-02-16 11:20:04 +00008631 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 tg3_stop_fw(tp);
8633 tw32(0x5000, 0x400);
8634 }
8635
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008636 if (tg3_flag(tp, IS_SSB_CORE)) {
8637 /*
8638 * BCM4785: In order to avoid repercussions from using
8639 * potentially defective internal ROM, stop the Rx RISC CPU,
8640 * which is not required.
8641 */
8642 tg3_stop_fw(tp);
8643 tg3_halt_cpu(tp, RX_CPU_BASE);
8644 }
8645
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 tw32(GRC_MODE, tp->grc_mode);
8647
Joe Perches41535772013-02-16 11:20:04 +00008648 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008649 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650
8651 tw32(0xc4, val | (1 << 15));
8652 }
8653
8654 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008655 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008656 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008657 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8659 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8660 }
8661
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008662 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008663 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008664 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008665 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008666 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008667 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008669 val = 0;
8670
8671 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672 udelay(40);
8673
Matt Carlson77b483f2008-08-15 14:07:24 -07008674 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8675
Michael Chan7a6f4362006-09-27 16:03:31 -07008676 err = tg3_poll_fw(tp);
8677 if (err)
8678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679
Matt Carlson0a9140c2009-08-28 12:27:50 +00008680 tg3_mdio_start(tp);
8681
Joe Perches63c3a662011-04-26 08:12:10 +00008682 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008683 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8684 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008685 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008686 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008687
8688 tw32(0x7c00, val | (1 << 25));
8689 }
8690
Joe Perches41535772013-02-16 11:20:04 +00008691 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008692 val = tr32(TG3_CPMU_CLCK_ORIDE);
8693 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8694 }
8695
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008697 tg3_flag_clear(tp, ENABLE_ASF);
8698 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8700 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8701 u32 nic_cfg;
8702
8703 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8704 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008705 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008706 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008707 if (tg3_flag(tp, 5750_PLUS))
8708 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008709 }
8710 }
8711
8712 return 0;
8713}
8714
Matt Carlson65ec6982012-02-28 23:33:37 +00008715static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8716static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008717
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008719static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008720{
8721 int err;
8722
8723 tg3_stop_fw(tp);
8724
Michael Chan944d9802005-05-29 14:57:48 -07008725 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008727 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008728 err = tg3_chip_reset(tp);
8729
Matt Carlsondaba2a62009-04-20 06:58:52 +00008730 __tg3_set_mac_addr(tp, 0);
8731
Michael Chan944d9802005-05-29 14:57:48 -07008732 tg3_write_sig_legacy(tp, kind);
8733 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734
Matt Carlson92feeab2011-12-08 14:40:14 +00008735 if (tp->hw_stats) {
8736 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008737 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008738 tg3_get_estats(tp, &tp->estats_prev);
8739
8740 /* And make sure the next sample is new data */
8741 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8742 }
8743
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 if (err)
8745 return err;
8746
8747 return 0;
8748}
8749
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750static int tg3_set_mac_addr(struct net_device *dev, void *p)
8751{
8752 struct tg3 *tp = netdev_priv(dev);
8753 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008754 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755
Michael Chanf9804dd2005-09-27 12:13:10 -07008756 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008757 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008758
Linus Torvalds1da177e2005-04-16 15:20:36 -07008759 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8760
Michael Chane75f7c92006-03-20 21:33:26 -08008761 if (!netif_running(dev))
8762 return 0;
8763
Joe Perches63c3a662011-04-26 08:12:10 +00008764 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008765 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008766
Michael Chan986e0ae2007-05-05 12:10:20 -07008767 addr0_high = tr32(MAC_ADDR_0_HIGH);
8768 addr0_low = tr32(MAC_ADDR_0_LOW);
8769 addr1_high = tr32(MAC_ADDR_1_HIGH);
8770 addr1_low = tr32(MAC_ADDR_1_LOW);
8771
8772 /* Skip MAC addr 1 if ASF is using it. */
8773 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8774 !(addr1_high == 0 && addr1_low == 0))
8775 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008776 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008777 spin_lock_bh(&tp->lock);
8778 __tg3_set_mac_addr(tp, skip_mac_1);
8779 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008780
Michael Chanb9ec6c12006-07-25 16:37:27 -07008781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008782}
8783
8784/* tp->lock is held. */
8785static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8786 dma_addr_t mapping, u32 maxlen_flags,
8787 u32 nic_addr)
8788{
8789 tg3_write_mem(tp,
8790 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8791 ((u64) mapping >> 32));
8792 tg3_write_mem(tp,
8793 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8794 ((u64) mapping & 0xffffffff));
8795 tg3_write_mem(tp,
8796 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8797 maxlen_flags);
8798
Joe Perches63c3a662011-04-26 08:12:10 +00008799 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008800 tg3_write_mem(tp,
8801 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8802 nic_addr);
8803}
8804
Michael Chana489b6d2012-09-28 07:12:39 +00008805
8806static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008807{
Michael Chana489b6d2012-09-28 07:12:39 +00008808 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008809
Joe Perches63c3a662011-04-26 08:12:10 +00008810 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008811 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8812 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8813 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008814 } else {
8815 tw32(HOSTCC_TXCOL_TICKS, 0);
8816 tw32(HOSTCC_TXMAX_FRAMES, 0);
8817 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008818
8819 for (; i < tp->txq_cnt; i++) {
8820 u32 reg;
8821
8822 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8823 tw32(reg, ec->tx_coalesce_usecs);
8824 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8825 tw32(reg, ec->tx_max_coalesced_frames);
8826 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8827 tw32(reg, ec->tx_max_coalesced_frames_irq);
8828 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008829 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008830
Michael Chana489b6d2012-09-28 07:12:39 +00008831 for (; i < tp->irq_max - 1; i++) {
8832 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8833 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8834 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8835 }
8836}
8837
8838static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8839{
8840 int i = 0;
8841 u32 limit = tp->rxq_cnt;
8842
Joe Perches63c3a662011-04-26 08:12:10 +00008843 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008844 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8845 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8846 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008847 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008848 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008849 tw32(HOSTCC_RXCOL_TICKS, 0);
8850 tw32(HOSTCC_RXMAX_FRAMES, 0);
8851 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008852 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008853
Michael Chana489b6d2012-09-28 07:12:39 +00008854 for (; i < limit; i++) {
8855 u32 reg;
8856
8857 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8858 tw32(reg, ec->rx_coalesce_usecs);
8859 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8860 tw32(reg, ec->rx_max_coalesced_frames);
8861 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8862 tw32(reg, ec->rx_max_coalesced_frames_irq);
8863 }
8864
8865 for (; i < tp->irq_max - 1; i++) {
8866 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8867 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8868 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8869 }
8870}
8871
8872static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8873{
8874 tg3_coal_tx_init(tp, ec);
8875 tg3_coal_rx_init(tp, ec);
8876
Joe Perches63c3a662011-04-26 08:12:10 +00008877 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008878 u32 val = ec->stats_block_coalesce_usecs;
8879
Matt Carlsonb6080e12009-09-01 13:12:00 +00008880 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8881 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8882
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008883 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008884 val = 0;
8885
8886 tw32(HOSTCC_STAT_COAL_TICKS, val);
8887 }
8888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889
8890/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008891static void tg3_rings_reset(struct tg3 *tp)
8892{
8893 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008894 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008895 struct tg3_napi *tnapi = &tp->napi[0];
8896
8897 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008899 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008900 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008901 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00008902 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008903 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00008904 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008905 else
8906 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8907
8908 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8909 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8910 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8911 BDINFO_FLAGS_DISABLED);
8912
8913
8914 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008915 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008916 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008917 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008918 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00008919 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8920 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008921 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008922 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8923 else
8924 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8925
8926 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8927 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8928 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8929 BDINFO_FLAGS_DISABLED);
8930
8931 /* Disable interrupts */
8932 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008933 tp->napi[0].chk_msi_cnt = 0;
8934 tp->napi[0].last_rx_cons = 0;
8935 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008936
8937 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008938 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008939 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008940 tp->napi[i].tx_prod = 0;
8941 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008942 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008943 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008944 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8945 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008946 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008947 tp->napi[i].last_rx_cons = 0;
8948 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008949 }
Joe Perches63c3a662011-04-26 08:12:10 +00008950 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008951 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008952 } else {
8953 tp->napi[0].tx_prod = 0;
8954 tp->napi[0].tx_cons = 0;
8955 tw32_mailbox(tp->napi[0].prodmbox, 0);
8956 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8957 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008958
8959 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008961 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8962 for (i = 0; i < 16; i++)
8963 tw32_tx_mbox(mbox + i * 8, 0);
8964 }
8965
8966 txrcb = NIC_SRAM_SEND_RCB;
8967 rxrcb = NIC_SRAM_RCV_RET_RCB;
8968
8969 /* Clear status block in ram. */
8970 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8971
8972 /* Set status block DMA address */
8973 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8974 ((u64) tnapi->status_mapping >> 32));
8975 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8976 ((u64) tnapi->status_mapping & 0xffffffff));
8977
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008978 if (tnapi->tx_ring) {
8979 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8980 (TG3_TX_RING_SIZE <<
8981 BDINFO_FLAGS_MAXLEN_SHIFT),
8982 NIC_SRAM_TX_BUFFER_DESC);
8983 txrcb += TG3_BDINFO_SIZE;
8984 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008985
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008986 if (tnapi->rx_rcb) {
8987 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008988 (tp->rx_ret_ring_mask + 1) <<
8989 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008990 rxrcb += TG3_BDINFO_SIZE;
8991 }
8992
8993 stblk = HOSTCC_STATBLCK_RING1;
8994
8995 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8996 u64 mapping = (u64)tnapi->status_mapping;
8997 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8998 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8999
9000 /* Clear status block in ram. */
9001 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9002
Matt Carlson19cfaec2009-12-03 08:36:20 +00009003 if (tnapi->tx_ring) {
9004 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9005 (TG3_TX_RING_SIZE <<
9006 BDINFO_FLAGS_MAXLEN_SHIFT),
9007 NIC_SRAM_TX_BUFFER_DESC);
9008 txrcb += TG3_BDINFO_SIZE;
9009 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009010
9011 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009012 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009013 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
9014
9015 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009016 rxrcb += TG3_BDINFO_SIZE;
9017 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009018}
9019
Matt Carlsoneb07a942011-04-20 07:57:36 +00009020static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9021{
9022 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9023
Joe Perches63c3a662011-04-26 08:12:10 +00009024 if (!tg3_flag(tp, 5750_PLUS) ||
9025 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009026 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9027 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009028 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009029 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009030 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9031 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009032 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9033 else
9034 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9035
9036 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9037 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9038
9039 val = min(nic_rep_thresh, host_rep_thresh);
9040 tw32(RCVBDI_STD_THRESH, val);
9041
Joe Perches63c3a662011-04-26 08:12:10 +00009042 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009043 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9044
Joe Perches63c3a662011-04-26 08:12:10 +00009045 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009046 return;
9047
Matt Carlson513aa6e2011-11-21 15:01:18 +00009048 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009049
9050 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9051
9052 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9053 tw32(RCVBDI_JUMBO_THRESH, val);
9054
Joe Perches63c3a662011-04-26 08:12:10 +00009055 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009056 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9057}
9058
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009059static inline u32 calc_crc(unsigned char *buf, int len)
9060{
9061 u32 reg;
9062 u32 tmp;
9063 int j, k;
9064
9065 reg = 0xffffffff;
9066
9067 for (j = 0; j < len; j++) {
9068 reg ^= buf[j];
9069
9070 for (k = 0; k < 8; k++) {
9071 tmp = reg & 0x01;
9072
9073 reg >>= 1;
9074
9075 if (tmp)
9076 reg ^= 0xedb88320;
9077 }
9078 }
9079
9080 return ~reg;
9081}
9082
9083static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9084{
9085 /* accept or reject all multicast frames */
9086 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9087 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9088 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9089 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9090}
9091
9092static void __tg3_set_rx_mode(struct net_device *dev)
9093{
9094 struct tg3 *tp = netdev_priv(dev);
9095 u32 rx_mode;
9096
9097 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9098 RX_MODE_KEEP_VLAN_TAG);
9099
9100#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9101 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9102 * flag clear.
9103 */
9104 if (!tg3_flag(tp, ENABLE_ASF))
9105 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9106#endif
9107
9108 if (dev->flags & IFF_PROMISC) {
9109 /* Promiscuous mode. */
9110 rx_mode |= RX_MODE_PROMISC;
9111 } else if (dev->flags & IFF_ALLMULTI) {
9112 /* Accept all multicast. */
9113 tg3_set_multi(tp, 1);
9114 } else if (netdev_mc_empty(dev)) {
9115 /* Reject all multicast. */
9116 tg3_set_multi(tp, 0);
9117 } else {
9118 /* Accept one or more multicast(s). */
9119 struct netdev_hw_addr *ha;
9120 u32 mc_filter[4] = { 0, };
9121 u32 regidx;
9122 u32 bit;
9123 u32 crc;
9124
9125 netdev_for_each_mc_addr(ha, dev) {
9126 crc = calc_crc(ha->addr, ETH_ALEN);
9127 bit = ~crc & 0x7f;
9128 regidx = (bit & 0x60) >> 5;
9129 bit &= 0x1f;
9130 mc_filter[regidx] |= (1 << bit);
9131 }
9132
9133 tw32(MAC_HASH_REG_0, mc_filter[0]);
9134 tw32(MAC_HASH_REG_1, mc_filter[1]);
9135 tw32(MAC_HASH_REG_2, mc_filter[2]);
9136 tw32(MAC_HASH_REG_3, mc_filter[3]);
9137 }
9138
9139 if (rx_mode != tp->rx_mode) {
9140 tp->rx_mode = rx_mode;
9141 tw32_f(MAC_RX_MODE, rx_mode);
9142 udelay(10);
9143 }
9144}
9145
Michael Chan91024262012-09-28 07:12:38 +00009146static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009147{
9148 int i;
9149
9150 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009151 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009152}
9153
9154static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009155{
9156 int i;
9157
9158 if (!tg3_flag(tp, SUPPORT_MSIX))
9159 return;
9160
Michael Chan0b3ba052012-11-14 14:44:29 +00009161 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009162 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009163 return;
9164 }
9165
9166 /* Validate table against current IRQ count */
9167 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009168 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009169 break;
9170 }
9171
9172 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009173 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009174}
9175
Matt Carlson90415472011-12-16 13:33:23 +00009176static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009177{
9178 int i = 0;
9179 u32 reg = MAC_RSS_INDIR_TBL_0;
9180
9181 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9182 u32 val = tp->rss_ind_tbl[i];
9183 i++;
9184 for (; i % 8; i++) {
9185 val <<= 4;
9186 val |= tp->rss_ind_tbl[i];
9187 }
9188 tw32(reg, val);
9189 reg += 4;
9190 }
9191}
9192
Matt Carlson2d31eca2009-09-01 12:53:31 +00009193/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009194static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009195{
9196 u32 val, rdmac_mode;
9197 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009198 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
9200 tg3_disable_ints(tp);
9201
9202 tg3_stop_fw(tp);
9203
9204 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9205
Joe Perches63c3a662011-04-26 08:12:10 +00009206 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009207 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208
Matt Carlson699c0192010-12-06 08:28:51 +00009209 /* Enable MAC control of LPI */
9210 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009211 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
9212 TG3_CPMU_EEE_LNKIDL_UART_IDL;
Joe Perches41535772013-02-16 11:20:04 +00009213 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00009214 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
9215
9216 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00009217
9218 tw32_f(TG3_CPMU_EEE_CTRL,
9219 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9220
Matt Carlsona386b902010-12-06 08:28:53 +00009221 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9222 TG3_CPMU_EEEMD_LPI_IN_TX |
9223 TG3_CPMU_EEEMD_LPI_IN_RX |
9224 TG3_CPMU_EEEMD_EEE_ENABLE;
9225
Joe Perches41535772013-02-16 11:20:04 +00009226 if (tg3_asic_rev(tp) != ASIC_REV_5717)
Matt Carlsona386b902010-12-06 08:28:53 +00009227 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9228
Joe Perches63c3a662011-04-26 08:12:10 +00009229 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009230 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9231
9232 tw32_f(TG3_CPMU_EEE_MODE, val);
9233
9234 tw32_f(TG3_CPMU_EEE_DBTMR1,
9235 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9236 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9237
9238 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009239 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009240 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009241 }
9242
Matt Carlson603f1172010-02-12 14:47:10 +00009243 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009244 tg3_phy_reset(tp);
9245
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 err = tg3_chip_reset(tp);
9247 if (err)
9248 return err;
9249
9250 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9251
Joe Perches41535772013-02-16 11:20:04 +00009252 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009253 val = tr32(TG3_CPMU_CTRL);
9254 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9255 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009256
9257 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9258 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9259 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9260 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9261
9262 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9263 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9264 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9265 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9266
9267 val = tr32(TG3_CPMU_HST_ACC);
9268 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9269 val |= CPMU_HST_ACC_MACCLK_6_25;
9270 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009271 }
9272
Joe Perches41535772013-02-16 11:20:04 +00009273 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009274 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9275 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9276 PCIE_PWR_MGMT_L1_THRESH_4MS;
9277 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009278
9279 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9280 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9281
9282 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009283
Matt Carlsonf40386c2009-11-02 14:24:02 +00009284 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9285 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009286 }
9287
Joe Perches63c3a662011-04-26 08:12:10 +00009288 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009289 u32 grc_mode = tr32(GRC_MODE);
9290
9291 /* Access the lower 1K of PL PCIE block registers. */
9292 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9293 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9294
9295 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9296 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9297 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9298
9299 tw32(GRC_MODE, grc_mode);
9300 }
9301
Matt Carlson55086ad2011-12-14 11:09:59 +00009302 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009303 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009304 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009305
Matt Carlson5093eed2010-11-24 08:31:45 +00009306 /* Access the lower 1K of PL PCIE block registers. */
9307 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9308 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009309
Matt Carlson5093eed2010-11-24 08:31:45 +00009310 val = tr32(TG3_PCIE_TLDLPL_PORT +
9311 TG3_PCIE_PL_LO_PHYCTL5);
9312 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9313 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009314
Matt Carlson5093eed2010-11-24 08:31:45 +00009315 tw32(GRC_MODE, grc_mode);
9316 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009317
Joe Perches41535772013-02-16 11:20:04 +00009318 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009319 u32 grc_mode;
9320
9321 /* Fix transmit hangs */
9322 val = tr32(TG3_CPMU_PADRNG_CTL);
9323 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9324 tw32(TG3_CPMU_PADRNG_CTL, val);
9325
9326 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009327
9328 /* Access the lower 1K of DL PCIE block registers. */
9329 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9330 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9331
9332 val = tr32(TG3_PCIE_TLDLPL_PORT +
9333 TG3_PCIE_DL_LO_FTSMAX);
9334 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9335 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9336 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9337
9338 tw32(GRC_MODE, grc_mode);
9339 }
9340
Matt Carlsona977dbe2010-04-12 06:58:26 +00009341 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9342 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9343 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9344 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009345 }
9346
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 /* This works around an issue with Athlon chipsets on
9348 * B3 tigon3 silicon. This bit has no effect on any
9349 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009350 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 */
Joe Perches63c3a662011-04-26 08:12:10 +00009352 if (!tg3_flag(tp, CPMU_PRESENT)) {
9353 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009354 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9355 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009357
Joe Perches41535772013-02-16 11:20:04 +00009358 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009359 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360 val = tr32(TG3PCI_PCISTATE);
9361 val |= PCISTATE_RETRY_SAME_DMA;
9362 tw32(TG3PCI_PCISTATE, val);
9363 }
9364
Joe Perches63c3a662011-04-26 08:12:10 +00009365 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009366 /* Allow reads and writes to the
9367 * APE register and memory space.
9368 */
9369 val = tr32(TG3PCI_PCISTATE);
9370 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009371 PCISTATE_ALLOW_APE_SHMEM_WR |
9372 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009373 tw32(TG3PCI_PCISTATE, val);
9374 }
9375
Joe Perches41535772013-02-16 11:20:04 +00009376 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009377 /* Enable some hw fixes. */
9378 val = tr32(TG3PCI_MSI_DATA);
9379 val |= (1 << 26) | (1 << 28) | (1 << 29);
9380 tw32(TG3PCI_MSI_DATA, val);
9381 }
9382
9383 /* Descriptor ring init may make accesses to the
9384 * NIC SRAM area to setup the TX descriptors, so we
9385 * can only do this after the hardware has been
9386 * successfully reset.
9387 */
Michael Chan32d8c572006-07-25 16:38:29 -07009388 err = tg3_init_rings(tp);
9389 if (err)
9390 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
Joe Perches63c3a662011-04-26 08:12:10 +00009392 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009393 val = tr32(TG3PCI_DMA_RW_CTRL) &
9394 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009395 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009396 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009397 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009398 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9399 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009400 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009401 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009402 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9403 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009404 /* This value is determined during the probe time DMA
9405 * engine test, tg3_test_dma.
9406 */
9407 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409
9410 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9411 GRC_MODE_4X_NIC_SEND_RINGS |
9412 GRC_MODE_NO_TX_PHDR_CSUM |
9413 GRC_MODE_NO_RX_PHDR_CSUM);
9414 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009415
9416 /* Pseudo-header checksum is done by hardware logic and not
9417 * the offload processers, so make the chip do the pseudo-
9418 * header checksums on receive. For transmit it is more
9419 * convenient to do the pseudo-header checksum in software
9420 * as Linux does that on transmit for us in all cases.
9421 */
9422 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009424 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9425 if (tp->rxptpctl)
9426 tw32(TG3_RX_PTP_CTL,
9427 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9428
9429 if (tg3_flag(tp, PTP_CAPABLE))
9430 val |= GRC_MODE_TIME_SYNC_ENABLE;
9431
9432 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433
9434 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9435 val = tr32(GRC_MISC_CFG);
9436 val &= ~0xff;
9437 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9438 tw32(GRC_MISC_CFG, val);
9439
9440 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009441 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009443 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009445 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9447 else
9448 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9449 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9450 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009451 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452 int fw_len;
9453
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009454 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9456 tw32(BUFMGR_MB_POOL_ADDR,
9457 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9458 tw32(BUFMGR_MB_POOL_SIZE,
9459 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461
Michael Chan0f893dc2005-07-25 12:30:38 -07009462 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9464 tp->bufmgr_config.mbuf_read_dma_low_water);
9465 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9466 tp->bufmgr_config.mbuf_mac_rx_low_water);
9467 tw32(BUFMGR_MB_HIGH_WATER,
9468 tp->bufmgr_config.mbuf_high_water);
9469 } else {
9470 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9471 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9472 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9473 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9474 tw32(BUFMGR_MB_HIGH_WATER,
9475 tp->bufmgr_config.mbuf_high_water_jumbo);
9476 }
9477 tw32(BUFMGR_DMA_LOW_WATER,
9478 tp->bufmgr_config.dma_low_water);
9479 tw32(BUFMGR_DMA_HIGH_WATER,
9480 tp->bufmgr_config.dma_high_water);
9481
Matt Carlsond309a462010-09-30 10:34:31 +00009482 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009483 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009484 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009485 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9486 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9487 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009488 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009489 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490 for (i = 0; i < 2000; i++) {
9491 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9492 break;
9493 udelay(10);
9494 }
9495 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009496 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009497 return -ENODEV;
9498 }
9499
Joe Perches41535772013-02-16 11:20:04 +00009500 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009501 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009502
Matt Carlsoneb07a942011-04-20 07:57:36 +00009503 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504
9505 /* Initialize TG3_BDINFO's at:
9506 * RCVDBDI_STD_BD: standard eth size rx ring
9507 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9508 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9509 *
9510 * like so:
9511 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9512 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9513 * ring attribute flags
9514 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9515 *
9516 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9517 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9518 *
9519 * The size of each ring is fixed in the firmware, but the location is
9520 * configurable.
9521 */
9522 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009523 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009525 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009526 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009527 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9528 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009530 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009531 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009532 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9533 BDINFO_FLAGS_DISABLED);
9534
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009535 /* Program the jumbo buffer descriptor ring control
9536 * blocks on those devices that have them.
9537 */
Joe Perches41535772013-02-16 11:20:04 +00009538 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009539 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540
Joe Perches63c3a662011-04-26 08:12:10 +00009541 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009543 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009545 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009546 val = TG3_RX_JMB_RING_SIZE(tp) <<
9547 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009549 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009550 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009551 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009552 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009553 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9554 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 } else {
9556 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9557 BDINFO_FLAGS_DISABLED);
9558 }
9559
Joe Perches63c3a662011-04-26 08:12:10 +00009560 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009561 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009562 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9563 val |= (TG3_RX_STD_DMA_SZ << 2);
9564 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009565 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009566 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009567 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009568
9569 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570
Matt Carlson411da642009-11-13 13:03:46 +00009571 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009572 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573
Joe Perches63c3a662011-04-26 08:12:10 +00009574 tpr->rx_jmb_prod_idx =
9575 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009576 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577
Matt Carlson2d31eca2009-09-01 12:53:31 +00009578 tg3_rings_reset(tp);
9579
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009581 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582
9583 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009584 tw32(MAC_RX_MTU_SIZE,
9585 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586
9587 /* The slot time is changed by tg3_setup_phy if we
9588 * run at gigabit with half duplex.
9589 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009590 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9591 (6 << TX_LENGTHS_IPG_SHIFT) |
9592 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9593
Joe Perches41535772013-02-16 11:20:04 +00009594 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9595 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009596 val |= tr32(MAC_TX_LENGTHS) &
9597 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9598 TX_LENGTHS_CNT_DWN_VAL_MSK);
9599
9600 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601
9602 /* Receive rules. */
9603 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9604 tw32(RCVLPC_CONFIG, 0x0181);
9605
9606 /* Calculate RDMAC_MODE setting early, we need it to determine
9607 * the RCVLPC_STATE_ENABLE mask.
9608 */
9609 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9610 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9611 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9612 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9613 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009614
Joe Perches41535772013-02-16 11:20:04 +00009615 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009616 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9617
Joe Perches41535772013-02-16 11:20:04 +00009618 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9619 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9620 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009621 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9622 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9623 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9624
Joe Perches41535772013-02-16 11:20:04 +00009625 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9626 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009627 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009628 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9630 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009631 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9633 }
9634 }
9635
Joe Perches63c3a662011-04-26 08:12:10 +00009636 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009637 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9638
Joe Perches41535772013-02-16 11:20:04 +00009639 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009640 tp->dma_limit = 0;
9641 if (tp->dev->mtu <= ETH_DATA_LEN) {
9642 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9643 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9644 }
9645 }
9646
Joe Perches63c3a662011-04-26 08:12:10 +00009647 if (tg3_flag(tp, HW_TSO_1) ||
9648 tg3_flag(tp, HW_TSO_2) ||
9649 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009650 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9651
Matt Carlson108a6c12011-05-19 12:12:47 +00009652 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009653 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9654 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009655 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656
Joe Perches41535772013-02-16 11:20:04 +00009657 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9658 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009659 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9660
Joe Perches41535772013-02-16 11:20:04 +00009661 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9662 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9663 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9664 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009665 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009666 u32 tgtreg;
9667
Joe Perches41535772013-02-16 11:20:04 +00009668 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009669 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9670 else
9671 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9672
9673 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009674 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9675 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009676 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9677 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9678 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9679 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9680 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9681 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009682 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009683 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009684 }
9685
Joe Perches41535772013-02-16 11:20:04 +00009686 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9687 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9688 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009689 u32 tgtreg;
9690
Joe Perches41535772013-02-16 11:20:04 +00009691 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009692 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9693 else
9694 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9695
9696 val = tr32(tgtreg);
9697 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009698 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9699 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9700 }
9701
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009703 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009704 val = tr32(RCVLPC_STATS_ENABLE);
9705 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9706 tw32(RCVLPC_STATS_ENABLE, val);
9707 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009708 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709 val = tr32(RCVLPC_STATS_ENABLE);
9710 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9711 tw32(RCVLPC_STATS_ENABLE, val);
9712 } else {
9713 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9714 }
9715 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9716 tw32(SNDDATAI_STATSENAB, 0xffffff);
9717 tw32(SNDDATAI_STATSCTRL,
9718 (SNDDATAI_SCTRL_ENABLE |
9719 SNDDATAI_SCTRL_FASTUPD));
9720
9721 /* Setup host coalescing engine. */
9722 tw32(HOSTCC_MODE, 0);
9723 for (i = 0; i < 2000; i++) {
9724 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9725 break;
9726 udelay(10);
9727 }
9728
Michael Chand244c892005-07-05 14:42:33 -07009729 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730
Joe Perches63c3a662011-04-26 08:12:10 +00009731 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 /* Status/statistics block address. See tg3_timer,
9733 * the tg3_periodic_fetch_stats call there, and
9734 * tg3_get_stats to see how this works for 5705/5750 chips.
9735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9737 ((u64) tp->stats_mapping >> 32));
9738 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9739 ((u64) tp->stats_mapping & 0xffffffff));
9740 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009741
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009743
9744 /* Clear statistics and status block memory areas */
9745 for (i = NIC_SRAM_STATS_BLK;
9746 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9747 i += sizeof(u32)) {
9748 tg3_write_mem(tp, i, 0);
9749 udelay(40);
9750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 }
9752
9753 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9754
9755 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9756 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009757 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9759
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009760 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9761 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009762 /* reset to prevent losing 1st rx packet intermittently */
9763 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9764 udelay(10);
9765 }
9766
Matt Carlson3bda1252008-08-15 14:08:22 -07009767 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009768 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9769 MAC_MODE_FHDE_ENABLE;
9770 if (tg3_flag(tp, ENABLE_APE))
9771 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009772 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009773 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009774 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009775 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9777 udelay(40);
9778
Michael Chan314fba32005-04-21 17:07:04 -07009779 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009780 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009781 * register to preserve the GPIO settings for LOMs. The GPIOs,
9782 * whether used as inputs or outputs, are set by boot code after
9783 * reset.
9784 */
Joe Perches63c3a662011-04-26 08:12:10 +00009785 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009786 u32 gpio_mask;
9787
Michael Chan9d26e212006-12-07 00:21:14 -08009788 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9789 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9790 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009791
Joe Perches41535772013-02-16 11:20:04 +00009792 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -07009793 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9794 GRC_LCLCTRL_GPIO_OUTPUT3;
9795
Joe Perches41535772013-02-16 11:20:04 +00009796 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -08009797 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9798
Gary Zambranoaaf84462007-05-05 11:51:45 -07009799 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009800 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9801
9802 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009803 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009804 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9805 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9808 udelay(100);
9809
Matt Carlsonc3b50032012-01-17 15:27:23 +00009810 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009811 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009812 val |= MSGINT_MODE_ENABLE;
9813 if (tp->irq_cnt > 1)
9814 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009815 if (!tg3_flag(tp, 1SHOT_MSI))
9816 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009817 tw32(MSGINT_MODE, val);
9818 }
9819
Joe Perches63c3a662011-04-26 08:12:10 +00009820 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9822 udelay(40);
9823 }
9824
9825 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9826 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9827 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9828 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9829 WDMAC_MODE_LNGREAD_ENAB);
9830
Joe Perches41535772013-02-16 11:20:04 +00009831 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9832 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009833 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009834 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
9835 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836 /* nothing */
9837 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009838 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 val |= WDMAC_MODE_RX_ACCEL;
9840 }
9841 }
9842
Michael Chand9ab5ad12006-03-20 22:27:35 -08009843 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009844 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009845 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009846
Joe Perches41535772013-02-16 11:20:04 +00009847 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +00009848 val |= WDMAC_MODE_BURST_ALL_DATA;
9849
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 tw32_f(WDMAC_MODE, val);
9851 udelay(40);
9852
Joe Perches63c3a662011-04-26 08:12:10 +00009853 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009854 u16 pcix_cmd;
9855
9856 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9857 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +00009858 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009859 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9860 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +00009861 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009862 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9863 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 }
Matt Carlson9974a352007-10-07 23:27:28 -07009865 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9866 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867 }
9868
9869 tw32_f(RDMAC_MODE, rdmac_mode);
9870 udelay(40);
9871
Joe Perches41535772013-02-16 11:20:04 +00009872 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +00009873 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9874 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9875 break;
9876 }
9877 if (i < TG3_NUM_RDMA_CHANNELS) {
9878 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9879 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9880 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9881 tg3_flag_set(tp, 5719_RDMA_BUG);
9882 }
9883 }
9884
Linus Torvalds1da177e2005-04-16 15:20:36 -07009885 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009886 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009888
Joe Perches41535772013-02-16 11:20:04 +00009889 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009890 tw32(SNDDATAC_MODE,
9891 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9892 else
9893 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9894
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9896 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009897 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009898 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009899 val |= RCVDBDI_MODE_LRG_RING_SZ;
9900 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009902 if (tg3_flag(tp, HW_TSO_1) ||
9903 tg3_flag(tp, HW_TSO_2) ||
9904 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009906 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009907 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009908 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9909 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9911
Joe Perches41535772013-02-16 11:20:04 +00009912 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 err = tg3_load_5701_a0_firmware_fix(tp);
9914 if (err)
9915 return err;
9916 }
9917
Nithin Sujirc4dab502013-03-06 17:02:34 +00009918 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
9919 /* Ignore any errors for the firmware download. If download
9920 * fails, the device will operate with EEE disabled
9921 */
9922 tg3_load_57766_firmware(tp);
9923 }
9924
Joe Perches63c3a662011-04-26 08:12:10 +00009925 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 err = tg3_load_tso_firmware(tp);
9927 if (err)
9928 return err;
9929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009930
9931 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009932
Joe Perches63c3a662011-04-26 08:12:10 +00009933 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009934 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +00009935 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009936
Joe Perches41535772013-02-16 11:20:04 +00009937 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9938 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00009939 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9940 tp->tx_mode &= ~val;
9941 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9942 }
9943
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 tw32_f(MAC_TX_MODE, tp->tx_mode);
9945 udelay(100);
9946
Joe Perches63c3a662011-04-26 08:12:10 +00009947 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009948 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009949
9950 /* Setup the "secret" hash key. */
9951 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9952 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9953 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9954 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9955 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9956 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9957 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9958 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9959 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9960 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9961 }
9962
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009964 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009965 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9966
Joe Perches63c3a662011-04-26 08:12:10 +00009967 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009968 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9969 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9970 RX_MODE_RSS_IPV6_HASH_EN |
9971 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9972 RX_MODE_RSS_IPV4_HASH_EN |
9973 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9974
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 tw32_f(MAC_RX_MODE, tp->rx_mode);
9976 udelay(10);
9977
Linus Torvalds1da177e2005-04-16 15:20:36 -07009978 tw32(MAC_LED_CTRL, tp->led_ctrl);
9979
9980 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009981 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9983 udelay(10);
9984 }
9985 tw32_f(MAC_RX_MODE, tp->rx_mode);
9986 udelay(10);
9987
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009988 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00009989 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
9990 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991 /* Set drive transmission level to 1.2V */
9992 /* only if the signal pre-emphasis bit is not set */
9993 val = tr32(MAC_SERDES_CFG);
9994 val &= 0xfffff000;
9995 val |= 0x880;
9996 tw32(MAC_SERDES_CFG, val);
9997 }
Joe Perches41535772013-02-16 11:20:04 +00009998 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009999 tw32(MAC_SERDES_CFG, 0x616000);
10000 }
10001
10002 /* Prevent chip from dropping frames when flow control
10003 * is enabled.
10004 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010005 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010006 val = 1;
10007 else
10008 val = 2;
10009 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010
Joe Perches41535772013-02-16 11:20:04 +000010011 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010012 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010014 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 }
10016
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010017 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010018 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010019 u32 tmp;
10020
10021 tmp = tr32(SERDES_RX_CTRL);
10022 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10023 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10024 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10025 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10026 }
10027
Joe Perches63c3a662011-04-26 08:12:10 +000010028 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010029 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010030 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031
Matt Carlsondd477002008-05-25 23:45:58 -070010032 err = tg3_setup_phy(tp, 0);
10033 if (err)
10034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010036 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10037 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010038 u32 tmp;
10039
10040 /* Clear CRC stats. */
10041 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10042 tg3_writephy(tp, MII_TG3_TEST1,
10043 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010044 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046 }
10047 }
10048
10049 __tg3_set_rx_mode(tp->dev);
10050
10051 /* Initialize receive rules. */
10052 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10053 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10054 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10055 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10056
Joe Perches63c3a662011-04-26 08:12:10 +000010057 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 limit = 8;
10059 else
10060 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010061 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 limit -= 4;
10063 switch (limit) {
10064 case 16:
10065 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10066 case 15:
10067 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10068 case 14:
10069 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10070 case 13:
10071 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10072 case 12:
10073 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10074 case 11:
10075 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10076 case 10:
10077 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10078 case 9:
10079 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10080 case 8:
10081 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10082 case 7:
10083 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10084 case 6:
10085 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10086 case 5:
10087 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10088 case 4:
10089 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10090 case 3:
10091 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10092 case 2:
10093 case 1:
10094
10095 default:
10096 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
Joe Perches63c3a662011-04-26 08:12:10 +000010099 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010100 /* Write our heartbeat update interval to APE. */
10101 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10102 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010103
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10105
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106 return 0;
10107}
10108
10109/* Called at device open time to get the chip ready for
10110 * packet processing. Invoked with tp->lock held.
10111 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010112static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114 tg3_switch_clocks(tp);
10115
10116 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10117
Matt Carlson2f751b62008-08-04 23:17:34 -070010118 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119}
10120
Michael Chanaed93e02012-07-16 16:24:02 +000010121static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10122{
10123 int i;
10124
10125 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10126 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10127
10128 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10129 off += len;
10130
10131 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10132 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10133 memset(ocir, 0, TG3_OCIR_LEN);
10134 }
10135}
10136
10137/* sysfs attributes for hwmon */
10138static ssize_t tg3_show_temp(struct device *dev,
10139 struct device_attribute *devattr, char *buf)
10140{
10141 struct pci_dev *pdev = to_pci_dev(dev);
10142 struct net_device *netdev = pci_get_drvdata(pdev);
10143 struct tg3 *tp = netdev_priv(netdev);
10144 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10145 u32 temperature;
10146
10147 spin_lock_bh(&tp->lock);
10148 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10149 sizeof(temperature));
10150 spin_unlock_bh(&tp->lock);
10151 return sprintf(buf, "%u\n", temperature);
10152}
10153
10154
10155static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10156 TG3_TEMP_SENSOR_OFFSET);
10157static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10158 TG3_TEMP_CAUTION_OFFSET);
10159static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10160 TG3_TEMP_MAX_OFFSET);
10161
10162static struct attribute *tg3_attributes[] = {
10163 &sensor_dev_attr_temp1_input.dev_attr.attr,
10164 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10165 &sensor_dev_attr_temp1_max.dev_attr.attr,
10166 NULL
10167};
10168
10169static const struct attribute_group tg3_group = {
10170 .attrs = tg3_attributes,
10171};
10172
Michael Chanaed93e02012-07-16 16:24:02 +000010173static void tg3_hwmon_close(struct tg3 *tp)
10174{
Michael Chanaed93e02012-07-16 16:24:02 +000010175 if (tp->hwmon_dev) {
10176 hwmon_device_unregister(tp->hwmon_dev);
10177 tp->hwmon_dev = NULL;
10178 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10179 }
Michael Chanaed93e02012-07-16 16:24:02 +000010180}
10181
10182static void tg3_hwmon_open(struct tg3 *tp)
10183{
Michael Chanaed93e02012-07-16 16:24:02 +000010184 int i, err;
10185 u32 size = 0;
10186 struct pci_dev *pdev = tp->pdev;
10187 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10188
10189 tg3_sd_scan_scratchpad(tp, ocirs);
10190
10191 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10192 if (!ocirs[i].src_data_length)
10193 continue;
10194
10195 size += ocirs[i].src_hdr_length;
10196 size += ocirs[i].src_data_length;
10197 }
10198
10199 if (!size)
10200 return;
10201
10202 /* Register hwmon sysfs hooks */
10203 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10204 if (err) {
10205 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10206 return;
10207 }
10208
10209 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10210 if (IS_ERR(tp->hwmon_dev)) {
10211 tp->hwmon_dev = NULL;
10212 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10213 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10214 }
Michael Chanaed93e02012-07-16 16:24:02 +000010215}
10216
10217
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218#define TG3_STAT_ADD32(PSTAT, REG) \
10219do { u32 __val = tr32(REG); \
10220 (PSTAT)->low += __val; \
10221 if ((PSTAT)->low < __val) \
10222 (PSTAT)->high += 1; \
10223} while (0)
10224
10225static void tg3_periodic_fetch_stats(struct tg3 *tp)
10226{
10227 struct tg3_hw_stats *sp = tp->hw_stats;
10228
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010229 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 return;
10231
10232 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10233 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10234 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10235 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10236 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10237 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10238 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10239 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10240 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10241 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10242 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10243 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10244 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010245 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10246 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10247 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10248 u32 val;
10249
10250 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10251 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10252 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10253 tg3_flag_clear(tp, 5719_RDMA_BUG);
10254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255
10256 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10257 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10258 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10259 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10260 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10261 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10262 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10263 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10264 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10265 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10266 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10267 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10268 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10269 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010270
10271 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010272 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10273 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10274 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010275 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10276 } else {
10277 u32 val = tr32(HOSTCC_FLOW_ATTN);
10278 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10279 if (val) {
10280 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10281 sp->rx_discards.low += val;
10282 if (sp->rx_discards.low < val)
10283 sp->rx_discards.high += 1;
10284 }
10285 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10286 }
Michael Chan463d3052006-05-22 16:36:27 -070010287 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288}
10289
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010290static void tg3_chk_missed_msi(struct tg3 *tp)
10291{
10292 u32 i;
10293
10294 for (i = 0; i < tp->irq_cnt; i++) {
10295 struct tg3_napi *tnapi = &tp->napi[i];
10296
10297 if (tg3_has_work(tnapi)) {
10298 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10299 tnapi->last_tx_cons == tnapi->tx_cons) {
10300 if (tnapi->chk_msi_cnt < 1) {
10301 tnapi->chk_msi_cnt++;
10302 return;
10303 }
Matt Carlson7f230732011-08-31 11:44:48 +000010304 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010305 }
10306 }
10307 tnapi->chk_msi_cnt = 0;
10308 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10309 tnapi->last_tx_cons = tnapi->tx_cons;
10310 }
10311}
10312
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313static void tg3_timer(unsigned long __opaque)
10314{
10315 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316
Matt Carlson5b190622011-11-04 09:15:04 +000010317 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010318 goto restart_timer;
10319
David S. Millerf47c11e2005-06-24 20:18:35 -070010320 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321
Joe Perches41535772013-02-16 11:20:04 +000010322 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010323 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010324 tg3_chk_missed_msi(tp);
10325
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010326 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10327 /* BCM4785: Flush posted writes from GbE to host memory. */
10328 tr32(HOSTCC_MODE);
10329 }
10330
Joe Perches63c3a662011-04-26 08:12:10 +000010331 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010332 /* All of this garbage is because when using non-tagged
10333 * IRQ status the mailbox/status_block protocol the chip
10334 * uses with the cpu is race prone.
10335 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010336 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010337 tw32(GRC_LOCAL_CTRL,
10338 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10339 } else {
10340 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010341 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010343
David S. Millerfac9b832005-05-18 22:46:34 -070010344 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010345 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010346 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010347 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349 }
10350
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351 /* This part only runs once per second. */
10352 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010353 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010354 tg3_periodic_fetch_stats(tp);
10355
Matt Carlsonb0c59432011-05-19 12:12:48 +000010356 if (tp->setlpicnt && !--tp->setlpicnt)
10357 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010358
Joe Perches63c3a662011-04-26 08:12:10 +000010359 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360 u32 mac_stat;
10361 int phy_event;
10362
10363 mac_stat = tr32(MAC_STATUS);
10364
10365 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010366 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010367 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10368 phy_event = 1;
10369 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10370 phy_event = 1;
10371
10372 if (phy_event)
10373 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010374 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 u32 mac_stat = tr32(MAC_STATUS);
10376 int need_setup = 0;
10377
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010378 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10380 need_setup = 1;
10381 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010382 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10384 MAC_STATUS_SIGNAL_DET))) {
10385 need_setup = 1;
10386 }
10387 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010388 if (!tp->serdes_counter) {
10389 tw32_f(MAC_MODE,
10390 (tp->mac_mode &
10391 ~MAC_MODE_PORT_MODE_MASK));
10392 udelay(40);
10393 tw32_f(MAC_MODE, tp->mac_mode);
10394 udelay(40);
10395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 tg3_setup_phy(tp, 0);
10397 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010398 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010399 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010400 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010402
10403 tp->timer_counter = tp->timer_multiplier;
10404 }
10405
Michael Chan130b8e42006-09-27 16:00:40 -070010406 /* Heartbeat is only sent once every 2 seconds.
10407 *
10408 * The heartbeat is to tell the ASF firmware that the host
10409 * driver is still alive. In the event that the OS crashes,
10410 * ASF needs to reset the hardware to free up the FIFO space
10411 * that may be filled with rx packets destined for the host.
10412 * If the FIFO is full, ASF will no longer function properly.
10413 *
10414 * Unintended resets have been reported on real time kernels
10415 * where the timer doesn't run on time. Netpoll will also have
10416 * same problem.
10417 *
10418 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10419 * to check the ring condition when the heartbeat is expiring
10420 * before doing the reset. This will prevent most unintended
10421 * resets.
10422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010424 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010425 tg3_wait_for_event_ack(tp);
10426
Michael Chanbbadf502006-04-06 21:46:34 -070010427 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010428 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010429 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010430 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10431 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010432
10433 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434 }
10435 tp->asf_counter = tp->asf_multiplier;
10436 }
10437
David S. Millerf47c11e2005-06-24 20:18:35 -070010438 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439
Michael Chanf475f162006-03-27 23:20:14 -080010440restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 tp->timer.expires = jiffies + tp->timer_offset;
10442 add_timer(&tp->timer);
10443}
10444
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010445static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010446{
10447 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010448 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010449 !tg3_flag(tp, 57765_CLASS))
10450 tp->timer_offset = HZ;
10451 else
10452 tp->timer_offset = HZ / 10;
10453
10454 BUG_ON(tp->timer_offset > HZ);
10455
10456 tp->timer_multiplier = (HZ / tp->timer_offset);
10457 tp->asf_multiplier = (HZ / tp->timer_offset) *
10458 TG3_FW_UPDATE_FREQ_SEC;
10459
10460 init_timer(&tp->timer);
10461 tp->timer.data = (unsigned long) tp;
10462 tp->timer.function = tg3_timer;
10463}
10464
10465static void tg3_timer_start(struct tg3 *tp)
10466{
10467 tp->asf_counter = tp->asf_multiplier;
10468 tp->timer_counter = tp->timer_multiplier;
10469
10470 tp->timer.expires = jiffies + tp->timer_offset;
10471 add_timer(&tp->timer);
10472}
10473
10474static void tg3_timer_stop(struct tg3 *tp)
10475{
10476 del_timer_sync(&tp->timer);
10477}
10478
10479/* Restart hardware after configuration changes, self-test, etc.
10480 * Invoked with tp->lock held.
10481 */
10482static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10483 __releases(tp->lock)
10484 __acquires(tp->lock)
10485{
10486 int err;
10487
10488 err = tg3_init_hw(tp, reset_phy);
10489 if (err) {
10490 netdev_err(tp->dev,
10491 "Failed to re-initialize device, aborting\n");
10492 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10493 tg3_full_unlock(tp);
10494 tg3_timer_stop(tp);
10495 tp->irq_sync = 0;
10496 tg3_napi_enable(tp);
10497 dev_close(tp->dev);
10498 tg3_full_lock(tp, 0);
10499 }
10500 return err;
10501}
10502
10503static void tg3_reset_task(struct work_struct *work)
10504{
10505 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10506 int err;
10507
10508 tg3_full_lock(tp, 0);
10509
10510 if (!netif_running(tp->dev)) {
10511 tg3_flag_clear(tp, RESET_TASK_PENDING);
10512 tg3_full_unlock(tp);
10513 return;
10514 }
10515
10516 tg3_full_unlock(tp);
10517
10518 tg3_phy_stop(tp);
10519
10520 tg3_netif_stop(tp);
10521
10522 tg3_full_lock(tp, 1);
10523
10524 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10525 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10526 tp->write32_rx_mbox = tg3_write_flush_reg32;
10527 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10528 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10529 }
10530
10531 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10532 err = tg3_init_hw(tp, 1);
10533 if (err)
10534 goto out;
10535
10536 tg3_netif_start(tp);
10537
10538out:
10539 tg3_full_unlock(tp);
10540
10541 if (!err)
10542 tg3_phy_start(tp);
10543
10544 tg3_flag_clear(tp, RESET_TASK_PENDING);
10545}
10546
Matt Carlson4f125f42009-09-01 12:55:02 +000010547static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010548{
David Howells7d12e782006-10-05 14:55:46 +010010549 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010550 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010551 char *name;
10552 struct tg3_napi *tnapi = &tp->napi[irq_num];
10553
10554 if (tp->irq_cnt == 1)
10555 name = tp->dev->name;
10556 else {
10557 name = &tnapi->irq_lbl[0];
10558 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10559 name[IFNAMSIZ-1] = 0;
10560 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010561
Joe Perches63c3a662011-04-26 08:12:10 +000010562 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010563 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010564 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010565 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010566 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010567 } else {
10568 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010569 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010570 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010571 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010572 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010573
10574 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010575}
10576
Michael Chan79381092005-04-21 17:13:59 -070010577static int tg3_test_interrupt(struct tg3 *tp)
10578{
Matt Carlson09943a12009-08-28 14:01:57 +000010579 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010580 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010581 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010582 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010583
Michael Chand4bc3922005-05-29 14:59:20 -070010584 if (!netif_running(dev))
10585 return -ENODEV;
10586
Michael Chan79381092005-04-21 17:13:59 -070010587 tg3_disable_ints(tp);
10588
Matt Carlson4f125f42009-09-01 12:55:02 +000010589 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010590
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010591 /*
10592 * Turn off MSI one shot mode. Otherwise this test has no
10593 * observable way to know whether the interrupt was delivered.
10594 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010595 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010596 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10597 tw32(MSGINT_MODE, val);
10598 }
10599
Matt Carlson4f125f42009-09-01 12:55:02 +000010600 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010601 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010602 if (err)
10603 return err;
10604
Matt Carlson898a56f2009-08-28 14:02:40 +000010605 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010606 tg3_enable_ints(tp);
10607
10608 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010609 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010610
10611 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010612 u32 int_mbox, misc_host_ctrl;
10613
Matt Carlson898a56f2009-08-28 14:02:40 +000010614 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010615 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10616
10617 if ((int_mbox != 0) ||
10618 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10619 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010620 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010621 }
10622
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010623 if (tg3_flag(tp, 57765_PLUS) &&
10624 tnapi->hw_status->status_tag != tnapi->last_tag)
10625 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10626
Michael Chan79381092005-04-21 17:13:59 -070010627 msleep(10);
10628 }
10629
10630 tg3_disable_ints(tp);
10631
Matt Carlson4f125f42009-09-01 12:55:02 +000010632 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010633
Matt Carlson4f125f42009-09-01 12:55:02 +000010634 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010635
10636 if (err)
10637 return err;
10638
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010639 if (intr_ok) {
10640 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010641 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010642 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10643 tw32(MSGINT_MODE, val);
10644 }
Michael Chan79381092005-04-21 17:13:59 -070010645 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010646 }
Michael Chan79381092005-04-21 17:13:59 -070010647
10648 return -EIO;
10649}
10650
10651/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10652 * successfully restored
10653 */
10654static int tg3_test_msi(struct tg3 *tp)
10655{
Michael Chan79381092005-04-21 17:13:59 -070010656 int err;
10657 u16 pci_cmd;
10658
Joe Perches63c3a662011-04-26 08:12:10 +000010659 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010660 return 0;
10661
10662 /* Turn off SERR reporting in case MSI terminates with Master
10663 * Abort.
10664 */
10665 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10666 pci_write_config_word(tp->pdev, PCI_COMMAND,
10667 pci_cmd & ~PCI_COMMAND_SERR);
10668
10669 err = tg3_test_interrupt(tp);
10670
10671 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10672
10673 if (!err)
10674 return 0;
10675
10676 /* other failures */
10677 if (err != -EIO)
10678 return err;
10679
10680 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010681 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10682 "to INTx mode. Please report this failure to the PCI "
10683 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010684
Matt Carlson4f125f42009-09-01 12:55:02 +000010685 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010686
Michael Chan79381092005-04-21 17:13:59 -070010687 pci_disable_msi(tp->pdev);
10688
Joe Perches63c3a662011-04-26 08:12:10 +000010689 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010690 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010691
Matt Carlson4f125f42009-09-01 12:55:02 +000010692 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010693 if (err)
10694 return err;
10695
10696 /* Need to reset the chip because the MSI cycle may have terminated
10697 * with Master Abort.
10698 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010699 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010700
Michael Chan944d9802005-05-29 14:57:48 -070010701 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010702 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010703
David S. Millerf47c11e2005-06-24 20:18:35 -070010704 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010705
10706 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010707 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010708
10709 return err;
10710}
10711
Matt Carlson9e9fd122009-01-19 16:57:45 -080010712static int tg3_request_firmware(struct tg3 *tp)
10713{
Nithin Sujir77997ea2013-03-06 17:02:32 +000010714 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010715
10716 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010717 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10718 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010719 return -ENOENT;
10720 }
10721
Nithin Sujir77997ea2013-03-06 17:02:32 +000010722 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010723
10724 /* Firmware blob starts with version numbers, followed by
10725 * start address and _full_ length including BSS sections
10726 * (which must be longer than the actual data, of course
10727 */
10728
Nithin Sujir77997ea2013-03-06 17:02:32 +000010729 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
10730 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010731 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10732 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010733 release_firmware(tp->fw);
10734 tp->fw = NULL;
10735 return -EINVAL;
10736 }
10737
10738 /* We no longer need firmware; we have it. */
10739 tp->fw_needed = NULL;
10740 return 0;
10741}
10742
Michael Chan91024262012-09-28 07:12:38 +000010743static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010744{
Michael Chan91024262012-09-28 07:12:38 +000010745 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010746
Michael Chan91024262012-09-28 07:12:38 +000010747 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010748 /* We want as many rx rings enabled as there are cpus.
10749 * In multiqueue MSI-X mode, the first MSI-X vector
10750 * only deals with link interrupts, etc, so we add
10751 * one to the number of vectors we are requesting.
10752 */
Michael Chan91024262012-09-28 07:12:38 +000010753 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010754 }
Matt Carlson679563f2009-09-01 12:55:46 +000010755
Michael Chan91024262012-09-28 07:12:38 +000010756 return irq_cnt;
10757}
10758
10759static bool tg3_enable_msix(struct tg3 *tp)
10760{
10761 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010762 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010763
Michael Chan09681692012-09-28 07:12:42 +000010764 tp->txq_cnt = tp->txq_req;
10765 tp->rxq_cnt = tp->rxq_req;
10766 if (!tp->rxq_cnt)
10767 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010768 if (tp->rxq_cnt > tp->rxq_max)
10769 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010770
10771 /* Disable multiple TX rings by default. Simple round-robin hardware
10772 * scheduling of the TX rings can cause starvation of rings with
10773 * small packets when other rings have TSO or jumbo packets.
10774 */
10775 if (!tp->txq_req)
10776 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010777
10778 tp->irq_cnt = tg3_irq_count(tp);
10779
Matt Carlson679563f2009-09-01 12:55:46 +000010780 for (i = 0; i < tp->irq_max; i++) {
10781 msix_ent[i].entry = i;
10782 msix_ent[i].vector = 0;
10783 }
10784
10785 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010786 if (rc < 0) {
10787 return false;
10788 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010789 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10790 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010791 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10792 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010793 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010794 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010795 if (tp->txq_cnt)
10796 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010797 }
10798
10799 for (i = 0; i < tp->irq_max; i++)
10800 tp->napi[i].irq_vec = msix_ent[i].vector;
10801
Michael Chan49a359e2012-09-28 07:12:37 +000010802 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010803 pci_disable_msix(tp->pdev);
10804 return false;
10805 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010806
Michael Chan91024262012-09-28 07:12:38 +000010807 if (tp->irq_cnt == 1)
10808 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010809
Michael Chan91024262012-09-28 07:12:38 +000010810 tg3_flag_set(tp, ENABLE_RSS);
10811
10812 if (tp->txq_cnt > 1)
10813 tg3_flag_set(tp, ENABLE_TSS);
10814
10815 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010816
Matt Carlson679563f2009-09-01 12:55:46 +000010817 return true;
10818}
10819
Matt Carlson07b01732009-08-28 14:01:15 +000010820static void tg3_ints_init(struct tg3 *tp)
10821{
Joe Perches63c3a662011-04-26 08:12:10 +000010822 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10823 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010824 /* All MSI supporting chips should support tagged
10825 * status. Assert that this is the case.
10826 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010827 netdev_warn(tp->dev,
10828 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010829 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010830 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010831
Joe Perches63c3a662011-04-26 08:12:10 +000010832 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10833 tg3_flag_set(tp, USING_MSIX);
10834 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10835 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010836
Joe Perches63c3a662011-04-26 08:12:10 +000010837 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010838 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010839 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010840 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010841 if (!tg3_flag(tp, 1SHOT_MSI))
10842 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010843 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10844 }
10845defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010846 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010847 tp->irq_cnt = 1;
10848 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010849 }
10850
10851 if (tp->irq_cnt == 1) {
10852 tp->txq_cnt = 1;
10853 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010854 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010855 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010856 }
Matt Carlson07b01732009-08-28 14:01:15 +000010857}
10858
10859static void tg3_ints_fini(struct tg3 *tp)
10860{
Joe Perches63c3a662011-04-26 08:12:10 +000010861 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010862 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010863 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010864 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010865 tg3_flag_clear(tp, USING_MSI);
10866 tg3_flag_clear(tp, USING_MSIX);
10867 tg3_flag_clear(tp, ENABLE_RSS);
10868 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010869}
10870
Matt Carlsonbe947302012-12-03 19:36:57 +000010871static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10872 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010873{
Michael Chand8f4cd32012-09-28 07:12:40 +000010874 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010875 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010876
Matt Carlson679563f2009-09-01 12:55:46 +000010877 /*
10878 * Setup interrupts first so we know how
10879 * many NAPI resources to allocate
10880 */
10881 tg3_ints_init(tp);
10882
Matt Carlson90415472011-12-16 13:33:23 +000010883 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010884
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885 /* The placement of this call is tied
10886 * to the setup and use of Host TX descriptors.
10887 */
10888 err = tg3_alloc_consistent(tp);
10889 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010890 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010891
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010892 tg3_napi_init(tp);
10893
Matt Carlsonfed97812009-09-01 13:10:19 +000010894 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010895
Matt Carlson4f125f42009-09-01 12:55:02 +000010896 for (i = 0; i < tp->irq_cnt; i++) {
10897 struct tg3_napi *tnapi = &tp->napi[i];
10898 err = tg3_request_irq(tp, i);
10899 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010900 for (i--; i >= 0; i--) {
10901 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010902 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010903 }
10904 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010905 }
10906 }
Matt Carlson07b01732009-08-28 14:01:15 +000010907
David S. Millerf47c11e2005-06-24 20:18:35 -070010908 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010909
Michael Chand8f4cd32012-09-28 07:12:40 +000010910 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010911 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010912 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010913 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010914 }
10915
David S. Millerf47c11e2005-06-24 20:18:35 -070010916 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010917
Matt Carlson07b01732009-08-28 14:01:15 +000010918 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010919 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010920
Michael Chand8f4cd32012-09-28 07:12:40 +000010921 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010922 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010923
Michael Chan79381092005-04-21 17:13:59 -070010924 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010925 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010926 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010927 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010928 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010929
Matt Carlson679563f2009-09-01 12:55:46 +000010930 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010931 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010932
Joe Perches63c3a662011-04-26 08:12:10 +000010933 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010934 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010935
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010936 tw32(PCIE_TRANSACTION_CFG,
10937 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010938 }
Michael Chan79381092005-04-21 17:13:59 -070010939 }
10940
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010941 tg3_phy_start(tp);
10942
Michael Chanaed93e02012-07-16 16:24:02 +000010943 tg3_hwmon_open(tp);
10944
David S. Millerf47c11e2005-06-24 20:18:35 -070010945 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010946
Matt Carlson21f76382012-02-22 12:35:21 +000010947 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010948 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010949 tg3_enable_ints(tp);
10950
Matt Carlsonbe947302012-12-03 19:36:57 +000010951 if (init)
10952 tg3_ptp_init(tp);
10953 else
10954 tg3_ptp_resume(tp);
10955
10956
David S. Millerf47c11e2005-06-24 20:18:35 -070010957 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010958
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010959 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010960
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010961 /*
10962 * Reset loopback feature if it was turned on while the device was down
10963 * make sure that it's installed properly now.
10964 */
10965 if (dev->features & NETIF_F_LOOPBACK)
10966 tg3_set_loopback(dev, dev->features);
10967
Linus Torvalds1da177e2005-04-16 15:20:36 -070010968 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010969
Matt Carlson679563f2009-09-01 12:55:46 +000010970err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010971 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10972 struct tg3_napi *tnapi = &tp->napi[i];
10973 free_irq(tnapi->irq_vec, tnapi);
10974 }
Matt Carlson07b01732009-08-28 14:01:15 +000010975
Matt Carlson679563f2009-09-01 12:55:46 +000010976err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010977 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010978 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010979 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010980
10981err_out1:
10982 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010983
Matt Carlson07b01732009-08-28 14:01:15 +000010984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010985}
10986
Michael Chan65138592012-09-28 07:12:41 +000010987static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988{
Matt Carlson4f125f42009-09-01 12:55:02 +000010989 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010990
Matt Carlsondb219972011-11-04 09:15:03 +000010991 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010992 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010993
Matt Carlson21f76382012-02-22 12:35:21 +000010994 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995
Michael Chanaed93e02012-07-16 16:24:02 +000010996 tg3_hwmon_close(tp);
10997
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010998 tg3_phy_stop(tp);
10999
David S. Millerf47c11e2005-06-24 20:18:35 -070011000 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011001
11002 tg3_disable_ints(tp);
11003
Michael Chan944d9802005-05-29 14:57:48 -070011004 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011005 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011006 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011007
David S. Millerf47c11e2005-06-24 20:18:35 -070011008 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009
Matt Carlson4f125f42009-09-01 12:55:02 +000011010 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11011 struct tg3_napi *tnapi = &tp->napi[i];
11012 free_irq(tnapi->irq_vec, tnapi);
11013 }
Matt Carlson07b01732009-08-28 14:01:15 +000011014
11015 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011016
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011017 tg3_napi_fini(tp);
11018
Linus Torvalds1da177e2005-04-16 15:20:36 -070011019 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011020}
11021
Michael Chand8f4cd32012-09-28 07:12:40 +000011022static int tg3_open(struct net_device *dev)
11023{
11024 struct tg3 *tp = netdev_priv(dev);
11025 int err;
11026
11027 if (tp->fw_needed) {
11028 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011029 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11030 if (err) {
11031 netdev_warn(tp->dev, "EEE capability disabled\n");
11032 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11033 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11034 netdev_warn(tp->dev, "EEE capability restored\n");
11035 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11036 }
11037 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011038 if (err)
11039 return err;
11040 } else if (err) {
11041 netdev_warn(tp->dev, "TSO capability disabled\n");
11042 tg3_flag_clear(tp, TSO_CAPABLE);
11043 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11044 netdev_notice(tp->dev, "TSO capability restored\n");
11045 tg3_flag_set(tp, TSO_CAPABLE);
11046 }
11047 }
11048
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011049 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011050
11051 err = tg3_power_up(tp);
11052 if (err)
11053 return err;
11054
11055 tg3_full_lock(tp, 0);
11056
11057 tg3_disable_ints(tp);
11058 tg3_flag_clear(tp, INIT_COMPLETE);
11059
11060 tg3_full_unlock(tp);
11061
Matt Carlsonbe947302012-12-03 19:36:57 +000011062 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011063 if (err) {
11064 tg3_frob_aux_power(tp, false);
11065 pci_set_power_state(tp->pdev, PCI_D3hot);
11066 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011067
Matt Carlson7d41e492012-12-03 19:36:58 +000011068 if (tg3_flag(tp, PTP_CAPABLE)) {
11069 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11070 &tp->pdev->dev);
11071 if (IS_ERR(tp->ptp_clock))
11072 tp->ptp_clock = NULL;
11073 }
11074
Linus Torvalds1da177e2005-04-16 15:20:36 -070011075 return err;
11076}
11077
11078static int tg3_close(struct net_device *dev)
11079{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011080 struct tg3 *tp = netdev_priv(dev);
11081
Matt Carlsonbe947302012-12-03 19:36:57 +000011082 tg3_ptp_fini(tp);
11083
Michael Chan65138592012-09-28 07:12:41 +000011084 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011085
11086 /* Clear stats across close / open calls */
11087 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11088 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011089
11090 tg3_power_down(tp);
11091
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011092 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011093
11094 return 0;
11095}
11096
11097static inline u64 get_stat64(tg3_stat64_t *val)
11098{
11099 return ((u64)val->high << 32) | ((u64)val->low);
11100}
11101
11102static u64 tg3_calc_crc_errors(struct tg3 *tp)
11103{
11104 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11105
11106 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011107 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11108 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011109 u32 val;
11110
11111 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11112 tg3_writephy(tp, MII_TG3_TEST1,
11113 val | MII_TG3_TEST1_CRC_EN);
11114 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11115 } else
11116 val = 0;
11117
11118 tp->phy_crc_errors += val;
11119
11120 return tp->phy_crc_errors;
11121 }
11122
11123 return get_stat64(&hw_stats->rx_fcs_errors);
11124}
11125
11126#define ESTAT_ADD(member) \
11127 estats->member = old_estats->member + \
11128 get_stat64(&hw_stats->member)
11129
11130static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11131{
11132 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11133 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11134
11135 ESTAT_ADD(rx_octets);
11136 ESTAT_ADD(rx_fragments);
11137 ESTAT_ADD(rx_ucast_packets);
11138 ESTAT_ADD(rx_mcast_packets);
11139 ESTAT_ADD(rx_bcast_packets);
11140 ESTAT_ADD(rx_fcs_errors);
11141 ESTAT_ADD(rx_align_errors);
11142 ESTAT_ADD(rx_xon_pause_rcvd);
11143 ESTAT_ADD(rx_xoff_pause_rcvd);
11144 ESTAT_ADD(rx_mac_ctrl_rcvd);
11145 ESTAT_ADD(rx_xoff_entered);
11146 ESTAT_ADD(rx_frame_too_long_errors);
11147 ESTAT_ADD(rx_jabbers);
11148 ESTAT_ADD(rx_undersize_packets);
11149 ESTAT_ADD(rx_in_length_errors);
11150 ESTAT_ADD(rx_out_length_errors);
11151 ESTAT_ADD(rx_64_or_less_octet_packets);
11152 ESTAT_ADD(rx_65_to_127_octet_packets);
11153 ESTAT_ADD(rx_128_to_255_octet_packets);
11154 ESTAT_ADD(rx_256_to_511_octet_packets);
11155 ESTAT_ADD(rx_512_to_1023_octet_packets);
11156 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11157 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11158 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11159 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11160 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11161
11162 ESTAT_ADD(tx_octets);
11163 ESTAT_ADD(tx_collisions);
11164 ESTAT_ADD(tx_xon_sent);
11165 ESTAT_ADD(tx_xoff_sent);
11166 ESTAT_ADD(tx_flow_control);
11167 ESTAT_ADD(tx_mac_errors);
11168 ESTAT_ADD(tx_single_collisions);
11169 ESTAT_ADD(tx_mult_collisions);
11170 ESTAT_ADD(tx_deferred);
11171 ESTAT_ADD(tx_excessive_collisions);
11172 ESTAT_ADD(tx_late_collisions);
11173 ESTAT_ADD(tx_collide_2times);
11174 ESTAT_ADD(tx_collide_3times);
11175 ESTAT_ADD(tx_collide_4times);
11176 ESTAT_ADD(tx_collide_5times);
11177 ESTAT_ADD(tx_collide_6times);
11178 ESTAT_ADD(tx_collide_7times);
11179 ESTAT_ADD(tx_collide_8times);
11180 ESTAT_ADD(tx_collide_9times);
11181 ESTAT_ADD(tx_collide_10times);
11182 ESTAT_ADD(tx_collide_11times);
11183 ESTAT_ADD(tx_collide_12times);
11184 ESTAT_ADD(tx_collide_13times);
11185 ESTAT_ADD(tx_collide_14times);
11186 ESTAT_ADD(tx_collide_15times);
11187 ESTAT_ADD(tx_ucast_packets);
11188 ESTAT_ADD(tx_mcast_packets);
11189 ESTAT_ADD(tx_bcast_packets);
11190 ESTAT_ADD(tx_carrier_sense_errors);
11191 ESTAT_ADD(tx_discards);
11192 ESTAT_ADD(tx_errors);
11193
11194 ESTAT_ADD(dma_writeq_full);
11195 ESTAT_ADD(dma_write_prioq_full);
11196 ESTAT_ADD(rxbds_empty);
11197 ESTAT_ADD(rx_discards);
11198 ESTAT_ADD(rx_errors);
11199 ESTAT_ADD(rx_threshold_hit);
11200
11201 ESTAT_ADD(dma_readq_full);
11202 ESTAT_ADD(dma_read_prioq_full);
11203 ESTAT_ADD(tx_comp_queue_full);
11204
11205 ESTAT_ADD(ring_set_send_prod_index);
11206 ESTAT_ADD(ring_status_update);
11207 ESTAT_ADD(nic_irqs);
11208 ESTAT_ADD(nic_avoided_irqs);
11209 ESTAT_ADD(nic_tx_threshold_hit);
11210
Matt Carlson4452d092011-05-19 12:12:51 +000011211 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011212}
11213
Matt Carlson65ec6982012-02-28 23:33:37 +000011214static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011215{
Eric Dumazet511d2222010-07-07 20:44:24 +000011216 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011217 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11218
Linus Torvalds1da177e2005-04-16 15:20:36 -070011219 stats->rx_packets = old_stats->rx_packets +
11220 get_stat64(&hw_stats->rx_ucast_packets) +
11221 get_stat64(&hw_stats->rx_mcast_packets) +
11222 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011223
Linus Torvalds1da177e2005-04-16 15:20:36 -070011224 stats->tx_packets = old_stats->tx_packets +
11225 get_stat64(&hw_stats->tx_ucast_packets) +
11226 get_stat64(&hw_stats->tx_mcast_packets) +
11227 get_stat64(&hw_stats->tx_bcast_packets);
11228
11229 stats->rx_bytes = old_stats->rx_bytes +
11230 get_stat64(&hw_stats->rx_octets);
11231 stats->tx_bytes = old_stats->tx_bytes +
11232 get_stat64(&hw_stats->tx_octets);
11233
11234 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011235 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011236 stats->tx_errors = old_stats->tx_errors +
11237 get_stat64(&hw_stats->tx_errors) +
11238 get_stat64(&hw_stats->tx_mac_errors) +
11239 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11240 get_stat64(&hw_stats->tx_discards);
11241
11242 stats->multicast = old_stats->multicast +
11243 get_stat64(&hw_stats->rx_mcast_packets);
11244 stats->collisions = old_stats->collisions +
11245 get_stat64(&hw_stats->tx_collisions);
11246
11247 stats->rx_length_errors = old_stats->rx_length_errors +
11248 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11249 get_stat64(&hw_stats->rx_undersize_packets);
11250
11251 stats->rx_over_errors = old_stats->rx_over_errors +
11252 get_stat64(&hw_stats->rxbds_empty);
11253 stats->rx_frame_errors = old_stats->rx_frame_errors +
11254 get_stat64(&hw_stats->rx_align_errors);
11255 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11256 get_stat64(&hw_stats->tx_discards);
11257 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11258 get_stat64(&hw_stats->tx_carrier_sense_errors);
11259
11260 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011261 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262
John W. Linville4f63b872005-09-12 14:43:18 -070011263 stats->rx_missed_errors = old_stats->rx_missed_errors +
11264 get_stat64(&hw_stats->rx_discards);
11265
Eric Dumazetb0057c52010-10-10 19:55:52 +000011266 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011267 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011268}
11269
Linus Torvalds1da177e2005-04-16 15:20:36 -070011270static int tg3_get_regs_len(struct net_device *dev)
11271{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011272 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011273}
11274
11275static void tg3_get_regs(struct net_device *dev,
11276 struct ethtool_regs *regs, void *_p)
11277{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011278 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011279
11280 regs->version = 0;
11281
Matt Carlson97bd8e42011-04-13 11:05:04 +000011282 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011283
Matt Carlson80096062010-08-02 11:26:06 +000011284 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011285 return;
11286
David S. Millerf47c11e2005-06-24 20:18:35 -070011287 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011288
Matt Carlson97bd8e42011-04-13 11:05:04 +000011289 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011290
David S. Millerf47c11e2005-06-24 20:18:35 -070011291 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011292}
11293
11294static int tg3_get_eeprom_len(struct net_device *dev)
11295{
11296 struct tg3 *tp = netdev_priv(dev);
11297
11298 return tp->nvram_size;
11299}
11300
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11302{
11303 struct tg3 *tp = netdev_priv(dev);
11304 int ret;
11305 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011306 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011307 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011308
Joe Perches63c3a662011-04-26 08:12:10 +000011309 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011310 return -EINVAL;
11311
Matt Carlson80096062010-08-02 11:26:06 +000011312 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011313 return -EAGAIN;
11314
Linus Torvalds1da177e2005-04-16 15:20:36 -070011315 offset = eeprom->offset;
11316 len = eeprom->len;
11317 eeprom->len = 0;
11318
11319 eeprom->magic = TG3_EEPROM_MAGIC;
11320
11321 if (offset & 3) {
11322 /* adjustments to start on required 4 byte boundary */
11323 b_offset = offset & 3;
11324 b_count = 4 - b_offset;
11325 if (b_count > len) {
11326 /* i.e. offset=1 len=2 */
11327 b_count = len;
11328 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011329 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011330 if (ret)
11331 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011332 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333 len -= b_count;
11334 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011335 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011336 }
11337
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011338 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011339 pd = &data[eeprom->len];
11340 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011341 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011342 if (ret) {
11343 eeprom->len += i;
11344 return ret;
11345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011346 memcpy(pd + i, &val, 4);
11347 }
11348 eeprom->len += i;
11349
11350 if (len & 3) {
11351 /* read last bytes not ending on 4 byte boundary */
11352 pd = &data[eeprom->len];
11353 b_count = len & 3;
11354 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011355 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011356 if (ret)
11357 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011358 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011359 eeprom->len += b_count;
11360 }
11361 return 0;
11362}
11363
Linus Torvalds1da177e2005-04-16 15:20:36 -070011364static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11365{
11366 struct tg3 *tp = netdev_priv(dev);
11367 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011368 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011369 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011370 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011371
Matt Carlson80096062010-08-02 11:26:06 +000011372 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011373 return -EAGAIN;
11374
Joe Perches63c3a662011-04-26 08:12:10 +000011375 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011376 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011377 return -EINVAL;
11378
11379 offset = eeprom->offset;
11380 len = eeprom->len;
11381
11382 if ((b_offset = (offset & 3))) {
11383 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011384 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385 if (ret)
11386 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011387 len += b_offset;
11388 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011389 if (len < 4)
11390 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011391 }
11392
11393 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011394 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011395 /* adjustments to end on required 4 byte boundary */
11396 odd_len = 1;
11397 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011398 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011399 if (ret)
11400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011401 }
11402
11403 buf = data;
11404 if (b_offset || odd_len) {
11405 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011406 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011407 return -ENOMEM;
11408 if (b_offset)
11409 memcpy(buf, &start, 4);
11410 if (odd_len)
11411 memcpy(buf+len-4, &end, 4);
11412 memcpy(buf + b_offset, data, eeprom->len);
11413 }
11414
11415 ret = tg3_nvram_write_block(tp, offset, len, buf);
11416
11417 if (buf != data)
11418 kfree(buf);
11419
11420 return ret;
11421}
11422
11423static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11424{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011425 struct tg3 *tp = netdev_priv(dev);
11426
Joe Perches63c3a662011-04-26 08:12:10 +000011427 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011428 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011429 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011430 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011431 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11432 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011433 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011434
Linus Torvalds1da177e2005-04-16 15:20:36 -070011435 cmd->supported = (SUPPORTED_Autoneg);
11436
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011437 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011438 cmd->supported |= (SUPPORTED_1000baseT_Half |
11439 SUPPORTED_1000baseT_Full);
11440
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011441 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011442 cmd->supported |= (SUPPORTED_100baseT_Half |
11443 SUPPORTED_100baseT_Full |
11444 SUPPORTED_10baseT_Half |
11445 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011446 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011447 cmd->port = PORT_TP;
11448 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011449 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011450 cmd->port = PORT_FIBRE;
11451 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011452
Linus Torvalds1da177e2005-04-16 15:20:36 -070011453 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011454 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11455 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11456 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11457 cmd->advertising |= ADVERTISED_Pause;
11458 } else {
11459 cmd->advertising |= ADVERTISED_Pause |
11460 ADVERTISED_Asym_Pause;
11461 }
11462 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11463 cmd->advertising |= ADVERTISED_Asym_Pause;
11464 }
11465 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011466 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011467 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011468 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011469 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011470 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11471 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11472 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11473 else
11474 cmd->eth_tp_mdix = ETH_TP_MDI;
11475 }
Matt Carlson64c22182010-10-14 10:37:44 +000011476 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011477 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11478 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011479 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011480 }
Matt Carlson882e9792009-09-01 13:21:36 +000011481 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011482 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011483 cmd->autoneg = tp->link_config.autoneg;
11484 cmd->maxtxpkt = 0;
11485 cmd->maxrxpkt = 0;
11486 return 0;
11487}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011488
Linus Torvalds1da177e2005-04-16 15:20:36 -070011489static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11490{
11491 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011492 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011493
Joe Perches63c3a662011-04-26 08:12:10 +000011494 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011495 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011496 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011497 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011498 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11499 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011500 }
11501
Matt Carlson7e5856b2009-02-25 14:23:01 +000011502 if (cmd->autoneg != AUTONEG_ENABLE &&
11503 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011504 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011505
11506 if (cmd->autoneg == AUTONEG_DISABLE &&
11507 cmd->duplex != DUPLEX_FULL &&
11508 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011510
Matt Carlson7e5856b2009-02-25 14:23:01 +000011511 if (cmd->autoneg == AUTONEG_ENABLE) {
11512 u32 mask = ADVERTISED_Autoneg |
11513 ADVERTISED_Pause |
11514 ADVERTISED_Asym_Pause;
11515
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011516 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011517 mask |= ADVERTISED_1000baseT_Half |
11518 ADVERTISED_1000baseT_Full;
11519
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011520 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011521 mask |= ADVERTISED_100baseT_Half |
11522 ADVERTISED_100baseT_Full |
11523 ADVERTISED_10baseT_Half |
11524 ADVERTISED_10baseT_Full |
11525 ADVERTISED_TP;
11526 else
11527 mask |= ADVERTISED_FIBRE;
11528
11529 if (cmd->advertising & ~mask)
11530 return -EINVAL;
11531
11532 mask &= (ADVERTISED_1000baseT_Half |
11533 ADVERTISED_1000baseT_Full |
11534 ADVERTISED_100baseT_Half |
11535 ADVERTISED_100baseT_Full |
11536 ADVERTISED_10baseT_Half |
11537 ADVERTISED_10baseT_Full);
11538
11539 cmd->advertising &= mask;
11540 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011541 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011542 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011543 return -EINVAL;
11544
11545 if (cmd->duplex != DUPLEX_FULL)
11546 return -EINVAL;
11547 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011548 if (speed != SPEED_100 &&
11549 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011550 return -EINVAL;
11551 }
11552 }
11553
David S. Millerf47c11e2005-06-24 20:18:35 -070011554 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011555
11556 tp->link_config.autoneg = cmd->autoneg;
11557 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011558 tp->link_config.advertising = (cmd->advertising |
11559 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011560 tp->link_config.speed = SPEED_UNKNOWN;
11561 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011562 } else {
11563 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011564 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011565 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011566 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011567
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568 if (netif_running(dev))
11569 tg3_setup_phy(tp, 1);
11570
David S. Millerf47c11e2005-06-24 20:18:35 -070011571 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011572
Linus Torvalds1da177e2005-04-16 15:20:36 -070011573 return 0;
11574}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011575
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11577{
11578 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011579
Rick Jones68aad782011-11-07 13:29:27 +000011580 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11581 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11582 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11583 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011585
Linus Torvalds1da177e2005-04-16 15:20:36 -070011586static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11587{
11588 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011589
Joe Perches63c3a662011-04-26 08:12:10 +000011590 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011591 wol->supported = WAKE_MAGIC;
11592 else
11593 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011595 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596 wol->wolopts = WAKE_MAGIC;
11597 memset(&wol->sopass, 0, sizeof(wol->sopass));
11598}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011599
Linus Torvalds1da177e2005-04-16 15:20:36 -070011600static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11601{
11602 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011603 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011604
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605 if (wol->wolopts & ~WAKE_MAGIC)
11606 return -EINVAL;
11607 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011608 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011609 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011610
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011611 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11612
David S. Millerf47c11e2005-06-24 20:18:35 -070011613 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011614 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011615 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011616 else
Joe Perches63c3a662011-04-26 08:12:10 +000011617 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011618 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011619
Linus Torvalds1da177e2005-04-16 15:20:36 -070011620 return 0;
11621}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011622
Linus Torvalds1da177e2005-04-16 15:20:36 -070011623static u32 tg3_get_msglevel(struct net_device *dev)
11624{
11625 struct tg3 *tp = netdev_priv(dev);
11626 return tp->msg_enable;
11627}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011628
Linus Torvalds1da177e2005-04-16 15:20:36 -070011629static void tg3_set_msglevel(struct net_device *dev, u32 value)
11630{
11631 struct tg3 *tp = netdev_priv(dev);
11632 tp->msg_enable = value;
11633}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011634
Linus Torvalds1da177e2005-04-16 15:20:36 -070011635static int tg3_nway_reset(struct net_device *dev)
11636{
11637 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011639
Linus Torvalds1da177e2005-04-16 15:20:36 -070011640 if (!netif_running(dev))
11641 return -EAGAIN;
11642
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011643 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011644 return -EINVAL;
11645
Joe Perches63c3a662011-04-26 08:12:10 +000011646 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011647 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011648 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011649 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011650 } else {
11651 u32 bmcr;
11652
11653 spin_lock_bh(&tp->lock);
11654 r = -EINVAL;
11655 tg3_readphy(tp, MII_BMCR, &bmcr);
11656 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11657 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011658 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011659 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11660 BMCR_ANENABLE);
11661 r = 0;
11662 }
11663 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011664 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011665
Linus Torvalds1da177e2005-04-16 15:20:36 -070011666 return r;
11667}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011668
Linus Torvalds1da177e2005-04-16 15:20:36 -070011669static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11670{
11671 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011672
Matt Carlson2c49a442010-09-30 10:34:35 +000011673 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011674 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011675 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011676 else
11677 ering->rx_jumbo_max_pending = 0;
11678
11679 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680
11681 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011682 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011683 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11684 else
11685 ering->rx_jumbo_pending = 0;
11686
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011687 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011689
Linus Torvalds1da177e2005-04-16 15:20:36 -070011690static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11691{
11692 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011693 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011694
Matt Carlson2c49a442010-09-30 10:34:35 +000011695 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11696 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011697 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11698 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011699 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011700 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011701 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011702
Michael Chanbbe832c2005-06-24 20:20:04 -070011703 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011704 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011705 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011706 irq_sync = 1;
11707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011708
Michael Chanbbe832c2005-06-24 20:20:04 -070011709 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011710
Linus Torvalds1da177e2005-04-16 15:20:36 -070011711 tp->rx_pending = ering->rx_pending;
11712
Joe Perches63c3a662011-04-26 08:12:10 +000011713 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714 tp->rx_pending > 63)
11715 tp->rx_pending = 63;
11716 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011717
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011718 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011719 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011720
11721 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011722 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011723 err = tg3_restart_hw(tp, 1);
11724 if (!err)
11725 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 }
11727
David S. Millerf47c11e2005-06-24 20:18:35 -070011728 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011729
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011730 if (irq_sync && !err)
11731 tg3_phy_start(tp);
11732
Michael Chanb9ec6c12006-07-25 16:37:27 -070011733 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011735
Linus Torvalds1da177e2005-04-16 15:20:36 -070011736static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11737{
11738 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011739
Joe Perches63c3a662011-04-26 08:12:10 +000011740 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011741
Matt Carlson4a2db502011-12-08 14:40:17 +000011742 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011743 epause->rx_pause = 1;
11744 else
11745 epause->rx_pause = 0;
11746
Matt Carlson4a2db502011-12-08 14:40:17 +000011747 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011748 epause->tx_pause = 1;
11749 else
11750 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011751}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011752
Linus Torvalds1da177e2005-04-16 15:20:36 -070011753static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11754{
11755 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011756 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011757
Joe Perches63c3a662011-04-26 08:12:10 +000011758 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011759 u32 newadv;
11760 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011761
Matt Carlson27121682010-02-17 15:16:57 +000011762 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011763
Matt Carlson27121682010-02-17 15:16:57 +000011764 if (!(phydev->supported & SUPPORTED_Pause) ||
11765 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011766 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011767 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011768
Matt Carlson27121682010-02-17 15:16:57 +000011769 tp->link_config.flowctrl = 0;
11770 if (epause->rx_pause) {
11771 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011772
Matt Carlson27121682010-02-17 15:16:57 +000011773 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011774 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011775 newadv = ADVERTISED_Pause;
11776 } else
11777 newadv = ADVERTISED_Pause |
11778 ADVERTISED_Asym_Pause;
11779 } else if (epause->tx_pause) {
11780 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11781 newadv = ADVERTISED_Asym_Pause;
11782 } else
11783 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011784
Matt Carlson27121682010-02-17 15:16:57 +000011785 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011786 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011787 else
Joe Perches63c3a662011-04-26 08:12:10 +000011788 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011789
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011790 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011791 u32 oldadv = phydev->advertising &
11792 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11793 if (oldadv != newadv) {
11794 phydev->advertising &=
11795 ~(ADVERTISED_Pause |
11796 ADVERTISED_Asym_Pause);
11797 phydev->advertising |= newadv;
11798 if (phydev->autoneg) {
11799 /*
11800 * Always renegotiate the link to
11801 * inform our link partner of our
11802 * flow control settings, even if the
11803 * flow control is forced. Let
11804 * tg3_adjust_link() do the final
11805 * flow control setup.
11806 */
11807 return phy_start_aneg(phydev);
11808 }
11809 }
11810
11811 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011812 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011813 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011814 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011815 ~(ADVERTISED_Pause |
11816 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011817 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011818 }
11819 } else {
11820 int irq_sync = 0;
11821
11822 if (netif_running(dev)) {
11823 tg3_netif_stop(tp);
11824 irq_sync = 1;
11825 }
11826
11827 tg3_full_lock(tp, irq_sync);
11828
11829 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011830 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011831 else
Joe Perches63c3a662011-04-26 08:12:10 +000011832 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011833 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011834 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011835 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011836 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011837 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011838 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011839 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011840 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011841
11842 if (netif_running(dev)) {
11843 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11844 err = tg3_restart_hw(tp, 1);
11845 if (!err)
11846 tg3_netif_start(tp);
11847 }
11848
11849 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011851
Michael Chanb9ec6c12006-07-25 16:37:27 -070011852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011853}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011854
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011855static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011856{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011857 switch (sset) {
11858 case ETH_SS_TEST:
11859 return TG3_NUM_TEST;
11860 case ETH_SS_STATS:
11861 return TG3_NUM_STATS;
11862 default:
11863 return -EOPNOTSUPP;
11864 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011865}
11866
Matt Carlson90415472011-12-16 13:33:23 +000011867static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11868 u32 *rules __always_unused)
11869{
11870 struct tg3 *tp = netdev_priv(dev);
11871
11872 if (!tg3_flag(tp, SUPPORT_MSIX))
11873 return -EOPNOTSUPP;
11874
11875 switch (info->cmd) {
11876 case ETHTOOL_GRXRINGS:
11877 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011878 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011879 else {
11880 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011881 if (info->data > TG3_RSS_MAX_NUM_QS)
11882 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011883 }
11884
11885 /* The first interrupt vector only
11886 * handles link interrupts.
11887 */
11888 info->data -= 1;
11889 return 0;
11890
11891 default:
11892 return -EOPNOTSUPP;
11893 }
11894}
11895
11896static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11897{
11898 u32 size = 0;
11899 struct tg3 *tp = netdev_priv(dev);
11900
11901 if (tg3_flag(tp, SUPPORT_MSIX))
11902 size = TG3_RSS_INDIR_TBL_SIZE;
11903
11904 return size;
11905}
11906
11907static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11908{
11909 struct tg3 *tp = netdev_priv(dev);
11910 int i;
11911
11912 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11913 indir[i] = tp->rss_ind_tbl[i];
11914
11915 return 0;
11916}
11917
11918static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11919{
11920 struct tg3 *tp = netdev_priv(dev);
11921 size_t i;
11922
11923 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11924 tp->rss_ind_tbl[i] = indir[i];
11925
11926 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11927 return 0;
11928
11929 /* It is legal to write the indirection
11930 * table while the device is running.
11931 */
11932 tg3_full_lock(tp, 0);
11933 tg3_rss_write_indir_tbl(tp);
11934 tg3_full_unlock(tp);
11935
11936 return 0;
11937}
11938
Michael Chan09681692012-09-28 07:12:42 +000011939static void tg3_get_channels(struct net_device *dev,
11940 struct ethtool_channels *channel)
11941{
11942 struct tg3 *tp = netdev_priv(dev);
11943 u32 deflt_qs = netif_get_num_default_rss_queues();
11944
11945 channel->max_rx = tp->rxq_max;
11946 channel->max_tx = tp->txq_max;
11947
11948 if (netif_running(dev)) {
11949 channel->rx_count = tp->rxq_cnt;
11950 channel->tx_count = tp->txq_cnt;
11951 } else {
11952 if (tp->rxq_req)
11953 channel->rx_count = tp->rxq_req;
11954 else
11955 channel->rx_count = min(deflt_qs, tp->rxq_max);
11956
11957 if (tp->txq_req)
11958 channel->tx_count = tp->txq_req;
11959 else
11960 channel->tx_count = min(deflt_qs, tp->txq_max);
11961 }
11962}
11963
11964static int tg3_set_channels(struct net_device *dev,
11965 struct ethtool_channels *channel)
11966{
11967 struct tg3 *tp = netdev_priv(dev);
11968
11969 if (!tg3_flag(tp, SUPPORT_MSIX))
11970 return -EOPNOTSUPP;
11971
11972 if (channel->rx_count > tp->rxq_max ||
11973 channel->tx_count > tp->txq_max)
11974 return -EINVAL;
11975
11976 tp->rxq_req = channel->rx_count;
11977 tp->txq_req = channel->tx_count;
11978
11979 if (!netif_running(dev))
11980 return 0;
11981
11982 tg3_stop(tp);
11983
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011984 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011985
Matt Carlsonbe947302012-12-03 19:36:57 +000011986 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011987
11988 return 0;
11989}
11990
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011991static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992{
11993 switch (stringset) {
11994 case ETH_SS_STATS:
11995 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11996 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011997 case ETH_SS_TEST:
11998 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000 default:
12001 WARN_ON(1); /* we need a WARN() */
12002 break;
12003 }
12004}
12005
stephen hemminger81b87092011-04-04 08:43:50 +000012006static int tg3_set_phys_id(struct net_device *dev,
12007 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012008{
12009 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012010
12011 if (!netif_running(tp->dev))
12012 return -EAGAIN;
12013
stephen hemminger81b87092011-04-04 08:43:50 +000012014 switch (state) {
12015 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012016 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012017
stephen hemminger81b87092011-04-04 08:43:50 +000012018 case ETHTOOL_ID_ON:
12019 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12020 LED_CTRL_1000MBPS_ON |
12021 LED_CTRL_100MBPS_ON |
12022 LED_CTRL_10MBPS_ON |
12023 LED_CTRL_TRAFFIC_OVERRIDE |
12024 LED_CTRL_TRAFFIC_BLINK |
12025 LED_CTRL_TRAFFIC_LED);
12026 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012027
stephen hemminger81b87092011-04-04 08:43:50 +000012028 case ETHTOOL_ID_OFF:
12029 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12030 LED_CTRL_TRAFFIC_OVERRIDE);
12031 break;
Michael Chan4009a932005-09-05 17:52:54 -070012032
stephen hemminger81b87092011-04-04 08:43:50 +000012033 case ETHTOOL_ID_INACTIVE:
12034 tw32(MAC_LED_CTRL, tp->led_ctrl);
12035 break;
Michael Chan4009a932005-09-05 17:52:54 -070012036 }
stephen hemminger81b87092011-04-04 08:43:50 +000012037
Michael Chan4009a932005-09-05 17:52:54 -070012038 return 0;
12039}
12040
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012041static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012042 struct ethtool_stats *estats, u64 *tmp_stats)
12043{
12044 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012045
Matt Carlsonb546e462012-02-13 15:20:09 +000012046 if (tp->hw_stats)
12047 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12048 else
12049 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012050}
12051
Matt Carlson535a4902011-07-20 10:20:56 +000012052static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012053{
12054 int i;
12055 __be32 *buf;
12056 u32 offset = 0, len = 0;
12057 u32 magic, val;
12058
Joe Perches63c3a662011-04-26 08:12:10 +000012059 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012060 return NULL;
12061
12062 if (magic == TG3_EEPROM_MAGIC) {
12063 for (offset = TG3_NVM_DIR_START;
12064 offset < TG3_NVM_DIR_END;
12065 offset += TG3_NVM_DIRENT_SIZE) {
12066 if (tg3_nvram_read(tp, offset, &val))
12067 return NULL;
12068
12069 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12070 TG3_NVM_DIRTYPE_EXTVPD)
12071 break;
12072 }
12073
12074 if (offset != TG3_NVM_DIR_END) {
12075 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12076 if (tg3_nvram_read(tp, offset + 4, &offset))
12077 return NULL;
12078
12079 offset = tg3_nvram_logical_addr(tp, offset);
12080 }
12081 }
12082
12083 if (!offset || !len) {
12084 offset = TG3_NVM_VPD_OFF;
12085 len = TG3_NVM_VPD_LEN;
12086 }
12087
12088 buf = kmalloc(len, GFP_KERNEL);
12089 if (buf == NULL)
12090 return NULL;
12091
12092 if (magic == TG3_EEPROM_MAGIC) {
12093 for (i = 0; i < len; i += 4) {
12094 /* The data is in little-endian format in NVRAM.
12095 * Use the big-endian read routines to preserve
12096 * the byte order as it exists in NVRAM.
12097 */
12098 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12099 goto error;
12100 }
12101 } else {
12102 u8 *ptr;
12103 ssize_t cnt;
12104 unsigned int pos = 0;
12105
12106 ptr = (u8 *)&buf[0];
12107 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12108 cnt = pci_read_vpd(tp->pdev, pos,
12109 len - pos, ptr);
12110 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12111 cnt = 0;
12112 else if (cnt < 0)
12113 goto error;
12114 }
12115 if (pos != len)
12116 goto error;
12117 }
12118
Matt Carlson535a4902011-07-20 10:20:56 +000012119 *vpdlen = len;
12120
Matt Carlsonc3e94502011-04-13 11:05:08 +000012121 return buf;
12122
12123error:
12124 kfree(buf);
12125 return NULL;
12126}
12127
Michael Chan566f86a2005-05-29 14:56:58 -070012128#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012129#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12130#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12131#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012132#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12133#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012134#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012135#define NVRAM_SELFBOOT_HW_SIZE 0x20
12136#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012137
12138static int tg3_test_nvram(struct tg3 *tp)
12139{
Matt Carlson535a4902011-07-20 10:20:56 +000012140 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012141 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012142 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012143
Joe Perches63c3a662011-04-26 08:12:10 +000012144 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012145 return 0;
12146
Matt Carlsone4f34112009-02-25 14:25:00 +000012147 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012148 return -EIO;
12149
Michael Chan1b277772006-03-20 22:27:48 -080012150 if (magic == TG3_EEPROM_MAGIC)
12151 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012152 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012153 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12154 TG3_EEPROM_SB_FORMAT_1) {
12155 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12156 case TG3_EEPROM_SB_REVISION_0:
12157 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12158 break;
12159 case TG3_EEPROM_SB_REVISION_2:
12160 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12161 break;
12162 case TG3_EEPROM_SB_REVISION_3:
12163 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12164 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012165 case TG3_EEPROM_SB_REVISION_4:
12166 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12167 break;
12168 case TG3_EEPROM_SB_REVISION_5:
12169 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12170 break;
12171 case TG3_EEPROM_SB_REVISION_6:
12172 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12173 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012174 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012175 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012176 }
12177 } else
Michael Chan1b277772006-03-20 22:27:48 -080012178 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012179 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12180 size = NVRAM_SELFBOOT_HW_SIZE;
12181 else
Michael Chan1b277772006-03-20 22:27:48 -080012182 return -EIO;
12183
12184 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012185 if (buf == NULL)
12186 return -ENOMEM;
12187
Michael Chan1b277772006-03-20 22:27:48 -080012188 err = -EIO;
12189 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012190 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12191 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012192 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012193 }
Michael Chan1b277772006-03-20 22:27:48 -080012194 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012195 goto out;
12196
Michael Chan1b277772006-03-20 22:27:48 -080012197 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012198 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012199 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012200 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012201 u8 *buf8 = (u8 *) buf, csum8 = 0;
12202
Al Virob9fc7dc2007-12-17 22:59:57 -080012203 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012204 TG3_EEPROM_SB_REVISION_2) {
12205 /* For rev 2, the csum doesn't include the MBA. */
12206 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12207 csum8 += buf8[i];
12208 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12209 csum8 += buf8[i];
12210 } else {
12211 for (i = 0; i < size; i++)
12212 csum8 += buf8[i];
12213 }
Michael Chan1b277772006-03-20 22:27:48 -080012214
Adrian Bunkad96b482006-04-05 22:21:04 -070012215 if (csum8 == 0) {
12216 err = 0;
12217 goto out;
12218 }
12219
12220 err = -EIO;
12221 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012222 }
Michael Chan566f86a2005-05-29 14:56:58 -070012223
Al Virob9fc7dc2007-12-17 22:59:57 -080012224 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012225 TG3_EEPROM_MAGIC_HW) {
12226 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012227 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012228 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012229
12230 /* Separate the parity bits and the data bytes. */
12231 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12232 if ((i == 0) || (i == 8)) {
12233 int l;
12234 u8 msk;
12235
12236 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12237 parity[k++] = buf8[i] & msk;
12238 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012239 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012240 int l;
12241 u8 msk;
12242
12243 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12244 parity[k++] = buf8[i] & msk;
12245 i++;
12246
12247 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12248 parity[k++] = buf8[i] & msk;
12249 i++;
12250 }
12251 data[j++] = buf8[i];
12252 }
12253
12254 err = -EIO;
12255 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12256 u8 hw8 = hweight8(data[i]);
12257
12258 if ((hw8 & 0x1) && parity[i])
12259 goto out;
12260 else if (!(hw8 & 0x1) && !parity[i])
12261 goto out;
12262 }
12263 err = 0;
12264 goto out;
12265 }
12266
Matt Carlson01c3a392011-03-09 16:58:20 +000012267 err = -EIO;
12268
Michael Chan566f86a2005-05-29 14:56:58 -070012269 /* Bootstrap checksum at offset 0x10 */
12270 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012271 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012272 goto out;
12273
12274 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12275 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012276 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012277 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012278
Matt Carlsonc3e94502011-04-13 11:05:08 +000012279 kfree(buf);
12280
Matt Carlson535a4902011-07-20 10:20:56 +000012281 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012282 if (!buf)
12283 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012284
Matt Carlson535a4902011-07-20 10:20:56 +000012285 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012286 if (i > 0) {
12287 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12288 if (j < 0)
12289 goto out;
12290
Matt Carlson535a4902011-07-20 10:20:56 +000012291 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012292 goto out;
12293
12294 i += PCI_VPD_LRDT_TAG_SIZE;
12295 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12296 PCI_VPD_RO_KEYWORD_CHKSUM);
12297 if (j > 0) {
12298 u8 csum8 = 0;
12299
12300 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12301
12302 for (i = 0; i <= j; i++)
12303 csum8 += ((u8 *)buf)[i];
12304
12305 if (csum8)
12306 goto out;
12307 }
12308 }
12309
Michael Chan566f86a2005-05-29 14:56:58 -070012310 err = 0;
12311
12312out:
12313 kfree(buf);
12314 return err;
12315}
12316
Michael Chanca430072005-05-29 14:57:23 -070012317#define TG3_SERDES_TIMEOUT_SEC 2
12318#define TG3_COPPER_TIMEOUT_SEC 6
12319
12320static int tg3_test_link(struct tg3 *tp)
12321{
12322 int i, max;
12323
12324 if (!netif_running(tp->dev))
12325 return -ENODEV;
12326
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012327 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012328 max = TG3_SERDES_TIMEOUT_SEC;
12329 else
12330 max = TG3_COPPER_TIMEOUT_SEC;
12331
12332 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012333 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012334 return 0;
12335
12336 if (msleep_interruptible(1000))
12337 break;
12338 }
12339
12340 return -EIO;
12341}
12342
Michael Chana71116d2005-05-29 14:58:11 -070012343/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012344static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012345{
Michael Chanb16250e2006-09-27 16:10:14 -070012346 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012347 u32 offset, read_mask, write_mask, val, save_val, read_val;
12348 static struct {
12349 u16 offset;
12350 u16 flags;
12351#define TG3_FL_5705 0x1
12352#define TG3_FL_NOT_5705 0x2
12353#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012354#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012355 u32 read_mask;
12356 u32 write_mask;
12357 } reg_tbl[] = {
12358 /* MAC Control Registers */
12359 { MAC_MODE, TG3_FL_NOT_5705,
12360 0x00000000, 0x00ef6f8c },
12361 { MAC_MODE, TG3_FL_5705,
12362 0x00000000, 0x01ef6b8c },
12363 { MAC_STATUS, TG3_FL_NOT_5705,
12364 0x03800107, 0x00000000 },
12365 { MAC_STATUS, TG3_FL_5705,
12366 0x03800100, 0x00000000 },
12367 { MAC_ADDR_0_HIGH, 0x0000,
12368 0x00000000, 0x0000ffff },
12369 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012370 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012371 { MAC_RX_MTU_SIZE, 0x0000,
12372 0x00000000, 0x0000ffff },
12373 { MAC_TX_MODE, 0x0000,
12374 0x00000000, 0x00000070 },
12375 { MAC_TX_LENGTHS, 0x0000,
12376 0x00000000, 0x00003fff },
12377 { MAC_RX_MODE, TG3_FL_NOT_5705,
12378 0x00000000, 0x000007fc },
12379 { MAC_RX_MODE, TG3_FL_5705,
12380 0x00000000, 0x000007dc },
12381 { MAC_HASH_REG_0, 0x0000,
12382 0x00000000, 0xffffffff },
12383 { MAC_HASH_REG_1, 0x0000,
12384 0x00000000, 0xffffffff },
12385 { MAC_HASH_REG_2, 0x0000,
12386 0x00000000, 0xffffffff },
12387 { MAC_HASH_REG_3, 0x0000,
12388 0x00000000, 0xffffffff },
12389
12390 /* Receive Data and Receive BD Initiator Control Registers. */
12391 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12392 0x00000000, 0xffffffff },
12393 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12394 0x00000000, 0xffffffff },
12395 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12396 0x00000000, 0x00000003 },
12397 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12398 0x00000000, 0xffffffff },
12399 { RCVDBDI_STD_BD+0, 0x0000,
12400 0x00000000, 0xffffffff },
12401 { RCVDBDI_STD_BD+4, 0x0000,
12402 0x00000000, 0xffffffff },
12403 { RCVDBDI_STD_BD+8, 0x0000,
12404 0x00000000, 0xffff0002 },
12405 { RCVDBDI_STD_BD+0xc, 0x0000,
12406 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012407
Michael Chana71116d2005-05-29 14:58:11 -070012408 /* Receive BD Initiator Control Registers. */
12409 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12410 0x00000000, 0xffffffff },
12411 { RCVBDI_STD_THRESH, TG3_FL_5705,
12412 0x00000000, 0x000003ff },
12413 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12414 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012415
Michael Chana71116d2005-05-29 14:58:11 -070012416 /* Host Coalescing Control Registers. */
12417 { HOSTCC_MODE, TG3_FL_NOT_5705,
12418 0x00000000, 0x00000004 },
12419 { HOSTCC_MODE, TG3_FL_5705,
12420 0x00000000, 0x000000f6 },
12421 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12422 0x00000000, 0xffffffff },
12423 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12424 0x00000000, 0x000003ff },
12425 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12426 0x00000000, 0xffffffff },
12427 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12428 0x00000000, 0x000003ff },
12429 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12430 0x00000000, 0xffffffff },
12431 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12432 0x00000000, 0x000000ff },
12433 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12434 0x00000000, 0xffffffff },
12435 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12436 0x00000000, 0x000000ff },
12437 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12438 0x00000000, 0xffffffff },
12439 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12440 0x00000000, 0xffffffff },
12441 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12442 0x00000000, 0xffffffff },
12443 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12444 0x00000000, 0x000000ff },
12445 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12446 0x00000000, 0xffffffff },
12447 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12448 0x00000000, 0x000000ff },
12449 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12450 0x00000000, 0xffffffff },
12451 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12452 0x00000000, 0xffffffff },
12453 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12454 0x00000000, 0xffffffff },
12455 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12456 0x00000000, 0xffffffff },
12457 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12458 0x00000000, 0xffffffff },
12459 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12460 0xffffffff, 0x00000000 },
12461 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12462 0xffffffff, 0x00000000 },
12463
12464 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012465 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012466 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012467 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012468 0x00000000, 0x007fffff },
12469 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12470 0x00000000, 0x0000003f },
12471 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12472 0x00000000, 0x000001ff },
12473 { BUFMGR_MB_HIGH_WATER, 0x0000,
12474 0x00000000, 0x000001ff },
12475 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12476 0xffffffff, 0x00000000 },
12477 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12478 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012479
Michael Chana71116d2005-05-29 14:58:11 -070012480 /* Mailbox Registers */
12481 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12482 0x00000000, 0x000001ff },
12483 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12484 0x00000000, 0x000001ff },
12485 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12486 0x00000000, 0x000007ff },
12487 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12488 0x00000000, 0x000001ff },
12489
12490 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12491 };
12492
Michael Chanb16250e2006-09-27 16:10:14 -070012493 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012494 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012495 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012496 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012497 is_5750 = 1;
12498 }
Michael Chana71116d2005-05-29 14:58:11 -070012499
12500 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12501 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12502 continue;
12503
12504 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12505 continue;
12506
Joe Perches63c3a662011-04-26 08:12:10 +000012507 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012508 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12509 continue;
12510
Michael Chanb16250e2006-09-27 16:10:14 -070012511 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12512 continue;
12513
Michael Chana71116d2005-05-29 14:58:11 -070012514 offset = (u32) reg_tbl[i].offset;
12515 read_mask = reg_tbl[i].read_mask;
12516 write_mask = reg_tbl[i].write_mask;
12517
12518 /* Save the original register content */
12519 save_val = tr32(offset);
12520
12521 /* Determine the read-only value. */
12522 read_val = save_val & read_mask;
12523
12524 /* Write zero to the register, then make sure the read-only bits
12525 * are not changed and the read/write bits are all zeros.
12526 */
12527 tw32(offset, 0);
12528
12529 val = tr32(offset);
12530
12531 /* Test the read-only and read/write bits. */
12532 if (((val & read_mask) != read_val) || (val & write_mask))
12533 goto out;
12534
12535 /* Write ones to all the bits defined by RdMask and WrMask, then
12536 * make sure the read-only bits are not changed and the
12537 * read/write bits are all ones.
12538 */
12539 tw32(offset, read_mask | write_mask);
12540
12541 val = tr32(offset);
12542
12543 /* Test the read-only bits. */
12544 if ((val & read_mask) != read_val)
12545 goto out;
12546
12547 /* Test the read/write bits. */
12548 if ((val & write_mask) != write_mask)
12549 goto out;
12550
12551 tw32(offset, save_val);
12552 }
12553
12554 return 0;
12555
12556out:
Michael Chan9f88f292006-12-07 00:22:54 -080012557 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012558 netdev_err(tp->dev,
12559 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012560 tw32(offset, save_val);
12561 return -EIO;
12562}
12563
Michael Chan7942e1d2005-05-29 14:58:36 -070012564static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12565{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012566 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012567 int i;
12568 u32 j;
12569
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012570 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012571 for (j = 0; j < len; j += 4) {
12572 u32 val;
12573
12574 tg3_write_mem(tp, offset + j, test_pattern[i]);
12575 tg3_read_mem(tp, offset + j, &val);
12576 if (val != test_pattern[i])
12577 return -EIO;
12578 }
12579 }
12580 return 0;
12581}
12582
12583static int tg3_test_memory(struct tg3 *tp)
12584{
12585 static struct mem_entry {
12586 u32 offset;
12587 u32 len;
12588 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012589 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012590 { 0x00002000, 0x1c000},
12591 { 0xffffffff, 0x00000}
12592 }, mem_tbl_5705[] = {
12593 { 0x00000100, 0x0000c},
12594 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012595 { 0x00004000, 0x00800},
12596 { 0x00006000, 0x01000},
12597 { 0x00008000, 0x02000},
12598 { 0x00010000, 0x0e000},
12599 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012600 }, mem_tbl_5755[] = {
12601 { 0x00000200, 0x00008},
12602 { 0x00004000, 0x00800},
12603 { 0x00006000, 0x00800},
12604 { 0x00008000, 0x02000},
12605 { 0x00010000, 0x0c000},
12606 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012607 }, mem_tbl_5906[] = {
12608 { 0x00000200, 0x00008},
12609 { 0x00004000, 0x00400},
12610 { 0x00006000, 0x00400},
12611 { 0x00008000, 0x01000},
12612 { 0x00010000, 0x01000},
12613 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012614 }, mem_tbl_5717[] = {
12615 { 0x00000200, 0x00008},
12616 { 0x00010000, 0x0a000},
12617 { 0x00020000, 0x13c00},
12618 { 0xffffffff, 0x00000}
12619 }, mem_tbl_57765[] = {
12620 { 0x00000200, 0x00008},
12621 { 0x00004000, 0x00800},
12622 { 0x00006000, 0x09800},
12623 { 0x00010000, 0x0a000},
12624 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012625 };
12626 struct mem_entry *mem_tbl;
12627 int err = 0;
12628 int i;
12629
Joe Perches63c3a662011-04-26 08:12:10 +000012630 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012631 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012632 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012633 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012634 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012635 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012636 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012637 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012638 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012639 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012640 mem_tbl = mem_tbl_5705;
12641 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012642 mem_tbl = mem_tbl_570x;
12643
12644 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012645 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12646 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012647 break;
12648 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012649
Michael Chan7942e1d2005-05-29 14:58:36 -070012650 return err;
12651}
12652
Matt Carlsonbb158d62011-04-25 12:42:47 +000012653#define TG3_TSO_MSS 500
12654
12655#define TG3_TSO_IP_HDR_LEN 20
12656#define TG3_TSO_TCP_HDR_LEN 20
12657#define TG3_TSO_TCP_OPT_LEN 12
12658
12659static const u8 tg3_tso_header[] = {
126600x08, 0x00,
126610x45, 0x00, 0x00, 0x00,
126620x00, 0x00, 0x40, 0x00,
126630x40, 0x06, 0x00, 0x00,
126640x0a, 0x00, 0x00, 0x01,
126650x0a, 0x00, 0x00, 0x02,
126660x0d, 0x00, 0xe0, 0x00,
126670x00, 0x00, 0x01, 0x00,
126680x00, 0x00, 0x02, 0x00,
126690x80, 0x10, 0x10, 0x00,
126700x14, 0x09, 0x00, 0x00,
126710x01, 0x01, 0x08, 0x0a,
126720x11, 0x11, 0x11, 0x11,
126730x11, 0x11, 0x11, 0x11,
12674};
Michael Chan9f40dea2005-09-05 17:53:06 -070012675
Matt Carlson28a45952011-08-19 13:58:22 +000012676static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012677{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012678 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012679 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012680 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012681 struct sk_buff *skb;
12682 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012683 dma_addr_t map;
12684 int num_pkts, tx_len, rx_len, i, err;
12685 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012686 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012687 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012688
Matt Carlsonc8873402010-02-12 14:47:11 +000012689 tnapi = &tp->napi[0];
12690 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012691 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012692 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012693 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012694 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012695 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012696 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012697 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012698
Michael Chanc76949a2005-05-29 14:58:59 -070012699 err = -EIO;
12700
Matt Carlson4852a862011-04-13 11:05:07 +000012701 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012702 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012703 if (!skb)
12704 return -ENOMEM;
12705
Michael Chanc76949a2005-05-29 14:58:59 -070012706 tx_data = skb_put(skb, tx_len);
12707 memcpy(tx_data, tp->dev->dev_addr, 6);
12708 memset(tx_data + 6, 0x0, 8);
12709
Matt Carlson4852a862011-04-13 11:05:07 +000012710 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012711
Matt Carlson28a45952011-08-19 13:58:22 +000012712 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012713 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12714
12715 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12716 TG3_TSO_TCP_OPT_LEN;
12717
12718 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12719 sizeof(tg3_tso_header));
12720 mss = TG3_TSO_MSS;
12721
12722 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12723 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12724
12725 /* Set the total length field in the IP header */
12726 iph->tot_len = htons((u16)(mss + hdr_len));
12727
12728 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12729 TXD_FLAG_CPU_POST_DMA);
12730
Joe Perches63c3a662011-04-26 08:12:10 +000012731 if (tg3_flag(tp, HW_TSO_1) ||
12732 tg3_flag(tp, HW_TSO_2) ||
12733 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012734 struct tcphdr *th;
12735 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12736 th = (struct tcphdr *)&tx_data[val];
12737 th->check = 0;
12738 } else
12739 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12740
Joe Perches63c3a662011-04-26 08:12:10 +000012741 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012742 mss |= (hdr_len & 0xc) << 12;
12743 if (hdr_len & 0x10)
12744 base_flags |= 0x00000010;
12745 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012746 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012747 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012748 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000012749 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012750 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12751 } else {
12752 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12753 }
12754
12755 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12756 } else {
12757 num_pkts = 1;
12758 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012759
12760 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12761 tx_len > VLAN_ETH_FRAME_LEN)
12762 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012763 }
12764
12765 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012766 tx_data[i] = (u8) (i & 0xff);
12767
Alexander Duyckf4188d82009-12-02 16:48:38 +000012768 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12769 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012770 dev_kfree_skb(skb);
12771 return -EIO;
12772 }
Michael Chanc76949a2005-05-29 14:58:59 -070012773
Matt Carlson0d681b22011-07-27 14:20:49 +000012774 val = tnapi->tx_prod;
12775 tnapi->tx_buffers[val].skb = skb;
12776 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12777
Michael Chanc76949a2005-05-29 14:58:59 -070012778 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012779 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012780
12781 udelay(10);
12782
Matt Carlson898a56f2009-08-28 14:02:40 +000012783 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012784
Matt Carlson84b67b22011-07-27 14:20:52 +000012785 budget = tg3_tx_avail(tnapi);
12786 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012787 base_flags | TXD_FLAG_END, mss, 0)) {
12788 tnapi->tx_buffers[val].skb = NULL;
12789 dev_kfree_skb(skb);
12790 return -EIO;
12791 }
Michael Chanc76949a2005-05-29 14:58:59 -070012792
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012793 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012794
Michael Chan6541b802012-03-04 14:48:14 +000012795 /* Sync BD data before updating mailbox */
12796 wmb();
12797
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012798 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12799 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012800
12801 udelay(10);
12802
Matt Carlson303fc922009-11-02 14:27:34 +000012803 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12804 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012805 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012806 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012807
12808 udelay(10);
12809
Matt Carlson898a56f2009-08-28 14:02:40 +000012810 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12811 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012812 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012813 (rx_idx == (rx_start_idx + num_pkts)))
12814 break;
12815 }
12816
Matt Carlsonba1142e2011-11-04 09:15:00 +000012817 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012818 dev_kfree_skb(skb);
12819
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012820 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012821 goto out;
12822
12823 if (rx_idx != rx_start_idx + num_pkts)
12824 goto out;
12825
Matt Carlsonbb158d62011-04-25 12:42:47 +000012826 val = data_off;
12827 while (rx_idx != rx_start_idx) {
12828 desc = &rnapi->rx_rcb[rx_start_idx++];
12829 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12830 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012831
Matt Carlsonbb158d62011-04-25 12:42:47 +000012832 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12833 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012834 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012835
Matt Carlsonbb158d62011-04-25 12:42:47 +000012836 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12837 - ETH_FCS_LEN;
12838
Matt Carlson28a45952011-08-19 13:58:22 +000012839 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012840 if (rx_len != tx_len)
12841 goto out;
12842
12843 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12844 if (opaque_key != RXD_OPAQUE_RING_STD)
12845 goto out;
12846 } else {
12847 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12848 goto out;
12849 }
12850 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12851 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012852 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012853 goto out;
12854 }
12855
12856 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012857 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012858 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12859 mapping);
12860 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012861 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012862 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12863 mapping);
12864 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012865 goto out;
12866
Matt Carlsonbb158d62011-04-25 12:42:47 +000012867 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12868 PCI_DMA_FROMDEVICE);
12869
Eric Dumazet9205fd92011-11-18 06:47:01 +000012870 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012871 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012872 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012873 goto out;
12874 }
Matt Carlson4852a862011-04-13 11:05:07 +000012875 }
12876
Michael Chanc76949a2005-05-29 14:58:59 -070012877 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012878
Eric Dumazet9205fd92011-11-18 06:47:01 +000012879 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012880out:
12881 return err;
12882}
12883
Matt Carlson00c266b2011-04-25 12:42:46 +000012884#define TG3_STD_LOOPBACK_FAILED 1
12885#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012886#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012887#define TG3_LOOPBACK_FAILED \
12888 (TG3_STD_LOOPBACK_FAILED | \
12889 TG3_JMB_LOOPBACK_FAILED | \
12890 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012891
Matt Carlson941ec902011-08-19 13:58:23 +000012892static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012893{
Matt Carlson28a45952011-08-19 13:58:22 +000012894 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012895 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012896 u32 jmb_pkt_sz = 9000;
12897
12898 if (tp->dma_limit)
12899 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012900
Matt Carlsonab789042011-01-25 15:58:54 +000012901 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12902 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12903
Matt Carlson28a45952011-08-19 13:58:22 +000012904 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012905 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12906 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012907 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012908 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012909 goto done;
12910 }
12911
Michael Chanb9ec6c12006-07-25 16:37:27 -070012912 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012913 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012914 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12915 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012916 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012917 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012918 goto done;
12919 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012920
Joe Perches63c3a662011-04-26 08:12:10 +000012921 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012922 int i;
12923
12924 /* Reroute all rx packets to the 1st queue */
12925 for (i = MAC_RSS_INDIR_TBL_0;
12926 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12927 tw32(i, 0x0);
12928 }
12929
Matt Carlson6e01b202011-08-19 13:58:20 +000012930 /* HW errata - mac loopback fails in some cases on 5780.
12931 * Normal traffic and PHY loopback are not affected by
12932 * errata. Also, the MAC loopback test is deprecated for
12933 * all newer ASIC revisions.
12934 */
Joe Perches41535772013-02-16 11:20:04 +000012935 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000012936 !tg3_flag(tp, CPMU_PRESENT)) {
12937 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012938
Matt Carlson28a45952011-08-19 13:58:22 +000012939 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012940 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012941
12942 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012943 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012944 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012945
12946 tg3_mac_loopback(tp, false);
12947 }
Matt Carlson4852a862011-04-13 11:05:07 +000012948
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012949 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012950 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012951 int i;
12952
Matt Carlson941ec902011-08-19 13:58:23 +000012953 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012954
12955 /* Wait for link */
12956 for (i = 0; i < 100; i++) {
12957 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12958 break;
12959 mdelay(1);
12960 }
12961
Matt Carlson28a45952011-08-19 13:58:22 +000012962 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012963 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012964 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012965 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012966 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012967 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012968 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012969 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012970
Matt Carlson941ec902011-08-19 13:58:23 +000012971 if (do_extlpbk) {
12972 tg3_phy_lpbk_set(tp, 0, true);
12973
12974 /* All link indications report up, but the hardware
12975 * isn't really ready for about 20 msec. Double it
12976 * to be sure.
12977 */
12978 mdelay(40);
12979
12980 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012981 data[TG3_EXT_LOOPB_TEST] |=
12982 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012983 if (tg3_flag(tp, TSO_CAPABLE) &&
12984 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012985 data[TG3_EXT_LOOPB_TEST] |=
12986 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012987 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012988 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012989 data[TG3_EXT_LOOPB_TEST] |=
12990 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012991 }
12992
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012993 /* Re-enable gphy autopowerdown. */
12994 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12995 tg3_phy_toggle_apd(tp, true);
12996 }
Matt Carlson6833c042008-11-21 17:18:59 -080012997
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012998 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12999 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013000
Matt Carlsonab789042011-01-25 15:58:54 +000013001done:
13002 tp->phy_flags |= eee_cap;
13003
Michael Chan9f40dea2005-09-05 17:53:06 -070013004 return err;
13005}
13006
Michael Chan4cafd3f2005-05-29 14:56:34 -070013007static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13008 u64 *data)
13009{
Michael Chan566f86a2005-05-29 14:56:58 -070013010 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013011 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013012
Matt Carlsonbed98292011-07-13 09:27:29 +000013013 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
13014 tg3_power_up(tp)) {
13015 etest->flags |= ETH_TEST_FL_FAILED;
13016 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13017 return;
13018 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013019
Michael Chan566f86a2005-05-29 14:56:58 -070013020 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13021
13022 if (tg3_test_nvram(tp) != 0) {
13023 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013024 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013025 }
Matt Carlson941ec902011-08-19 13:58:23 +000013026 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013027 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013028 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013029 }
Michael Chana71116d2005-05-29 14:58:11 -070013030 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013031 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013032
Michael Chanbbe832c2005-06-24 20:20:04 -070013033 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013034 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013035 tg3_netif_stop(tp);
13036 irq_sync = 1;
13037 }
13038
13039 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013040 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013041 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013042 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013043 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013044 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013045 if (!err)
13046 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013047
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013048 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013049 tg3_phy_reset(tp);
13050
Michael Chana71116d2005-05-29 14:58:11 -070013051 if (tg3_test_registers(tp) != 0) {
13052 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013053 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013054 }
Matt Carlson28a45952011-08-19 13:58:22 +000013055
Michael Chan7942e1d2005-05-29 14:58:36 -070013056 if (tg3_test_memory(tp) != 0) {
13057 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013058 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013059 }
Matt Carlson28a45952011-08-19 13:58:22 +000013060
Matt Carlson941ec902011-08-19 13:58:23 +000013061 if (doextlpbk)
13062 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13063
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013064 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013065 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013066
David S. Millerf47c11e2005-06-24 20:18:35 -070013067 tg3_full_unlock(tp);
13068
Michael Chand4bc3922005-05-29 14:59:20 -070013069 if (tg3_test_interrupt(tp) != 0) {
13070 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013071 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013072 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013073
13074 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013075
Michael Chana71116d2005-05-29 14:58:11 -070013076 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13077 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013078 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013079 err2 = tg3_restart_hw(tp, 1);
13080 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013081 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013082 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013083
13084 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013085
13086 if (irq_sync && !err2)
13087 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013088 }
Matt Carlson80096062010-08-02 11:26:06 +000013089 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013090 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013091
Michael Chan4cafd3f2005-05-29 14:56:34 -070013092}
13093
Matt Carlson0a633ac2012-12-03 19:36:59 +000013094static int tg3_hwtstamp_ioctl(struct net_device *dev,
13095 struct ifreq *ifr, int cmd)
13096{
13097 struct tg3 *tp = netdev_priv(dev);
13098 struct hwtstamp_config stmpconf;
13099
13100 if (!tg3_flag(tp, PTP_CAPABLE))
13101 return -EINVAL;
13102
13103 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13104 return -EFAULT;
13105
13106 if (stmpconf.flags)
13107 return -EINVAL;
13108
13109 switch (stmpconf.tx_type) {
13110 case HWTSTAMP_TX_ON:
13111 tg3_flag_set(tp, TX_TSTAMP_EN);
13112 break;
13113 case HWTSTAMP_TX_OFF:
13114 tg3_flag_clear(tp, TX_TSTAMP_EN);
13115 break;
13116 default:
13117 return -ERANGE;
13118 }
13119
13120 switch (stmpconf.rx_filter) {
13121 case HWTSTAMP_FILTER_NONE:
13122 tp->rxptpctl = 0;
13123 break;
13124 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13125 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13126 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13127 break;
13128 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13129 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13130 TG3_RX_PTP_CTL_SYNC_EVNT;
13131 break;
13132 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13133 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13134 TG3_RX_PTP_CTL_DELAY_REQ;
13135 break;
13136 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13137 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13138 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13139 break;
13140 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13141 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13142 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13143 break;
13144 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13145 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13146 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13147 break;
13148 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13149 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13150 TG3_RX_PTP_CTL_SYNC_EVNT;
13151 break;
13152 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13153 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13154 TG3_RX_PTP_CTL_SYNC_EVNT;
13155 break;
13156 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13157 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13158 TG3_RX_PTP_CTL_SYNC_EVNT;
13159 break;
13160 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13161 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13162 TG3_RX_PTP_CTL_DELAY_REQ;
13163 break;
13164 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13165 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13166 TG3_RX_PTP_CTL_DELAY_REQ;
13167 break;
13168 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13169 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13170 TG3_RX_PTP_CTL_DELAY_REQ;
13171 break;
13172 default:
13173 return -ERANGE;
13174 }
13175
13176 if (netif_running(dev) && tp->rxptpctl)
13177 tw32(TG3_RX_PTP_CTL,
13178 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13179
13180 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13181 -EFAULT : 0;
13182}
13183
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13185{
13186 struct mii_ioctl_data *data = if_mii(ifr);
13187 struct tg3 *tp = netdev_priv(dev);
13188 int err;
13189
Joe Perches63c3a662011-04-26 08:12:10 +000013190 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013191 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013192 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013193 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013194 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013195 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013196 }
13197
Matt Carlson33f401a2010-04-05 10:19:27 +000013198 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013200 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013201
13202 /* fallthru */
13203 case SIOCGMIIREG: {
13204 u32 mii_regval;
13205
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013206 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013207 break; /* We have no PHY */
13208
Matt Carlson34eea5a2011-04-20 07:57:38 +000013209 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013210 return -EAGAIN;
13211
David S. Millerf47c11e2005-06-24 20:18:35 -070013212 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013213 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13214 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013215 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013216
13217 data->val_out = mii_regval;
13218
13219 return err;
13220 }
13221
13222 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013223 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013224 break; /* We have no PHY */
13225
Matt Carlson34eea5a2011-04-20 07:57:38 +000013226 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013227 return -EAGAIN;
13228
David S. Millerf47c11e2005-06-24 20:18:35 -070013229 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013230 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13231 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013232 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013233
13234 return err;
13235
Matt Carlson0a633ac2012-12-03 19:36:59 +000013236 case SIOCSHWTSTAMP:
13237 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13238
Linus Torvalds1da177e2005-04-16 15:20:36 -070013239 default:
13240 /* do nothing */
13241 break;
13242 }
13243 return -EOPNOTSUPP;
13244}
13245
David S. Miller15f98502005-05-18 22:49:26 -070013246static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13247{
13248 struct tg3 *tp = netdev_priv(dev);
13249
13250 memcpy(ec, &tp->coal, sizeof(*ec));
13251 return 0;
13252}
13253
Michael Chand244c892005-07-05 14:42:33 -070013254static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13255{
13256 struct tg3 *tp = netdev_priv(dev);
13257 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13258 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13259
Joe Perches63c3a662011-04-26 08:12:10 +000013260 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013261 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13262 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13263 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13264 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13265 }
13266
13267 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13268 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13269 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13270 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13271 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13272 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13273 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13274 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13275 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13276 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13277 return -EINVAL;
13278
13279 /* No rx interrupts will be generated if both are zero */
13280 if ((ec->rx_coalesce_usecs == 0) &&
13281 (ec->rx_max_coalesced_frames == 0))
13282 return -EINVAL;
13283
13284 /* No tx interrupts will be generated if both are zero */
13285 if ((ec->tx_coalesce_usecs == 0) &&
13286 (ec->tx_max_coalesced_frames == 0))
13287 return -EINVAL;
13288
13289 /* Only copy relevant parameters, ignore all others. */
13290 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13291 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13292 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13293 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13294 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13295 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13296 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13297 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13298 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13299
13300 if (netif_running(dev)) {
13301 tg3_full_lock(tp, 0);
13302 __tg3_set_coalesce(tp, &tp->coal);
13303 tg3_full_unlock(tp);
13304 }
13305 return 0;
13306}
13307
Jeff Garzik7282d492006-09-13 14:30:00 -040013308static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013309 .get_settings = tg3_get_settings,
13310 .set_settings = tg3_set_settings,
13311 .get_drvinfo = tg3_get_drvinfo,
13312 .get_regs_len = tg3_get_regs_len,
13313 .get_regs = tg3_get_regs,
13314 .get_wol = tg3_get_wol,
13315 .set_wol = tg3_set_wol,
13316 .get_msglevel = tg3_get_msglevel,
13317 .set_msglevel = tg3_set_msglevel,
13318 .nway_reset = tg3_nway_reset,
13319 .get_link = ethtool_op_get_link,
13320 .get_eeprom_len = tg3_get_eeprom_len,
13321 .get_eeprom = tg3_get_eeprom,
13322 .set_eeprom = tg3_set_eeprom,
13323 .get_ringparam = tg3_get_ringparam,
13324 .set_ringparam = tg3_set_ringparam,
13325 .get_pauseparam = tg3_get_pauseparam,
13326 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013327 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013328 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013329 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013330 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013331 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013332 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013333 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013334 .get_rxnfc = tg3_get_rxnfc,
13335 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13336 .get_rxfh_indir = tg3_get_rxfh_indir,
13337 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013338 .get_channels = tg3_get_channels,
13339 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013340 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013341};
13342
David S. Millerb4017c52012-03-01 17:57:40 -050013343static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13344 struct rtnl_link_stats64 *stats)
13345{
13346 struct tg3 *tp = netdev_priv(dev);
13347
David S. Millerb4017c52012-03-01 17:57:40 -050013348 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013349 if (!tp->hw_stats) {
13350 spin_unlock_bh(&tp->lock);
13351 return &tp->net_stats_prev;
13352 }
13353
David S. Millerb4017c52012-03-01 17:57:40 -050013354 tg3_get_nstats(tp, stats);
13355 spin_unlock_bh(&tp->lock);
13356
13357 return stats;
13358}
13359
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013360static void tg3_set_rx_mode(struct net_device *dev)
13361{
13362 struct tg3 *tp = netdev_priv(dev);
13363
13364 if (!netif_running(dev))
13365 return;
13366
13367 tg3_full_lock(tp, 0);
13368 __tg3_set_rx_mode(dev);
13369 tg3_full_unlock(tp);
13370}
13371
Matt Carlsonfaf16272012-02-13 10:20:07 +000013372static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13373 int new_mtu)
13374{
13375 dev->mtu = new_mtu;
13376
13377 if (new_mtu > ETH_DATA_LEN) {
13378 if (tg3_flag(tp, 5780_CLASS)) {
13379 netdev_update_features(dev);
13380 tg3_flag_clear(tp, TSO_CAPABLE);
13381 } else {
13382 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13383 }
13384 } else {
13385 if (tg3_flag(tp, 5780_CLASS)) {
13386 tg3_flag_set(tp, TSO_CAPABLE);
13387 netdev_update_features(dev);
13388 }
13389 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13390 }
13391}
13392
13393static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13394{
13395 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013396 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013397
13398 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13399 return -EINVAL;
13400
13401 if (!netif_running(dev)) {
13402 /* We'll just catch it later when the
13403 * device is up'd.
13404 */
13405 tg3_set_mtu(dev, tp, new_mtu);
13406 return 0;
13407 }
13408
13409 tg3_phy_stop(tp);
13410
13411 tg3_netif_stop(tp);
13412
13413 tg3_full_lock(tp, 1);
13414
13415 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13416
13417 tg3_set_mtu(dev, tp, new_mtu);
13418
Michael Chan2fae5e32012-03-04 14:48:15 +000013419 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13420 * breaks all requests to 256 bytes.
13421 */
Joe Perches41535772013-02-16 11:20:04 +000013422 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Michael Chan2fae5e32012-03-04 14:48:15 +000013423 reset_phy = 1;
13424
13425 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013426
13427 if (!err)
13428 tg3_netif_start(tp);
13429
13430 tg3_full_unlock(tp);
13431
13432 if (!err)
13433 tg3_phy_start(tp);
13434
13435 return err;
13436}
13437
13438static const struct net_device_ops tg3_netdev_ops = {
13439 .ndo_open = tg3_open,
13440 .ndo_stop = tg3_close,
13441 .ndo_start_xmit = tg3_start_xmit,
13442 .ndo_get_stats64 = tg3_get_stats64,
13443 .ndo_validate_addr = eth_validate_addr,
13444 .ndo_set_rx_mode = tg3_set_rx_mode,
13445 .ndo_set_mac_address = tg3_set_mac_addr,
13446 .ndo_do_ioctl = tg3_ioctl,
13447 .ndo_tx_timeout = tg3_tx_timeout,
13448 .ndo_change_mtu = tg3_change_mtu,
13449 .ndo_fix_features = tg3_fix_features,
13450 .ndo_set_features = tg3_set_features,
13451#ifdef CONFIG_NET_POLL_CONTROLLER
13452 .ndo_poll_controller = tg3_poll_controller,
13453#endif
13454};
13455
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013456static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013457{
Michael Chan1b277772006-03-20 22:27:48 -080013458 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013459
13460 tp->nvram_size = EEPROM_CHIP_SIZE;
13461
Matt Carlsone4f34112009-02-25 14:25:00 +000013462 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463 return;
13464
Michael Chanb16250e2006-09-27 16:10:14 -070013465 if ((magic != TG3_EEPROM_MAGIC) &&
13466 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13467 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468 return;
13469
13470 /*
13471 * Size the chip by reading offsets at increasing powers of two.
13472 * When we encounter our validation signature, we know the addressing
13473 * has wrapped around, and thus have our chip size.
13474 */
Michael Chan1b277772006-03-20 22:27:48 -080013475 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013476
13477 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013478 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013479 return;
13480
Michael Chan18201802006-03-20 22:29:15 -080013481 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482 break;
13483
13484 cursize <<= 1;
13485 }
13486
13487 tp->nvram_size = cursize;
13488}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013489
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013490static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013491{
13492 u32 val;
13493
Joe Perches63c3a662011-04-26 08:12:10 +000013494 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013495 return;
13496
13497 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013498 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013499 tg3_get_eeprom_size(tp);
13500 return;
13501 }
13502
Matt Carlson6d348f22009-02-25 14:25:52 +000013503 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013504 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013505 /* This is confusing. We want to operate on the
13506 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13507 * call will read from NVRAM and byteswap the data
13508 * according to the byteswapping settings for all
13509 * other register accesses. This ensures the data we
13510 * want will always reside in the lower 16-bits.
13511 * However, the data in NVRAM is in LE format, which
13512 * means the data from the NVRAM read will always be
13513 * opposite the endianness of the CPU. The 16-bit
13514 * byteswap then brings the data to CPU endianness.
13515 */
13516 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517 return;
13518 }
13519 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013520 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013521}
13522
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013523static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013524{
13525 u32 nvcfg1;
13526
13527 nvcfg1 = tr32(NVRAM_CFG1);
13528 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013529 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013531 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13532 tw32(NVRAM_CFG1, nvcfg1);
13533 }
13534
Joe Perches41535772013-02-16 11:20:04 +000013535 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013536 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013538 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13539 tp->nvram_jedecnum = JEDEC_ATMEL;
13540 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013541 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013542 break;
13543 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13544 tp->nvram_jedecnum = JEDEC_ATMEL;
13545 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13546 break;
13547 case FLASH_VENDOR_ATMEL_EEPROM:
13548 tp->nvram_jedecnum = JEDEC_ATMEL;
13549 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013550 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013551 break;
13552 case FLASH_VENDOR_ST:
13553 tp->nvram_jedecnum = JEDEC_ST;
13554 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013555 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013556 break;
13557 case FLASH_VENDOR_SAIFUN:
13558 tp->nvram_jedecnum = JEDEC_SAIFUN;
13559 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13560 break;
13561 case FLASH_VENDOR_SST_SMALL:
13562 case FLASH_VENDOR_SST_LARGE:
13563 tp->nvram_jedecnum = JEDEC_SST;
13564 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013566 }
Matt Carlson8590a602009-08-28 12:29:16 +000013567 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013568 tp->nvram_jedecnum = JEDEC_ATMEL;
13569 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013570 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013571 }
13572}
13573
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013574static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013575{
13576 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13577 case FLASH_5752PAGE_SIZE_256:
13578 tp->nvram_pagesize = 256;
13579 break;
13580 case FLASH_5752PAGE_SIZE_512:
13581 tp->nvram_pagesize = 512;
13582 break;
13583 case FLASH_5752PAGE_SIZE_1K:
13584 tp->nvram_pagesize = 1024;
13585 break;
13586 case FLASH_5752PAGE_SIZE_2K:
13587 tp->nvram_pagesize = 2048;
13588 break;
13589 case FLASH_5752PAGE_SIZE_4K:
13590 tp->nvram_pagesize = 4096;
13591 break;
13592 case FLASH_5752PAGE_SIZE_264:
13593 tp->nvram_pagesize = 264;
13594 break;
13595 case FLASH_5752PAGE_SIZE_528:
13596 tp->nvram_pagesize = 528;
13597 break;
13598 }
13599}
13600
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013601static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013602{
13603 u32 nvcfg1;
13604
13605 nvcfg1 = tr32(NVRAM_CFG1);
13606
Michael Chane6af3012005-04-21 17:12:05 -070013607 /* NVRAM protection for TPM */
13608 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013609 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013610
Michael Chan361b4ac2005-04-21 17:11:21 -070013611 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013612 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13613 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13614 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013615 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013616 break;
13617 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13618 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013619 tg3_flag_set(tp, NVRAM_BUFFERED);
13620 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013621 break;
13622 case FLASH_5752VENDOR_ST_M45PE10:
13623 case FLASH_5752VENDOR_ST_M45PE20:
13624 case FLASH_5752VENDOR_ST_M45PE40:
13625 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013626 tg3_flag_set(tp, NVRAM_BUFFERED);
13627 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013628 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013629 }
13630
Joe Perches63c3a662011-04-26 08:12:10 +000013631 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013632 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013633 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013634 /* For eeprom, set pagesize to maximum eeprom size */
13635 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13636
13637 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13638 tw32(NVRAM_CFG1, nvcfg1);
13639 }
13640}
13641
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013642static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013643{
Matt Carlson989a9d22007-05-05 11:51:05 -070013644 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013645
13646 nvcfg1 = tr32(NVRAM_CFG1);
13647
13648 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013649 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013650 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013651 protect = 1;
13652 }
Michael Chand3c7b882006-03-23 01:28:25 -080013653
Matt Carlson989a9d22007-05-05 11:51:05 -070013654 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13655 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013656 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13657 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13658 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13659 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13660 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013661 tg3_flag_set(tp, NVRAM_BUFFERED);
13662 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013663 tp->nvram_pagesize = 264;
13664 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13665 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13666 tp->nvram_size = (protect ? 0x3e200 :
13667 TG3_NVRAM_SIZE_512KB);
13668 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13669 tp->nvram_size = (protect ? 0x1f200 :
13670 TG3_NVRAM_SIZE_256KB);
13671 else
13672 tp->nvram_size = (protect ? 0x1f200 :
13673 TG3_NVRAM_SIZE_128KB);
13674 break;
13675 case FLASH_5752VENDOR_ST_M45PE10:
13676 case FLASH_5752VENDOR_ST_M45PE20:
13677 case FLASH_5752VENDOR_ST_M45PE40:
13678 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013679 tg3_flag_set(tp, NVRAM_BUFFERED);
13680 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013681 tp->nvram_pagesize = 256;
13682 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13683 tp->nvram_size = (protect ?
13684 TG3_NVRAM_SIZE_64KB :
13685 TG3_NVRAM_SIZE_128KB);
13686 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13687 tp->nvram_size = (protect ?
13688 TG3_NVRAM_SIZE_64KB :
13689 TG3_NVRAM_SIZE_256KB);
13690 else
13691 tp->nvram_size = (protect ?
13692 TG3_NVRAM_SIZE_128KB :
13693 TG3_NVRAM_SIZE_512KB);
13694 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013695 }
13696}
13697
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013698static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013699{
13700 u32 nvcfg1;
13701
13702 nvcfg1 = tr32(NVRAM_CFG1);
13703
13704 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013705 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13706 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13707 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13708 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13709 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013710 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013711 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013712
Matt Carlson8590a602009-08-28 12:29:16 +000013713 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13714 tw32(NVRAM_CFG1, nvcfg1);
13715 break;
13716 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13717 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13718 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13719 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13720 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013721 tg3_flag_set(tp, NVRAM_BUFFERED);
13722 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013723 tp->nvram_pagesize = 264;
13724 break;
13725 case FLASH_5752VENDOR_ST_M45PE10:
13726 case FLASH_5752VENDOR_ST_M45PE20:
13727 case FLASH_5752VENDOR_ST_M45PE40:
13728 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013729 tg3_flag_set(tp, NVRAM_BUFFERED);
13730 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013731 tp->nvram_pagesize = 256;
13732 break;
Michael Chan1b277772006-03-20 22:27:48 -080013733 }
13734}
13735
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013736static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013737{
13738 u32 nvcfg1, protect = 0;
13739
13740 nvcfg1 = tr32(NVRAM_CFG1);
13741
13742 /* NVRAM protection for TPM */
13743 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013744 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013745 protect = 1;
13746 }
13747
13748 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13749 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013750 case FLASH_5761VENDOR_ATMEL_ADB021D:
13751 case FLASH_5761VENDOR_ATMEL_ADB041D:
13752 case FLASH_5761VENDOR_ATMEL_ADB081D:
13753 case FLASH_5761VENDOR_ATMEL_ADB161D:
13754 case FLASH_5761VENDOR_ATMEL_MDB021D:
13755 case FLASH_5761VENDOR_ATMEL_MDB041D:
13756 case FLASH_5761VENDOR_ATMEL_MDB081D:
13757 case FLASH_5761VENDOR_ATMEL_MDB161D:
13758 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013759 tg3_flag_set(tp, NVRAM_BUFFERED);
13760 tg3_flag_set(tp, FLASH);
13761 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013762 tp->nvram_pagesize = 256;
13763 break;
13764 case FLASH_5761VENDOR_ST_A_M45PE20:
13765 case FLASH_5761VENDOR_ST_A_M45PE40:
13766 case FLASH_5761VENDOR_ST_A_M45PE80:
13767 case FLASH_5761VENDOR_ST_A_M45PE16:
13768 case FLASH_5761VENDOR_ST_M_M45PE20:
13769 case FLASH_5761VENDOR_ST_M_M45PE40:
13770 case FLASH_5761VENDOR_ST_M_M45PE80:
13771 case FLASH_5761VENDOR_ST_M_M45PE16:
13772 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013773 tg3_flag_set(tp, NVRAM_BUFFERED);
13774 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013775 tp->nvram_pagesize = 256;
13776 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013777 }
13778
13779 if (protect) {
13780 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13781 } else {
13782 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013783 case FLASH_5761VENDOR_ATMEL_ADB161D:
13784 case FLASH_5761VENDOR_ATMEL_MDB161D:
13785 case FLASH_5761VENDOR_ST_A_M45PE16:
13786 case FLASH_5761VENDOR_ST_M_M45PE16:
13787 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13788 break;
13789 case FLASH_5761VENDOR_ATMEL_ADB081D:
13790 case FLASH_5761VENDOR_ATMEL_MDB081D:
13791 case FLASH_5761VENDOR_ST_A_M45PE80:
13792 case FLASH_5761VENDOR_ST_M_M45PE80:
13793 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13794 break;
13795 case FLASH_5761VENDOR_ATMEL_ADB041D:
13796 case FLASH_5761VENDOR_ATMEL_MDB041D:
13797 case FLASH_5761VENDOR_ST_A_M45PE40:
13798 case FLASH_5761VENDOR_ST_M_M45PE40:
13799 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13800 break;
13801 case FLASH_5761VENDOR_ATMEL_ADB021D:
13802 case FLASH_5761VENDOR_ATMEL_MDB021D:
13803 case FLASH_5761VENDOR_ST_A_M45PE20:
13804 case FLASH_5761VENDOR_ST_M_M45PE20:
13805 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13806 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013807 }
13808 }
13809}
13810
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013811static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013812{
13813 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013814 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013815 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13816}
13817
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013818static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013819{
13820 u32 nvcfg1;
13821
13822 nvcfg1 = tr32(NVRAM_CFG1);
13823
13824 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13825 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13826 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13827 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013828 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013829 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13830
13831 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13832 tw32(NVRAM_CFG1, nvcfg1);
13833 return;
13834 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13835 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13836 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13837 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13838 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13839 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13840 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13841 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013842 tg3_flag_set(tp, NVRAM_BUFFERED);
13843 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013844
13845 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13846 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13847 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13848 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13849 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13850 break;
13851 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13852 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13853 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13854 break;
13855 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13856 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13857 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13858 break;
13859 }
13860 break;
13861 case FLASH_5752VENDOR_ST_M45PE10:
13862 case FLASH_5752VENDOR_ST_M45PE20:
13863 case FLASH_5752VENDOR_ST_M45PE40:
13864 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013865 tg3_flag_set(tp, NVRAM_BUFFERED);
13866 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013867
13868 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13869 case FLASH_5752VENDOR_ST_M45PE10:
13870 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13871 break;
13872 case FLASH_5752VENDOR_ST_M45PE20:
13873 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13874 break;
13875 case FLASH_5752VENDOR_ST_M45PE40:
13876 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13877 break;
13878 }
13879 break;
13880 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013881 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013882 return;
13883 }
13884
Matt Carlsona1b950d2009-09-01 13:20:17 +000013885 tg3_nvram_get_pagesize(tp, nvcfg1);
13886 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013887 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013888}
13889
13890
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013891static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013892{
13893 u32 nvcfg1;
13894
13895 nvcfg1 = tr32(NVRAM_CFG1);
13896
13897 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13898 case FLASH_5717VENDOR_ATMEL_EEPROM:
13899 case FLASH_5717VENDOR_MICRO_EEPROM:
13900 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013901 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013902 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13903
13904 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13905 tw32(NVRAM_CFG1, nvcfg1);
13906 return;
13907 case FLASH_5717VENDOR_ATMEL_MDB011D:
13908 case FLASH_5717VENDOR_ATMEL_ADB011B:
13909 case FLASH_5717VENDOR_ATMEL_ADB011D:
13910 case FLASH_5717VENDOR_ATMEL_MDB021D:
13911 case FLASH_5717VENDOR_ATMEL_ADB021B:
13912 case FLASH_5717VENDOR_ATMEL_ADB021D:
13913 case FLASH_5717VENDOR_ATMEL_45USPT:
13914 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013915 tg3_flag_set(tp, NVRAM_BUFFERED);
13916 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013917
13918 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13919 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013920 /* Detect size with tg3_nvram_get_size() */
13921 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013922 case FLASH_5717VENDOR_ATMEL_ADB021B:
13923 case FLASH_5717VENDOR_ATMEL_ADB021D:
13924 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13925 break;
13926 default:
13927 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13928 break;
13929 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013930 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013931 case FLASH_5717VENDOR_ST_M_M25PE10:
13932 case FLASH_5717VENDOR_ST_A_M25PE10:
13933 case FLASH_5717VENDOR_ST_M_M45PE10:
13934 case FLASH_5717VENDOR_ST_A_M45PE10:
13935 case FLASH_5717VENDOR_ST_M_M25PE20:
13936 case FLASH_5717VENDOR_ST_A_M25PE20:
13937 case FLASH_5717VENDOR_ST_M_M45PE20:
13938 case FLASH_5717VENDOR_ST_A_M45PE20:
13939 case FLASH_5717VENDOR_ST_25USPT:
13940 case FLASH_5717VENDOR_ST_45USPT:
13941 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013942 tg3_flag_set(tp, NVRAM_BUFFERED);
13943 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013944
13945 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13946 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013947 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013948 /* Detect size with tg3_nvram_get_size() */
13949 break;
13950 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013951 case FLASH_5717VENDOR_ST_A_M45PE20:
13952 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13953 break;
13954 default:
13955 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13956 break;
13957 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013958 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013959 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013960 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013961 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013962 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013963
13964 tg3_nvram_get_pagesize(tp, nvcfg1);
13965 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013966 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013967}
13968
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013969static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013970{
13971 u32 nvcfg1, nvmpinstrp;
13972
13973 nvcfg1 = tr32(NVRAM_CFG1);
13974 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13975
Joe Perches41535772013-02-16 11:20:04 +000013976 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013977 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
13978 tg3_flag_set(tp, NO_NVRAM);
13979 return;
13980 }
13981
13982 switch (nvmpinstrp) {
13983 case FLASH_5762_EEPROM_HD:
13984 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013985 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013986 case FLASH_5762_EEPROM_LD:
13987 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013988 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013989 }
13990 }
13991
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013992 switch (nvmpinstrp) {
13993 case FLASH_5720_EEPROM_HD:
13994 case FLASH_5720_EEPROM_LD:
13995 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013996 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013997
13998 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13999 tw32(NVRAM_CFG1, nvcfg1);
14000 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14001 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14002 else
14003 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14004 return;
14005 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14006 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14007 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14008 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14009 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14010 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14011 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14012 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14013 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14014 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14015 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14016 case FLASH_5720VENDOR_ATMEL_45USPT:
14017 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014018 tg3_flag_set(tp, NVRAM_BUFFERED);
14019 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014020
14021 switch (nvmpinstrp) {
14022 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14023 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14024 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14025 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14026 break;
14027 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14028 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14029 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14030 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14031 break;
14032 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14033 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14034 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14035 break;
14036 default:
Joe Perches41535772013-02-16 11:20:04 +000014037 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014038 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014039 break;
14040 }
14041 break;
14042 case FLASH_5720VENDOR_M_ST_M25PE10:
14043 case FLASH_5720VENDOR_M_ST_M45PE10:
14044 case FLASH_5720VENDOR_A_ST_M25PE10:
14045 case FLASH_5720VENDOR_A_ST_M45PE10:
14046 case FLASH_5720VENDOR_M_ST_M25PE20:
14047 case FLASH_5720VENDOR_M_ST_M45PE20:
14048 case FLASH_5720VENDOR_A_ST_M25PE20:
14049 case FLASH_5720VENDOR_A_ST_M45PE20:
14050 case FLASH_5720VENDOR_M_ST_M25PE40:
14051 case FLASH_5720VENDOR_M_ST_M45PE40:
14052 case FLASH_5720VENDOR_A_ST_M25PE40:
14053 case FLASH_5720VENDOR_A_ST_M45PE40:
14054 case FLASH_5720VENDOR_M_ST_M25PE80:
14055 case FLASH_5720VENDOR_M_ST_M45PE80:
14056 case FLASH_5720VENDOR_A_ST_M25PE80:
14057 case FLASH_5720VENDOR_A_ST_M45PE80:
14058 case FLASH_5720VENDOR_ST_25USPT:
14059 case FLASH_5720VENDOR_ST_45USPT:
14060 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014061 tg3_flag_set(tp, NVRAM_BUFFERED);
14062 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014063
14064 switch (nvmpinstrp) {
14065 case FLASH_5720VENDOR_M_ST_M25PE20:
14066 case FLASH_5720VENDOR_M_ST_M45PE20:
14067 case FLASH_5720VENDOR_A_ST_M25PE20:
14068 case FLASH_5720VENDOR_A_ST_M45PE20:
14069 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14070 break;
14071 case FLASH_5720VENDOR_M_ST_M25PE40:
14072 case FLASH_5720VENDOR_M_ST_M45PE40:
14073 case FLASH_5720VENDOR_A_ST_M25PE40:
14074 case FLASH_5720VENDOR_A_ST_M45PE40:
14075 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14076 break;
14077 case FLASH_5720VENDOR_M_ST_M25PE80:
14078 case FLASH_5720VENDOR_M_ST_M45PE80:
14079 case FLASH_5720VENDOR_A_ST_M25PE80:
14080 case FLASH_5720VENDOR_A_ST_M45PE80:
14081 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14082 break;
14083 default:
Joe Perches41535772013-02-16 11:20:04 +000014084 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014085 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014086 break;
14087 }
14088 break;
14089 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014090 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014091 return;
14092 }
14093
14094 tg3_nvram_get_pagesize(tp, nvcfg1);
14095 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014096 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014097
Joe Perches41535772013-02-16 11:20:04 +000014098 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014099 u32 val;
14100
14101 if (tg3_nvram_read(tp, 0, &val))
14102 return;
14103
14104 if (val != TG3_EEPROM_MAGIC &&
14105 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14106 tg3_flag_set(tp, NO_NVRAM);
14107 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014108}
14109
Linus Torvalds1da177e2005-04-16 15:20:36 -070014110/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014111static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014113 if (tg3_flag(tp, IS_SSB_CORE)) {
14114 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14115 tg3_flag_clear(tp, NVRAM);
14116 tg3_flag_clear(tp, NVRAM_BUFFERED);
14117 tg3_flag_set(tp, NO_NVRAM);
14118 return;
14119 }
14120
Linus Torvalds1da177e2005-04-16 15:20:36 -070014121 tw32_f(GRC_EEPROM_ADDR,
14122 (EEPROM_ADDR_FSM_RESET |
14123 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14124 EEPROM_ADDR_CLKPERD_SHIFT)));
14125
Michael Chan9d57f012006-12-07 00:23:25 -080014126 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014127
14128 /* Enable seeprom accesses. */
14129 tw32_f(GRC_LOCAL_CTRL,
14130 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14131 udelay(100);
14132
Joe Perches41535772013-02-16 11:20:04 +000014133 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14134 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014135 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014136
Michael Chanec41c7d2006-01-17 02:40:55 -080014137 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014138 netdev_warn(tp->dev,
14139 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014140 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014141 return;
14142 }
Michael Chane6af3012005-04-21 17:12:05 -070014143 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144
Matt Carlson989a9d22007-05-05 11:51:05 -070014145 tp->nvram_size = 0;
14146
Joe Perches41535772013-02-16 11:20:04 +000014147 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014148 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014149 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014150 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014151 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14152 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14153 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014154 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014155 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014156 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014157 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014158 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014159 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014160 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014161 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014162 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14163 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014164 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014165 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14166 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014167 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014168 else
14169 tg3_get_nvram_info(tp);
14170
Matt Carlson989a9d22007-05-05 11:51:05 -070014171 if (tp->nvram_size == 0)
14172 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173
Michael Chane6af3012005-04-21 17:12:05 -070014174 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014175 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014176
14177 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014178 tg3_flag_clear(tp, NVRAM);
14179 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180
14181 tg3_get_eeprom_size(tp);
14182 }
14183}
14184
Linus Torvalds1da177e2005-04-16 15:20:36 -070014185struct subsys_tbl_ent {
14186 u16 subsys_vendor, subsys_devid;
14187 u32 phy_id;
14188};
14189
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014190static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014191 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014192 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014193 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014194 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014195 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014196 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014197 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014198 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14199 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14200 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014201 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014202 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014203 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014204 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14205 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14206 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014207 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014208 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014209 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014210 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014211 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014212 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014213 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014214
14215 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014216 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014217 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014218 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014219 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014220 { TG3PCI_SUBVENDOR_ID_3COM,
14221 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14222 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014223 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014224 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014225 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014226
14227 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014228 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014229 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014230 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014231 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014232 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014233 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014234 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014235 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236
14237 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014238 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014239 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014240 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014241 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014242 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14243 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14244 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014245 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014246 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014247 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248
14249 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014250 { TG3PCI_SUBVENDOR_ID_IBM,
14251 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014252};
14253
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014254static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255{
14256 int i;
14257
14258 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14259 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14260 tp->pdev->subsystem_vendor) &&
14261 (subsys_id_to_phy_id[i].subsys_devid ==
14262 tp->pdev->subsystem_device))
14263 return &subsys_id_to_phy_id[i];
14264 }
14265 return NULL;
14266}
14267
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014268static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014269{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014271
Matt Carlson79eb6902010-02-17 15:17:03 +000014272 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014273 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14274
Gary Zambranoa85feb82007-05-05 11:52:19 -070014275 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014276 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14277 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014278
Joe Perches41535772013-02-16 11:20:04 +000014279 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014280 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014281 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14282 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014283 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014284 val = tr32(VCPU_CFGSHDW);
14285 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014286 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014287 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014288 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014289 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014290 device_set_wakeup_enable(&tp->pdev->dev, true);
14291 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014292 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014293 }
14294
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14296 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14297 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014298 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014299 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300
14301 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14302 tp->nic_sram_data_cfg = nic_cfg;
14303
14304 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14305 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014306 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14307 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14308 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309 (ver > 0) && (ver < 0x100))
14310 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14311
Joe Perches41535772013-02-16 11:20:04 +000014312 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014313 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14314
Linus Torvalds1da177e2005-04-16 15:20:36 -070014315 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14316 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14317 eeprom_phy_serdes = 1;
14318
14319 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14320 if (nic_phy_id != 0) {
14321 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14322 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14323
14324 eeprom_phy_id = (id1 >> 16) << 10;
14325 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14326 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14327 } else
14328 eeprom_phy_id = 0;
14329
Michael Chan7d0c41e2005-04-21 17:06:20 -070014330 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014331 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014332 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014333 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014334 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014335 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014336 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014337
Joe Perches63c3a662011-04-26 08:12:10 +000014338 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14340 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014341 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14343
14344 switch (led_cfg) {
14345 default:
14346 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14347 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14348 break;
14349
14350 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14351 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14352 break;
14353
14354 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14355 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014356
14357 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14358 * read on some older 5700/5701 bootcode.
14359 */
Joe Perches41535772013-02-16 11:20:04 +000014360 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14361 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014362 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14363
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 break;
14365
14366 case SHASTA_EXT_LED_SHARED:
14367 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014368 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14369 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14371 LED_CTRL_MODE_PHY_2);
14372 break;
14373
14374 case SHASTA_EXT_LED_MAC:
14375 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14376 break;
14377
14378 case SHASTA_EXT_LED_COMBO:
14379 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014380 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014381 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14382 LED_CTRL_MODE_PHY_2);
14383 break;
14384
Stephen Hemminger855e1112008-04-16 16:37:28 -070014385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014386
Joe Perches41535772013-02-16 11:20:04 +000014387 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14388 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14390 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14391
Joe Perches41535772013-02-16 11:20:04 +000014392 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014393 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014394
Michael Chan9d26e212006-12-07 00:21:14 -080014395 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014396 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014397 if ((tp->pdev->subsystem_vendor ==
14398 PCI_VENDOR_ID_ARIMA) &&
14399 (tp->pdev->subsystem_device == 0x205a ||
14400 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014401 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014402 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014403 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14404 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406
14407 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014408 tg3_flag_set(tp, ENABLE_ASF);
14409 if (tg3_flag(tp, 5750_PLUS))
14410 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014412
14413 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014414 tg3_flag(tp, 5750_PLUS))
14415 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014416
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014417 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014418 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014419 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420
Joe Perches63c3a662011-04-26 08:12:10 +000014421 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014422 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014423 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014424 device_set_wakeup_enable(&tp->pdev->dev, true);
14425 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014426
Linus Torvalds1da177e2005-04-16 15:20:36 -070014427 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014428 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014429
14430 /* serdes signal pre-emphasis in register 0x590 set by */
14431 /* bootcode if bit 18 is set */
14432 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014433 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014434
Joe Perches63c3a662011-04-26 08:12:10 +000014435 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014436 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14437 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014438 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014439 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014440
Joe Perches63c3a662011-04-26 08:12:10 +000014441 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000014442 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014443 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014444 u32 cfg3;
14445
14446 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14447 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014448 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014449 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014450
Matt Carlson14417062010-02-17 15:16:59 +000014451 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014452 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014453 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014454 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014455 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014456 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014457 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014458done:
Joe Perches63c3a662011-04-26 08:12:10 +000014459 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014460 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014461 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014462 else
14463 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014464}
14465
Michael Chanc86a8562013-01-06 12:51:08 +000014466static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14467{
14468 int i, err;
14469 u32 val2, off = offset * 8;
14470
14471 err = tg3_nvram_lock(tp);
14472 if (err)
14473 return err;
14474
14475 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14476 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14477 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14478 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14479 udelay(10);
14480
14481 for (i = 0; i < 100; i++) {
14482 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14483 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14484 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14485 break;
14486 }
14487 udelay(10);
14488 }
14489
14490 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14491
14492 tg3_nvram_unlock(tp);
14493 if (val2 & APE_OTP_STATUS_CMD_DONE)
14494 return 0;
14495
14496 return -EBUSY;
14497}
14498
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014499static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014500{
14501 int i;
14502 u32 val;
14503
14504 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14505 tw32(OTP_CTRL, cmd);
14506
14507 /* Wait for up to 1 ms for command to execute. */
14508 for (i = 0; i < 100; i++) {
14509 val = tr32(OTP_STATUS);
14510 if (val & OTP_STATUS_CMD_DONE)
14511 break;
14512 udelay(10);
14513 }
14514
14515 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14516}
14517
14518/* Read the gphy configuration from the OTP region of the chip. The gphy
14519 * configuration is a 32-bit value that straddles the alignment boundary.
14520 * We do two 32-bit reads and then shift and merge the results.
14521 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014522static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014523{
14524 u32 bhalf_otp, thalf_otp;
14525
14526 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14527
14528 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14529 return 0;
14530
14531 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14532
14533 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14534 return 0;
14535
14536 thalf_otp = tr32(OTP_READ_DATA);
14537
14538 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14539
14540 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14541 return 0;
14542
14543 bhalf_otp = tr32(OTP_READ_DATA);
14544
14545 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14546}
14547
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014548static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014549{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014550 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014551
14552 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14553 adv |= ADVERTISED_1000baseT_Half |
14554 ADVERTISED_1000baseT_Full;
14555
14556 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14557 adv |= ADVERTISED_100baseT_Half |
14558 ADVERTISED_100baseT_Full |
14559 ADVERTISED_10baseT_Half |
14560 ADVERTISED_10baseT_Full |
14561 ADVERTISED_TP;
14562 else
14563 adv |= ADVERTISED_FIBRE;
14564
14565 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014566 tp->link_config.speed = SPEED_UNKNOWN;
14567 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014568 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014569 tp->link_config.active_speed = SPEED_UNKNOWN;
14570 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014571
14572 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014573}
14574
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014575static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014576{
14577 u32 hw_phy_id_1, hw_phy_id_2;
14578 u32 hw_phy_id, hw_phy_id_masked;
14579 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014580
Matt Carlsone256f8a2011-03-09 16:58:24 +000014581 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014582 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014583 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14584
Michael Chan8151ad52012-07-29 19:15:41 +000014585 if (tg3_flag(tp, ENABLE_APE)) {
14586 switch (tp->pci_fn) {
14587 case 0:
14588 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14589 break;
14590 case 1:
14591 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14592 break;
14593 case 2:
14594 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14595 break;
14596 case 3:
14597 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14598 break;
14599 }
14600 }
14601
Joe Perches63c3a662011-04-26 08:12:10 +000014602 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014603 return tg3_phy_init(tp);
14604
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014606 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014607 */
14608 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014609 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014610 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 } else {
14612 /* Now read the physical PHY_ID from the chip and verify
14613 * that it is sane. If it doesn't look good, we fall back
14614 * to either the hard-coded table based PHY_ID and failing
14615 * that the value found in the eeprom area.
14616 */
14617 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14618 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14619
14620 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14621 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14622 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14623
Matt Carlson79eb6902010-02-17 15:17:03 +000014624 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625 }
14626
Matt Carlson79eb6902010-02-17 15:17:03 +000014627 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014629 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014630 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014631 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014632 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014633 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014634 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014635 /* Do nothing, phy ID already set up in
14636 * tg3_get_eeprom_hw_cfg().
14637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014638 } else {
14639 struct subsys_tbl_ent *p;
14640
14641 /* No eeprom signature? Try the hardcoded
14642 * subsys device table.
14643 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014644 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014645 if (p) {
14646 tp->phy_id = p->phy_id;
14647 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14648 /* For now we saw the IDs 0xbc050cd0,
14649 * 0xbc050f80 and 0xbc050c30 on devices
14650 * connected to an BCM4785 and there are
14651 * probably more. Just assume that the phy is
14652 * supported when it is connected to a SSB core
14653 * for now.
14654 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014657
Linus Torvalds1da177e2005-04-16 15:20:36 -070014658 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014659 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014660 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 }
14662 }
14663
Matt Carlsona6b68da2010-12-06 08:28:52 +000014664 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014665 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14666 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000014667 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000014668 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14669 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14670 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14671 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
14672 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014673 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14674
Matt Carlsone256f8a2011-03-09 16:58:24 +000014675 tg3_phy_init_link_config(tp);
14676
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014677 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014678 !tg3_flag(tp, ENABLE_APE) &&
14679 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014680 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681
14682 tg3_readphy(tp, MII_BMSR, &bmsr);
14683 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14684 (bmsr & BMSR_LSTATUS))
14685 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014686
Linus Torvalds1da177e2005-04-16 15:20:36 -070014687 err = tg3_phy_reset(tp);
14688 if (err)
14689 return err;
14690
Matt Carlson42b64a42011-05-19 12:12:49 +000014691 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014693 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014694 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14695 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696
14697 tg3_writephy(tp, MII_BMCR,
14698 BMCR_ANENABLE | BMCR_ANRESTART);
14699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014700 }
14701
14702skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014703 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704 err = tg3_init_5401phy_dsp(tp);
14705 if (err)
14706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014707
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 err = tg3_init_5401phy_dsp(tp);
14709 }
14710
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711 return err;
14712}
14713
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014714static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014715{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014716 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014717 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014718 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014719 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720
Matt Carlson535a4902011-07-20 10:20:56 +000014721 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014722 if (!vpd_data)
14723 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724
Matt Carlson535a4902011-07-20 10:20:56 +000014725 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014726 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014727 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014728
14729 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14730 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14731 i += PCI_VPD_LRDT_TAG_SIZE;
14732
Matt Carlson535a4902011-07-20 10:20:56 +000014733 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014734 goto out_not_found;
14735
Matt Carlson184b8902010-04-05 10:19:25 +000014736 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14737 PCI_VPD_RO_KEYWORD_MFR_ID);
14738 if (j > 0) {
14739 len = pci_vpd_info_field_size(&vpd_data[j]);
14740
14741 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14742 if (j + len > block_end || len != 4 ||
14743 memcmp(&vpd_data[j], "1028", 4))
14744 goto partno;
14745
14746 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14747 PCI_VPD_RO_KEYWORD_VENDOR0);
14748 if (j < 0)
14749 goto partno;
14750
14751 len = pci_vpd_info_field_size(&vpd_data[j]);
14752
14753 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14754 if (j + len > block_end)
14755 goto partno;
14756
Kees Cook715230a2013-03-27 06:40:50 +000014757 if (len >= sizeof(tp->fw_ver))
14758 len = sizeof(tp->fw_ver) - 1;
14759 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
14760 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
14761 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000014762 }
14763
14764partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014765 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14766 PCI_VPD_RO_KEYWORD_PARTNO);
14767 if (i < 0)
14768 goto out_not_found;
14769
14770 len = pci_vpd_info_field_size(&vpd_data[i]);
14771
14772 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14773 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014774 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014775 goto out_not_found;
14776
14777 memcpy(tp->board_part_number, &vpd_data[i], len);
14778
Linus Torvalds1da177e2005-04-16 15:20:36 -070014779out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014780 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014781 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014782 return;
14783
14784out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000014785 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014786 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014788 strcpy(tp->board_part_number, "BCM5717");
14789 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14790 strcpy(tp->board_part_number, "BCM5718");
14791 else
14792 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014793 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014794 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14795 strcpy(tp->board_part_number, "BCM57780");
14796 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14797 strcpy(tp->board_part_number, "BCM57760");
14798 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14799 strcpy(tp->board_part_number, "BCM57790");
14800 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14801 strcpy(tp->board_part_number, "BCM57788");
14802 else
14803 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014804 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014805 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14806 strcpy(tp->board_part_number, "BCM57761");
14807 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14808 strcpy(tp->board_part_number, "BCM57765");
14809 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14810 strcpy(tp->board_part_number, "BCM57781");
14811 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14812 strcpy(tp->board_part_number, "BCM57785");
14813 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14814 strcpy(tp->board_part_number, "BCM57791");
14815 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14816 strcpy(tp->board_part_number, "BCM57795");
14817 else
14818 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014819 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000014820 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14821 strcpy(tp->board_part_number, "BCM57762");
14822 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14823 strcpy(tp->board_part_number, "BCM57766");
14824 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14825 strcpy(tp->board_part_number, "BCM57782");
14826 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14827 strcpy(tp->board_part_number, "BCM57786");
14828 else
14829 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014830 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014831 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014832 } else {
14833nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014834 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836}
14837
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014838static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014839{
14840 u32 val;
14841
Matt Carlsone4f34112009-02-25 14:25:00 +000014842 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014843 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014844 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014845 val != 0)
14846 return 0;
14847
14848 return 1;
14849}
14850
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014851static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014852{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014853 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014854 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014855 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014856
14857 if (tg3_nvram_read(tp, 0xc, &offset) ||
14858 tg3_nvram_read(tp, 0x4, &start))
14859 return;
14860
14861 offset = tg3_nvram_logical_addr(tp, offset);
14862
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014863 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014864 return;
14865
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014866 if ((val & 0xfc000000) == 0x0c000000) {
14867 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014868 return;
14869
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014870 if (val == 0)
14871 newver = true;
14872 }
14873
Matt Carlson75f99362010-04-05 10:19:24 +000014874 dst_off = strlen(tp->fw_ver);
14875
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014876 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014877 if (TG3_VER_SIZE - dst_off < 16 ||
14878 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014879 return;
14880
14881 offset = offset + ver_offset - start;
14882 for (i = 0; i < 16; i += 4) {
14883 __be32 v;
14884 if (tg3_nvram_read_be32(tp, offset + i, &v))
14885 return;
14886
Matt Carlson75f99362010-04-05 10:19:24 +000014887 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014888 }
14889 } else {
14890 u32 major, minor;
14891
14892 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14893 return;
14894
14895 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14896 TG3_NVM_BCVER_MAJSFT;
14897 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014898 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14899 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014900 }
14901}
14902
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014903static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014904{
14905 u32 val, major, minor;
14906
14907 /* Use native endian representation */
14908 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14909 return;
14910
14911 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14912 TG3_NVM_HWSB_CFG1_MAJSFT;
14913 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14914 TG3_NVM_HWSB_CFG1_MINSFT;
14915
14916 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14917}
14918
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014919static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014920{
14921 u32 offset, major, minor, build;
14922
Matt Carlson75f99362010-04-05 10:19:24 +000014923 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014924
14925 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14926 return;
14927
14928 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14929 case TG3_EEPROM_SB_REVISION_0:
14930 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14931 break;
14932 case TG3_EEPROM_SB_REVISION_2:
14933 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14934 break;
14935 case TG3_EEPROM_SB_REVISION_3:
14936 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14937 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014938 case TG3_EEPROM_SB_REVISION_4:
14939 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14940 break;
14941 case TG3_EEPROM_SB_REVISION_5:
14942 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14943 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014944 case TG3_EEPROM_SB_REVISION_6:
14945 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14946 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014947 default:
14948 return;
14949 }
14950
Matt Carlsone4f34112009-02-25 14:25:00 +000014951 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014952 return;
14953
14954 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14955 TG3_EEPROM_SB_EDH_BLD_SHFT;
14956 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14957 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14958 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14959
14960 if (minor > 99 || build > 26)
14961 return;
14962
Matt Carlson75f99362010-04-05 10:19:24 +000014963 offset = strlen(tp->fw_ver);
14964 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14965 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014966
14967 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014968 offset = strlen(tp->fw_ver);
14969 if (offset < TG3_VER_SIZE - 1)
14970 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014971 }
14972}
14973
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014974static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014975{
14976 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014977 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014978
14979 for (offset = TG3_NVM_DIR_START;
14980 offset < TG3_NVM_DIR_END;
14981 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014982 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014983 return;
14984
14985 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14986 break;
14987 }
14988
14989 if (offset == TG3_NVM_DIR_END)
14990 return;
14991
Joe Perches63c3a662011-04-26 08:12:10 +000014992 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014993 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014994 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014995 return;
14996
Matt Carlsone4f34112009-02-25 14:25:00 +000014997 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014998 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014999 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015000 return;
15001
15002 offset += val - start;
15003
Matt Carlsonacd9c112009-02-25 14:26:33 +000015004 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015005
Matt Carlsonacd9c112009-02-25 14:26:33 +000015006 tp->fw_ver[vlen++] = ',';
15007 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015008
15009 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015010 __be32 v;
15011 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015012 return;
15013
Al Virob9fc7dc2007-12-17 22:59:57 -080015014 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015015
Matt Carlsonacd9c112009-02-25 14:26:33 +000015016 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15017 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015018 break;
15019 }
15020
Matt Carlsonacd9c112009-02-25 14:26:33 +000015021 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15022 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015023 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015024}
15025
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015026static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015027{
Matt Carlson7fd76442009-02-25 14:27:20 +000015028 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015029
15030 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15031 if (apedata != APE_SEG_SIG_MAGIC)
15032 return;
15033
15034 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15035 if (!(apedata & APE_FW_STATUS_READY))
15036 return;
15037
Michael Chan165f4d12012-07-16 16:23:59 +000015038 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15039 tg3_flag_set(tp, APE_HAS_NCSI);
15040}
15041
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015042static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015043{
15044 int vlen;
15045 u32 apedata;
15046 char *fwtype;
15047
Matt Carlson7fd76442009-02-25 14:27:20 +000015048 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15049
Michael Chan165f4d12012-07-16 16:23:59 +000015050 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015051 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015052 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15053 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015054 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015055 fwtype = "DASH";
15056
Matt Carlson7fd76442009-02-25 14:27:20 +000015057 vlen = strlen(tp->fw_ver);
15058
Matt Carlsonecc79642010-08-02 11:26:01 +000015059 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15060 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015061 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15062 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15063 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15064 (apedata & APE_FW_VERSION_BLDMSK));
15065}
15066
Michael Chanc86a8562013-01-06 12:51:08 +000015067static void tg3_read_otp_ver(struct tg3 *tp)
15068{
15069 u32 val, val2;
15070
Joe Perches41535772013-02-16 11:20:04 +000015071 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015072 return;
15073
15074 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15075 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15076 TG3_OTP_MAGIC0_VALID(val)) {
15077 u64 val64 = (u64) val << 32 | val2;
15078 u32 ver = 0;
15079 int i, vlen;
15080
15081 for (i = 0; i < 7; i++) {
15082 if ((val64 & 0xff) == 0)
15083 break;
15084 ver = val64 & 0xff;
15085 val64 >>= 8;
15086 }
15087 vlen = strlen(tp->fw_ver);
15088 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15089 }
15090}
15091
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015092static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015093{
15094 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015095 bool vpd_vers = false;
15096
15097 if (tp->fw_ver[0] != 0)
15098 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015099
Joe Perches63c3a662011-04-26 08:12:10 +000015100 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015101 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015102 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015103 return;
15104 }
15105
Matt Carlsonacd9c112009-02-25 14:26:33 +000015106 if (tg3_nvram_read(tp, 0, &val))
15107 return;
15108
15109 if (val == TG3_EEPROM_MAGIC)
15110 tg3_read_bc_ver(tp);
15111 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15112 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015113 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15114 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015115
Michael Chan165f4d12012-07-16 16:23:59 +000015116 if (tg3_flag(tp, ENABLE_ASF)) {
15117 if (tg3_flag(tp, ENABLE_APE)) {
15118 tg3_probe_ncsi(tp);
15119 if (!vpd_vers)
15120 tg3_read_dash_ver(tp);
15121 } else if (!vpd_vers) {
15122 tg3_read_mgmtfw_ver(tp);
15123 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015124 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015125
15126 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015127}
15128
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015129static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15130{
Joe Perches63c3a662011-04-26 08:12:10 +000015131 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015132 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015133 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015134 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015135 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015136 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015137}
15138
Matt Carlson41434702011-03-09 16:58:22 +000015139static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015140 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15141 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15142 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15143 { },
15144};
15145
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015146static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015147{
15148 struct pci_dev *peer;
15149 unsigned int func, devnr = tp->pdev->devfn & ~7;
15150
15151 for (func = 0; func < 8; func++) {
15152 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15153 if (peer && peer != tp->pdev)
15154 break;
15155 pci_dev_put(peer);
15156 }
15157 /* 5704 can be configured in single-port mode, set peer to
15158 * tp->pdev in that case.
15159 */
15160 if (!peer) {
15161 peer = tp->pdev;
15162 return peer;
15163 }
15164
15165 /*
15166 * We don't need to keep the refcount elevated; there's no way
15167 * to remove one half of this device without removing the other
15168 */
15169 pci_dev_put(peer);
15170
15171 return peer;
15172}
15173
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015174static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015175{
15176 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015177 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015178 u32 reg;
15179
15180 /* All devices that use the alternate
15181 * ASIC REV location have a CPMU.
15182 */
15183 tg3_flag_set(tp, CPMU_PRESENT);
15184
15185 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015186 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015187 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15188 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015189 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15190 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15191 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15192 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015193 reg = TG3PCI_GEN2_PRODID_ASICREV;
15194 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15195 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15196 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15197 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15198 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15199 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15200 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15201 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15202 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15203 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15204 reg = TG3PCI_GEN15_PRODID_ASICREV;
15205 else
15206 reg = TG3PCI_PRODID_ASICREV;
15207
15208 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15209 }
15210
15211 /* Wrong chip ID in 5752 A0. This code can be removed later
15212 * as A0 is not in production.
15213 */
Joe Perches41535772013-02-16 11:20:04 +000015214 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015215 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15216
Joe Perches41535772013-02-16 11:20:04 +000015217 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015218 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15219
Joe Perches41535772013-02-16 11:20:04 +000015220 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15221 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15222 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015223 tg3_flag_set(tp, 5717_PLUS);
15224
Joe Perches41535772013-02-16 11:20:04 +000015225 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15226 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015227 tg3_flag_set(tp, 57765_CLASS);
15228
Michael Chanc65a17f2013-01-06 12:51:07 +000015229 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015230 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015231 tg3_flag_set(tp, 57765_PLUS);
15232
15233 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015234 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15235 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15236 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15237 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15238 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15239 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015240 tg3_flag(tp, 57765_PLUS))
15241 tg3_flag_set(tp, 5755_PLUS);
15242
Joe Perches41535772013-02-16 11:20:04 +000015243 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15244 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015245 tg3_flag_set(tp, 5780_CLASS);
15246
Joe Perches41535772013-02-16 11:20:04 +000015247 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15248 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15249 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015250 tg3_flag(tp, 5755_PLUS) ||
15251 tg3_flag(tp, 5780_CLASS))
15252 tg3_flag_set(tp, 5750_PLUS);
15253
Joe Perches41535772013-02-16 11:20:04 +000015254 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015255 tg3_flag(tp, 5750_PLUS))
15256 tg3_flag_set(tp, 5705_PLUS);
15257}
15258
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015259static bool tg3_10_100_only_device(struct tg3 *tp,
15260 const struct pci_device_id *ent)
15261{
15262 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15263
Joe Perches41535772013-02-16 11:20:04 +000015264 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15265 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015266 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15267 return true;
15268
15269 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015270 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015271 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15272 return true;
15273 } else {
15274 return true;
15275 }
15276 }
15277
15278 return false;
15279}
15280
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015281static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015282{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015284 u32 pci_state_reg, grc_misc_cfg;
15285 u32 val;
15286 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015287 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
Linus Torvalds1da177e2005-04-16 15:20:36 -070015289 /* Force memory write invalidate off. If we leave it on,
15290 * then on 5700_BX chips we have to enable a workaround.
15291 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15292 * to match the cacheline size. The Broadcom driver have this
15293 * workaround but turns MWI off all the times so never uses
15294 * it. This seems to suggest that the workaround is insufficient.
15295 */
15296 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15297 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15298 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15299
Matt Carlson16821282011-07-13 09:27:28 +000015300 /* Important! -- Make sure register accesses are byteswapped
15301 * correctly. Also, for those chips that require it, make
15302 * sure that indirect register accesses are enabled before
15303 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304 */
15305 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15306 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015307 tp->misc_host_ctrl |= (misc_ctrl_reg &
15308 MISC_HOST_CTRL_CHIPREV);
15309 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15310 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015311
Matt Carlson42b123b2012-02-13 15:20:13 +000015312 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015313
Michael Chan68929142005-08-09 20:17:14 -070015314 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15315 * we need to disable memory and use config. cycles
15316 * only to access all registers. The 5702/03 chips
15317 * can mistakenly decode the special cycles from the
15318 * ICH chipsets as memory write cycles, causing corruption
15319 * of register and memory space. Only certain ICH bridges
15320 * will drive special cycles with non-zero data during the
15321 * address phase which can fall within the 5703's address
15322 * range. This is not an ICH bug as the PCI spec allows
15323 * non-zero address during special cycles. However, only
15324 * these ICH bridges are known to drive non-zero addresses
15325 * during special cycles.
15326 *
15327 * Since special cycles do not cross PCI bridges, we only
15328 * enable this workaround if the 5703 is on the secondary
15329 * bus of these ICH bridges.
15330 */
Joe Perches41535772013-02-16 11:20:04 +000015331 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15332 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015333 static struct tg3_dev_id {
15334 u32 vendor;
15335 u32 device;
15336 u32 rev;
15337 } ich_chipsets[] = {
15338 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15339 PCI_ANY_ID },
15340 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15341 PCI_ANY_ID },
15342 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15343 0xa },
15344 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15345 PCI_ANY_ID },
15346 { },
15347 };
15348 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15349 struct pci_dev *bridge = NULL;
15350
15351 while (pci_id->vendor != 0) {
15352 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15353 bridge);
15354 if (!bridge) {
15355 pci_id++;
15356 continue;
15357 }
15358 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015359 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015360 continue;
15361 }
15362 if (bridge->subordinate &&
15363 (bridge->subordinate->number ==
15364 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015365 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015366 pci_dev_put(bridge);
15367 break;
15368 }
15369 }
15370 }
15371
Joe Perches41535772013-02-16 11:20:04 +000015372 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015373 static struct tg3_dev_id {
15374 u32 vendor;
15375 u32 device;
15376 } bridge_chipsets[] = {
15377 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15378 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15379 { },
15380 };
15381 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15382 struct pci_dev *bridge = NULL;
15383
15384 while (pci_id->vendor != 0) {
15385 bridge = pci_get_device(pci_id->vendor,
15386 pci_id->device,
15387 bridge);
15388 if (!bridge) {
15389 pci_id++;
15390 continue;
15391 }
15392 if (bridge->subordinate &&
15393 (bridge->subordinate->number <=
15394 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015395 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015396 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015397 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015398 pci_dev_put(bridge);
15399 break;
15400 }
15401 }
15402 }
15403
Michael Chan4a29cc22006-03-19 13:21:12 -080015404 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15405 * DMA addresses > 40-bit. This bridge may have other additional
15406 * 57xx devices behind it in some 4-port NIC designs for example.
15407 * Any tg3 device found behind the bridge will also need the 40-bit
15408 * DMA workaround.
15409 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015410 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015411 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015412 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015413 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015414 struct pci_dev *bridge = NULL;
15415
15416 do {
15417 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15418 PCI_DEVICE_ID_SERVERWORKS_EPB,
15419 bridge);
15420 if (bridge && bridge->subordinate &&
15421 (bridge->subordinate->number <=
15422 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015423 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015424 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015425 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015426 pci_dev_put(bridge);
15427 break;
15428 }
15429 } while (bridge);
15430 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015431
Joe Perches41535772013-02-16 11:20:04 +000015432 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15433 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015434 tp->pdev_peer = tg3_find_peer(tp);
15435
Matt Carlson507399f2009-11-13 13:03:37 +000015436 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015437 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015438 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015439 else if (tg3_flag(tp, 57765_PLUS))
15440 tg3_flag_set(tp, HW_TSO_3);
15441 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015442 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015443 tg3_flag_set(tp, HW_TSO_2);
15444 else if (tg3_flag(tp, 5750_PLUS)) {
15445 tg3_flag_set(tp, HW_TSO_1);
15446 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015447 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15448 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015449 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015450 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15451 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15452 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015453 tg3_flag_set(tp, FW_TSO);
15454 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015455 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015456 tp->fw_needed = FIRMWARE_TG3TSO5;
15457 else
15458 tp->fw_needed = FIRMWARE_TG3TSO;
15459 }
15460
Matt Carlsondabc5c62011-05-19 12:12:52 +000015461 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015462 if (tg3_flag(tp, HW_TSO_1) ||
15463 tg3_flag(tp, HW_TSO_2) ||
15464 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015465 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015466 /* For firmware TSO, assume ASF is disabled.
15467 * We'll disable TSO later if we discover ASF
15468 * is enabled in tg3_get_eeprom_hw_cfg().
15469 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015470 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015471 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015472 tg3_flag_clear(tp, TSO_CAPABLE);
15473 tg3_flag_clear(tp, TSO_BUG);
15474 tp->fw_needed = NULL;
15475 }
15476
Joe Perches41535772013-02-16 11:20:04 +000015477 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015478 tp->fw_needed = FIRMWARE_TG3;
15479
Nithin Sujirc4dab502013-03-06 17:02:34 +000015480 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15481 tp->fw_needed = FIRMWARE_TG357766;
15482
Matt Carlson507399f2009-11-13 13:03:37 +000015483 tp->irq_max = 1;
15484
Joe Perches63c3a662011-04-26 08:12:10 +000015485 if (tg3_flag(tp, 5750_PLUS)) {
15486 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015487 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15488 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15489 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15490 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015491 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015492 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015493
Joe Perches63c3a662011-04-26 08:12:10 +000015494 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015495 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015496 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015498
Joe Perches63c3a662011-04-26 08:12:10 +000015499 if (tg3_flag(tp, 57765_PLUS)) {
15500 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015501 tp->irq_max = TG3_IRQ_MAX_VECS;
15502 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015503 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015504
Michael Chan91024262012-09-28 07:12:38 +000015505 tp->txq_max = 1;
15506 tp->rxq_max = 1;
15507 if (tp->irq_max > 1) {
15508 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15509 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15510
Joe Perches41535772013-02-16 11:20:04 +000015511 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15512 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015513 tp->txq_max = tp->irq_max - 1;
15514 }
15515
Matt Carlsonb7abee62012-06-07 12:56:54 +000015516 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015517 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015518 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015519
Joe Perches41535772013-02-16 11:20:04 +000015520 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015521 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015522
Joe Perches41535772013-02-16 11:20:04 +000015523 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15524 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15525 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15526 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015527 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015528
Joe Perches63c3a662011-04-26 08:12:10 +000015529 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015530 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015531 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015532
Joe Perches63c3a662011-04-26 08:12:10 +000015533 if (!tg3_flag(tp, 5705_PLUS) ||
15534 tg3_flag(tp, 5780_CLASS) ||
15535 tg3_flag(tp, USE_JUMBO_BDFLAG))
15536 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015537
Matt Carlson52f44902008-11-21 17:17:04 -080015538 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15539 &pci_state_reg);
15540
Jon Mason708ebb3a2011-06-27 12:56:50 +000015541 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015542 u16 lnkctl;
15543
Joe Perches63c3a662011-04-26 08:12:10 +000015544 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015545
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015546 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015547 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015548 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015549 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015550 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015551 }
Joe Perches41535772013-02-16 11:20:04 +000015552 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15553 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15554 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15555 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015556 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015557 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015558 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015559 }
Joe Perches41535772013-02-16 11:20:04 +000015560 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015561 /* BCM5785 devices are effectively PCIe devices, and should
15562 * follow PCIe codepaths, but do not have a PCIe capabilities
15563 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015564 */
Joe Perches63c3a662011-04-26 08:12:10 +000015565 tg3_flag_set(tp, PCI_EXPRESS);
15566 } else if (!tg3_flag(tp, 5705_PLUS) ||
15567 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015568 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15569 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015570 dev_err(&tp->pdev->dev,
15571 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015572 return -EIO;
15573 }
15574
15575 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015576 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578
Michael Chan399de502005-10-03 14:02:39 -070015579 /* If we have an AMD 762 or VIA K8T800 chipset, write
15580 * reordering to the mailbox registers done by the host
15581 * controller can cause major troubles. We read back from
15582 * every mailbox register write to force the writes to be
15583 * posted to the chip in order.
15584 */
Matt Carlson41434702011-03-09 16:58:22 +000015585 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015586 !tg3_flag(tp, PCI_EXPRESS))
15587 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015588
Matt Carlson69fc4052008-12-21 20:19:57 -080015589 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15590 &tp->pci_cacheline_sz);
15591 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15592 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015593 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015594 tp->pci_lat_timer < 64) {
15595 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015596 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15597 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015598 }
15599
Matt Carlson16821282011-07-13 09:27:28 +000015600 /* Important! -- It is critical that the PCI-X hw workaround
15601 * situation is decided before the first MMIO register access.
15602 */
Joe Perches41535772013-02-16 11:20:04 +000015603 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015604 /* 5700 BX chips need to have their TX producer index
15605 * mailboxes written twice to workaround a bug.
15606 */
Joe Perches63c3a662011-04-26 08:12:10 +000015607 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015608
Matt Carlson52f44902008-11-21 17:17:04 -080015609 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015610 *
15611 * The workaround is to use indirect register accesses
15612 * for all chip writes not to mailbox registers.
15613 */
Joe Perches63c3a662011-04-26 08:12:10 +000015614 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015615 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015616
Joe Perches63c3a662011-04-26 08:12:10 +000015617 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015618
15619 /* The chip can have it's power management PCI config
15620 * space registers clobbered due to this bug.
15621 * So explicitly force the chip into D0 here.
15622 */
Matt Carlson9974a352007-10-07 23:27:28 -070015623 pci_read_config_dword(tp->pdev,
15624 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015625 &pm_reg);
15626 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15627 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015628 pci_write_config_dword(tp->pdev,
15629 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015630 pm_reg);
15631
15632 /* Also, force SERR#/PERR# in PCI command. */
15633 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15634 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15635 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15636 }
15637 }
15638
Linus Torvalds1da177e2005-04-16 15:20:36 -070015639 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015640 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015642 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015643
15644 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015645 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15647 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15648 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15649 }
15650
Michael Chan1ee582d2005-08-09 20:16:46 -070015651 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015652 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015653 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015654 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015655 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015656 tp->write32_tx_mbox = tg3_write32;
15657 tp->write32_rx_mbox = tg3_write32;
15658
15659 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015660 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015661 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015662 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015663 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015664 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015665 /*
15666 * Back to back register writes can cause problems on these
15667 * chips, the workaround is to read back all reg writes
15668 * except those to mailbox regs.
15669 *
15670 * See tg3_write_indirect_reg32().
15671 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015672 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015673 }
15674
Joe Perches63c3a662011-04-26 08:12:10 +000015675 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015676 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015677 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015678 tp->write32_rx_mbox = tg3_write_flush_reg32;
15679 }
Michael Chan20094932005-08-09 20:16:32 -070015680
Joe Perches63c3a662011-04-26 08:12:10 +000015681 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015682 tp->read32 = tg3_read_indirect_reg32;
15683 tp->write32 = tg3_write_indirect_reg32;
15684 tp->read32_mbox = tg3_read_indirect_mbox;
15685 tp->write32_mbox = tg3_write_indirect_mbox;
15686 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15687 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15688
15689 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015690 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015691
15692 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15693 pci_cmd &= ~PCI_COMMAND_MEMORY;
15694 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15695 }
Joe Perches41535772013-02-16 11:20:04 +000015696 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015697 tp->read32_mbox = tg3_read32_mbox_5906;
15698 tp->write32_mbox = tg3_write32_mbox_5906;
15699 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15700 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15701 }
Michael Chan68929142005-08-09 20:17:14 -070015702
Michael Chanbbadf502006-04-06 21:46:34 -070015703 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015704 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000015705 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15706 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015707 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015708
Matt Carlson16821282011-07-13 09:27:28 +000015709 /* The memory arbiter has to be enabled in order for SRAM accesses
15710 * to succeed. Normally on powerup the tg3 chip firmware will make
15711 * sure it is enabled, but other entities such as system netboot
15712 * code might disable it.
15713 */
15714 val = tr32(MEMARB_MODE);
15715 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15716
Matt Carlson9dc5e342011-11-04 09:15:02 +000015717 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000015718 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015719 tg3_flag(tp, 5780_CLASS)) {
15720 if (tg3_flag(tp, PCIX_MODE)) {
15721 pci_read_config_dword(tp->pdev,
15722 tp->pcix_cap + PCI_X_STATUS,
15723 &val);
15724 tp->pci_fn = val & 0x7;
15725 }
Joe Perches41535772013-02-16 11:20:04 +000015726 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15727 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15728 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000015729 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000015730 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
15731 val = tr32(TG3_CPMU_STATUS);
15732
Joe Perches41535772013-02-16 11:20:04 +000015733 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000015734 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
15735 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000015736 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15737 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000015738 }
15739
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015740 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
15741 tp->write32_tx_mbox = tg3_write_flush_reg32;
15742 tp->write32_rx_mbox = tg3_write_flush_reg32;
15743 }
15744
Michael Chan7d0c41e2005-04-21 17:06:20 -070015745 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015746 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015747 * determined before calling tg3_set_power_state() so that
15748 * we know whether or not to switch out of Vaux power.
15749 * When the flag is set, it means that GPIO1 is used for eeprom
15750 * write protect and also implies that it is a LOM where GPIOs
15751 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015752 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015753 tg3_get_eeprom_hw_cfg(tp);
15754
Matt Carlson1caf13e2013-03-06 17:02:29 +000015755 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015756 tg3_flag_clear(tp, TSO_CAPABLE);
15757 tg3_flag_clear(tp, TSO_BUG);
15758 tp->fw_needed = NULL;
15759 }
15760
Joe Perches63c3a662011-04-26 08:12:10 +000015761 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015762 /* Allow reads and writes to the
15763 * APE register and memory space.
15764 */
15765 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015766 PCISTATE_ALLOW_APE_SHMEM_WR |
15767 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015768 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15769 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015770
15771 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015772 }
15773
Matt Carlson16821282011-07-13 09:27:28 +000015774 /* Set up tp->grc_local_ctrl before calling
15775 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15776 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015777 * It is also used as eeprom write protect on LOMs.
15778 */
15779 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000015780 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015781 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015782 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15783 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015784 /* Unused GPIO3 must be driven as output on 5752 because there
15785 * are no pull-up resistors on unused GPIO pins.
15786 */
Joe Perches41535772013-02-16 11:20:04 +000015787 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070015788 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015789
Joe Perches41535772013-02-16 11:20:04 +000015790 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15791 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015792 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015793 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15794
Matt Carlson8d519ab2009-04-20 06:58:01 +000015795 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15796 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015797 /* Turn off the debug UART. */
15798 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015799 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015800 /* Keep VMain power. */
15801 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15802 GRC_LCLCTRL_GPIO_OUTPUT0;
15803 }
15804
Joe Perches41535772013-02-16 11:20:04 +000015805 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015806 tp->grc_local_ctrl |=
15807 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
15808
Matt Carlson16821282011-07-13 09:27:28 +000015809 /* Switch out of Vaux if it is a NIC */
15810 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015811
Linus Torvalds1da177e2005-04-16 15:20:36 -070015812 /* Derive initial jumbo mode from MTU assigned in
15813 * ether_setup() via the alloc_etherdev() call
15814 */
Joe Perches63c3a662011-04-26 08:12:10 +000015815 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15816 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015817
15818 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000015819 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15820 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15821 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15822 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015823 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015824 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015825 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015826 }
15827
Joe Perches41535772013-02-16 11:20:04 +000015828 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015829 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015830
Linus Torvalds1da177e2005-04-16 15:20:36 -070015831 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000015832 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15833 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
15834 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
15835 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015836 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15837 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15838 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015839
Joe Perches41535772013-02-16 11:20:04 +000015840 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
15841 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015842 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000015843 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015844 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015845
Joe Perches63c3a662011-04-26 08:12:10 +000015846 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015847 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000015848 tg3_asic_rev(tp) != ASIC_REV_5785 &&
15849 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015850 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000015851 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15852 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15853 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15854 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015855 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15856 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015857 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015858 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015859 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015860 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015861 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015863
Joe Perches41535772013-02-16 11:20:04 +000015864 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15865 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015866 tp->phy_otp = tg3_read_otp_phycfg(tp);
15867 if (tp->phy_otp == 0)
15868 tp->phy_otp = TG3_OTP_DEFAULT;
15869 }
15870
Joe Perches63c3a662011-04-26 08:12:10 +000015871 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015872 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15873 else
15874 tp->mi_mode = MAC_MI_MODE_BASE;
15875
Linus Torvalds1da177e2005-04-16 15:20:36 -070015876 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000015877 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
15878 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015879 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15880
Matt Carlson4d958472011-04-20 07:57:35 +000015881 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000015882 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15883 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
15884 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000015885 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15886 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15887 }
15888
Joe Perches41535772013-02-16 11:20:04 +000015889 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
15890 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015891 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015892
Matt Carlson158d7ab2008-05-29 01:37:54 -070015893 err = tg3_mdio_init(tp);
15894 if (err)
15895 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896
15897 /* Initialize data/descriptor byte/word swapping. */
15898 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000015899 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15900 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000015901 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15902 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15903 GRC_MODE_B2HRX_ENABLE |
15904 GRC_MODE_HTX2B_ENABLE |
15905 GRC_MODE_HOST_STACKUP);
15906 else
15907 val &= GRC_MODE_HOST_STACKUP;
15908
Linus Torvalds1da177e2005-04-16 15:20:36 -070015909 tw32(GRC_MODE, val | tp->grc_mode);
15910
15911 tg3_switch_clocks(tp);
15912
15913 /* Clear this out for sanity. */
15914 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15915
15916 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15917 &pci_state_reg);
15918 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015919 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000015920 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15921 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15922 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
15923 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015924 void __iomem *sram_base;
15925
15926 /* Write some dummy words into the SRAM status block
15927 * area, see if it reads back correctly. If the return
15928 * value is bad, force enable the PCIX workaround.
15929 */
15930 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15931
15932 writel(0x00000000, sram_base);
15933 writel(0x00000000, sram_base + 4);
15934 writel(0xffffffff, sram_base + 4);
15935 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015936 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015937 }
15938 }
15939
15940 udelay(50);
15941 tg3_nvram_init(tp);
15942
Nithin Sujirc4dab502013-03-06 17:02:34 +000015943 /* If the device has an NVRAM, no need to load patch firmware */
15944 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
15945 !tg3_flag(tp, NO_NVRAM))
15946 tp->fw_needed = NULL;
15947
Linus Torvalds1da177e2005-04-16 15:20:36 -070015948 grc_misc_cfg = tr32(GRC_MISC_CFG);
15949 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15950
Joe Perches41535772013-02-16 11:20:04 +000015951 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015952 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15953 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015954 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015955
Joe Perches63c3a662011-04-26 08:12:10 +000015956 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000015957 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015958 tg3_flag_set(tp, TAGGED_STATUS);
15959 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015960 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15961 HOSTCC_MODE_CLRTICK_TXBD);
15962
15963 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15964 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15965 tp->misc_host_ctrl);
15966 }
15967
Matt Carlson3bda1252008-08-15 14:08:22 -070015968 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015969 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015970 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015971 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015972 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015973
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015974 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015975 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015976
15977 err = tg3_phy_probe(tp);
15978 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015979 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015980 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015981 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015982 }
15983
Matt Carlson184b8902010-04-05 10:19:25 +000015984 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015985 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015986
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015987 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15988 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015989 } else {
Joe Perches41535772013-02-16 11:20:04 +000015990 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015991 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015992 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015993 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015994 }
15995
15996 /* 5700 {AX,BX} chips have a broken status block link
15997 * change bit implementation, so we must use the
15998 * status register in those cases.
15999 */
Joe Perches41535772013-02-16 11:20:04 +000016000 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016001 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016002 else
Joe Perches63c3a662011-04-26 08:12:10 +000016003 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016004
16005 /* The led_ctrl is set during tg3_phy_probe, here we might
16006 * have to force the link status polling mechanism based
16007 * upon subsystem IDs.
16008 */
16009 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016010 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016011 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16012 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016013 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016014 }
16015
16016 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016017 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016018 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016019 else
Joe Perches63c3a662011-04-26 08:12:10 +000016020 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016021
Eric Dumazet9205fd92011-11-18 06:47:01 +000016022 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016023 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016024 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016025 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016026 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016027#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016028 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016029#endif
16030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016031
Matt Carlson2c49a442010-09-30 10:34:35 +000016032 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16033 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016034 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16035
Matt Carlson2c49a442010-09-30 10:34:35 +000016036 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016037
16038 /* Increment the rx prod index on the rx std ring by at most
16039 * 8 for these chips to workaround hw errata.
16040 */
Joe Perches41535772013-02-16 11:20:04 +000016041 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16042 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16043 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016044 tp->rx_std_max_post = 8;
16045
Joe Perches63c3a662011-04-26 08:12:10 +000016046 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016047 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16048 PCIE_PWR_MGMT_L1_THRESH_MSK;
16049
Linus Torvalds1da177e2005-04-16 15:20:36 -070016050 return err;
16051}
16052
David S. Miller49b6e95f2007-03-29 01:38:42 -070016053#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016054static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016055{
16056 struct net_device *dev = tp->dev;
16057 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016058 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016059 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016060 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016061
David S. Miller49b6e95f2007-03-29 01:38:42 -070016062 addr = of_get_property(dp, "local-mac-address", &len);
16063 if (addr && len == 6) {
16064 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016066 }
16067 return -ENODEV;
16068}
16069
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016070static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016071{
16072 struct net_device *dev = tp->dev;
16073
16074 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16075 return 0;
16076}
16077#endif
16078
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016079static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016080{
16081 struct net_device *dev = tp->dev;
16082 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016083 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016084 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016085
David S. Miller49b6e95f2007-03-29 01:38:42 -070016086#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016087 if (!tg3_get_macaddr_sparc(tp))
16088 return 0;
16089#endif
16090
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016091 if (tg3_flag(tp, IS_SSB_CORE)) {
16092 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16093 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16094 return 0;
16095 }
16096
Linus Torvalds1da177e2005-04-16 15:20:36 -070016097 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016098 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016099 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016100 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16101 mac_offset = 0xcc;
16102 if (tg3_nvram_lock(tp))
16103 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16104 else
16105 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016106 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016107 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016108 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016109 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016110 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016111 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016112 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016113
16114 /* First try to get it from MAC address mailbox. */
16115 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16116 if ((hi >> 16) == 0x484b) {
16117 dev->dev_addr[0] = (hi >> 8) & 0xff;
16118 dev->dev_addr[1] = (hi >> 0) & 0xff;
16119
16120 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16121 dev->dev_addr[2] = (lo >> 24) & 0xff;
16122 dev->dev_addr[3] = (lo >> 16) & 0xff;
16123 dev->dev_addr[4] = (lo >> 8) & 0xff;
16124 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016125
Michael Chan008652b2006-03-27 23:14:53 -080016126 /* Some old bootcode may report a 0 MAC address in SRAM */
16127 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16128 }
16129 if (!addr_ok) {
16130 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016131 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016132 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016133 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016134 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16135 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016136 }
16137 /* Finally just fetch it out of the MAC control regs. */
16138 else {
16139 hi = tr32(MAC_ADDR_0_HIGH);
16140 lo = tr32(MAC_ADDR_0_LOW);
16141
16142 dev->dev_addr[5] = lo & 0xff;
16143 dev->dev_addr[4] = (lo >> 8) & 0xff;
16144 dev->dev_addr[3] = (lo >> 16) & 0xff;
16145 dev->dev_addr[2] = (lo >> 24) & 0xff;
16146 dev->dev_addr[1] = hi & 0xff;
16147 dev->dev_addr[0] = (hi >> 8) & 0xff;
16148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016149 }
16150
16151 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016152#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016153 if (!tg3_get_default_macaddr_sparc(tp))
16154 return 0;
16155#endif
16156 return -EINVAL;
16157 }
16158 return 0;
16159}
16160
David S. Miller59e6b432005-05-18 22:50:10 -070016161#define BOUNDARY_SINGLE_CACHELINE 1
16162#define BOUNDARY_MULTI_CACHELINE 2
16163
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016164static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016165{
16166 int cacheline_size;
16167 u8 byte;
16168 int goal;
16169
16170 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16171 if (byte == 0)
16172 cacheline_size = 1024;
16173 else
16174 cacheline_size = (int) byte * 4;
16175
16176 /* On 5703 and later chips, the boundary bits have no
16177 * effect.
16178 */
Joe Perches41535772013-02-16 11:20:04 +000016179 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16180 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016181 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016182 goto out;
16183
16184#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16185 goal = BOUNDARY_MULTI_CACHELINE;
16186#else
16187#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16188 goal = BOUNDARY_SINGLE_CACHELINE;
16189#else
16190 goal = 0;
16191#endif
16192#endif
16193
Joe Perches63c3a662011-04-26 08:12:10 +000016194 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016195 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16196 goto out;
16197 }
16198
David S. Miller59e6b432005-05-18 22:50:10 -070016199 if (!goal)
16200 goto out;
16201
16202 /* PCI controllers on most RISC systems tend to disconnect
16203 * when a device tries to burst across a cache-line boundary.
16204 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16205 *
16206 * Unfortunately, for PCI-E there are only limited
16207 * write-side controls for this, and thus for reads
16208 * we will still get the disconnects. We'll also waste
16209 * these PCI cycles for both read and write for chips
16210 * other than 5700 and 5701 which do not implement the
16211 * boundary bits.
16212 */
Joe Perches63c3a662011-04-26 08:12:10 +000016213 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016214 switch (cacheline_size) {
16215 case 16:
16216 case 32:
16217 case 64:
16218 case 128:
16219 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16220 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16221 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16222 } else {
16223 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16224 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16225 }
16226 break;
16227
16228 case 256:
16229 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16230 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16231 break;
16232
16233 default:
16234 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16235 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16236 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016237 }
Joe Perches63c3a662011-04-26 08:12:10 +000016238 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016239 switch (cacheline_size) {
16240 case 16:
16241 case 32:
16242 case 64:
16243 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16244 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16245 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16246 break;
16247 }
16248 /* fallthrough */
16249 case 128:
16250 default:
16251 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16252 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16253 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016254 }
David S. Miller59e6b432005-05-18 22:50:10 -070016255 } else {
16256 switch (cacheline_size) {
16257 case 16:
16258 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16259 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16260 DMA_RWCTRL_WRITE_BNDRY_16);
16261 break;
16262 }
16263 /* fallthrough */
16264 case 32:
16265 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16266 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16267 DMA_RWCTRL_WRITE_BNDRY_32);
16268 break;
16269 }
16270 /* fallthrough */
16271 case 64:
16272 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16273 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16274 DMA_RWCTRL_WRITE_BNDRY_64);
16275 break;
16276 }
16277 /* fallthrough */
16278 case 128:
16279 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16280 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16281 DMA_RWCTRL_WRITE_BNDRY_128);
16282 break;
16283 }
16284 /* fallthrough */
16285 case 256:
16286 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16287 DMA_RWCTRL_WRITE_BNDRY_256);
16288 break;
16289 case 512:
16290 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16291 DMA_RWCTRL_WRITE_BNDRY_512);
16292 break;
16293 case 1024:
16294 default:
16295 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16296 DMA_RWCTRL_WRITE_BNDRY_1024);
16297 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016298 }
David S. Miller59e6b432005-05-18 22:50:10 -070016299 }
16300
16301out:
16302 return val;
16303}
16304
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016305static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
16306 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016307{
16308 struct tg3_internal_buffer_desc test_desc;
16309 u32 sram_dma_descs;
16310 int i, ret;
16311
16312 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16313
16314 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16315 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16316 tw32(RDMAC_STATUS, 0);
16317 tw32(WDMAC_STATUS, 0);
16318
16319 tw32(BUFMGR_MODE, 0);
16320 tw32(FTQ_RESET, 0);
16321
16322 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16323 test_desc.addr_lo = buf_dma & 0xffffffff;
16324 test_desc.nic_mbuf = 0x00002100;
16325 test_desc.len = size;
16326
16327 /*
16328 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16329 * the *second* time the tg3 driver was getting loaded after an
16330 * initial scan.
16331 *
16332 * Broadcom tells me:
16333 * ...the DMA engine is connected to the GRC block and a DMA
16334 * reset may affect the GRC block in some unpredictable way...
16335 * The behavior of resets to individual blocks has not been tested.
16336 *
16337 * Broadcom noted the GRC reset will also reset all sub-components.
16338 */
16339 if (to_device) {
16340 test_desc.cqid_sqid = (13 << 8) | 2;
16341
16342 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16343 udelay(40);
16344 } else {
16345 test_desc.cqid_sqid = (16 << 8) | 7;
16346
16347 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16348 udelay(40);
16349 }
16350 test_desc.flags = 0x00000005;
16351
16352 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16353 u32 val;
16354
16355 val = *(((u32 *)&test_desc) + i);
16356 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16357 sram_dma_descs + (i * sizeof(u32)));
16358 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16359 }
16360 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16361
Matt Carlson859a588792010-04-05 10:19:28 +000016362 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016363 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016364 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016365 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016366
16367 ret = -ENODEV;
16368 for (i = 0; i < 40; i++) {
16369 u32 val;
16370
16371 if (to_device)
16372 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16373 else
16374 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16375 if ((val & 0xffff) == sram_dma_descs) {
16376 ret = 0;
16377 break;
16378 }
16379
16380 udelay(100);
16381 }
16382
16383 return ret;
16384}
16385
David S. Millerded73402005-05-23 13:59:47 -070016386#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016387
Matt Carlson41434702011-03-09 16:58:22 +000016388static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016389 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16390 { },
16391};
16392
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016393static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016394{
16395 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016396 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016397 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016398
Matt Carlson4bae65c2010-11-24 08:31:52 +000016399 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16400 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016401 if (!buf) {
16402 ret = -ENOMEM;
16403 goto out_nofree;
16404 }
16405
16406 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16407 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16408
David S. Miller59e6b432005-05-18 22:50:10 -070016409 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016410
Joe Perches63c3a662011-04-26 08:12:10 +000016411 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016412 goto out;
16413
Joe Perches63c3a662011-04-26 08:12:10 +000016414 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016415 /* DMA read watermark not used on PCIE */
16416 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016417 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016418 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16419 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016420 tp->dma_rwctrl |= 0x003f0000;
16421 else
16422 tp->dma_rwctrl |= 0x003f000f;
16423 } else {
Joe Perches41535772013-02-16 11:20:04 +000016424 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16425 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016426 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016427 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016428
Michael Chan4a29cc22006-03-19 13:21:12 -080016429 /* If the 5704 is behind the EPB bridge, we can
16430 * do the less restrictive ONE_DMA workaround for
16431 * better performance.
16432 */
Joe Perches63c3a662011-04-26 08:12:10 +000016433 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016434 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016435 tp->dma_rwctrl |= 0x8000;
16436 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016437 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16438
Joe Perches41535772013-02-16 11:20:04 +000016439 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016440 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016441 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016442 tp->dma_rwctrl |=
16443 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16444 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16445 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016446 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016447 /* 5780 always in PCIX mode */
16448 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016449 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016450 /* 5714 always in PCIX mode */
16451 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016452 } else {
16453 tp->dma_rwctrl |= 0x001b000f;
16454 }
16455 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016456 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16457 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016458
Joe Perches41535772013-02-16 11:20:04 +000016459 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16460 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016461 tp->dma_rwctrl &= 0xfffffff0;
16462
Joe Perches41535772013-02-16 11:20:04 +000016463 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16464 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016465 /* Remove this if it causes problems for some boards. */
16466 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16467
16468 /* On 5700/5701 chips, we need to set this bit.
16469 * Otherwise the chip will issue cacheline transactions
16470 * to streamable DMA memory with not all the byte
16471 * enables turned on. This is an error on several
16472 * RISC PCI controllers, in particular sparc64.
16473 *
16474 * On 5703/5704 chips, this bit has been reassigned
16475 * a different meaning. In particular, it is used
16476 * on those chips to enable a PCI-X workaround.
16477 */
16478 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16479 }
16480
16481 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16482
16483#if 0
16484 /* Unneeded, already done by tg3_get_invariants. */
16485 tg3_switch_clocks(tp);
16486#endif
16487
Joe Perches41535772013-02-16 11:20:04 +000016488 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16489 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016490 goto out;
16491
David S. Miller59e6b432005-05-18 22:50:10 -070016492 /* It is best to perform DMA test with maximum write burst size
16493 * to expose the 5700/5701 write DMA bug.
16494 */
16495 saved_dma_rwctrl = tp->dma_rwctrl;
16496 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16497 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16498
Linus Torvalds1da177e2005-04-16 15:20:36 -070016499 while (1) {
16500 u32 *p = buf, i;
16501
16502 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16503 p[i] = i;
16504
16505 /* Send the buffer to the chip. */
16506 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16507 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016508 dev_err(&tp->pdev->dev,
16509 "%s: Buffer write failed. err = %d\n",
16510 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016511 break;
16512 }
16513
16514#if 0
16515 /* validate data reached card RAM correctly. */
16516 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16517 u32 val;
16518 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16519 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016520 dev_err(&tp->pdev->dev,
16521 "%s: Buffer corrupted on device! "
16522 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016523 /* ret = -ENODEV here? */
16524 }
16525 p[i] = 0;
16526 }
16527#endif
16528 /* Now read it back. */
16529 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16530 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016531 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16532 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016533 break;
16534 }
16535
16536 /* Verify it. */
16537 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16538 if (p[i] == i)
16539 continue;
16540
David S. Miller59e6b432005-05-18 22:50:10 -070016541 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16542 DMA_RWCTRL_WRITE_BNDRY_16) {
16543 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016544 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16545 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16546 break;
16547 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016548 dev_err(&tp->pdev->dev,
16549 "%s: Buffer corrupted on read back! "
16550 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016551 ret = -ENODEV;
16552 goto out;
16553 }
16554 }
16555
16556 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16557 /* Success. */
16558 ret = 0;
16559 break;
16560 }
16561 }
David S. Miller59e6b432005-05-18 22:50:10 -070016562 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16563 DMA_RWCTRL_WRITE_BNDRY_16) {
16564 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016565 * now look for chipsets that are known to expose the
16566 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016567 */
Matt Carlson41434702011-03-09 16:58:22 +000016568 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016569 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16570 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016571 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016572 /* Safe to use the calculated DMA boundary. */
16573 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016574 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016575
David S. Miller59e6b432005-05-18 22:50:10 -070016576 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016578
16579out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016580 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016581out_nofree:
16582 return ret;
16583}
16584
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016585static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016586{
Joe Perches63c3a662011-04-26 08:12:10 +000016587 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016588 tp->bufmgr_config.mbuf_read_dma_low_water =
16589 DEFAULT_MB_RDMA_LOW_WATER_5705;
16590 tp->bufmgr_config.mbuf_mac_rx_low_water =
16591 DEFAULT_MB_MACRX_LOW_WATER_57765;
16592 tp->bufmgr_config.mbuf_high_water =
16593 DEFAULT_MB_HIGH_WATER_57765;
16594
16595 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16596 DEFAULT_MB_RDMA_LOW_WATER_5705;
16597 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16598 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16599 tp->bufmgr_config.mbuf_high_water_jumbo =
16600 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016601 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016602 tp->bufmgr_config.mbuf_read_dma_low_water =
16603 DEFAULT_MB_RDMA_LOW_WATER_5705;
16604 tp->bufmgr_config.mbuf_mac_rx_low_water =
16605 DEFAULT_MB_MACRX_LOW_WATER_5705;
16606 tp->bufmgr_config.mbuf_high_water =
16607 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016608 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016609 tp->bufmgr_config.mbuf_mac_rx_low_water =
16610 DEFAULT_MB_MACRX_LOW_WATER_5906;
16611 tp->bufmgr_config.mbuf_high_water =
16612 DEFAULT_MB_HIGH_WATER_5906;
16613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016614
Michael Chanfdfec1722005-07-25 12:31:48 -070016615 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16616 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16617 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16618 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16619 tp->bufmgr_config.mbuf_high_water_jumbo =
16620 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16621 } else {
16622 tp->bufmgr_config.mbuf_read_dma_low_water =
16623 DEFAULT_MB_RDMA_LOW_WATER;
16624 tp->bufmgr_config.mbuf_mac_rx_low_water =
16625 DEFAULT_MB_MACRX_LOW_WATER;
16626 tp->bufmgr_config.mbuf_high_water =
16627 DEFAULT_MB_HIGH_WATER;
16628
16629 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16630 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16631 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16632 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16633 tp->bufmgr_config.mbuf_high_water_jumbo =
16634 DEFAULT_MB_HIGH_WATER_JUMBO;
16635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016636
16637 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16638 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16639}
16640
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016641static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016642{
Matt Carlson79eb6902010-02-17 15:17:03 +000016643 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16644 case TG3_PHY_ID_BCM5400: return "5400";
16645 case TG3_PHY_ID_BCM5401: return "5401";
16646 case TG3_PHY_ID_BCM5411: return "5411";
16647 case TG3_PHY_ID_BCM5701: return "5701";
16648 case TG3_PHY_ID_BCM5703: return "5703";
16649 case TG3_PHY_ID_BCM5704: return "5704";
16650 case TG3_PHY_ID_BCM5705: return "5705";
16651 case TG3_PHY_ID_BCM5750: return "5750";
16652 case TG3_PHY_ID_BCM5752: return "5752";
16653 case TG3_PHY_ID_BCM5714: return "5714";
16654 case TG3_PHY_ID_BCM5780: return "5780";
16655 case TG3_PHY_ID_BCM5755: return "5755";
16656 case TG3_PHY_ID_BCM5787: return "5787";
16657 case TG3_PHY_ID_BCM5784: return "5784";
16658 case TG3_PHY_ID_BCM5756: return "5722/5756";
16659 case TG3_PHY_ID_BCM5906: return "5906";
16660 case TG3_PHY_ID_BCM5761: return "5761";
16661 case TG3_PHY_ID_BCM5718C: return "5718C";
16662 case TG3_PHY_ID_BCM5718S: return "5718S";
16663 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016664 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016665 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016666 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016667 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016668 case 0: return "serdes";
16669 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016671}
16672
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016673static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016674{
Joe Perches63c3a662011-04-26 08:12:10 +000016675 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016676 strcpy(str, "PCI Express");
16677 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016678 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016679 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16680
16681 strcpy(str, "PCIX:");
16682
16683 if ((clock_ctrl == 7) ||
16684 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16685 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16686 strcat(str, "133MHz");
16687 else if (clock_ctrl == 0)
16688 strcat(str, "33MHz");
16689 else if (clock_ctrl == 2)
16690 strcat(str, "50MHz");
16691 else if (clock_ctrl == 4)
16692 strcat(str, "66MHz");
16693 else if (clock_ctrl == 6)
16694 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016695 } else {
16696 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016697 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016698 strcat(str, "66MHz");
16699 else
16700 strcat(str, "33MHz");
16701 }
Joe Perches63c3a662011-04-26 08:12:10 +000016702 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016703 strcat(str, ":32-bit");
16704 else
16705 strcat(str, ":64-bit");
16706 return str;
16707}
16708
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016709static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016710{
16711 struct ethtool_coalesce *ec = &tp->coal;
16712
16713 memset(ec, 0, sizeof(*ec));
16714 ec->cmd = ETHTOOL_GCOALESCE;
16715 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16716 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16717 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16718 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16719 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16720 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16721 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16722 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16723 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16724
16725 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16726 HOSTCC_MODE_CLRTICK_TXBD)) {
16727 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16728 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16729 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16730 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16731 }
Michael Chand244c892005-07-05 14:42:33 -070016732
Joe Perches63c3a662011-04-26 08:12:10 +000016733 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016734 ec->rx_coalesce_usecs_irq = 0;
16735 ec->tx_coalesce_usecs_irq = 0;
16736 ec->stats_block_coalesce_usecs = 0;
16737 }
David S. Miller15f98502005-05-18 22:49:26 -070016738}
16739
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016740static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016741 const struct pci_device_id *ent)
16742{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016743 struct net_device *dev;
16744 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016745 int i, err, pm_cap;
16746 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016747 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016748 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016749 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016750
Joe Perches05dbe002010-02-17 19:44:19 +000016751 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016752
16753 err = pci_enable_device(pdev);
16754 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016755 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016756 return err;
16757 }
16758
Linus Torvalds1da177e2005-04-16 15:20:36 -070016759 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16760 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016761 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016762 goto err_out_disable_pdev;
16763 }
16764
16765 pci_set_master(pdev);
16766
16767 /* Find power-management capability. */
16768 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16769 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016770 dev_err(&pdev->dev,
16771 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016772 err = -EIO;
16773 goto err_out_free_res;
16774 }
16775
Matt Carlson16821282011-07-13 09:27:28 +000016776 err = pci_set_power_state(pdev, PCI_D0);
16777 if (err) {
16778 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16779 goto err_out_free_res;
16780 }
16781
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016782 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016783 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016784 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016785 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016786 }
16787
Linus Torvalds1da177e2005-04-16 15:20:36 -070016788 SET_NETDEV_DEV(dev, &pdev->dev);
16789
Linus Torvalds1da177e2005-04-16 15:20:36 -070016790 tp = netdev_priv(dev);
16791 tp->pdev = pdev;
16792 tp->dev = dev;
16793 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016794 tp->rx_mode = TG3_DEF_RX_MODE;
16795 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016796 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016797
Linus Torvalds1da177e2005-04-16 15:20:36 -070016798 if (tg3_debug > 0)
16799 tp->msg_enable = tg3_debug;
16800 else
16801 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16802
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016803 if (pdev_is_ssb_gige_core(pdev)) {
16804 tg3_flag_set(tp, IS_SSB_CORE);
16805 if (ssb_gige_must_flush_posted_writes(pdev))
16806 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
16807 if (ssb_gige_one_dma_at_once(pdev))
16808 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
16809 if (ssb_gige_have_roboswitch(pdev))
16810 tg3_flag_set(tp, ROBOSWITCH);
16811 if (ssb_gige_is_rgmii(pdev))
16812 tg3_flag_set(tp, RGMII_MODE);
16813 }
16814
Linus Torvalds1da177e2005-04-16 15:20:36 -070016815 /* The word/byte swap controls here control register access byte
16816 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16817 * setting below.
16818 */
16819 tp->misc_host_ctrl =
16820 MISC_HOST_CTRL_MASK_PCI_INT |
16821 MISC_HOST_CTRL_WORD_SWAP |
16822 MISC_HOST_CTRL_INDIR_ACCESS |
16823 MISC_HOST_CTRL_PCISTATE_RW;
16824
16825 /* The NONFRM (non-frame) byte/word swap controls take effect
16826 * on descriptor entries, anything which isn't packet data.
16827 *
16828 * The StrongARM chips on the board (one for tx, one for rx)
16829 * are running in big-endian mode.
16830 */
16831 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16832 GRC_MODE_WSWAP_NONFRM_DATA);
16833#ifdef __BIG_ENDIAN
16834 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16835#endif
16836 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016837 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016838 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016839
Matt Carlsond5fe4882008-11-21 17:20:32 -080016840 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016841 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016842 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016843 err = -ENOMEM;
16844 goto err_out_free_dev;
16845 }
16846
Matt Carlsonc9cab242011-07-13 09:27:27 +000016847 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16848 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16849 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16850 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16851 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016852 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016853 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16854 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016855 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
16856 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16857 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
16858 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000016859 tg3_flag_set(tp, ENABLE_APE);
16860 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16861 if (!tp->aperegs) {
16862 dev_err(&pdev->dev,
16863 "Cannot map APE registers, aborting\n");
16864 err = -ENOMEM;
16865 goto err_out_iounmap;
16866 }
16867 }
16868
Linus Torvalds1da177e2005-04-16 15:20:36 -070016869 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16870 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016871
Linus Torvalds1da177e2005-04-16 15:20:36 -070016872 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016873 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016874 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016875 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016876
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016877 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016878 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016879 dev_err(&pdev->dev,
16880 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016881 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016882 }
16883
Michael Chan4a29cc22006-03-19 13:21:12 -080016884 /* The EPB bridge inside 5714, 5715, and 5780 and any
16885 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016886 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16887 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16888 * do DMA address check in tg3_start_xmit().
16889 */
Joe Perches63c3a662011-04-26 08:12:10 +000016890 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016891 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016892 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016893 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016894#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016895 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016896#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016897 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016898 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016899
16900 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016901 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016902 err = pci_set_dma_mask(pdev, dma_mask);
16903 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016904 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016905 err = pci_set_consistent_dma_mask(pdev,
16906 persist_dma_mask);
16907 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016908 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16909 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016910 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016911 }
16912 }
16913 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016914 if (err || dma_mask == DMA_BIT_MASK(32)) {
16915 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016916 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016917 dev_err(&pdev->dev,
16918 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016919 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016920 }
16921 }
16922
Michael Chanfdfec1722005-07-25 12:31:48 -070016923 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016924
Matt Carlson0da06062011-05-19 12:12:53 +000016925 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16926
16927 /* 5700 B0 chips do not support checksumming correctly due
16928 * to hardware bugs.
16929 */
Joe Perches41535772013-02-16 11:20:04 +000016930 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000016931 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16932
16933 if (tg3_flag(tp, 5755_PLUS))
16934 features |= NETIF_F_IPV6_CSUM;
16935 }
16936
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016937 /* TSO is on by default on chips that support hardware TSO.
16938 * Firmware TSO on older chips gives lower performance, so it
16939 * is off by default, but can be enabled using ethtool.
16940 */
Joe Perches63c3a662011-04-26 08:12:10 +000016941 if ((tg3_flag(tp, HW_TSO_1) ||
16942 tg3_flag(tp, HW_TSO_2) ||
16943 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016944 (features & NETIF_F_IP_CSUM))
16945 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016946 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016947 if (features & NETIF_F_IPV6_CSUM)
16948 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016949 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000016950 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16951 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16952 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
16953 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16954 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016955 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957
Matt Carlsond542fe22011-05-19 16:02:43 +000016958 dev->features |= features;
16959 dev->vlan_features |= features;
16960
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016961 /*
16962 * Add loopback capability only for a subset of devices that support
16963 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16964 * loopback for the remaining devices.
16965 */
Joe Perches41535772013-02-16 11:20:04 +000016966 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016967 !tg3_flag(tp, CPMU_PRESENT))
16968 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016969 features |= NETIF_F_LOOPBACK;
16970
Matt Carlson0da06062011-05-19 12:12:53 +000016971 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016972
Joe Perches41535772013-02-16 11:20:04 +000016973 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016974 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016975 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016976 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016977 tp->rx_pending = 63;
16978 }
16979
Linus Torvalds1da177e2005-04-16 15:20:36 -070016980 err = tg3_get_device_address(tp);
16981 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016982 dev_err(&pdev->dev,
16983 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016984 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016985 }
16986
Matt Carlsonc88864d2007-11-12 21:07:01 -080016987 /*
16988 * Reset chip in case UNDI or EFI driver did not shutdown
16989 * DMA self test will enable WDMAC and we'll see (spurious)
16990 * pending DMA on the PCI bus at that point.
16991 */
16992 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16993 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16994 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16995 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16996 }
16997
16998 err = tg3_test_dma(tp);
16999 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017000 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017001 goto err_out_apeunmap;
17002 }
17003
Matt Carlson78f90dc2009-11-13 13:03:42 +000017004 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17005 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17006 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017007 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017008 struct tg3_napi *tnapi = &tp->napi[i];
17009
17010 tnapi->tp = tp;
17011 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17012
17013 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017014 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017015 intmbx += 0x8;
17016 else
17017 intmbx += 0x4;
17018
17019 tnapi->consmbox = rcvmbx;
17020 tnapi->prodmbox = sndmbx;
17021
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017022 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017023 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017024 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017025 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017026
Joe Perches63c3a662011-04-26 08:12:10 +000017027 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017028 break;
17029
17030 /*
17031 * If we support MSIX, we'll be using RSS. If we're using
17032 * RSS, the first vector only handles link interrupts and the
17033 * remaining vectors handle rx and tx interrupts. Reuse the
17034 * mailbox values for the next iteration. The values we setup
17035 * above are still useful for the single vectored mode.
17036 */
17037 if (!i)
17038 continue;
17039
17040 rcvmbx += 0x8;
17041
17042 if (sndmbx & 0x4)
17043 sndmbx -= 0x4;
17044 else
17045 sndmbx += 0xc;
17046 }
17047
Matt Carlsonc88864d2007-11-12 21:07:01 -080017048 tg3_init_coal(tp);
17049
Michael Chanc49a1562006-12-17 17:07:29 -080017050 pci_set_drvdata(pdev, dev);
17051
Joe Perches41535772013-02-16 11:20:04 +000017052 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17053 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17054 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017055 tg3_flag_set(tp, PTP_CAPABLE);
17056
Matt Carlsoncd0d7222011-07-13 09:27:33 +000017057 if (tg3_flag(tp, 5717_PLUS)) {
17058 /* Resume a low-power mode */
17059 tg3_frob_aux_power(tp, false);
17060 }
17061
Matt Carlson21f76382012-02-22 12:35:21 +000017062 tg3_timer_init(tp);
17063
Michael Chan402e1392013-02-14 12:13:41 +000017064 tg3_carrier_off(tp);
17065
Linus Torvalds1da177e2005-04-16 15:20:36 -070017066 err = register_netdev(dev);
17067 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017068 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017069 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017070 }
17071
Joe Perches05dbe002010-02-17 19:44:19 +000017072 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17073 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017074 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017075 tg3_bus_string(tp, str),
17076 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017077
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017078 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017079 struct phy_device *phydev;
17080 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017081 netdev_info(dev,
17082 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017083 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017084 } else {
17085 char *ethtype;
17086
17087 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17088 ethtype = "10/100Base-TX";
17089 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17090 ethtype = "1000Base-SX";
17091 else
17092 ethtype = "10/100/1000Base-T";
17093
Matt Carlson5129c3a2010-04-05 10:19:23 +000017094 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017095 "(WireSpeed[%d], EEE[%d])\n",
17096 tg3_phy_string(tp), ethtype,
17097 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17098 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017099 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017100
Joe Perches05dbe002010-02-17 19:44:19 +000017101 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017102 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017103 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017104 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017105 tg3_flag(tp, ENABLE_ASF) != 0,
17106 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017107 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17108 tp->dma_rwctrl,
17109 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17110 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017111
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017112 pci_save_state(pdev);
17113
Linus Torvalds1da177e2005-04-16 15:20:36 -070017114 return 0;
17115
Matt Carlson0d3031d2007-10-10 18:02:43 -070017116err_out_apeunmap:
17117 if (tp->aperegs) {
17118 iounmap(tp->aperegs);
17119 tp->aperegs = NULL;
17120 }
17121
Linus Torvalds1da177e2005-04-16 15:20:36 -070017122err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017123 if (tp->regs) {
17124 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017125 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017127
17128err_out_free_dev:
17129 free_netdev(dev);
17130
Matt Carlson16821282011-07-13 09:27:28 +000017131err_out_power_down:
17132 pci_set_power_state(pdev, PCI_D3hot);
17133
Linus Torvalds1da177e2005-04-16 15:20:36 -070017134err_out_free_res:
17135 pci_release_regions(pdev);
17136
17137err_out_disable_pdev:
17138 pci_disable_device(pdev);
17139 pci_set_drvdata(pdev, NULL);
17140 return err;
17141}
17142
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017143static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017144{
17145 struct net_device *dev = pci_get_drvdata(pdev);
17146
17147 if (dev) {
17148 struct tg3 *tp = netdev_priv(dev);
17149
Jesper Juhle3c55302012-04-09 22:50:15 +020017150 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017151
Matt Carlsondb219972011-11-04 09:15:03 +000017152 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017153
David S. Miller1805b2f2011-10-24 18:18:09 -040017154 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017155 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017156 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017157 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017158
Linus Torvalds1da177e2005-04-16 15:20:36 -070017159 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017160 if (tp->aperegs) {
17161 iounmap(tp->aperegs);
17162 tp->aperegs = NULL;
17163 }
Michael Chan68929142005-08-09 20:17:14 -070017164 if (tp->regs) {
17165 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017166 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017168 free_netdev(dev);
17169 pci_release_regions(pdev);
17170 pci_disable_device(pdev);
17171 pci_set_drvdata(pdev, NULL);
17172 }
17173}
17174
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017175#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017176static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017177{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017178 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017179 struct net_device *dev = pci_get_drvdata(pdev);
17180 struct tg3 *tp = netdev_priv(dev);
17181 int err;
17182
17183 if (!netif_running(dev))
17184 return 0;
17185
Matt Carlsondb219972011-11-04 09:15:03 +000017186 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017187 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017188 tg3_netif_stop(tp);
17189
Matt Carlson21f76382012-02-22 12:35:21 +000017190 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017191
David S. Millerf47c11e2005-06-24 20:18:35 -070017192 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017193 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017194 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017195
17196 netif_device_detach(dev);
17197
David S. Millerf47c11e2005-06-24 20:18:35 -070017198 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017199 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017200 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017201 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017202
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017203 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017204 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017205 int err2;
17206
David S. Millerf47c11e2005-06-24 20:18:35 -070017207 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017208
Joe Perches63c3a662011-04-26 08:12:10 +000017209 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017210 err2 = tg3_restart_hw(tp, 1);
17211 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017213
Matt Carlson21f76382012-02-22 12:35:21 +000017214 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017215
17216 netif_device_attach(dev);
17217 tg3_netif_start(tp);
17218
Michael Chanb9ec6c12006-07-25 16:37:27 -070017219out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017220 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017221
17222 if (!err2)
17223 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017224 }
17225
17226 return err;
17227}
17228
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017229static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017230{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017231 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017232 struct net_device *dev = pci_get_drvdata(pdev);
17233 struct tg3 *tp = netdev_priv(dev);
17234 int err;
17235
17236 if (!netif_running(dev))
17237 return 0;
17238
Linus Torvalds1da177e2005-04-16 15:20:36 -070017239 netif_device_attach(dev);
17240
David S. Millerf47c11e2005-06-24 20:18:35 -070017241 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017242
Joe Perches63c3a662011-04-26 08:12:10 +000017243 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070017244 err = tg3_restart_hw(tp, 1);
17245 if (err)
17246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017247
Matt Carlson21f76382012-02-22 12:35:21 +000017248 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017249
Linus Torvalds1da177e2005-04-16 15:20:36 -070017250 tg3_netif_start(tp);
17251
Michael Chanb9ec6c12006-07-25 16:37:27 -070017252out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017253 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017254
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017255 if (!err)
17256 tg3_phy_start(tp);
17257
Michael Chanb9ec6c12006-07-25 16:37:27 -070017258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017259}
17260
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017261static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017262#define TG3_PM_OPS (&tg3_pm_ops)
17263
17264#else
17265
17266#define TG3_PM_OPS NULL
17267
17268#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017269
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017270/**
17271 * tg3_io_error_detected - called when PCI error is detected
17272 * @pdev: Pointer to PCI device
17273 * @state: The current pci connection state
17274 *
17275 * This function is called after a PCI bus error affecting
17276 * this device has been detected.
17277 */
17278static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17279 pci_channel_state_t state)
17280{
17281 struct net_device *netdev = pci_get_drvdata(pdev);
17282 struct tg3 *tp = netdev_priv(netdev);
17283 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17284
17285 netdev_info(netdev, "PCI I/O error detected\n");
17286
17287 rtnl_lock();
17288
17289 if (!netif_running(netdev))
17290 goto done;
17291
17292 tg3_phy_stop(tp);
17293
17294 tg3_netif_stop(tp);
17295
Matt Carlson21f76382012-02-22 12:35:21 +000017296 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017297
17298 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017299 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017300
17301 netif_device_detach(netdev);
17302
17303 /* Clean up software state, even if MMIO is blocked */
17304 tg3_full_lock(tp, 0);
17305 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17306 tg3_full_unlock(tp);
17307
17308done:
17309 if (state == pci_channel_io_perm_failure)
17310 err = PCI_ERS_RESULT_DISCONNECT;
17311 else
17312 pci_disable_device(pdev);
17313
17314 rtnl_unlock();
17315
17316 return err;
17317}
17318
17319/**
17320 * tg3_io_slot_reset - called after the pci bus has been reset.
17321 * @pdev: Pointer to PCI device
17322 *
17323 * Restart the card from scratch, as if from a cold-boot.
17324 * At this point, the card has exprienced a hard reset,
17325 * followed by fixups by BIOS, and has its config space
17326 * set up identically to what it was at cold boot.
17327 */
17328static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17329{
17330 struct net_device *netdev = pci_get_drvdata(pdev);
17331 struct tg3 *tp = netdev_priv(netdev);
17332 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17333 int err;
17334
17335 rtnl_lock();
17336
17337 if (pci_enable_device(pdev)) {
17338 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17339 goto done;
17340 }
17341
17342 pci_set_master(pdev);
17343 pci_restore_state(pdev);
17344 pci_save_state(pdev);
17345
17346 if (!netif_running(netdev)) {
17347 rc = PCI_ERS_RESULT_RECOVERED;
17348 goto done;
17349 }
17350
17351 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017352 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017353 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017354
17355 rc = PCI_ERS_RESULT_RECOVERED;
17356
17357done:
17358 rtnl_unlock();
17359
17360 return rc;
17361}
17362
17363/**
17364 * tg3_io_resume - called when traffic can start flowing again.
17365 * @pdev: Pointer to PCI device
17366 *
17367 * This callback is called when the error recovery driver tells
17368 * us that its OK to resume normal operation.
17369 */
17370static void tg3_io_resume(struct pci_dev *pdev)
17371{
17372 struct net_device *netdev = pci_get_drvdata(pdev);
17373 struct tg3 *tp = netdev_priv(netdev);
17374 int err;
17375
17376 rtnl_lock();
17377
17378 if (!netif_running(netdev))
17379 goto done;
17380
17381 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017382 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017383 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017384 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017385 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017386 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17387 goto done;
17388 }
17389
17390 netif_device_attach(netdev);
17391
Matt Carlson21f76382012-02-22 12:35:21 +000017392 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017393
17394 tg3_netif_start(tp);
17395
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017396 tg3_full_unlock(tp);
17397
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017398 tg3_phy_start(tp);
17399
17400done:
17401 rtnl_unlock();
17402}
17403
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017404static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017405 .error_detected = tg3_io_error_detected,
17406 .slot_reset = tg3_io_slot_reset,
17407 .resume = tg3_io_resume
17408};
17409
Linus Torvalds1da177e2005-04-16 15:20:36 -070017410static struct pci_driver tg3_driver = {
17411 .name = DRV_MODULE_NAME,
17412 .id_table = tg3_pci_tbl,
17413 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017414 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017415 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017416 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017417};
17418
17419static int __init tg3_init(void)
17420{
Jeff Garzik29917622006-08-19 17:48:59 -040017421 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017422}
17423
17424static void __exit tg3_cleanup(void)
17425{
17426 pci_unregister_driver(&tg3_driver);
17427}
17428
17429module_init(tg3_init);
17430module_exit(tg3_cleanup);