blob: ce985729f3e6e30894641bfa283c336ba552b87f [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
Nithin Sujir7a28fde2013-04-09 08:48:10 +000097#define TG3_MIN_NUM 131
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Nithin Sujir7a28fde2013-04-09 08:48:10 +0000100#define DRV_MODULE_RELDATE "April 09, 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
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001877static u32 tg3_decode_flowctrl_1000T(u32 adv)
1878{
1879 u32 flowctrl = 0;
1880
1881 if (adv & ADVERTISE_PAUSE_CAP) {
1882 flowctrl |= FLOW_CTRL_RX;
1883 if (!(adv & ADVERTISE_PAUSE_ASYM))
1884 flowctrl |= FLOW_CTRL_TX;
1885 } else if (adv & ADVERTISE_PAUSE_ASYM)
1886 flowctrl |= FLOW_CTRL_TX;
1887
1888 return flowctrl;
1889}
1890
Matt Carlson95e28692008-05-25 23:44:14 -07001891static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1892{
1893 u16 miireg;
1894
Steve Glendinninge18ce342008-12-16 02:00:00 -08001895 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001896 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001897 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001898 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001899 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001900 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1901 else
1902 miireg = 0;
1903
1904 return miireg;
1905}
1906
Nithin Sujirfdad8de2013-04-09 08:48:08 +00001907static u32 tg3_decode_flowctrl_1000X(u32 adv)
1908{
1909 u32 flowctrl = 0;
1910
1911 if (adv & ADVERTISE_1000XPAUSE) {
1912 flowctrl |= FLOW_CTRL_RX;
1913 if (!(adv & ADVERTISE_1000XPSE_ASYM))
1914 flowctrl |= FLOW_CTRL_TX;
1915 } else if (adv & ADVERTISE_1000XPSE_ASYM)
1916 flowctrl |= FLOW_CTRL_TX;
1917
1918 return flowctrl;
1919}
1920
Matt Carlson95e28692008-05-25 23:44:14 -07001921static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1922{
1923 u8 cap = 0;
1924
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001925 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1926 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1927 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1928 if (lcladv & ADVERTISE_1000XPAUSE)
1929 cap = FLOW_CTRL_RX;
1930 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001931 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001932 }
1933
1934 return cap;
1935}
1936
Matt Carlsonf51f3562008-05-25 23:45:08 -07001937static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001938{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001939 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001940 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001941 u32 old_rx_mode = tp->rx_mode;
1942 u32 old_tx_mode = tp->tx_mode;
1943
Joe Perches63c3a662011-04-26 08:12:10 +00001944 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001945 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001946 else
1947 autoneg = tp->link_config.autoneg;
1948
Joe Perches63c3a662011-04-26 08:12:10 +00001949 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001950 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001951 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001952 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001953 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001954 } else
1955 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001956
Matt Carlsonf51f3562008-05-25 23:45:08 -07001957 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001958
Steve Glendinninge18ce342008-12-16 02:00:00 -08001959 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001960 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1961 else
1962 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1963
Matt Carlsonf51f3562008-05-25 23:45:08 -07001964 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001965 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001966
Steve Glendinninge18ce342008-12-16 02:00:00 -08001967 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001968 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1969 else
1970 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1971
Matt Carlsonf51f3562008-05-25 23:45:08 -07001972 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001973 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001974}
1975
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976static void tg3_adjust_link(struct net_device *dev)
1977{
1978 u8 oldflowctrl, linkmesg = 0;
1979 u32 mac_mode, lcl_adv, rmt_adv;
1980 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001981 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001982
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001983 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001984
1985 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1986 MAC_MODE_HALF_DUPLEX);
1987
1988 oldflowctrl = tp->link_config.active_flowctrl;
1989
1990 if (phydev->link) {
1991 lcl_adv = 0;
1992 rmt_adv = 0;
1993
1994 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1995 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001996 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001997 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001998 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001999 else
2000 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002001
2002 if (phydev->duplex == DUPLEX_HALF)
2003 mac_mode |= MAC_MODE_HALF_DUPLEX;
2004 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00002005 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006 tp->link_config.flowctrl);
2007
2008 if (phydev->pause)
2009 rmt_adv = LPA_PAUSE_CAP;
2010 if (phydev->asym_pause)
2011 rmt_adv |= LPA_PAUSE_ASYM;
2012 }
2013
2014 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
2015 } else
2016 mac_mode |= MAC_MODE_PORT_MODE_GMII;
2017
2018 if (mac_mode != tp->mac_mode) {
2019 tp->mac_mode = mac_mode;
2020 tw32_f(MAC_MODE, tp->mac_mode);
2021 udelay(40);
2022 }
2023
Joe Perches41535772013-02-16 11:20:04 +00002024 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08002025 if (phydev->speed == SPEED_10)
2026 tw32(MAC_MI_STAT,
2027 MAC_MI_STAT_10MBPS_MODE |
2028 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2029 else
2030 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2031 }
2032
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2034 tw32(MAC_TX_LENGTHS,
2035 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2036 (6 << TX_LENGTHS_IPG_SHIFT) |
2037 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2038 else
2039 tw32(MAC_TX_LENGTHS,
2040 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2041 (6 << TX_LENGTHS_IPG_SHIFT) |
2042 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2043
Matt Carlson34655ad2012-02-22 12:35:18 +00002044 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002045 phydev->speed != tp->link_config.active_speed ||
2046 phydev->duplex != tp->link_config.active_duplex ||
2047 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002048 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002049
Matt Carlson34655ad2012-02-22 12:35:18 +00002050 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002051 tp->link_config.active_speed = phydev->speed;
2052 tp->link_config.active_duplex = phydev->duplex;
2053
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002054 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002055
2056 if (linkmesg)
2057 tg3_link_report(tp);
2058}
2059
2060static int tg3_phy_init(struct tg3 *tp)
2061{
2062 struct phy_device *phydev;
2063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002064 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002065 return 0;
2066
2067 /* Bring the PHY back to a known state. */
2068 tg3_bmcr_reset(tp);
2069
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002070 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002071
2072 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002073 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2074 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002075 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002076 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002077 return PTR_ERR(phydev);
2078 }
2079
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002080 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002081 switch (phydev->interface) {
2082 case PHY_INTERFACE_MODE_GMII:
2083 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002084 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002085 phydev->supported &= (PHY_GBIT_FEATURES |
2086 SUPPORTED_Pause |
2087 SUPPORTED_Asym_Pause);
2088 break;
2089 }
2090 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002091 case PHY_INTERFACE_MODE_MII:
2092 phydev->supported &= (PHY_BASIC_FEATURES |
2093 SUPPORTED_Pause |
2094 SUPPORTED_Asym_Pause);
2095 break;
2096 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002097 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002098 return -EINVAL;
2099 }
2100
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002101 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002102
2103 phydev->advertising = phydev->supported;
2104
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002105 return 0;
2106}
2107
2108static void tg3_phy_start(struct tg3 *tp)
2109{
2110 struct phy_device *phydev;
2111
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002112 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002113 return;
2114
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002115 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002116
Matt Carlson80096062010-08-02 11:26:06 +00002117 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2118 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002119 phydev->speed = tp->link_config.speed;
2120 phydev->duplex = tp->link_config.duplex;
2121 phydev->autoneg = tp->link_config.autoneg;
2122 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002123 }
2124
2125 phy_start(phydev);
2126
2127 phy_start_aneg(phydev);
2128}
2129
2130static void tg3_phy_stop(struct tg3 *tp)
2131{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002132 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002133 return;
2134
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002135 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002136}
2137
2138static void tg3_phy_fini(struct tg3 *tp)
2139{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002140 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002141 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002142 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002143 }
2144}
2145
Matt Carlson941ec902011-08-19 13:58:23 +00002146static int tg3_phy_set_extloopbk(struct tg3 *tp)
2147{
2148 int err;
2149 u32 val;
2150
2151 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2152 return 0;
2153
2154 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2155 /* Cannot do read-modify-write on 5401 */
2156 err = tg3_phy_auxctl_write(tp,
2157 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2158 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2159 0x4c20);
2160 goto done;
2161 }
2162
2163 err = tg3_phy_auxctl_read(tp,
2164 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2165 if (err)
2166 return err;
2167
2168 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2169 err = tg3_phy_auxctl_write(tp,
2170 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2171
2172done:
2173 return err;
2174}
2175
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002176static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2177{
2178 u32 phytest;
2179
2180 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2181 u32 phy;
2182
2183 tg3_writephy(tp, MII_TG3_FET_TEST,
2184 phytest | MII_TG3_FET_SHADOW_EN);
2185 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2186 if (enable)
2187 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2188 else
2189 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2190 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2191 }
2192 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2193 }
2194}
2195
Matt Carlson6833c042008-11-21 17:18:59 -08002196static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2197{
2198 u32 reg;
2199
Joe Perches63c3a662011-04-26 08:12:10 +00002200 if (!tg3_flag(tp, 5705_PLUS) ||
2201 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002202 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002203 return;
2204
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002205 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002206 tg3_phy_fet_toggle_apd(tp, enable);
2207 return;
2208 }
2209
Matt Carlson6833c042008-11-21 17:18:59 -08002210 reg = MII_TG3_MISC_SHDW_WREN |
2211 MII_TG3_MISC_SHDW_SCR5_SEL |
2212 MII_TG3_MISC_SHDW_SCR5_LPED |
2213 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2214 MII_TG3_MISC_SHDW_SCR5_SDTL |
2215 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002216 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002217 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2218
2219 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2220
2221
2222 reg = MII_TG3_MISC_SHDW_WREN |
2223 MII_TG3_MISC_SHDW_APD_SEL |
2224 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2225 if (enable)
2226 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2227
2228 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2229}
2230
Joe Perches953c96e2013-04-09 10:18:14 +00002231static void tg3_phy_toggle_automdix(struct tg3 *tp, bool enable)
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002232{
2233 u32 phy;
2234
Joe Perches63c3a662011-04-26 08:12:10 +00002235 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002236 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002237 return;
2238
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002239 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002240 u32 ephy;
2241
Matt Carlson535ef6e2009-08-25 10:09:36 +00002242 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2243 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2244
2245 tg3_writephy(tp, MII_TG3_FET_TEST,
2246 ephy | MII_TG3_FET_SHADOW_EN);
2247 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002248 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002249 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002250 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002251 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2252 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002253 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002254 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002255 }
2256 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002257 int ret;
2258
2259 ret = tg3_phy_auxctl_read(tp,
2260 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2261 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002262 if (enable)
2263 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2264 else
2265 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002266 tg3_phy_auxctl_write(tp,
2267 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002268 }
2269 }
2270}
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272static void tg3_phy_set_wirespeed(struct tg3 *tp)
2273{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 u32 val;
2276
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002277 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return;
2279
Matt Carlson15ee95c2011-04-20 07:57:40 +00002280 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2281 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002282 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2283 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002286static void tg3_phy_apply_otp(struct tg3 *tp)
2287{
2288 u32 otp, phy;
2289
2290 if (!tp->phy_otp)
2291 return;
2292
2293 otp = tp->phy_otp;
2294
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002295 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002296 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002297
2298 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2299 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2300 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2301
2302 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2303 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2304 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2305
2306 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2307 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2308 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2309
2310 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2311 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2312
2313 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2314 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2315
2316 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2317 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2318 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2319
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002320 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002321}
2322
Joe Perches953c96e2013-04-09 10:18:14 +00002323static void tg3_phy_eee_adjust(struct tg3 *tp, bool current_link_up)
Matt Carlson52b02d02010-10-14 10:37:41 +00002324{
2325 u32 val;
2326
2327 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2328 return;
2329
2330 tp->setlpicnt = 0;
2331
2332 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
Joe Perches953c96e2013-04-09 10:18:14 +00002333 current_link_up &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002334 tp->link_config.active_duplex == DUPLEX_FULL &&
2335 (tp->link_config.active_speed == SPEED_100 ||
2336 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002337 u32 eeectl;
2338
2339 if (tp->link_config.active_speed == SPEED_1000)
2340 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2341 else
2342 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2343
2344 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2345
Matt Carlson3110f5f52010-12-06 08:28:50 +00002346 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2347 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002348
Matt Carlsonb0c59432011-05-19 12:12:48 +00002349 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2350 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002351 tp->setlpicnt = 2;
2352 }
2353
2354 if (!tp->setlpicnt) {
Joe Perches953c96e2013-04-09 10:18:14 +00002355 if (current_link_up &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002356 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002357 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002358 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002359 }
2360
Matt Carlson52b02d02010-10-14 10:37:41 +00002361 val = tr32(TG3_CPMU_EEE_MODE);
2362 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2363 }
2364}
2365
Matt Carlsonb0c59432011-05-19 12:12:48 +00002366static void tg3_phy_eee_enable(struct tg3 *tp)
2367{
2368 u32 val;
2369
2370 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002371 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2372 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002373 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002374 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002375 val = MII_TG3_DSP_TAP26_ALNOKO |
2376 MII_TG3_DSP_TAP26_RMRXSTO;
2377 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002378 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002379 }
2380
2381 val = tr32(TG3_CPMU_EEE_MODE);
2382 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385static int tg3_wait_macro_done(struct tg3 *tp)
2386{
2387 int limit = 100;
2388
2389 while (limit--) {
2390 u32 tmp32;
2391
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002392 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if ((tmp32 & 0x1000) == 0)
2394 break;
2395 }
2396 }
Roel Kluind4675b52009-02-12 16:33:27 -08002397 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return -EBUSY;
2399
2400 return 0;
2401}
2402
2403static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2404{
2405 static const u32 test_pat[4][6] = {
2406 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2407 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2408 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2409 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2410 };
2411 int chan;
2412
2413 for (chan = 0; chan < 4; chan++) {
2414 int i;
2415
2416 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2417 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002418 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 for (i = 0; i < 6; i++)
2421 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2422 test_pat[chan][i]);
2423
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002424 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (tg3_wait_macro_done(tp)) {
2426 *resetp = 1;
2427 return -EBUSY;
2428 }
2429
2430 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2431 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002432 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (tg3_wait_macro_done(tp)) {
2434 *resetp = 1;
2435 return -EBUSY;
2436 }
2437
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002438 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (tg3_wait_macro_done(tp)) {
2440 *resetp = 1;
2441 return -EBUSY;
2442 }
2443
2444 for (i = 0; i < 6; i += 2) {
2445 u32 low, high;
2446
2447 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2448 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2449 tg3_wait_macro_done(tp)) {
2450 *resetp = 1;
2451 return -EBUSY;
2452 }
2453 low &= 0x7fff;
2454 high &= 0x000f;
2455 if (low != test_pat[chan][i] ||
2456 high != test_pat[chan][i+1]) {
2457 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2458 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2459 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2460
2461 return -EBUSY;
2462 }
2463 }
2464 }
2465
2466 return 0;
2467}
2468
2469static int tg3_phy_reset_chanpat(struct tg3 *tp)
2470{
2471 int chan;
2472
2473 for (chan = 0; chan < 4; chan++) {
2474 int i;
2475
2476 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2477 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002478 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 for (i = 0; i < 6; i++)
2480 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002481 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (tg3_wait_macro_done(tp))
2483 return -EBUSY;
2484 }
2485
2486 return 0;
2487}
2488
2489static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2490{
2491 u32 reg32, phy9_orig;
2492 int retries, do_phy_reset, err;
2493
2494 retries = 10;
2495 do_phy_reset = 1;
2496 do {
2497 if (do_phy_reset) {
2498 err = tg3_bmcr_reset(tp);
2499 if (err)
2500 return err;
2501 do_phy_reset = 0;
2502 }
2503
2504 /* Disable transmitter and interrupt. */
2505 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2506 continue;
2507
2508 reg32 |= 0x3000;
2509 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2510
2511 /* Set full-duplex, 1000 mbps. */
2512 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002513 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002516 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 continue;
2518
Matt Carlson221c5632011-06-13 13:39:01 +00002519 tg3_writephy(tp, MII_CTRL1000,
2520 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002522 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002523 if (err)
2524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002527 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2530 if (!err)
2531 break;
2532 } while (--retries);
2533
2534 err = tg3_phy_reset_chanpat(tp);
2535 if (err)
2536 return err;
2537
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002538 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002541 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002543 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Matt Carlson221c5632011-06-13 13:39:01 +00002545 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2548 reg32 &= ~0x3000;
2549 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2550 } else if (!err)
2551 err = -EBUSY;
2552
2553 return err;
2554}
2555
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002556static void tg3_carrier_off(struct tg3 *tp)
2557{
2558 netif_carrier_off(tp->dev);
2559 tp->link_up = false;
2560}
2561
Nithin Sujirce20f162013-04-09 08:48:04 +00002562static void tg3_warn_mgmt_link_flap(struct tg3 *tp)
2563{
2564 if (tg3_flag(tp, ENABLE_ASF))
2565 netdev_warn(tp->dev,
2566 "Management side-band traffic will be interrupted during phy settings change\n");
2567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/* This will reset the tigon3 PHY if there is no valid
2570 * link unless the FORCE argument is non-zero.
2571 */
2572static int tg3_phy_reset(struct tg3 *tp)
2573{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002574 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 int err;
2576
Joe Perches41535772013-02-16 11:20:04 +00002577 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002578 val = tr32(GRC_MISC_CFG);
2579 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2580 udelay(40);
2581 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002582 err = tg3_readphy(tp, MII_BMSR, &val);
2583 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (err != 0)
2585 return -EBUSY;
2586
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002587 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002588 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002589 tg3_link_report(tp);
2590 }
2591
Joe Perches41535772013-02-16 11:20:04 +00002592 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2593 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2594 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 err = tg3_phy_reset_5703_4_5(tp);
2596 if (err)
2597 return err;
2598 goto out;
2599 }
2600
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002601 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002602 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2603 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002604 cpmuctrl = tr32(TG3_CPMU_CTRL);
2605 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2606 tw32(TG3_CPMU_CTRL,
2607 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2608 }
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 err = tg3_bmcr_reset(tp);
2611 if (err)
2612 return err;
2613
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002614 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002615 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2616 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002617
2618 tw32(TG3_CPMU_CTRL, cpmuctrl);
2619 }
2620
Joe Perches41535772013-02-16 11:20:04 +00002621 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2622 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002623 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2624 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2625 CPMU_LSPD_1000MB_MACCLK_12_5) {
2626 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2627 udelay(40);
2628 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2629 }
2630 }
2631
Joe Perches63c3a662011-04-26 08:12:10 +00002632 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002633 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002634 return 0;
2635
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002636 tg3_phy_apply_otp(tp);
2637
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002638 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002639 tg3_phy_toggle_apd(tp, true);
2640 else
2641 tg3_phy_toggle_apd(tp, false);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002644 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002645 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002646 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2647 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002648 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002650
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002651 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002652 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2653 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002655
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002656 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002657 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002658 tg3_phydsp_write(tp, 0x000a, 0x310b);
2659 tg3_phydsp_write(tp, 0x201f, 0x9506);
2660 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002661 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002662 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002663 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002664 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002665 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2666 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2667 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2668 tg3_writephy(tp, MII_TG3_TEST1,
2669 MII_TG3_TEST1_TRIM_EN | 0x4);
2670 } else
2671 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2672
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002673 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002674 }
Michael Chanc424cb22006-04-29 18:56:34 -07002675 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* Set Extended packet length bit (bit 14) on all chips that */
2678 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002679 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002681 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002682 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002684 err = tg3_phy_auxctl_read(tp,
2685 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2686 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002687 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2688 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690
2691 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2692 * jumbo frames transmission.
2693 */
Joe Perches63c3a662011-04-26 08:12:10 +00002694 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002695 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002696 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002697 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
Joe Perches41535772013-02-16 11:20:04 +00002700 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002701 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002702 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002703 }
2704
Joe Perches41535772013-02-16 11:20:04 +00002705 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002706 tg3_phydsp_write(tp, 0xffb, 0x4000);
2707
Joe Perches953c96e2013-04-09 10:18:14 +00002708 tg3_phy_toggle_automdix(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 tg3_phy_set_wirespeed(tp);
2710 return 0;
2711}
2712
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002713#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2714#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2715#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2716 TG3_GPIO_MSG_NEED_VAUX)
2717#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2718 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2719 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2720 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2721 (TG3_GPIO_MSG_DRVR_PRES << 12))
2722
2723#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2724 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2725 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2726 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2727 (TG3_GPIO_MSG_NEED_VAUX << 12))
2728
2729static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2730{
2731 u32 status, shift;
2732
Joe Perches41535772013-02-16 11:20:04 +00002733 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2734 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002735 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2736 else
2737 status = tr32(TG3_CPMU_DRV_STATUS);
2738
2739 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2740 status &= ~(TG3_GPIO_MSG_MASK << shift);
2741 status |= (newstat << shift);
2742
Joe Perches41535772013-02-16 11:20:04 +00002743 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2744 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002745 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2746 else
2747 tw32(TG3_CPMU_DRV_STATUS, status);
2748
2749 return status >> TG3_APE_GPIO_MSG_SHIFT;
2750}
2751
Matt Carlson520b2752011-06-13 13:39:02 +00002752static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2753{
2754 if (!tg3_flag(tp, IS_NIC))
2755 return 0;
2756
Joe Perches41535772013-02-16 11:20:04 +00002757 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2758 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2759 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002760 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2761 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002762
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002763 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2764
2765 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2766 TG3_GRC_LCLCTL_PWRSW_DELAY);
2767
2768 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2769 } else {
2770 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2771 TG3_GRC_LCLCTL_PWRSW_DELAY);
2772 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002773
Matt Carlson520b2752011-06-13 13:39:02 +00002774 return 0;
2775}
2776
2777static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2778{
2779 u32 grc_local_ctrl;
2780
2781 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002782 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2783 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002784 return;
2785
2786 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2787
2788 tw32_wait_f(GRC_LOCAL_CTRL,
2789 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2790 TG3_GRC_LCLCTL_PWRSW_DELAY);
2791
2792 tw32_wait_f(GRC_LOCAL_CTRL,
2793 grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 tw32_wait_f(GRC_LOCAL_CTRL,
2797 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2798 TG3_GRC_LCLCTL_PWRSW_DELAY);
2799}
2800
2801static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2802{
2803 if (!tg3_flag(tp, IS_NIC))
2804 return;
2805
Joe Perches41535772013-02-16 11:20:04 +00002806 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2807 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002808 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2809 (GRC_LCLCTRL_GPIO_OE0 |
2810 GRC_LCLCTRL_GPIO_OE1 |
2811 GRC_LCLCTRL_GPIO_OE2 |
2812 GRC_LCLCTRL_GPIO_OUTPUT0 |
2813 GRC_LCLCTRL_GPIO_OUTPUT1),
2814 TG3_GRC_LCLCTL_PWRSW_DELAY);
2815 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2816 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2817 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2818 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2819 GRC_LCLCTRL_GPIO_OE1 |
2820 GRC_LCLCTRL_GPIO_OE2 |
2821 GRC_LCLCTRL_GPIO_OUTPUT0 |
2822 GRC_LCLCTRL_GPIO_OUTPUT1 |
2823 tp->grc_local_ctrl;
2824 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2825 TG3_GRC_LCLCTL_PWRSW_DELAY);
2826
2827 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2828 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2829 TG3_GRC_LCLCTL_PWRSW_DELAY);
2830
2831 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2832 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2833 TG3_GRC_LCLCTL_PWRSW_DELAY);
2834 } else {
2835 u32 no_gpio2;
2836 u32 grc_local_ctrl = 0;
2837
2838 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002839 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002840 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2841 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2842 grc_local_ctrl,
2843 TG3_GRC_LCLCTL_PWRSW_DELAY);
2844 }
2845
2846 /* On 5753 and variants, GPIO2 cannot be used. */
2847 no_gpio2 = tp->nic_sram_data_cfg &
2848 NIC_SRAM_DATA_CFG_NO_GPIO2;
2849
2850 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2851 GRC_LCLCTRL_GPIO_OE1 |
2852 GRC_LCLCTRL_GPIO_OE2 |
2853 GRC_LCLCTRL_GPIO_OUTPUT1 |
2854 GRC_LCLCTRL_GPIO_OUTPUT2;
2855 if (no_gpio2) {
2856 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2857 GRC_LCLCTRL_GPIO_OUTPUT2);
2858 }
2859 tw32_wait_f(GRC_LOCAL_CTRL,
2860 tp->grc_local_ctrl | grc_local_ctrl,
2861 TG3_GRC_LCLCTL_PWRSW_DELAY);
2862
2863 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2864
2865 tw32_wait_f(GRC_LOCAL_CTRL,
2866 tp->grc_local_ctrl | grc_local_ctrl,
2867 TG3_GRC_LCLCTL_PWRSW_DELAY);
2868
2869 if (!no_gpio2) {
2870 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2871 tw32_wait_f(GRC_LOCAL_CTRL,
2872 tp->grc_local_ctrl | grc_local_ctrl,
2873 TG3_GRC_LCLCTL_PWRSW_DELAY);
2874 }
2875 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002876}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002877
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002878static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002879{
2880 u32 msg = 0;
2881
2882 /* Serialize power state transitions */
2883 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2884 return;
2885
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002886 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002887 msg = TG3_GPIO_MSG_NEED_VAUX;
2888
2889 msg = tg3_set_function_status(tp, msg);
2890
2891 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2892 goto done;
2893
2894 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2895 tg3_pwrsrc_switch_to_vaux(tp);
2896 else
2897 tg3_pwrsrc_die_with_vmain(tp);
2898
2899done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002900 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002901}
2902
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002903static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Matt Carlson683644b2011-03-09 16:58:23 +00002905 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Matt Carlson334355a2010-01-20 16:58:10 +00002907 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002908 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return;
2910
Joe Perches41535772013-02-16 11:20:04 +00002911 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2912 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2913 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002914 tg3_frob_aux_power_5717(tp, include_wol ?
2915 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002916 return;
2917 }
2918
2919 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002920 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002922 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002923
Michael Chanbc1c7562006-03-20 17:48:03 -08002924 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002925 if (dev_peer) {
2926 struct tg3 *tp_peer = netdev_priv(dev_peer);
2927
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002929 return;
2930
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002931 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002932 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002933 need_vaux = true;
2934 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002937 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2938 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002939 need_vaux = true;
2940
Matt Carlson520b2752011-06-13 13:39:02 +00002941 if (need_vaux)
2942 tg3_pwrsrc_switch_to_vaux(tp);
2943 else
2944 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002947static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2948{
2949 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2950 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002951 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002952 if (speed != SPEED_10)
2953 return 1;
2954 } else if (speed == SPEED_10)
2955 return 1;
2956
2957 return 0;
2958}
2959
Matt Carlson0a459aa2008-11-03 16:54:15 -08002960static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002961{
Matt Carlsonce057f02007-11-12 21:08:03 -08002962 u32 val;
2963
Nithin Sujir942d1af2013-04-09 08:48:07 +00002964 if (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)
2965 return;
2966
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002967 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00002968 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08002969 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2970 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2971
2972 sg_dig_ctrl |=
2973 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2974 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2975 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2976 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002977 return;
Michael Chan51297242007-02-13 12:17:57 -08002978 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002979
Joe Perches41535772013-02-16 11:20:04 +00002980 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002981 tg3_bmcr_reset(tp);
2982 val = tr32(GRC_MISC_CFG);
2983 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2984 udelay(40);
2985 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002986 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002987 u32 phytest;
2988 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2989 u32 phy;
2990
2991 tg3_writephy(tp, MII_ADVERTISE, 0);
2992 tg3_writephy(tp, MII_BMCR,
2993 BMCR_ANENABLE | BMCR_ANRESTART);
2994
2995 tg3_writephy(tp, MII_TG3_FET_TEST,
2996 phytest | MII_TG3_FET_SHADOW_EN);
2997 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2998 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2999 tg3_writephy(tp,
3000 MII_TG3_FET_SHDW_AUXMODE4,
3001 phy);
3002 }
3003 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
3004 }
3005 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003006 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07003007 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3008 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003009
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003010 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3011 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
3012 MII_TG3_AUXCTL_PCTL_VREG_11V;
3013 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07003014 }
Michael Chan3f7045c2006-09-27 16:02:29 -07003015
Michael Chan15c3b692006-03-22 01:06:52 -08003016 /* The PHY should not be powered down on some chips because
3017 * of bugs.
3018 */
Joe Perches41535772013-02-16 11:20:04 +00003019 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3020 tg3_asic_rev(tp) == ASIC_REV_5704 ||
3021 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003022 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00003023 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00003024 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08003025 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08003026
Joe Perches41535772013-02-16 11:20:04 +00003027 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
3028 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08003029 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
3030 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
3031 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
3032 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
3033 }
3034
Michael Chan15c3b692006-03-22 01:06:52 -08003035 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
3036}
3037
Matt Carlson3f007892008-11-03 16:51:36 -08003038/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003039static int tg3_nvram_lock(struct tg3 *tp)
3040{
Joe Perches63c3a662011-04-26 08:12:10 +00003041 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003042 int i;
3043
3044 if (tp->nvram_lock_cnt == 0) {
3045 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3046 for (i = 0; i < 8000; i++) {
3047 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3048 break;
3049 udelay(20);
3050 }
3051 if (i == 8000) {
3052 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3053 return -ENODEV;
3054 }
3055 }
3056 tp->nvram_lock_cnt++;
3057 }
3058 return 0;
3059}
3060
3061/* tp->lock is held. */
3062static void tg3_nvram_unlock(struct tg3 *tp)
3063{
Joe Perches63c3a662011-04-26 08:12:10 +00003064 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003065 if (tp->nvram_lock_cnt > 0)
3066 tp->nvram_lock_cnt--;
3067 if (tp->nvram_lock_cnt == 0)
3068 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3069 }
3070}
3071
3072/* tp->lock is held. */
3073static void tg3_enable_nvram_access(struct tg3 *tp)
3074{
Joe Perches63c3a662011-04-26 08:12:10 +00003075 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003076 u32 nvaccess = tr32(NVRAM_ACCESS);
3077
3078 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3079 }
3080}
3081
3082/* tp->lock is held. */
3083static void tg3_disable_nvram_access(struct tg3 *tp)
3084{
Joe Perches63c3a662011-04-26 08:12:10 +00003085 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003086 u32 nvaccess = tr32(NVRAM_ACCESS);
3087
3088 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3089 }
3090}
3091
3092static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3093 u32 offset, u32 *val)
3094{
3095 u32 tmp;
3096 int i;
3097
3098 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3099 return -EINVAL;
3100
3101 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3102 EEPROM_ADDR_DEVID_MASK |
3103 EEPROM_ADDR_READ);
3104 tw32(GRC_EEPROM_ADDR,
3105 tmp |
3106 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3107 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3108 EEPROM_ADDR_ADDR_MASK) |
3109 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3110
3111 for (i = 0; i < 1000; i++) {
3112 tmp = tr32(GRC_EEPROM_ADDR);
3113
3114 if (tmp & EEPROM_ADDR_COMPLETE)
3115 break;
3116 msleep(1);
3117 }
3118 if (!(tmp & EEPROM_ADDR_COMPLETE))
3119 return -EBUSY;
3120
Matt Carlson62cedd12009-04-20 14:52:29 -07003121 tmp = tr32(GRC_EEPROM_DATA);
3122
3123 /*
3124 * The data will always be opposite the native endian
3125 * format. Perform a blind byteswap to compensate.
3126 */
3127 *val = swab32(tmp);
3128
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003129 return 0;
3130}
3131
3132#define NVRAM_CMD_TIMEOUT 10000
3133
3134static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3135{
3136 int i;
3137
3138 tw32(NVRAM_CMD, nvram_cmd);
3139 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3140 udelay(10);
3141 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3142 udelay(10);
3143 break;
3144 }
3145 }
3146
3147 if (i == NVRAM_CMD_TIMEOUT)
3148 return -EBUSY;
3149
3150 return 0;
3151}
3152
3153static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3154{
Joe Perches63c3a662011-04-26 08:12:10 +00003155 if (tg3_flag(tp, NVRAM) &&
3156 tg3_flag(tp, NVRAM_BUFFERED) &&
3157 tg3_flag(tp, FLASH) &&
3158 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003159 (tp->nvram_jedecnum == JEDEC_ATMEL))
3160
3161 addr = ((addr / tp->nvram_pagesize) <<
3162 ATMEL_AT45DB0X1B_PAGE_POS) +
3163 (addr % tp->nvram_pagesize);
3164
3165 return addr;
3166}
3167
3168static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3169{
Joe Perches63c3a662011-04-26 08:12:10 +00003170 if (tg3_flag(tp, NVRAM) &&
3171 tg3_flag(tp, NVRAM_BUFFERED) &&
3172 tg3_flag(tp, FLASH) &&
3173 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003174 (tp->nvram_jedecnum == JEDEC_ATMEL))
3175
3176 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3177 tp->nvram_pagesize) +
3178 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3179
3180 return addr;
3181}
3182
Matt Carlsone4f34112009-02-25 14:25:00 +00003183/* NOTE: Data read in from NVRAM is byteswapped according to
3184 * the byteswapping settings for all other register accesses.
3185 * tg3 devices are BE devices, so on a BE machine, the data
3186 * returned will be exactly as it is seen in NVRAM. On a LE
3187 * machine, the 32-bit value will be byteswapped.
3188 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003189static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3190{
3191 int ret;
3192
Joe Perches63c3a662011-04-26 08:12:10 +00003193 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003194 return tg3_nvram_read_using_eeprom(tp, offset, val);
3195
3196 offset = tg3_nvram_phys_addr(tp, offset);
3197
3198 if (offset > NVRAM_ADDR_MSK)
3199 return -EINVAL;
3200
3201 ret = tg3_nvram_lock(tp);
3202 if (ret)
3203 return ret;
3204
3205 tg3_enable_nvram_access(tp);
3206
3207 tw32(NVRAM_ADDR, offset);
3208 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3209 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3210
3211 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003212 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003213
3214 tg3_disable_nvram_access(tp);
3215
3216 tg3_nvram_unlock(tp);
3217
3218 return ret;
3219}
3220
Matt Carlsona9dc5292009-02-25 14:25:30 +00003221/* Ensures NVRAM data is in bytestream format. */
3222static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003223{
3224 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003225 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003227 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003228 return res;
3229}
3230
Matt Carlsondbe9b922012-02-13 10:20:09 +00003231static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3232 u32 offset, u32 len, u8 *buf)
3233{
3234 int i, j, rc = 0;
3235 u32 val;
3236
3237 for (i = 0; i < len; i += 4) {
3238 u32 addr;
3239 __be32 data;
3240
3241 addr = offset + i;
3242
3243 memcpy(&data, buf + i, 4);
3244
3245 /*
3246 * The SEEPROM interface expects the data to always be opposite
3247 * the native endian format. We accomplish this by reversing
3248 * all the operations that would have been performed on the
3249 * data from a call to tg3_nvram_read_be32().
3250 */
3251 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3252
3253 val = tr32(GRC_EEPROM_ADDR);
3254 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3255
3256 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3257 EEPROM_ADDR_READ);
3258 tw32(GRC_EEPROM_ADDR, val |
3259 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3260 (addr & EEPROM_ADDR_ADDR_MASK) |
3261 EEPROM_ADDR_START |
3262 EEPROM_ADDR_WRITE);
3263
3264 for (j = 0; j < 1000; j++) {
3265 val = tr32(GRC_EEPROM_ADDR);
3266
3267 if (val & EEPROM_ADDR_COMPLETE)
3268 break;
3269 msleep(1);
3270 }
3271 if (!(val & EEPROM_ADDR_COMPLETE)) {
3272 rc = -EBUSY;
3273 break;
3274 }
3275 }
3276
3277 return rc;
3278}
3279
3280/* offset and length are dword aligned */
3281static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3282 u8 *buf)
3283{
3284 int ret = 0;
3285 u32 pagesize = tp->nvram_pagesize;
3286 u32 pagemask = pagesize - 1;
3287 u32 nvram_cmd;
3288 u8 *tmp;
3289
3290 tmp = kmalloc(pagesize, GFP_KERNEL);
3291 if (tmp == NULL)
3292 return -ENOMEM;
3293
3294 while (len) {
3295 int j;
3296 u32 phy_addr, page_off, size;
3297
3298 phy_addr = offset & ~pagemask;
3299
3300 for (j = 0; j < pagesize; j += 4) {
3301 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3302 (__be32 *) (tmp + j));
3303 if (ret)
3304 break;
3305 }
3306 if (ret)
3307 break;
3308
3309 page_off = offset & pagemask;
3310 size = pagesize;
3311 if (len < size)
3312 size = len;
3313
3314 len -= size;
3315
3316 memcpy(tmp + page_off, buf, size);
3317
3318 offset = offset + (pagesize - page_off);
3319
3320 tg3_enable_nvram_access(tp);
3321
3322 /*
3323 * Before we can erase the flash page, we need
3324 * to issue a special "write enable" command.
3325 */
3326 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3327
3328 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3329 break;
3330
3331 /* Erase the target page */
3332 tw32(NVRAM_ADDR, phy_addr);
3333
3334 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3335 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3336
3337 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3338 break;
3339
3340 /* Issue another write enable to start the write. */
3341 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3342
3343 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3344 break;
3345
3346 for (j = 0; j < pagesize; j += 4) {
3347 __be32 data;
3348
3349 data = *((__be32 *) (tmp + j));
3350
3351 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3352
3353 tw32(NVRAM_ADDR, phy_addr + j);
3354
3355 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3356 NVRAM_CMD_WR;
3357
3358 if (j == 0)
3359 nvram_cmd |= NVRAM_CMD_FIRST;
3360 else if (j == (pagesize - 4))
3361 nvram_cmd |= NVRAM_CMD_LAST;
3362
3363 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3364 if (ret)
3365 break;
3366 }
3367 if (ret)
3368 break;
3369 }
3370
3371 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3372 tg3_nvram_exec_cmd(tp, nvram_cmd);
3373
3374 kfree(tmp);
3375
3376 return ret;
3377}
3378
3379/* offset and length are dword aligned */
3380static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3381 u8 *buf)
3382{
3383 int i, ret = 0;
3384
3385 for (i = 0; i < len; i += 4, offset += 4) {
3386 u32 page_off, phy_addr, nvram_cmd;
3387 __be32 data;
3388
3389 memcpy(&data, buf + i, 4);
3390 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3391
3392 page_off = offset % tp->nvram_pagesize;
3393
3394 phy_addr = tg3_nvram_phys_addr(tp, offset);
3395
Matt Carlsondbe9b922012-02-13 10:20:09 +00003396 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3397
3398 if (page_off == 0 || i == 0)
3399 nvram_cmd |= NVRAM_CMD_FIRST;
3400 if (page_off == (tp->nvram_pagesize - 4))
3401 nvram_cmd |= NVRAM_CMD_LAST;
3402
3403 if (i == (len - 4))
3404 nvram_cmd |= NVRAM_CMD_LAST;
3405
Matt Carlson42278222012-02-13 15:20:11 +00003406 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3407 !tg3_flag(tp, FLASH) ||
3408 !tg3_flag(tp, 57765_PLUS))
3409 tw32(NVRAM_ADDR, phy_addr);
3410
Joe Perches41535772013-02-16 11:20:04 +00003411 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003412 !tg3_flag(tp, 5755_PLUS) &&
3413 (tp->nvram_jedecnum == JEDEC_ST) &&
3414 (nvram_cmd & NVRAM_CMD_FIRST)) {
3415 u32 cmd;
3416
3417 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3418 ret = tg3_nvram_exec_cmd(tp, cmd);
3419 if (ret)
3420 break;
3421 }
3422 if (!tg3_flag(tp, FLASH)) {
3423 /* We always do complete word writes to eeprom. */
3424 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3425 }
3426
3427 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3428 if (ret)
3429 break;
3430 }
3431 return ret;
3432}
3433
3434/* offset and length are dword aligned */
3435static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3436{
3437 int ret;
3438
3439 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3440 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3441 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3442 udelay(40);
3443 }
3444
3445 if (!tg3_flag(tp, NVRAM)) {
3446 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3447 } else {
3448 u32 grc_mode;
3449
3450 ret = tg3_nvram_lock(tp);
3451 if (ret)
3452 return ret;
3453
3454 tg3_enable_nvram_access(tp);
3455 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3456 tw32(NVRAM_WRITE1, 0x406);
3457
3458 grc_mode = tr32(GRC_MODE);
3459 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3460
3461 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3462 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3463 buf);
3464 } else {
3465 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3466 buf);
3467 }
3468
3469 grc_mode = tr32(GRC_MODE);
3470 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3471
3472 tg3_disable_nvram_access(tp);
3473 tg3_nvram_unlock(tp);
3474 }
3475
3476 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3477 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3478 udelay(40);
3479 }
3480
3481 return ret;
3482}
3483
Matt Carlson997b4f12011-08-31 11:44:53 +00003484#define RX_CPU_SCRATCH_BASE 0x30000
3485#define RX_CPU_SCRATCH_SIZE 0x04000
3486#define TX_CPU_SCRATCH_BASE 0x34000
3487#define TX_CPU_SCRATCH_SIZE 0x04000
3488
3489/* tp->lock is held. */
Nithin Sujir837c45b2013-03-06 17:02:30 +00003490static int tg3_pause_cpu(struct tg3 *tp, u32 cpu_base)
Matt Carlson997b4f12011-08-31 11:44:53 +00003491{
3492 int i;
Nithin Sujir837c45b2013-03-06 17:02:30 +00003493 const int iters = 10000;
Matt Carlson997b4f12011-08-31 11:44:53 +00003494
Nithin Sujir837c45b2013-03-06 17:02:30 +00003495 for (i = 0; i < iters; i++) {
3496 tw32(cpu_base + CPU_STATE, 0xffffffff);
3497 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3498 if (tr32(cpu_base + CPU_MODE) & CPU_MODE_HALT)
3499 break;
3500 }
3501
3502 return (i == iters) ? -EBUSY : 0;
3503}
3504
3505/* tp->lock is held. */
3506static int tg3_rxcpu_pause(struct tg3 *tp)
3507{
3508 int rc = tg3_pause_cpu(tp, RX_CPU_BASE);
3509
3510 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3511 tw32_f(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3512 udelay(10);
3513
3514 return rc;
3515}
3516
3517/* tp->lock is held. */
3518static int tg3_txcpu_pause(struct tg3 *tp)
3519{
3520 return tg3_pause_cpu(tp, TX_CPU_BASE);
3521}
3522
3523/* tp->lock is held. */
3524static void tg3_resume_cpu(struct tg3 *tp, u32 cpu_base)
3525{
3526 tw32(cpu_base + CPU_STATE, 0xffffffff);
3527 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3528}
3529
3530/* tp->lock is held. */
3531static void tg3_rxcpu_resume(struct tg3 *tp)
3532{
3533 tg3_resume_cpu(tp, RX_CPU_BASE);
3534}
3535
3536/* tp->lock is held. */
3537static int tg3_halt_cpu(struct tg3 *tp, u32 cpu_base)
3538{
3539 int rc;
3540
3541 BUG_ON(cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Matt Carlson997b4f12011-08-31 11:44:53 +00003542
Joe Perches41535772013-02-16 11:20:04 +00003543 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003544 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3545
3546 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3547 return 0;
3548 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003549 if (cpu_base == RX_CPU_BASE) {
3550 rc = tg3_rxcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003551 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003552 /*
3553 * There is only an Rx CPU for the 5750 derivative in the
3554 * BCM4785.
3555 */
3556 if (tg3_flag(tp, IS_SSB_CORE))
3557 return 0;
3558
Nithin Sujir837c45b2013-03-06 17:02:30 +00003559 rc = tg3_txcpu_pause(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003560 }
3561
Nithin Sujir837c45b2013-03-06 17:02:30 +00003562 if (rc) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003563 netdev_err(tp->dev, "%s timed out, %s CPU\n",
Nithin Sujir837c45b2013-03-06 17:02:30 +00003564 __func__, cpu_base == RX_CPU_BASE ? "RX" : "TX");
Matt Carlson997b4f12011-08-31 11:44:53 +00003565 return -ENODEV;
3566 }
3567
3568 /* Clear firmware's nvram arbitration. */
3569 if (tg3_flag(tp, NVRAM))
3570 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3571 return 0;
3572}
3573
Nithin Sujir31f11a92013-03-06 17:02:33 +00003574static int tg3_fw_data_len(struct tg3 *tp,
3575 const struct tg3_firmware_hdr *fw_hdr)
3576{
3577 int fw_len;
3578
3579 /* Non fragmented firmware have one firmware header followed by a
3580 * contiguous chunk of data to be written. The length field in that
3581 * header is not the length of data to be written but the complete
3582 * length of the bss. The data length is determined based on
3583 * tp->fw->size minus headers.
3584 *
3585 * Fragmented firmware have a main header followed by multiple
3586 * fragments. Each fragment is identical to non fragmented firmware
3587 * with a firmware header followed by a contiguous chunk of data. In
3588 * the main header, the length field is unused and set to 0xffffffff.
3589 * In each fragment header the length is the entire size of that
3590 * fragment i.e. fragment data + header length. Data length is
3591 * therefore length field in the header minus TG3_FW_HDR_LEN.
3592 */
3593 if (tp->fw_len == 0xffffffff)
3594 fw_len = be32_to_cpu(fw_hdr->len);
3595 else
3596 fw_len = tp->fw->size;
3597
3598 return (fw_len - TG3_FW_HDR_LEN) / sizeof(u32);
3599}
3600
Matt Carlson997b4f12011-08-31 11:44:53 +00003601/* tp->lock is held. */
3602static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3603 u32 cpu_scratch_base, int cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003604 const struct tg3_firmware_hdr *fw_hdr)
Matt Carlson997b4f12011-08-31 11:44:53 +00003605{
Nithin Sujirc4dab502013-03-06 17:02:34 +00003606 int err, i;
Matt Carlson997b4f12011-08-31 11:44:53 +00003607 void (*write_op)(struct tg3 *, u32, u32);
Nithin Sujir31f11a92013-03-06 17:02:33 +00003608 int total_len = tp->fw->size;
Matt Carlson997b4f12011-08-31 11:44:53 +00003609
3610 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3611 netdev_err(tp->dev,
3612 "%s: Trying to load TX cpu firmware which is 5705\n",
3613 __func__);
3614 return -EINVAL;
3615 }
3616
Nithin Sujirc4dab502013-03-06 17:02:34 +00003617 if (tg3_flag(tp, 5705_PLUS) && tg3_asic_rev(tp) != ASIC_REV_57766)
Matt Carlson997b4f12011-08-31 11:44:53 +00003618 write_op = tg3_write_mem;
3619 else
3620 write_op = tg3_write_indirect_reg32;
3621
Nithin Sujirc4dab502013-03-06 17:02:34 +00003622 if (tg3_asic_rev(tp) != ASIC_REV_57766) {
3623 /* It is possible that bootcode is still loading at this point.
3624 * Get the nvram lock first before halting the cpu.
3625 */
3626 int lock_err = tg3_nvram_lock(tp);
3627 err = tg3_halt_cpu(tp, cpu_base);
3628 if (!lock_err)
3629 tg3_nvram_unlock(tp);
3630 if (err)
3631 goto out;
Matt Carlson997b4f12011-08-31 11:44:53 +00003632
Nithin Sujirc4dab502013-03-06 17:02:34 +00003633 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3634 write_op(tp, cpu_scratch_base + i, 0);
3635 tw32(cpu_base + CPU_STATE, 0xffffffff);
3636 tw32(cpu_base + CPU_MODE,
3637 tr32(cpu_base + CPU_MODE) | CPU_MODE_HALT);
3638 } else {
3639 /* Subtract additional main header for fragmented firmware and
3640 * advance to the first fragment
3641 */
3642 total_len -= TG3_FW_HDR_LEN;
3643 fw_hdr++;
3644 }
Nithin Sujir77997ea2013-03-06 17:02:32 +00003645
Nithin Sujir31f11a92013-03-06 17:02:33 +00003646 do {
3647 u32 *fw_data = (u32 *)(fw_hdr + 1);
3648 for (i = 0; i < tg3_fw_data_len(tp, fw_hdr); i++)
3649 write_op(tp, cpu_scratch_base +
3650 (be32_to_cpu(fw_hdr->base_addr) & 0xffff) +
3651 (i * sizeof(u32)),
3652 be32_to_cpu(fw_data[i]));
3653
3654 total_len -= be32_to_cpu(fw_hdr->len);
3655
3656 /* Advance to next fragment */
3657 fw_hdr = (struct tg3_firmware_hdr *)
3658 ((void *)fw_hdr + be32_to_cpu(fw_hdr->len));
3659 } while (total_len > 0);
Matt Carlson997b4f12011-08-31 11:44:53 +00003660
3661 err = 0;
3662
3663out:
3664 return err;
3665}
3666
3667/* tp->lock is held. */
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003668static int tg3_pause_cpu_and_set_pc(struct tg3 *tp, u32 cpu_base, u32 pc)
3669{
3670 int i;
3671 const int iters = 5;
3672
3673 tw32(cpu_base + CPU_STATE, 0xffffffff);
3674 tw32_f(cpu_base + CPU_PC, pc);
3675
3676 for (i = 0; i < iters; i++) {
3677 if (tr32(cpu_base + CPU_PC) == pc)
3678 break;
3679 tw32(cpu_base + CPU_STATE, 0xffffffff);
3680 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3681 tw32_f(cpu_base + CPU_PC, pc);
3682 udelay(1000);
3683 }
3684
3685 return (i == iters) ? -EBUSY : 0;
3686}
3687
3688/* tp->lock is held. */
Matt Carlson997b4f12011-08-31 11:44:53 +00003689static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3690{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003691 const struct tg3_firmware_hdr *fw_hdr;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003692 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003693
Nithin Sujir77997ea2013-03-06 17:02:32 +00003694 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003695
3696 /* Firmware blob starts with version numbers, followed by
3697 start address and length. We are setting complete length.
3698 length = end_address_of_bss - start_address_of_text.
3699 Remainder is the blob to be loaded contiguously
3700 from start address. */
3701
Matt Carlson997b4f12011-08-31 11:44:53 +00003702 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3703 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003704 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003705 if (err)
3706 return err;
3707
3708 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3709 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003710 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003711 if (err)
3712 return err;
3713
3714 /* Now startup only the RX cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003715 err = tg3_pause_cpu_and_set_pc(tp, RX_CPU_BASE,
3716 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003717 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003718 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3719 "should be %08x\n", __func__,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003720 tr32(RX_CPU_BASE + CPU_PC),
3721 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003722 return -ENODEV;
3723 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003724
3725 tg3_rxcpu_resume(tp);
Matt Carlson997b4f12011-08-31 11:44:53 +00003726
3727 return 0;
3728}
3729
Nithin Sujirc4dab502013-03-06 17:02:34 +00003730static int tg3_validate_rxcpu_state(struct tg3 *tp)
3731{
3732 const int iters = 1000;
3733 int i;
3734 u32 val;
3735
3736 /* Wait for boot code to complete initialization and enter service
3737 * loop. It is then safe to download service patches
3738 */
3739 for (i = 0; i < iters; i++) {
3740 if (tr32(RX_CPU_HWBKPT) == TG3_SBROM_IN_SERVICE_LOOP)
3741 break;
3742
3743 udelay(10);
3744 }
3745
3746 if (i == iters) {
3747 netdev_err(tp->dev, "Boot code not ready for service patches\n");
3748 return -EBUSY;
3749 }
3750
3751 val = tg3_read_indirect_reg32(tp, TG3_57766_FW_HANDSHAKE);
3752 if (val & 0xff) {
3753 netdev_warn(tp->dev,
3754 "Other patches exist. Not downloading EEE patch\n");
3755 return -EEXIST;
3756 }
3757
3758 return 0;
3759}
3760
3761/* tp->lock is held. */
3762static void tg3_load_57766_firmware(struct tg3 *tp)
3763{
3764 struct tg3_firmware_hdr *fw_hdr;
3765
3766 if (!tg3_flag(tp, NO_NVRAM))
3767 return;
3768
3769 if (tg3_validate_rxcpu_state(tp))
3770 return;
3771
3772 if (!tp->fw)
3773 return;
3774
3775 /* This firmware blob has a different format than older firmware
3776 * releases as given below. The main difference is we have fragmented
3777 * data to be written to non-contiguous locations.
3778 *
3779 * In the beginning we have a firmware header identical to other
3780 * firmware which consists of version, base addr and length. The length
3781 * here is unused and set to 0xffffffff.
3782 *
3783 * This is followed by a series of firmware fragments which are
3784 * individually identical to previous firmware. i.e. they have the
3785 * firmware header and followed by data for that fragment. The version
3786 * field of the individual fragment header is unused.
3787 */
3788
3789 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
3790 if (be32_to_cpu(fw_hdr->base_addr) != TG3_57766_FW_BASE_ADDR)
3791 return;
3792
3793 if (tg3_rxcpu_pause(tp))
3794 return;
3795
3796 /* tg3_load_firmware_cpu() will always succeed for the 57766 */
3797 tg3_load_firmware_cpu(tp, 0, TG3_57766_FW_BASE_ADDR, 0, fw_hdr);
3798
3799 tg3_rxcpu_resume(tp);
3800}
3801
Matt Carlson997b4f12011-08-31 11:44:53 +00003802/* tp->lock is held. */
3803static int tg3_load_tso_firmware(struct tg3 *tp)
3804{
Nithin Sujir77997ea2013-03-06 17:02:32 +00003805 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson997b4f12011-08-31 11:44:53 +00003806 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003807 int err;
Matt Carlson997b4f12011-08-31 11:44:53 +00003808
Matt Carlson1caf13e2013-03-06 17:02:29 +00003809 if (!tg3_flag(tp, FW_TSO))
Matt Carlson997b4f12011-08-31 11:44:53 +00003810 return 0;
3811
Nithin Sujir77997ea2013-03-06 17:02:32 +00003812 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson997b4f12011-08-31 11:44:53 +00003813
3814 /* Firmware blob starts with version numbers, followed by
3815 start address and length. We are setting complete length.
3816 length = end_address_of_bss - start_address_of_text.
3817 Remainder is the blob to be loaded contiguously
3818 from start address. */
3819
Matt Carlson997b4f12011-08-31 11:44:53 +00003820 cpu_scratch_size = tp->fw_len;
Matt Carlson997b4f12011-08-31 11:44:53 +00003821
Joe Perches41535772013-02-16 11:20:04 +00003822 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003823 cpu_base = RX_CPU_BASE;
3824 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3825 } else {
3826 cpu_base = TX_CPU_BASE;
3827 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3828 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3829 }
3830
3831 err = tg3_load_firmware_cpu(tp, cpu_base,
3832 cpu_scratch_base, cpu_scratch_size,
Nithin Sujir77997ea2013-03-06 17:02:32 +00003833 fw_hdr);
Matt Carlson997b4f12011-08-31 11:44:53 +00003834 if (err)
3835 return err;
3836
3837 /* Now startup the cpu. */
Nithin Sujir77997ea2013-03-06 17:02:32 +00003838 err = tg3_pause_cpu_and_set_pc(tp, cpu_base,
3839 be32_to_cpu(fw_hdr->base_addr));
Nithin Sujirf4bffb22013-03-06 17:02:31 +00003840 if (err) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003841 netdev_err(tp->dev,
3842 "%s fails to set CPU PC, is %08x should be %08x\n",
Nithin Sujir77997ea2013-03-06 17:02:32 +00003843 __func__, tr32(cpu_base + CPU_PC),
3844 be32_to_cpu(fw_hdr->base_addr));
Matt Carlson997b4f12011-08-31 11:44:53 +00003845 return -ENODEV;
3846 }
Nithin Sujir837c45b2013-03-06 17:02:30 +00003847
3848 tg3_resume_cpu(tp, cpu_base);
Matt Carlson997b4f12011-08-31 11:44:53 +00003849 return 0;
3850}
3851
3852
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003853/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00003854static void __tg3_set_mac_addr(struct tg3 *tp, bool skip_mac_1)
Matt Carlson3f007892008-11-03 16:51:36 -08003855{
3856 u32 addr_high, addr_low;
3857 int i;
3858
3859 addr_high = ((tp->dev->dev_addr[0] << 8) |
3860 tp->dev->dev_addr[1]);
3861 addr_low = ((tp->dev->dev_addr[2] << 24) |
3862 (tp->dev->dev_addr[3] << 16) |
3863 (tp->dev->dev_addr[4] << 8) |
3864 (tp->dev->dev_addr[5] << 0));
3865 for (i = 0; i < 4; i++) {
3866 if (i == 1 && skip_mac_1)
3867 continue;
3868 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3869 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3870 }
3871
Joe Perches41535772013-02-16 11:20:04 +00003872 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3873 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003874 for (i = 0; i < 12; i++) {
3875 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3876 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3877 }
3878 }
3879
3880 addr_high = (tp->dev->dev_addr[0] +
3881 tp->dev->dev_addr[1] +
3882 tp->dev->dev_addr[2] +
3883 tp->dev->dev_addr[3] +
3884 tp->dev->dev_addr[4] +
3885 tp->dev->dev_addr[5]) &
3886 TX_BACKOFF_SEED_MASK;
3887 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3888}
3889
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003890static void tg3_enable_register_access(struct tg3 *tp)
3891{
3892 /*
3893 * Make sure register accesses (indirect or otherwise) will function
3894 * correctly.
3895 */
3896 pci_write_config_dword(tp->pdev,
3897 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3898}
3899
3900static int tg3_power_up(struct tg3 *tp)
3901{
Matt Carlsonbed98292011-07-13 09:27:29 +00003902 int err;
3903
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003904 tg3_enable_register_access(tp);
3905
Matt Carlsonbed98292011-07-13 09:27:29 +00003906 err = pci_set_power_state(tp->pdev, PCI_D0);
3907 if (!err) {
3908 /* Switch out of Vaux if it is a NIC */
3909 tg3_pwrsrc_switch_to_vmain(tp);
3910 } else {
3911 netdev_err(tp->dev, "Transition to D0 failed\n");
3912 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003913
Matt Carlsonbed98292011-07-13 09:27:29 +00003914 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003915}
3916
Joe Perches953c96e2013-04-09 10:18:14 +00003917static int tg3_setup_phy(struct tg3 *, bool);
Matt Carlson4b409522012-02-13 10:20:11 +00003918
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003919static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
3921 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003922 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003924 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003925
3926 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003927 if (tg3_flag(tp, CLKREQ_BUG))
3928 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3929 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3932 tw32(TG3PCI_MISC_HOST_CTRL,
3933 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3934
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003935 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003936 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003937
Joe Perches63c3a662011-04-26 08:12:10 +00003938 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003939 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003940 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003941 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003942 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003943 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003944
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003945 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003946
Matt Carlson80096062010-08-02 11:26:06 +00003947 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003948
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003949 tp->link_config.speed = phydev->speed;
3950 tp->link_config.duplex = phydev->duplex;
3951 tp->link_config.autoneg = phydev->autoneg;
3952 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003953
3954 advertising = ADVERTISED_TP |
3955 ADVERTISED_Pause |
3956 ADVERTISED_Autoneg |
3957 ADVERTISED_10baseT_Half;
3958
Joe Perches63c3a662011-04-26 08:12:10 +00003959 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3960 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003961 advertising |=
3962 ADVERTISED_100baseT_Half |
3963 ADVERTISED_100baseT_Full |
3964 ADVERTISED_10baseT_Full;
3965 else
3966 advertising |= ADVERTISED_10baseT_Full;
3967 }
3968
3969 phydev->advertising = advertising;
3970
3971 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003972
3973 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003974 if (phyid != PHY_ID_BCMAC131) {
3975 phyid &= PHY_BCM_OUI_MASK;
3976 if (phyid == PHY_BCM_OUI_1 ||
3977 phyid == PHY_BCM_OUI_2 ||
3978 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003979 do_low_power = true;
3980 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003981 }
Matt Carlsondd477002008-05-25 23:45:58 -07003982 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003983 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003984
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003985 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003986 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Matt Carlson2855b9f2012-02-13 15:20:14 +00003988 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Joe Perches953c96e2013-04-09 10:18:14 +00003989 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
3991
Joe Perches41535772013-02-16 11:20:04 +00003992 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003993 u32 val;
3994
3995 val = tr32(GRC_VCPU_EXT_CTRL);
3996 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003997 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003998 int i;
3999 u32 val;
4000
4001 for (i = 0; i < 200; i++) {
4002 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
4003 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
4004 break;
4005 msleep(1);
4006 }
4007 }
Joe Perches63c3a662011-04-26 08:12:10 +00004008 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07004009 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
4010 WOL_DRV_STATE_SHUTDOWN |
4011 WOL_DRV_WOL |
4012 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08004013
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004014 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 u32 mac_mode;
4016
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004017 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004018 if (do_low_power &&
4019 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
4020 tg3_phy_auxctl_write(tp,
4021 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
4022 MII_TG3_AUXCTL_PCTL_WOL_EN |
4023 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
4024 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07004025 udelay(40);
4026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004028 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07004029 mac_mode = MAC_MODE_PORT_MODE_GMII;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004030 else if (tp->phy_flags &
4031 TG3_PHYFLG_KEEP_LINK_ON_PWRDN) {
4032 if (tp->link_config.active_speed == SPEED_1000)
4033 mac_mode = MAC_MODE_PORT_MODE_GMII;
4034 else
4035 mac_mode = MAC_MODE_PORT_MODE_MII;
4036 } else
Michael Chan3f7045c2006-09-27 16:02:29 -07004037 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004039 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00004040 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00004041 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004042 SPEED_100 : SPEED_10;
4043 if (tg3_5700_link_polarity(tp, speed))
4044 mac_mode |= MAC_MODE_LINK_POLARITY;
4045 else
4046 mac_mode &= ~MAC_MODE_LINK_POLARITY;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 } else {
4049 mac_mode = MAC_MODE_PORT_MODE_TBI;
4050 }
4051
Joe Perches63c3a662011-04-26 08:12:10 +00004052 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 tw32(MAC_LED_CTRL, tp->led_ctrl);
4054
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004055 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00004056 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
4057 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08004058 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Joe Perches63c3a662011-04-26 08:12:10 +00004060 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00004061 mac_mode |= MAC_MODE_APE_TX_EN |
4062 MAC_MODE_APE_RX_EN |
4063 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07004064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 tw32_f(MAC_MODE, mac_mode);
4066 udelay(100);
4067
4068 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
4069 udelay(10);
4070 }
4071
Joe Perches63c3a662011-04-26 08:12:10 +00004072 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00004073 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4074 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 u32 base_val;
4076
4077 base_val = tp->pci_clock_ctrl;
4078 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
4079 CLOCK_CTRL_TXCLK_DISABLE);
4080
Michael Chanb401e9e2005-12-19 16:27:04 -08004081 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
4082 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00004083 } else if (tg3_flag(tp, 5780_CLASS) ||
4084 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00004085 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07004086 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00004087 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 u32 newbits1, newbits2;
4089
Joe Perches41535772013-02-16 11:20:04 +00004090 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4091 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
4093 CLOCK_CTRL_TXCLK_DISABLE |
4094 CLOCK_CTRL_ALTCLK);
4095 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00004096 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 newbits1 = CLOCK_CTRL_625_CORE;
4098 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
4099 } else {
4100 newbits1 = CLOCK_CTRL_ALTCLK;
4101 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
4102 }
4103
Michael Chanb401e9e2005-12-19 16:27:04 -08004104 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
4105 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Michael Chanb401e9e2005-12-19 16:27:04 -08004107 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
4108 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Joe Perches63c3a662011-04-26 08:12:10 +00004110 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 u32 newbits3;
4112
Joe Perches41535772013-02-16 11:20:04 +00004113 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4114 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
4116 CLOCK_CTRL_TXCLK_DISABLE |
4117 CLOCK_CTRL_44MHZ_CORE);
4118 } else {
4119 newbits3 = CLOCK_CTRL_44MHZ_CORE;
4120 }
4121
Michael Chanb401e9e2005-12-19 16:27:04 -08004122 tw32_wait_f(TG3PCI_CLOCK_CTRL,
4123 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125 }
4126
Joe Perches63c3a662011-04-26 08:12:10 +00004127 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08004128 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08004129
Matt Carlsoncd0d7222011-07-13 09:27:33 +00004130 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004133 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00004134 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
4135 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 u32 val = tr32(0x7d00);
4137
4138 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
4139 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00004140 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08004141 int err;
4142
4143 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08004145 if (!err)
4146 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08004147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
4149
Michael Chanbbadf502006-04-06 21:46:34 -07004150 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153}
4154
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004155static void tg3_power_down(struct tg3 *tp)
4156{
4157 tg3_power_down_prepare(tp);
4158
Joe Perches63c3a662011-04-26 08:12:10 +00004159 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00004160 pci_set_power_state(tp->pdev, PCI_D3hot);
4161}
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
4164{
4165 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
4166 case MII_TG3_AUX_STAT_10HALF:
4167 *speed = SPEED_10;
4168 *duplex = DUPLEX_HALF;
4169 break;
4170
4171 case MII_TG3_AUX_STAT_10FULL:
4172 *speed = SPEED_10;
4173 *duplex = DUPLEX_FULL;
4174 break;
4175
4176 case MII_TG3_AUX_STAT_100HALF:
4177 *speed = SPEED_100;
4178 *duplex = DUPLEX_HALF;
4179 break;
4180
4181 case MII_TG3_AUX_STAT_100FULL:
4182 *speed = SPEED_100;
4183 *duplex = DUPLEX_FULL;
4184 break;
4185
4186 case MII_TG3_AUX_STAT_1000HALF:
4187 *speed = SPEED_1000;
4188 *duplex = DUPLEX_HALF;
4189 break;
4190
4191 case MII_TG3_AUX_STAT_1000FULL:
4192 *speed = SPEED_1000;
4193 *duplex = DUPLEX_FULL;
4194 break;
4195
4196 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004197 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004198 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4199 SPEED_10;
4200 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4201 DUPLEX_HALF;
4202 break;
4203 }
Matt Carlsone7405222012-02-13 15:20:16 +00004204 *speed = SPEED_UNKNOWN;
4205 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209
Matt Carlson42b64a42011-05-19 12:12:49 +00004210static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Matt Carlson42b64a42011-05-19 12:12:49 +00004212 int err = 0;
4213 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Matt Carlson42b64a42011-05-19 12:12:49 +00004215 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004216 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004217 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Matt Carlson42b64a42011-05-19 12:12:49 +00004219 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4220 if (err)
4221 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Matt Carlson4f272092011-12-14 11:09:57 +00004223 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4224 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004225
Joe Perches41535772013-02-16 11:20:04 +00004226 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4227 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004228 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004229
Matt Carlson4f272092011-12-14 11:09:57 +00004230 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4231 if (err)
4232 goto done;
4233 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004234
Matt Carlson42b64a42011-05-19 12:12:49 +00004235 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4236 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Matt Carlson42b64a42011-05-19 12:12:49 +00004238 tw32(TG3_CPMU_EEE_MODE,
4239 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004240
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004241 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004242 if (!err) {
4243 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004244
Matt Carlsona6b68da2010-12-06 08:28:52 +00004245 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004246 /* Advertise 100-BaseTX EEE ability */
4247 if (advertise & ADVERTISED_100baseT_Full)
4248 val |= MDIO_AN_EEE_ADV_100TX;
4249 /* Advertise 1000-BaseT EEE ability */
4250 if (advertise & ADVERTISED_1000baseT_Full)
4251 val |= MDIO_AN_EEE_ADV_1000T;
4252 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004253 if (err)
4254 val = 0;
4255
Joe Perches41535772013-02-16 11:20:04 +00004256 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004257 case ASIC_REV_5717:
4258 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004259 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004260 case ASIC_REV_5719:
4261 /* If we advertised any eee advertisements above... */
4262 if (val)
4263 val = MII_TG3_DSP_TAP26_ALNOKO |
4264 MII_TG3_DSP_TAP26_RMRXSTO |
4265 MII_TG3_DSP_TAP26_OPCSINPT;
4266 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4267 /* Fall through */
4268 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004269 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004270 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4271 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4272 MII_TG3_DSP_CH34TP2_HIBW01);
4273 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004274
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004275 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004276 if (!err)
4277 err = err2;
4278 }
4279
4280done:
4281 return err;
4282}
4283
4284static void tg3_phy_copper_begin(struct tg3 *tp)
4285{
Matt Carlsond13ba512012-02-22 12:35:19 +00004286 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4287 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4288 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004289
Nithin Sujir942d1af2013-04-09 08:48:07 +00004290 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4291 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
Matt Carlsond13ba512012-02-22 12:35:19 +00004292 adv = ADVERTISED_10baseT_Half |
4293 ADVERTISED_10baseT_Full;
4294 if (tg3_flag(tp, WOL_SPEED_100MB))
4295 adv |= ADVERTISED_100baseT_Half |
4296 ADVERTISED_100baseT_Full;
Nithin Sujir942d1af2013-04-09 08:48:07 +00004297 if (tp->phy_flags & TG3_PHYFLG_1G_ON_VAUX_OK)
4298 adv |= ADVERTISED_1000baseT_Half |
4299 ADVERTISED_1000baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004300
Matt Carlsond13ba512012-02-22 12:35:19 +00004301 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004302 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004303 adv = tp->link_config.advertising;
4304 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4305 adv &= ~(ADVERTISED_1000baseT_Half |
4306 ADVERTISED_1000baseT_Full);
4307
4308 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004309 }
4310
Matt Carlsond13ba512012-02-22 12:35:19 +00004311 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004312
Nithin Sujir942d1af2013-04-09 08:48:07 +00004313 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
4314 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN)) {
4315 /* Normally during power down we want to autonegotiate
4316 * the lowest possible speed for WOL. However, to avoid
4317 * link flap, we leave it untouched.
4318 */
4319 return;
4320 }
4321
Matt Carlsond13ba512012-02-22 12:35:19 +00004322 tg3_writephy(tp, MII_BMCR,
4323 BMCR_ANENABLE | BMCR_ANRESTART);
4324 } else {
4325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 u32 bmcr, orig_bmcr;
4327
4328 tp->link_config.active_speed = tp->link_config.speed;
4329 tp->link_config.active_duplex = tp->link_config.duplex;
4330
Nithin Sujir7c6cdea2013-03-12 15:32:48 +00004331 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
4332 /* With autoneg disabled, 5715 only links up when the
4333 * advertisement register has the configured speed
4334 * enabled.
4335 */
4336 tg3_writephy(tp, MII_ADVERTISE, ADVERTISE_ALL);
4337 }
4338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 bmcr = 0;
4340 switch (tp->link_config.speed) {
4341 default:
4342 case SPEED_10:
4343 break;
4344
4345 case SPEED_100:
4346 bmcr |= BMCR_SPEED100;
4347 break;
4348
4349 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004350 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 if (tp->link_config.duplex == DUPLEX_FULL)
4355 bmcr |= BMCR_FULLDPLX;
4356
4357 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4358 (bmcr != orig_bmcr)) {
4359 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4360 for (i = 0; i < 1500; i++) {
4361 u32 tmp;
4362
4363 udelay(10);
4364 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4365 tg3_readphy(tp, MII_BMSR, &tmp))
4366 continue;
4367 if (!(tmp & BMSR_LSTATUS)) {
4368 udelay(40);
4369 break;
4370 }
4371 }
4372 tg3_writephy(tp, MII_BMCR, bmcr);
4373 udelay(40);
4374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
4376}
4377
Nithin Sujirfdad8de2013-04-09 08:48:08 +00004378static int tg3_phy_pull_config(struct tg3 *tp)
4379{
4380 int err;
4381 u32 val;
4382
4383 err = tg3_readphy(tp, MII_BMCR, &val);
4384 if (err)
4385 goto done;
4386
4387 if (!(val & BMCR_ANENABLE)) {
4388 tp->link_config.autoneg = AUTONEG_DISABLE;
4389 tp->link_config.advertising = 0;
4390 tg3_flag_clear(tp, PAUSE_AUTONEG);
4391
4392 err = -EIO;
4393
4394 switch (val & (BMCR_SPEED1000 | BMCR_SPEED100)) {
4395 case 0:
4396 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4397 goto done;
4398
4399 tp->link_config.speed = SPEED_10;
4400 break;
4401 case BMCR_SPEED100:
4402 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
4403 goto done;
4404
4405 tp->link_config.speed = SPEED_100;
4406 break;
4407 case BMCR_SPEED1000:
4408 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4409 tp->link_config.speed = SPEED_1000;
4410 break;
4411 }
4412 /* Fall through */
4413 default:
4414 goto done;
4415 }
4416
4417 if (val & BMCR_FULLDPLX)
4418 tp->link_config.duplex = DUPLEX_FULL;
4419 else
4420 tp->link_config.duplex = DUPLEX_HALF;
4421
4422 tp->link_config.flowctrl = FLOW_CTRL_RX | FLOW_CTRL_TX;
4423
4424 err = 0;
4425 goto done;
4426 }
4427
4428 tp->link_config.autoneg = AUTONEG_ENABLE;
4429 tp->link_config.advertising = ADVERTISED_Autoneg;
4430 tg3_flag_set(tp, PAUSE_AUTONEG);
4431
4432 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4433 u32 adv;
4434
4435 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4436 if (err)
4437 goto done;
4438
4439 adv = mii_adv_to_ethtool_adv_t(val & ADVERTISE_ALL);
4440 tp->link_config.advertising |= adv | ADVERTISED_TP;
4441
4442 tp->link_config.flowctrl = tg3_decode_flowctrl_1000T(val);
4443 } else {
4444 tp->link_config.advertising |= ADVERTISED_FIBRE;
4445 }
4446
4447 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4448 u32 adv;
4449
4450 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
4451 err = tg3_readphy(tp, MII_CTRL1000, &val);
4452 if (err)
4453 goto done;
4454
4455 adv = mii_ctrl1000_to_ethtool_adv_t(val);
4456 } else {
4457 err = tg3_readphy(tp, MII_ADVERTISE, &val);
4458 if (err)
4459 goto done;
4460
4461 adv = tg3_decode_flowctrl_1000X(val);
4462 tp->link_config.flowctrl = adv;
4463
4464 val &= (ADVERTISE_1000XHALF | ADVERTISE_1000XFULL);
4465 adv = mii_adv_to_ethtool_adv_x(val);
4466 }
4467
4468 tp->link_config.advertising |= adv;
4469 }
4470
4471done:
4472 return err;
4473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475static int tg3_init_5401phy_dsp(struct tg3 *tp)
4476{
4477 int err;
4478
4479 /* Turn off tap power management. */
4480 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004481 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004483 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4484 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4485 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4486 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4487 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 udelay(40);
4490
4491 return err;
4492}
4493
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004494static bool tg3_phy_eee_config_ok(struct tg3 *tp)
4495{
4496 u32 val;
4497 u32 tgtadv = 0;
4498 u32 advertising = tp->link_config.advertising;
4499
4500 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4501 return true;
4502
4503 if (tg3_phy_cl45_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, &val))
4504 return false;
4505
4506 val &= (MDIO_AN_EEE_ADV_100TX | MDIO_AN_EEE_ADV_1000T);
4507
4508
4509 if (advertising & ADVERTISED_100baseT_Full)
4510 tgtadv |= MDIO_AN_EEE_ADV_100TX;
4511 if (advertising & ADVERTISED_1000baseT_Full)
4512 tgtadv |= MDIO_AN_EEE_ADV_1000T;
4513
4514 if (val != tgtadv)
4515 return false;
4516
4517 return true;
4518}
4519
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004520static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004522 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004523
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004524 advertising = tp->link_config.advertising;
4525 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004527 advmsk = ADVERTISE_ALL;
4528 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004529 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004530 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004533 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4534 return false;
4535
4536 if ((*lcladv & advmsk) != tgtadv)
4537 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004538
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004539 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 u32 tg3_ctrl;
4541
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004542 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004543
Matt Carlson221c5632011-06-13 13:39:01 +00004544 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004545 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Matt Carlson3198e072012-02-13 15:20:10 +00004547 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004548 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4549 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004550 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4551 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4552 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4553 } else {
4554 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4555 }
4556
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004557 if (tg3_ctrl != tgtadv)
4558 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004560
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004561 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004562}
4563
Matt Carlson859edb22011-12-08 14:40:16 +00004564static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4565{
4566 u32 lpeth = 0;
4567
4568 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4569 u32 val;
4570
4571 if (tg3_readphy(tp, MII_STAT1000, &val))
4572 return false;
4573
4574 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4575 }
4576
4577 if (tg3_readphy(tp, MII_LPA, rmtadv))
4578 return false;
4579
4580 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4581 tp->link_config.rmt_adv = lpeth;
4582
4583 return true;
4584}
4585
Joe Perches953c96e2013-04-09 10:18:14 +00004586static bool tg3_test_and_report_link_chg(struct tg3 *tp, bool curr_link_up)
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004587{
4588 if (curr_link_up != tp->link_up) {
4589 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004590 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004591 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004592 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004593 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4594 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4595 }
4596
4597 tg3_link_report(tp);
4598 return true;
4599 }
4600
4601 return false;
4602}
4603
Michael Chan3310e242013-04-09 08:48:05 +00004604static void tg3_clear_mac_status(struct tg3 *tp)
4605{
4606 tw32(MAC_EVENT, 0);
4607
4608 tw32_f(MAC_STATUS,
4609 MAC_STATUS_SYNC_CHANGED |
4610 MAC_STATUS_CFG_CHANGED |
4611 MAC_STATUS_MI_COMPLETION |
4612 MAC_STATUS_LNKSTATE_CHANGED);
4613 udelay(40);
4614}
4615
Joe Perches953c96e2013-04-09 10:18:14 +00004616static int tg3_setup_copper_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Joe Perches953c96e2013-04-09 10:18:14 +00004618 bool current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004619 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004620 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 u16 current_speed;
4622 u8 current_duplex;
4623 int i, err;
4624
Michael Chan3310e242013-04-09 08:48:05 +00004625 tg3_clear_mac_status(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Matt Carlson8ef21422008-05-02 16:47:53 -07004627 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4628 tw32_f(MAC_MI_MODE,
4629 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4630 udelay(80);
4631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004633 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 /* Some third-party PHYs need to be reset on link going
4636 * down.
4637 */
Joe Perches41535772013-02-16 11:20:04 +00004638 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4639 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4640 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004641 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 tg3_readphy(tp, MII_BMSR, &bmsr);
4643 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4644 !(bmsr & BMSR_LSTATUS))
Joe Perches953c96e2013-04-09 10:18:14 +00004645 force_reset = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 }
4647 if (force_reset)
4648 tg3_phy_reset(tp);
4649
Matt Carlson79eb6902010-02-17 15:17:03 +00004650 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 tg3_readphy(tp, MII_BMSR, &bmsr);
4652 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004653 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 bmsr = 0;
4655
4656 if (!(bmsr & BMSR_LSTATUS)) {
4657 err = tg3_init_5401phy_dsp(tp);
4658 if (err)
4659 return err;
4660
4661 tg3_readphy(tp, MII_BMSR, &bmsr);
4662 for (i = 0; i < 1000; i++) {
4663 udelay(10);
4664 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4665 (bmsr & BMSR_LSTATUS)) {
4666 udelay(40);
4667 break;
4668 }
4669 }
4670
Matt Carlson79eb6902010-02-17 15:17:03 +00004671 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4672 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 !(bmsr & BMSR_LSTATUS) &&
4674 tp->link_config.active_speed == SPEED_1000) {
4675 err = tg3_phy_reset(tp);
4676 if (!err)
4677 err = tg3_init_5401phy_dsp(tp);
4678 if (err)
4679 return err;
4680 }
4681 }
Joe Perches41535772013-02-16 11:20:04 +00004682 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4683 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /* 5701 {A0,B0} CRC bug workaround */
4685 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004686 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4687 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4688 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004692 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4693 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004695 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004697 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4699
Joe Perches41535772013-02-16 11:20:04 +00004700 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4701 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4703 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4704 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4705 else
4706 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4707 }
4708
Joe Perches953c96e2013-04-09 10:18:14 +00004709 current_link_up = false;
Matt Carlsone7405222012-02-13 15:20:16 +00004710 current_speed = SPEED_UNKNOWN;
4711 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004712 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004713 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004715 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004716 err = tg3_phy_auxctl_read(tp,
4717 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4718 &val);
4719 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004720 tg3_phy_auxctl_write(tp,
4721 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4722 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 goto relink;
4724 }
4725 }
4726
4727 bmsr = 0;
4728 for (i = 0; i < 100; i++) {
4729 tg3_readphy(tp, MII_BMSR, &bmsr);
4730 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4731 (bmsr & BMSR_LSTATUS))
4732 break;
4733 udelay(40);
4734 }
4735
4736 if (bmsr & BMSR_LSTATUS) {
4737 u32 aux_stat, bmcr;
4738
4739 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4740 for (i = 0; i < 2000; i++) {
4741 udelay(10);
4742 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4743 aux_stat)
4744 break;
4745 }
4746
4747 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4748 &current_speed,
4749 &current_duplex);
4750
4751 bmcr = 0;
4752 for (i = 0; i < 200; i++) {
4753 tg3_readphy(tp, MII_BMCR, &bmcr);
4754 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4755 continue;
4756 if (bmcr && bmcr != 0x7fff)
4757 break;
4758 udelay(10);
4759 }
4760
Matt Carlsonef167e22007-12-20 20:10:01 -08004761 lcl_adv = 0;
4762 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Matt Carlsonef167e22007-12-20 20:10:01 -08004764 tp->link_config.active_speed = current_speed;
4765 tp->link_config.active_duplex = current_duplex;
4766
4767 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004768 bool eee_config_ok = tg3_phy_eee_config_ok(tp);
4769
Matt Carlsonef167e22007-12-20 20:10:01 -08004770 if ((bmcr & BMCR_ANENABLE) &&
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004771 eee_config_ok &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004772 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004773 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Joe Perches953c96e2013-04-09 10:18:14 +00004774 current_link_up = true;
Nithin Sujired1ff5c2013-04-09 08:48:09 +00004775
4776 /* EEE settings changes take effect only after a phy
4777 * reset. If we have skipped a reset due to Link Flap
4778 * Avoidance being enabled, do it now.
4779 */
4780 if (!eee_config_ok &&
4781 (tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
4782 !force_reset)
4783 tg3_phy_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 } else {
4785 if (!(bmcr & BMCR_ANENABLE) &&
4786 tp->link_config.speed == current_speed &&
Nithin Sujirf0fcd7a2013-04-09 08:48:01 +00004787 tp->link_config.duplex == current_duplex) {
Joe Perches953c96e2013-04-09 10:18:14 +00004788 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 }
4790 }
4791
Joe Perches953c96e2013-04-09 10:18:14 +00004792 if (current_link_up &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004793 tp->link_config.active_duplex == DUPLEX_FULL) {
4794 u32 reg, bit;
4795
4796 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4797 reg = MII_TG3_FET_GEN_STAT;
4798 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4799 } else {
4800 reg = MII_TG3_EXT_STAT;
4801 bit = MII_TG3_EXT_STAT_MDIX;
4802 }
4803
4804 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4805 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4806
Matt Carlsonef167e22007-12-20 20:10:01 -08004807 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 }
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811relink:
Joe Perches953c96e2013-04-09 10:18:14 +00004812 if (!current_link_up || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 tg3_phy_copper_begin(tp);
4814
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004815 if (tg3_flag(tp, ROBOSWITCH)) {
Joe Perches953c96e2013-04-09 10:18:14 +00004816 current_link_up = true;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004817 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4818 current_speed = SPEED_1000;
4819 current_duplex = DUPLEX_FULL;
4820 tp->link_config.active_speed = current_speed;
4821 tp->link_config.active_duplex = current_duplex;
4822 }
4823
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004824 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004825 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4826 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Joe Perches953c96e2013-04-09 10:18:14 +00004827 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 }
4829
4830 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
Joe Perches953c96e2013-04-09 10:18:14 +00004831 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 if (tp->link_config.active_speed == SPEED_100 ||
4833 tp->link_config.active_speed == SPEED_10)
4834 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4835 else
4836 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004837 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004838 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4839 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4841
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004842 /* In order for the 5750 core in BCM4785 chip to work properly
4843 * in RGMII mode, the Led Control Register must be set up.
4844 */
4845 if (tg3_flag(tp, RGMII_MODE)) {
4846 u32 led_ctrl = tr32(MAC_LED_CTRL);
4847 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4848
4849 if (tp->link_config.active_speed == SPEED_10)
4850 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4851 else if (tp->link_config.active_speed == SPEED_100)
4852 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4853 LED_CTRL_100MBPS_ON);
4854 else if (tp->link_config.active_speed == SPEED_1000)
4855 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4856 LED_CTRL_1000MBPS_ON);
4857
4858 tw32(MAC_LED_CTRL, led_ctrl);
4859 udelay(40);
4860 }
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4863 if (tp->link_config.active_duplex == DUPLEX_HALF)
4864 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4865
Joe Perches41535772013-02-16 11:20:04 +00004866 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches953c96e2013-04-09 10:18:14 +00004867 if (current_link_up &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004868 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004870 else
4871 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
4873
4874 /* ??? Without this setting Netgear GA302T PHY does not
4875 * ??? send/receive packets...
4876 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004877 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004878 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4880 tw32_f(MAC_MI_MODE, tp->mi_mode);
4881 udelay(80);
4882 }
4883
4884 tw32_f(MAC_MODE, tp->mac_mode);
4885 udelay(40);
4886
Matt Carlson52b02d02010-10-14 10:37:41 +00004887 tg3_phy_eee_adjust(tp, current_link_up);
4888
Joe Perches63c3a662011-04-26 08:12:10 +00004889 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 /* Polled via timer. */
4891 tw32_f(MAC_EVENT, 0);
4892 } else {
4893 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4894 }
4895 udelay(40);
4896
Joe Perches41535772013-02-16 11:20:04 +00004897 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Joe Perches953c96e2013-04-09 10:18:14 +00004898 current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004900 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 udelay(120);
4902 tw32_f(MAC_STATUS,
4903 (MAC_STATUS_SYNC_CHANGED |
4904 MAC_STATUS_CFG_CHANGED));
4905 udelay(40);
4906 tg3_write_mem(tp,
4907 NIC_SRAM_FIRMWARE_MBOX,
4908 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4909 }
4910
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004911 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004912 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004913 if (tp->link_config.active_speed == SPEED_100 ||
4914 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004915 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4916 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004917 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004918 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4919 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004920 }
4921
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004922 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 return 0;
4925}
4926
4927struct tg3_fiber_aneginfo {
4928 int state;
4929#define ANEG_STATE_UNKNOWN 0
4930#define ANEG_STATE_AN_ENABLE 1
4931#define ANEG_STATE_RESTART_INIT 2
4932#define ANEG_STATE_RESTART 3
4933#define ANEG_STATE_DISABLE_LINK_OK 4
4934#define ANEG_STATE_ABILITY_DETECT_INIT 5
4935#define ANEG_STATE_ABILITY_DETECT 6
4936#define ANEG_STATE_ACK_DETECT_INIT 7
4937#define ANEG_STATE_ACK_DETECT 8
4938#define ANEG_STATE_COMPLETE_ACK_INIT 9
4939#define ANEG_STATE_COMPLETE_ACK 10
4940#define ANEG_STATE_IDLE_DETECT_INIT 11
4941#define ANEG_STATE_IDLE_DETECT 12
4942#define ANEG_STATE_LINK_OK 13
4943#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4944#define ANEG_STATE_NEXT_PAGE_WAIT 15
4945
4946 u32 flags;
4947#define MR_AN_ENABLE 0x00000001
4948#define MR_RESTART_AN 0x00000002
4949#define MR_AN_COMPLETE 0x00000004
4950#define MR_PAGE_RX 0x00000008
4951#define MR_NP_LOADED 0x00000010
4952#define MR_TOGGLE_TX 0x00000020
4953#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4954#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4955#define MR_LP_ADV_SYM_PAUSE 0x00000100
4956#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4957#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4958#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4959#define MR_LP_ADV_NEXT_PAGE 0x00001000
4960#define MR_TOGGLE_RX 0x00002000
4961#define MR_NP_RX 0x00004000
4962
4963#define MR_LINK_OK 0x80000000
4964
4965 unsigned long link_time, cur_time;
4966
4967 u32 ability_match_cfg;
4968 int ability_match_count;
4969
4970 char ability_match, idle_match, ack_match;
4971
4972 u32 txconfig, rxconfig;
4973#define ANEG_CFG_NP 0x00000080
4974#define ANEG_CFG_ACK 0x00000040
4975#define ANEG_CFG_RF2 0x00000020
4976#define ANEG_CFG_RF1 0x00000010
4977#define ANEG_CFG_PS2 0x00000001
4978#define ANEG_CFG_PS1 0x00008000
4979#define ANEG_CFG_HD 0x00004000
4980#define ANEG_CFG_FD 0x00002000
4981#define ANEG_CFG_INVAL 0x00001f06
4982
4983};
4984#define ANEG_OK 0
4985#define ANEG_DONE 1
4986#define ANEG_TIMER_ENAB 2
4987#define ANEG_FAILED -1
4988
4989#define ANEG_STATE_SETTLE_TIME 10000
4990
4991static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4992 struct tg3_fiber_aneginfo *ap)
4993{
Matt Carlson5be73b42007-12-20 20:09:29 -08004994 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 unsigned long delta;
4996 u32 rx_cfg_reg;
4997 int ret;
4998
4999 if (ap->state == ANEG_STATE_UNKNOWN) {
5000 ap->rxconfig = 0;
5001 ap->link_time = 0;
5002 ap->cur_time = 0;
5003 ap->ability_match_cfg = 0;
5004 ap->ability_match_count = 0;
5005 ap->ability_match = 0;
5006 ap->idle_match = 0;
5007 ap->ack_match = 0;
5008 }
5009 ap->cur_time++;
5010
5011 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
5012 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
5013
5014 if (rx_cfg_reg != ap->ability_match_cfg) {
5015 ap->ability_match_cfg = rx_cfg_reg;
5016 ap->ability_match = 0;
5017 ap->ability_match_count = 0;
5018 } else {
5019 if (++ap->ability_match_count > 1) {
5020 ap->ability_match = 1;
5021 ap->ability_match_cfg = rx_cfg_reg;
5022 }
5023 }
5024 if (rx_cfg_reg & ANEG_CFG_ACK)
5025 ap->ack_match = 1;
5026 else
5027 ap->ack_match = 0;
5028
5029 ap->idle_match = 0;
5030 } else {
5031 ap->idle_match = 1;
5032 ap->ability_match_cfg = 0;
5033 ap->ability_match_count = 0;
5034 ap->ability_match = 0;
5035 ap->ack_match = 0;
5036
5037 rx_cfg_reg = 0;
5038 }
5039
5040 ap->rxconfig = rx_cfg_reg;
5041 ret = ANEG_OK;
5042
Matt Carlson33f401a2010-04-05 10:19:27 +00005043 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 case ANEG_STATE_UNKNOWN:
5045 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
5046 ap->state = ANEG_STATE_AN_ENABLE;
5047
5048 /* fallthru */
5049 case ANEG_STATE_AN_ENABLE:
5050 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
5051 if (ap->flags & MR_AN_ENABLE) {
5052 ap->link_time = 0;
5053 ap->cur_time = 0;
5054 ap->ability_match_cfg = 0;
5055 ap->ability_match_count = 0;
5056 ap->ability_match = 0;
5057 ap->idle_match = 0;
5058 ap->ack_match = 0;
5059
5060 ap->state = ANEG_STATE_RESTART_INIT;
5061 } else {
5062 ap->state = ANEG_STATE_DISABLE_LINK_OK;
5063 }
5064 break;
5065
5066 case ANEG_STATE_RESTART_INIT:
5067 ap->link_time = ap->cur_time;
5068 ap->flags &= ~(MR_NP_LOADED);
5069 ap->txconfig = 0;
5070 tw32(MAC_TX_AUTO_NEG, 0);
5071 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5072 tw32_f(MAC_MODE, tp->mac_mode);
5073 udelay(40);
5074
5075 ret = ANEG_TIMER_ENAB;
5076 ap->state = ANEG_STATE_RESTART;
5077
5078 /* fallthru */
5079 case ANEG_STATE_RESTART:
5080 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00005081 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00005083 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 break;
5086
5087 case ANEG_STATE_DISABLE_LINK_OK:
5088 ret = ANEG_DONE;
5089 break;
5090
5091 case ANEG_STATE_ABILITY_DETECT_INIT:
5092 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08005093 ap->txconfig = ANEG_CFG_FD;
5094 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5095 if (flowctrl & ADVERTISE_1000XPAUSE)
5096 ap->txconfig |= ANEG_CFG_PS1;
5097 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5098 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5100 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5101 tw32_f(MAC_MODE, tp->mac_mode);
5102 udelay(40);
5103
5104 ap->state = ANEG_STATE_ABILITY_DETECT;
5105 break;
5106
5107 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00005108 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 break;
5111
5112 case ANEG_STATE_ACK_DETECT_INIT:
5113 ap->txconfig |= ANEG_CFG_ACK;
5114 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
5115 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
5116 tw32_f(MAC_MODE, tp->mac_mode);
5117 udelay(40);
5118
5119 ap->state = ANEG_STATE_ACK_DETECT;
5120
5121 /* fallthru */
5122 case ANEG_STATE_ACK_DETECT:
5123 if (ap->ack_match != 0) {
5124 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
5125 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
5126 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
5127 } else {
5128 ap->state = ANEG_STATE_AN_ENABLE;
5129 }
5130 } else if (ap->ability_match != 0 &&
5131 ap->rxconfig == 0) {
5132 ap->state = ANEG_STATE_AN_ENABLE;
5133 }
5134 break;
5135
5136 case ANEG_STATE_COMPLETE_ACK_INIT:
5137 if (ap->rxconfig & ANEG_CFG_INVAL) {
5138 ret = ANEG_FAILED;
5139 break;
5140 }
5141 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
5142 MR_LP_ADV_HALF_DUPLEX |
5143 MR_LP_ADV_SYM_PAUSE |
5144 MR_LP_ADV_ASYM_PAUSE |
5145 MR_LP_ADV_REMOTE_FAULT1 |
5146 MR_LP_ADV_REMOTE_FAULT2 |
5147 MR_LP_ADV_NEXT_PAGE |
5148 MR_TOGGLE_RX |
5149 MR_NP_RX);
5150 if (ap->rxconfig & ANEG_CFG_FD)
5151 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
5152 if (ap->rxconfig & ANEG_CFG_HD)
5153 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
5154 if (ap->rxconfig & ANEG_CFG_PS1)
5155 ap->flags |= MR_LP_ADV_SYM_PAUSE;
5156 if (ap->rxconfig & ANEG_CFG_PS2)
5157 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
5158 if (ap->rxconfig & ANEG_CFG_RF1)
5159 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
5160 if (ap->rxconfig & ANEG_CFG_RF2)
5161 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
5162 if (ap->rxconfig & ANEG_CFG_NP)
5163 ap->flags |= MR_LP_ADV_NEXT_PAGE;
5164
5165 ap->link_time = ap->cur_time;
5166
5167 ap->flags ^= (MR_TOGGLE_TX);
5168 if (ap->rxconfig & 0x0008)
5169 ap->flags |= MR_TOGGLE_RX;
5170 if (ap->rxconfig & ANEG_CFG_NP)
5171 ap->flags |= MR_NP_RX;
5172 ap->flags |= MR_PAGE_RX;
5173
5174 ap->state = ANEG_STATE_COMPLETE_ACK;
5175 ret = ANEG_TIMER_ENAB;
5176 break;
5177
5178 case ANEG_STATE_COMPLETE_ACK:
5179 if (ap->ability_match != 0 &&
5180 ap->rxconfig == 0) {
5181 ap->state = ANEG_STATE_AN_ENABLE;
5182 break;
5183 }
5184 delta = ap->cur_time - ap->link_time;
5185 if (delta > ANEG_STATE_SETTLE_TIME) {
5186 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
5187 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5188 } else {
5189 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
5190 !(ap->flags & MR_NP_RX)) {
5191 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
5192 } else {
5193 ret = ANEG_FAILED;
5194 }
5195 }
5196 }
5197 break;
5198
5199 case ANEG_STATE_IDLE_DETECT_INIT:
5200 ap->link_time = ap->cur_time;
5201 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5202 tw32_f(MAC_MODE, tp->mac_mode);
5203 udelay(40);
5204
5205 ap->state = ANEG_STATE_IDLE_DETECT;
5206 ret = ANEG_TIMER_ENAB;
5207 break;
5208
5209 case ANEG_STATE_IDLE_DETECT:
5210 if (ap->ability_match != 0 &&
5211 ap->rxconfig == 0) {
5212 ap->state = ANEG_STATE_AN_ENABLE;
5213 break;
5214 }
5215 delta = ap->cur_time - ap->link_time;
5216 if (delta > ANEG_STATE_SETTLE_TIME) {
5217 /* XXX another gem from the Broadcom driver :( */
5218 ap->state = ANEG_STATE_LINK_OK;
5219 }
5220 break;
5221
5222 case ANEG_STATE_LINK_OK:
5223 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
5224 ret = ANEG_DONE;
5225 break;
5226
5227 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
5228 /* ??? unimplemented */
5229 break;
5230
5231 case ANEG_STATE_NEXT_PAGE_WAIT:
5232 /* ??? unimplemented */
5233 break;
5234
5235 default:
5236 ret = ANEG_FAILED;
5237 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
5240 return ret;
5241}
5242
Matt Carlson5be73b42007-12-20 20:09:29 -08005243static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244{
5245 int res = 0;
5246 struct tg3_fiber_aneginfo aninfo;
5247 int status = ANEG_FAILED;
5248 unsigned int tick;
5249 u32 tmp;
5250
5251 tw32_f(MAC_TX_AUTO_NEG, 0);
5252
5253 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
5254 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
5255 udelay(40);
5256
5257 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
5258 udelay(40);
5259
5260 memset(&aninfo, 0, sizeof(aninfo));
5261 aninfo.flags |= MR_AN_ENABLE;
5262 aninfo.state = ANEG_STATE_UNKNOWN;
5263 aninfo.cur_time = 0;
5264 tick = 0;
5265 while (++tick < 195000) {
5266 status = tg3_fiber_aneg_smachine(tp, &aninfo);
5267 if (status == ANEG_DONE || status == ANEG_FAILED)
5268 break;
5269
5270 udelay(1);
5271 }
5272
5273 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
5274 tw32_f(MAC_MODE, tp->mac_mode);
5275 udelay(40);
5276
Matt Carlson5be73b42007-12-20 20:09:29 -08005277 *txflags = aninfo.txconfig;
5278 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
5280 if (status == ANEG_DONE &&
5281 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
5282 MR_LP_ADV_FULL_DUPLEX)))
5283 res = 1;
5284
5285 return res;
5286}
5287
5288static void tg3_init_bcm8002(struct tg3 *tp)
5289{
5290 u32 mac_status = tr32(MAC_STATUS);
5291 int i;
5292
5293 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00005294 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 !(mac_status & MAC_STATUS_PCS_SYNCED))
5296 return;
5297
5298 /* Set PLL lock range. */
5299 tg3_writephy(tp, 0x16, 0x8007);
5300
5301 /* SW reset */
5302 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
5303
5304 /* Wait for reset to complete. */
5305 /* XXX schedule_timeout() ... */
5306 for (i = 0; i < 500; i++)
5307 udelay(10);
5308
5309 /* Config mode; select PMA/Ch 1 regs. */
5310 tg3_writephy(tp, 0x10, 0x8411);
5311
5312 /* Enable auto-lock and comdet, select txclk for tx. */
5313 tg3_writephy(tp, 0x11, 0x0a10);
5314
5315 tg3_writephy(tp, 0x18, 0x00a0);
5316 tg3_writephy(tp, 0x16, 0x41ff);
5317
5318 /* Assert and deassert POR. */
5319 tg3_writephy(tp, 0x13, 0x0400);
5320 udelay(40);
5321 tg3_writephy(tp, 0x13, 0x0000);
5322
5323 tg3_writephy(tp, 0x11, 0x0a50);
5324 udelay(40);
5325 tg3_writephy(tp, 0x11, 0x0a10);
5326
5327 /* Wait for signal to stabilize */
5328 /* XXX schedule_timeout() ... */
5329 for (i = 0; i < 15000; i++)
5330 udelay(10);
5331
5332 /* Deselect the channel register so we can read the PHYID
5333 * later.
5334 */
5335 tg3_writephy(tp, 0x10, 0x8011);
5336}
5337
Joe Perches953c96e2013-04-09 10:18:14 +00005338static bool tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Matt Carlson82cd3d12007-12-20 20:09:00 -08005340 u16 flowctrl;
Joe Perches953c96e2013-04-09 10:18:14 +00005341 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 u32 sg_dig_ctrl, sg_dig_status;
5343 u32 serdes_cfg, expected_sg_dig_ctrl;
5344 int workaround, port_a;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
5346 serdes_cfg = 0;
5347 expected_sg_dig_ctrl = 0;
5348 workaround = 0;
5349 port_a = 1;
Joe Perches953c96e2013-04-09 10:18:14 +00005350 current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Joe Perches41535772013-02-16 11:20:04 +00005352 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5353 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 workaround = 1;
5355 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5356 port_a = 0;
5357
5358 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5359 /* preserve bits 20-23 for voltage regulator */
5360 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5361 }
5362
5363 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5364
5365 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005366 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (workaround) {
5368 u32 val = serdes_cfg;
5369
5370 if (port_a)
5371 val |= 0xc010000;
5372 else
5373 val |= 0x4010000;
5374 tw32_f(MAC_SERDES_CFG, val);
5375 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005376
5377 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 }
5379 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5380 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005381 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 }
5383 goto out;
5384 }
5385
5386 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005387 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Matt Carlson82cd3d12007-12-20 20:09:00 -08005389 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5390 if (flowctrl & ADVERTISE_1000XPAUSE)
5391 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5392 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5393 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005396 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005397 tp->serdes_counter &&
5398 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5399 MAC_STATUS_RCVD_CFG)) ==
5400 MAC_STATUS_PCS_SYNCED)) {
5401 tp->serdes_counter--;
Joe Perches953c96e2013-04-09 10:18:14 +00005402 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005403 goto out;
5404 }
5405restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 if (workaround)
5407 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005408 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 udelay(5);
5410 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5411
Michael Chan3d3ebe72006-09-27 15:59:15 -07005412 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005413 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5415 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005416 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 mac_status = tr32(MAC_STATUS);
5418
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005419 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005421 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
Matt Carlson82cd3d12007-12-20 20:09:00 -08005423 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5424 local_adv |= ADVERTISE_1000XPAUSE;
5425 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5426 local_adv |= ADVERTISE_1000XPSE_ASYM;
5427
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005428 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005429 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005430 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005431 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Matt Carlson859edb22011-12-08 14:40:16 +00005433 tp->link_config.rmt_adv =
5434 mii_adv_to_ethtool_adv_x(remote_adv);
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 tg3_setup_flow_control(tp, local_adv, remote_adv);
Joe Perches953c96e2013-04-09 10:18:14 +00005437 current_link_up = true;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005438 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005439 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005440 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005441 if (tp->serdes_counter)
5442 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 else {
5444 if (workaround) {
5445 u32 val = serdes_cfg;
5446
5447 if (port_a)
5448 val |= 0xc010000;
5449 else
5450 val |= 0x4010000;
5451
5452 tw32_f(MAC_SERDES_CFG, val);
5453 }
5454
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005455 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 udelay(40);
5457
5458 /* Link parallel detection - link is up */
5459 /* only if we have PCS_SYNC and not */
5460 /* receiving config code words */
5461 mac_status = tr32(MAC_STATUS);
5462 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5463 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5464 tg3_setup_flow_control(tp, 0, 0);
Joe Perches953c96e2013-04-09 10:18:14 +00005465 current_link_up = true;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005466 tp->phy_flags |=
5467 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005468 tp->serdes_counter =
5469 SERDES_PARALLEL_DET_TIMEOUT;
5470 } else
5471 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005474 } else {
5475 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005476 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 }
5478
5479out:
5480 return current_link_up;
5481}
5482
Joe Perches953c96e2013-04-09 10:18:14 +00005483static bool tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
Joe Perches953c96e2013-04-09 10:18:14 +00005485 bool current_link_up = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005487 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005491 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005493
Matt Carlson5be73b42007-12-20 20:09:29 -08005494 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5495 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Matt Carlson5be73b42007-12-20 20:09:29 -08005497 if (txflags & ANEG_CFG_PS1)
5498 local_adv |= ADVERTISE_1000XPAUSE;
5499 if (txflags & ANEG_CFG_PS2)
5500 local_adv |= ADVERTISE_1000XPSE_ASYM;
5501
5502 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5503 remote_adv |= LPA_1000XPAUSE;
5504 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5505 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Matt Carlson859edb22011-12-08 14:40:16 +00005507 tp->link_config.rmt_adv =
5508 mii_adv_to_ethtool_adv_x(remote_adv);
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 tg3_setup_flow_control(tp, local_adv, remote_adv);
5511
Joe Perches953c96e2013-04-09 10:18:14 +00005512 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
5514 for (i = 0; i < 30; i++) {
5515 udelay(20);
5516 tw32_f(MAC_STATUS,
5517 (MAC_STATUS_SYNC_CHANGED |
5518 MAC_STATUS_CFG_CHANGED));
5519 udelay(40);
5520 if ((tr32(MAC_STATUS) &
5521 (MAC_STATUS_SYNC_CHANGED |
5522 MAC_STATUS_CFG_CHANGED)) == 0)
5523 break;
5524 }
5525
5526 mac_status = tr32(MAC_STATUS);
Joe Perches953c96e2013-04-09 10:18:14 +00005527 if (!current_link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5529 !(mac_status & MAC_STATUS_RCVD_CFG))
Joe Perches953c96e2013-04-09 10:18:14 +00005530 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005532 tg3_setup_flow_control(tp, 0, 0);
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 /* Forcing 1000FD link up. */
Joe Perches953c96e2013-04-09 10:18:14 +00005535 current_link_up = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5538 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005539
5540 tw32_f(MAC_MODE, tp->mac_mode);
5541 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
5543
5544out:
5545 return current_link_up;
5546}
5547
Joe Perches953c96e2013-04-09 10:18:14 +00005548static int tg3_setup_fiber_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
5550 u32 orig_pause_cfg;
5551 u16 orig_active_speed;
5552 u8 orig_active_duplex;
5553 u32 mac_status;
Joe Perches953c96e2013-04-09 10:18:14 +00005554 bool current_link_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 int i;
5556
Matt Carlson8d018622007-12-20 20:05:44 -08005557 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 orig_active_speed = tp->link_config.active_speed;
5559 orig_active_duplex = tp->link_config.active_duplex;
5560
Joe Perches63c3a662011-04-26 08:12:10 +00005561 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005562 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005563 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 mac_status = tr32(MAC_STATUS);
5565 mac_status &= (MAC_STATUS_PCS_SYNCED |
5566 MAC_STATUS_SIGNAL_DET |
5567 MAC_STATUS_CFG_CHANGED |
5568 MAC_STATUS_RCVD_CFG);
5569 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5570 MAC_STATUS_SIGNAL_DET)) {
5571 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5572 MAC_STATUS_CFG_CHANGED));
5573 return 0;
5574 }
5575 }
5576
5577 tw32_f(MAC_TX_AUTO_NEG, 0);
5578
5579 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5580 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5581 tw32_f(MAC_MODE, tp->mac_mode);
5582 udelay(40);
5583
Matt Carlson79eb6902010-02-17 15:17:03 +00005584 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 tg3_init_bcm8002(tp);
5586
5587 /* Enable link change event even when serdes polling. */
5588 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5589 udelay(40);
5590
Joe Perches953c96e2013-04-09 10:18:14 +00005591 current_link_up = false;
Matt Carlson859edb22011-12-08 14:40:16 +00005592 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 mac_status = tr32(MAC_STATUS);
5594
Joe Perches63c3a662011-04-26 08:12:10 +00005595 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5597 else
5598 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5599
Matt Carlson898a56f2009-08-28 14:02:40 +00005600 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005602 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 for (i = 0; i < 100; i++) {
5605 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5606 MAC_STATUS_CFG_CHANGED));
5607 udelay(5);
5608 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005609 MAC_STATUS_CFG_CHANGED |
5610 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 break;
5612 }
5613
5614 mac_status = tr32(MAC_STATUS);
5615 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
Joe Perches953c96e2013-04-09 10:18:14 +00005616 current_link_up = false;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005617 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5618 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 tw32_f(MAC_MODE, (tp->mac_mode |
5620 MAC_MODE_SEND_CONFIGS));
5621 udelay(1);
5622 tw32_f(MAC_MODE, tp->mac_mode);
5623 }
5624 }
5625
Joe Perches953c96e2013-04-09 10:18:14 +00005626 if (current_link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 tp->link_config.active_speed = SPEED_1000;
5628 tp->link_config.active_duplex = DUPLEX_FULL;
5629 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5630 LED_CTRL_LNKLED_OVERRIDE |
5631 LED_CTRL_1000MBPS_ON));
5632 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005633 tp->link_config.active_speed = SPEED_UNKNOWN;
5634 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5636 LED_CTRL_LNKLED_OVERRIDE |
5637 LED_CTRL_TRAFFIC_OVERRIDE));
5638 }
5639
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005640 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005641 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (orig_pause_cfg != now_pause_cfg ||
5643 orig_active_speed != tp->link_config.active_speed ||
5644 orig_active_duplex != tp->link_config.active_duplex)
5645 tg3_link_report(tp);
5646 }
5647
5648 return 0;
5649}
5650
Joe Perches953c96e2013-04-09 10:18:14 +00005651static int tg3_setup_fiber_mii_phy(struct tg3 *tp, bool force_reset)
Michael Chan747e8f82005-07-25 12:33:22 -07005652{
Joe Perches953c96e2013-04-09 10:18:14 +00005653 int err = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005654 u32 bmsr, bmcr;
Michael Chan85730a62013-04-09 08:48:06 +00005655 u16 current_speed = SPEED_UNKNOWN;
5656 u8 current_duplex = DUPLEX_UNKNOWN;
Joe Perches953c96e2013-04-09 10:18:14 +00005657 bool current_link_up = false;
Michael Chan85730a62013-04-09 08:48:06 +00005658 u32 local_adv, remote_adv, sgsr;
5659
5660 if ((tg3_asic_rev(tp) == ASIC_REV_5719 ||
5661 tg3_asic_rev(tp) == ASIC_REV_5720) &&
5662 !tg3_readphy(tp, SERDES_TG3_1000X_STATUS, &sgsr) &&
5663 (sgsr & SERDES_TG3_SGMII_MODE)) {
5664
5665 if (force_reset)
5666 tg3_phy_reset(tp);
5667
5668 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
5669
5670 if (!(sgsr & SERDES_TG3_LINK_UP)) {
5671 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5672 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005673 current_link_up = true;
Michael Chan85730a62013-04-09 08:48:06 +00005674 if (sgsr & SERDES_TG3_SPEED_1000) {
5675 current_speed = SPEED_1000;
5676 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5677 } else if (sgsr & SERDES_TG3_SPEED_100) {
5678 current_speed = SPEED_100;
5679 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5680 } else {
5681 current_speed = SPEED_10;
5682 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
5683 }
5684
5685 if (sgsr & SERDES_TG3_FULL_DUPLEX)
5686 current_duplex = DUPLEX_FULL;
5687 else
5688 current_duplex = DUPLEX_HALF;
5689 }
5690
5691 tw32_f(MAC_MODE, tp->mac_mode);
5692 udelay(40);
5693
5694 tg3_clear_mac_status(tp);
5695
5696 goto fiber_setup_done;
5697 }
Michael Chan747e8f82005-07-25 12:33:22 -07005698
5699 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5700 tw32_f(MAC_MODE, tp->mac_mode);
5701 udelay(40);
5702
Michael Chan3310e242013-04-09 08:48:05 +00005703 tg3_clear_mac_status(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005704
5705 if (force_reset)
5706 tg3_phy_reset(tp);
5707
Matt Carlson859edb22011-12-08 14:40:16 +00005708 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005709
5710 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5711 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005712 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005713 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5714 bmsr |= BMSR_LSTATUS;
5715 else
5716 bmsr &= ~BMSR_LSTATUS;
5717 }
Michael Chan747e8f82005-07-25 12:33:22 -07005718
5719 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5720
5721 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005722 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005723 /* do nothing, just check for link up at the end */
5724 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005725 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005726
5727 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005728 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5729 ADVERTISE_1000XPAUSE |
5730 ADVERTISE_1000XPSE_ASYM |
5731 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005732
Matt Carlson28011cf2011-11-16 18:36:59 -05005733 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005734 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005735
Matt Carlson28011cf2011-11-16 18:36:59 -05005736 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5737 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005738 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5739 tg3_writephy(tp, MII_BMCR, bmcr);
5740
5741 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005742 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005743 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005744
5745 return err;
5746 }
5747 } else {
5748 u32 new_bmcr;
5749
5750 bmcr &= ~BMCR_SPEED1000;
5751 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5752
5753 if (tp->link_config.duplex == DUPLEX_FULL)
5754 new_bmcr |= BMCR_FULLDPLX;
5755
5756 if (new_bmcr != bmcr) {
5757 /* BMCR_SPEED1000 is a reserved bit that needs
5758 * to be set on write.
5759 */
5760 new_bmcr |= BMCR_SPEED1000;
5761
5762 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005763 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005764 u32 adv;
5765
5766 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5767 adv &= ~(ADVERTISE_1000XFULL |
5768 ADVERTISE_1000XHALF |
5769 ADVERTISE_SLCT);
5770 tg3_writephy(tp, MII_ADVERTISE, adv);
5771 tg3_writephy(tp, MII_BMCR, bmcr |
5772 BMCR_ANRESTART |
5773 BMCR_ANENABLE);
5774 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005775 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005776 }
5777 tg3_writephy(tp, MII_BMCR, new_bmcr);
5778 bmcr = new_bmcr;
5779 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5780 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005781 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005782 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5783 bmsr |= BMSR_LSTATUS;
5784 else
5785 bmsr &= ~BMSR_LSTATUS;
5786 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005787 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005788 }
5789 }
5790
5791 if (bmsr & BMSR_LSTATUS) {
5792 current_speed = SPEED_1000;
Joe Perches953c96e2013-04-09 10:18:14 +00005793 current_link_up = true;
Michael Chan747e8f82005-07-25 12:33:22 -07005794 if (bmcr & BMCR_FULLDPLX)
5795 current_duplex = DUPLEX_FULL;
5796 else
5797 current_duplex = DUPLEX_HALF;
5798
Matt Carlsonef167e22007-12-20 20:10:01 -08005799 local_adv = 0;
5800 remote_adv = 0;
5801
Michael Chan747e8f82005-07-25 12:33:22 -07005802 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005803 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005804
5805 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5806 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5807 common = local_adv & remote_adv;
5808 if (common & (ADVERTISE_1000XHALF |
5809 ADVERTISE_1000XFULL)) {
5810 if (common & ADVERTISE_1000XFULL)
5811 current_duplex = DUPLEX_FULL;
5812 else
5813 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005814
5815 tp->link_config.rmt_adv =
5816 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005817 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005818 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005819 } else {
Joe Perches953c96e2013-04-09 10:18:14 +00005820 current_link_up = false;
Matt Carlson859a588792010-04-05 10:19:28 +00005821 }
Michael Chan747e8f82005-07-25 12:33:22 -07005822 }
5823 }
5824
Michael Chan85730a62013-04-09 08:48:06 +00005825fiber_setup_done:
Joe Perches953c96e2013-04-09 10:18:14 +00005826 if (current_link_up && current_duplex == DUPLEX_FULL)
Matt Carlsonef167e22007-12-20 20:10:01 -08005827 tg3_setup_flow_control(tp, local_adv, remote_adv);
5828
Michael Chan747e8f82005-07-25 12:33:22 -07005829 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5830 if (tp->link_config.active_duplex == DUPLEX_HALF)
5831 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5832
5833 tw32_f(MAC_MODE, tp->mac_mode);
5834 udelay(40);
5835
5836 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5837
5838 tp->link_config.active_speed = current_speed;
5839 tp->link_config.active_duplex = current_duplex;
5840
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005841 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005842 return err;
5843}
5844
5845static void tg3_serdes_parallel_detect(struct tg3 *tp)
5846{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005847 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005848 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005849 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005850 return;
5851 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005852
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005853 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005854 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5855 u32 bmcr;
5856
5857 tg3_readphy(tp, MII_BMCR, &bmcr);
5858 if (bmcr & BMCR_ANENABLE) {
5859 u32 phy1, phy2;
5860
5861 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005862 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5863 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005864
5865 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005866 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5867 MII_TG3_DSP_EXP1_INT_STAT);
5868 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5869 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005870
5871 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5872 /* We have signal detect and not receiving
5873 * config code words, link is up by parallel
5874 * detection.
5875 */
5876
5877 bmcr &= ~BMCR_ANENABLE;
5878 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5879 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005880 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005881 }
5882 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005883 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005884 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005885 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005886 u32 phy2;
5887
5888 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005889 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5890 MII_TG3_DSP_EXP1_INT_STAT);
5891 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005892 if (phy2 & 0x20) {
5893 u32 bmcr;
5894
5895 /* Config code words received, turn on autoneg. */
5896 tg3_readphy(tp, MII_BMCR, &bmcr);
5897 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5898
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005899 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005900
5901 }
5902 }
5903}
5904
Joe Perches953c96e2013-04-09 10:18:14 +00005905static int tg3_setup_phy(struct tg3 *tp, bool force_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005907 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 int err;
5909
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005910 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005912 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005913 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005914 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Joe Perches41535772013-02-16 11:20:04 +00005917 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005918 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005919
5920 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5921 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5922 scale = 65;
5923 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5924 scale = 6;
5925 else
5926 scale = 12;
5927
5928 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5929 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5930 tw32(GRC_MISC_CFG, val);
5931 }
5932
Matt Carlsonf2096f92011-04-05 14:22:48 +00005933 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5934 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005935 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5936 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005937 val |= tr32(MAC_TX_LENGTHS) &
5938 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5939 TX_LENGTHS_CNT_DWN_VAL_MSK);
5940
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 if (tp->link_config.active_speed == SPEED_1000 &&
5942 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005943 tw32(MAC_TX_LENGTHS, val |
5944 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005946 tw32(MAC_TX_LENGTHS, val |
5947 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Joe Perches63c3a662011-04-26 08:12:10 +00005949 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005950 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005952 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 } else {
5954 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5955 }
5956 }
5957
Joe Perches63c3a662011-04-26 08:12:10 +00005958 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005959 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005960 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005961 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5962 tp->pwrmgmt_thresh;
5963 else
5964 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5965 tw32(PCIE_PWR_MGMT_THRESH, val);
5966 }
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 return err;
5969}
5970
Matt Carlsonbe947302012-12-03 19:36:57 +00005971/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005972static u64 tg3_refclk_read(struct tg3 *tp)
5973{
5974 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5975 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5976}
5977
5978/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005979static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5980{
5981 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5982 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5983 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5984 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5985}
5986
Matt Carlson7d41e492012-12-03 19:36:58 +00005987static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5988static inline void tg3_full_unlock(struct tg3 *tp);
5989static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5990{
5991 struct tg3 *tp = netdev_priv(dev);
5992
5993 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5994 SOF_TIMESTAMPING_RX_SOFTWARE |
5995 SOF_TIMESTAMPING_SOFTWARE |
5996 SOF_TIMESTAMPING_TX_HARDWARE |
5997 SOF_TIMESTAMPING_RX_HARDWARE |
5998 SOF_TIMESTAMPING_RAW_HARDWARE;
5999
6000 if (tp->ptp_clock)
6001 info->phc_index = ptp_clock_index(tp->ptp_clock);
6002 else
6003 info->phc_index = -1;
6004
6005 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
6006
6007 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
6008 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
6009 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
6010 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
6011 return 0;
6012}
6013
6014static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
6015{
6016 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6017 bool neg_adj = false;
6018 u32 correction = 0;
6019
6020 if (ppb < 0) {
6021 neg_adj = true;
6022 ppb = -ppb;
6023 }
6024
6025 /* Frequency adjustment is performed using hardware with a 24 bit
6026 * accumulator and a programmable correction value. On each clk, the
6027 * correction value gets added to the accumulator and when it
6028 * overflows, the time counter is incremented/decremented.
6029 *
6030 * So conversion from ppb to correction value is
6031 * ppb * (1 << 24) / 1000000000
6032 */
6033 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
6034 TG3_EAV_REF_CLK_CORRECT_MASK;
6035
6036 tg3_full_lock(tp, 0);
6037
6038 if (correction)
6039 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
6040 TG3_EAV_REF_CLK_CORRECT_EN |
6041 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
6042 else
6043 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
6044
6045 tg3_full_unlock(tp);
6046
6047 return 0;
6048}
6049
6050static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
6051{
6052 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6053
6054 tg3_full_lock(tp, 0);
6055 tp->ptp_adjust += delta;
6056 tg3_full_unlock(tp);
6057
6058 return 0;
6059}
6060
6061static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
6062{
6063 u64 ns;
6064 u32 remainder;
6065 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6066
6067 tg3_full_lock(tp, 0);
6068 ns = tg3_refclk_read(tp);
6069 ns += tp->ptp_adjust;
6070 tg3_full_unlock(tp);
6071
6072 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
6073 ts->tv_nsec = remainder;
6074
6075 return 0;
6076}
6077
6078static int tg3_ptp_settime(struct ptp_clock_info *ptp,
6079 const struct timespec *ts)
6080{
6081 u64 ns;
6082 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
6083
6084 ns = timespec_to_ns(ts);
6085
6086 tg3_full_lock(tp, 0);
6087 tg3_refclk_write(tp, ns);
6088 tp->ptp_adjust = 0;
6089 tg3_full_unlock(tp);
6090
6091 return 0;
6092}
6093
6094static int tg3_ptp_enable(struct ptp_clock_info *ptp,
6095 struct ptp_clock_request *rq, int on)
6096{
6097 return -EOPNOTSUPP;
6098}
6099
6100static const struct ptp_clock_info tg3_ptp_caps = {
6101 .owner = THIS_MODULE,
6102 .name = "tg3 clock",
6103 .max_adj = 250000000,
6104 .n_alarm = 0,
6105 .n_ext_ts = 0,
6106 .n_per_out = 0,
6107 .pps = 0,
6108 .adjfreq = tg3_ptp_adjfreq,
6109 .adjtime = tg3_ptp_adjtime,
6110 .gettime = tg3_ptp_gettime,
6111 .settime = tg3_ptp_settime,
6112 .enable = tg3_ptp_enable,
6113};
6114
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006115static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
6116 struct skb_shared_hwtstamps *timestamp)
6117{
6118 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
6119 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
6120 tp->ptp_adjust);
6121}
6122
Matt Carlsonbe947302012-12-03 19:36:57 +00006123/* tp->lock must be held */
6124static void tg3_ptp_init(struct tg3 *tp)
6125{
6126 if (!tg3_flag(tp, PTP_CAPABLE))
6127 return;
6128
6129 /* Initialize the hardware clock to the system time. */
6130 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
6131 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00006132 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00006133}
6134
6135/* tp->lock must be held */
6136static void tg3_ptp_resume(struct tg3 *tp)
6137{
6138 if (!tg3_flag(tp, PTP_CAPABLE))
6139 return;
6140
6141 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
6142 tp->ptp_adjust = 0;
6143}
6144
6145static void tg3_ptp_fini(struct tg3 *tp)
6146{
6147 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
6148 return;
6149
Matt Carlson7d41e492012-12-03 19:36:58 +00006150 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00006151 tp->ptp_clock = NULL;
6152 tp->ptp_adjust = 0;
6153}
6154
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006155static inline int tg3_irq_sync(struct tg3 *tp)
6156{
6157 return tp->irq_sync;
6158}
6159
Matt Carlson97bd8e42011-04-13 11:05:04 +00006160static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
6161{
6162 int i;
6163
6164 dst = (u32 *)((u8 *)dst + off);
6165 for (i = 0; i < len; i += sizeof(u32))
6166 *dst++ = tr32(off + i);
6167}
6168
6169static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
6170{
6171 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
6172 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
6173 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
6174 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
6175 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
6176 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
6177 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
6178 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
6179 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
6180 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
6181 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
6182 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
6183 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
6184 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
6185 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
6186 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
6187 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
6188 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
6189 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
6190
Joe Perches63c3a662011-04-26 08:12:10 +00006191 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006192 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
6193
6194 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
6195 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
6196 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
6197 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
6198 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
6199 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
6200 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
6201 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
6202
Joe Perches63c3a662011-04-26 08:12:10 +00006203 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006204 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
6205 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
6206 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
6207 }
6208
6209 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
6210 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
6211 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
6212 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
6213 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
6214
Joe Perches63c3a662011-04-26 08:12:10 +00006215 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00006216 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
6217}
6218
6219static void tg3_dump_state(struct tg3 *tp)
6220{
6221 int i;
6222 u32 *regs;
6223
6224 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00006225 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00006226 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00006227
Joe Perches63c3a662011-04-26 08:12:10 +00006228 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00006229 /* Read up to but not including private PCI registers */
6230 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
6231 regs[i / sizeof(u32)] = tr32(i);
6232 } else
6233 tg3_dump_legacy_regs(tp, regs);
6234
6235 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
6236 if (!regs[i + 0] && !regs[i + 1] &&
6237 !regs[i + 2] && !regs[i + 3])
6238 continue;
6239
6240 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
6241 i * 4,
6242 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
6243 }
6244
6245 kfree(regs);
6246
6247 for (i = 0; i < tp->irq_cnt; i++) {
6248 struct tg3_napi *tnapi = &tp->napi[i];
6249
6250 /* SW status block */
6251 netdev_err(tp->dev,
6252 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
6253 i,
6254 tnapi->hw_status->status,
6255 tnapi->hw_status->status_tag,
6256 tnapi->hw_status->rx_jumbo_consumer,
6257 tnapi->hw_status->rx_consumer,
6258 tnapi->hw_status->rx_mini_consumer,
6259 tnapi->hw_status->idx[0].rx_producer,
6260 tnapi->hw_status->idx[0].tx_consumer);
6261
6262 netdev_err(tp->dev,
6263 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
6264 i,
6265 tnapi->last_tag, tnapi->last_irq_tag,
6266 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
6267 tnapi->rx_rcb_ptr,
6268 tnapi->prodring.rx_std_prod_idx,
6269 tnapi->prodring.rx_std_cons_idx,
6270 tnapi->prodring.rx_jmb_prod_idx,
6271 tnapi->prodring.rx_jmb_cons_idx);
6272 }
6273}
6274
Michael Chandf3e6542006-05-26 17:48:07 -07006275/* This is called whenever we suspect that the system chipset is re-
6276 * ordering the sequence of MMIO to the tx send mailbox. The symptom
6277 * is bogus tx completions. We try to recover by setting the
6278 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
6279 * in the workqueue.
6280 */
6281static void tg3_tx_recover(struct tg3 *tp)
6282{
Joe Perches63c3a662011-04-26 08:12:10 +00006283 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07006284 tp->write32_tx_mbox == tg3_write_indirect_mbox);
6285
Matt Carlson5129c3a2010-04-05 10:19:23 +00006286 netdev_warn(tp->dev,
6287 "The system may be re-ordering memory-mapped I/O "
6288 "cycles to the network device, attempting to recover. "
6289 "Please report the problem to the driver maintainer "
6290 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07006291
6292 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006293 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006294 spin_unlock(&tp->lock);
6295}
6296
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006297static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07006298{
Matt Carlsonf65aac12010-08-02 11:26:03 +00006299 /* Tell compiler to fetch tx indices from memory. */
6300 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006301 return tnapi->tx_pending -
6302 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07006303}
6304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305/* Tigon3 never reports partial packet sends. So we do not
6306 * need special logic to handle SKBs that have not had all
6307 * of their frags sent yet, like SunGEM does.
6308 */
Matt Carlson17375d22009-08-28 14:02:18 +00006309static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
Matt Carlson17375d22009-08-28 14:02:18 +00006311 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006312 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006313 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006314 struct netdev_queue *txq;
6315 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00006316 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006317
Joe Perches63c3a662011-04-26 08:12:10 +00006318 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006319 index--;
6320
6321 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
6323 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006324 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07006326 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
Michael Chandf3e6542006-05-26 17:48:07 -07006328 if (unlikely(skb == NULL)) {
6329 tg3_tx_recover(tp);
6330 return;
6331 }
6332
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006333 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
6334 struct skb_shared_hwtstamps timestamp;
6335 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
6336 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
6337
6338 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
6339
6340 skb_tstamp_tx(skb, &timestamp);
6341 }
6342
Alexander Duyckf4188d82009-12-02 16:48:38 +00006343 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006344 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006345 skb_headlen(skb),
6346 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
6348 ri->skb = NULL;
6349
Matt Carlsone01ee142011-07-27 14:20:50 +00006350 while (ri->fragmented) {
6351 ri->fragmented = false;
6352 sw_idx = NEXT_TX(sw_idx);
6353 ri = &tnapi->tx_buffers[sw_idx];
6354 }
6355
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 sw_idx = NEXT_TX(sw_idx);
6357
6358 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006359 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07006360 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
6361 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006362
6363 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006364 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006365 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006366 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006367
6368 while (ri->fragmented) {
6369 ri->fragmented = false;
6370 sw_idx = NEXT_TX(sw_idx);
6371 ri = &tnapi->tx_buffers[sw_idx];
6372 }
6373
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 sw_idx = NEXT_TX(sw_idx);
6375 }
6376
Tom Herbert298376d2011-11-28 16:33:30 +00006377 pkts_compl++;
6378 bytes_compl += skb->len;
6379
David S. Millerf47c11e2005-06-24 20:18:35 -07006380 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006381
6382 if (unlikely(tx_bug)) {
6383 tg3_tx_recover(tp);
6384 return;
6385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 }
6387
Tom Herbert5cb917b2012-03-05 19:53:50 +00006388 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006389
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006390 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Michael Chan1b2a7202006-08-07 21:46:02 -07006392 /* Need to make the tx_cons update visible to tg3_start_xmit()
6393 * before checking for netif_queue_stopped(). Without the
6394 * memory barrier, there is a small possibility that tg3_start_xmit()
6395 * will miss it and cause the queue to be stopped forever.
6396 */
6397 smp_mb();
6398
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006399 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006400 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006401 __netif_tx_lock(txq, smp_processor_id());
6402 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006403 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006404 netif_tx_wake_queue(txq);
6405 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407}
6408
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006409static void tg3_frag_free(bool is_frag, void *data)
6410{
6411 if (is_frag)
6412 put_page(virt_to_head_page(data));
6413 else
6414 kfree(data);
6415}
6416
Eric Dumazet9205fd92011-11-18 06:47:01 +00006417static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006418{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006419 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6420 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6421
Eric Dumazet9205fd92011-11-18 06:47:01 +00006422 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006423 return;
6424
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006425 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006426 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006427 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006428 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006429}
6430
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006431
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432/* Returns size of skb allocated or < 0 on error.
6433 *
6434 * We only need to fill in the address because the other members
6435 * of the RX descriptor are invariant, see tg3_init_rings.
6436 *
6437 * Note the purposeful assymetry of cpu vs. chip accesses. For
6438 * posting buffers we only dirty the first cache line of the RX
6439 * descriptor (containing the address). Whereas for the RX status
6440 * buffers the cpu only reads the last cacheline of the RX descriptor
6441 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6442 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006443static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006444 u32 opaque_key, u32 dest_idx_unmasked,
6445 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446{
6447 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006448 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006449 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006451 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 switch (opaque_key) {
6454 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006455 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006456 desc = &tpr->rx_std[dest_idx];
6457 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006458 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 break;
6460
6461 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006462 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006463 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006464 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006465 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 break;
6467
6468 default:
6469 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
6472 /* Do not overwrite any of the map or rp information
6473 * until we are sure we can commit to a new buffer.
6474 *
6475 * Callers depend upon this behavior and assume that
6476 * we leave everything unchanged if we fail.
6477 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006478 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6479 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006480 if (skb_size <= PAGE_SIZE) {
6481 data = netdev_alloc_frag(skb_size);
6482 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006483 } else {
6484 data = kmalloc(skb_size, GFP_ATOMIC);
6485 *frag_size = 0;
6486 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006487 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 return -ENOMEM;
6489
Eric Dumazet9205fd92011-11-18 06:47:01 +00006490 mapping = pci_map_single(tp->pdev,
6491 data + TG3_RX_OFFSET(tp),
6492 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006494 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006495 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006496 return -EIO;
6497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
Eric Dumazet9205fd92011-11-18 06:47:01 +00006499 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006500 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 desc->addr_hi = ((u64)mapping >> 32);
6503 desc->addr_lo = ((u64)mapping & 0xffffffff);
6504
Eric Dumazet9205fd92011-11-18 06:47:01 +00006505 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
6507
6508/* We only need to move over in the address because the other
6509 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006510 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 */
Matt Carlsona3896162009-11-13 13:03:44 +00006512static void tg3_recycle_rx(struct tg3_napi *tnapi,
6513 struct tg3_rx_prodring_set *dpr,
6514 u32 opaque_key, int src_idx,
6515 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516{
Matt Carlson17375d22009-08-28 14:02:18 +00006517 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6519 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006520 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006521 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
6523 switch (opaque_key) {
6524 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006525 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006526 dest_desc = &dpr->rx_std[dest_idx];
6527 dest_map = &dpr->rx_std_buffers[dest_idx];
6528 src_desc = &spr->rx_std[src_idx];
6529 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 break;
6531
6532 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006533 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006534 dest_desc = &dpr->rx_jmb[dest_idx].std;
6535 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6536 src_desc = &spr->rx_jmb[src_idx].std;
6537 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 break;
6539
6540 default:
6541 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
Eric Dumazet9205fd92011-11-18 06:47:01 +00006544 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006545 dma_unmap_addr_set(dest_map, mapping,
6546 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 dest_desc->addr_hi = src_desc->addr_hi;
6548 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006549
6550 /* Ensure that the update to the skb happens after the physical
6551 * addresses have been transferred to the new BD location.
6552 */
6553 smp_wmb();
6554
Eric Dumazet9205fd92011-11-18 06:47:01 +00006555 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556}
6557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558/* The RX ring scheme is composed of multiple rings which post fresh
6559 * buffers to the chip, and one special ring the chip uses to report
6560 * status back to the host.
6561 *
6562 * The special ring reports the status of received packets to the
6563 * host. The chip does not write into the original descriptor the
6564 * RX buffer was obtained from. The chip simply takes the original
6565 * descriptor as provided by the host, updates the status and length
6566 * field, then writes this into the next status ring entry.
6567 *
6568 * Each ring the host uses to post buffers to the chip is described
6569 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6570 * it is first placed into the on-chip ram. When the packet's length
6571 * is known, it walks down the TG3_BDINFO entries to select the ring.
6572 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6573 * which is within the range of the new packet's length is chosen.
6574 *
6575 * The "separate ring for rx status" scheme may sound queer, but it makes
6576 * sense from a cache coherency perspective. If only the host writes
6577 * to the buffer post rings, and only the chip writes to the rx status
6578 * rings, then cache lines never move beyond shared-modified state.
6579 * If both the host and chip were to write into the same ring, cache line
6580 * eviction could occur since both entities want it in an exclusive state.
6581 */
Matt Carlson17375d22009-08-28 14:02:18 +00006582static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583{
Matt Carlson17375d22009-08-28 14:02:18 +00006584 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006585 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006586 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006587 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006588 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006590 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006592 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 /*
6594 * We need to order the read of hw_idx and the read of
6595 * the opaque cookie.
6596 */
6597 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 work_mask = 0;
6599 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006600 std_prod_idx = tpr->rx_std_prod_idx;
6601 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006603 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006604 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 unsigned int len;
6606 struct sk_buff *skb;
6607 dma_addr_t dma_addr;
6608 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006609 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006610 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
6612 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6613 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6614 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006615 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006616 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006617 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006618 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006619 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006621 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006622 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006623 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006624 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006625 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
6628 work_mask |= opaque_key;
6629
6630 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6631 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6632 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006633 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 desc_idx, *post_ptr);
6635 drop_it_no_recycle:
6636 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006637 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 goto next_pkt;
6639 }
6640
Eric Dumazet9205fd92011-11-18 06:47:01 +00006641 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006642 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6643 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006645 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6646 RXD_FLAG_PTPSTAT_PTPV1 ||
6647 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6648 RXD_FLAG_PTPSTAT_PTPV2) {
6649 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6650 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6651 }
6652
Matt Carlsond2757fc2010-04-12 06:58:27 +00006653 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006655 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Eric Dumazet9205fd92011-11-18 06:47:01 +00006657 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006658 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 if (skb_size < 0)
6660 goto drop_it;
6661
Matt Carlson287be122009-08-28 13:58:46 +00006662 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 PCI_DMA_FROMDEVICE);
6664
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006665 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006666 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006667 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006668 goto drop_it_no_recycle;
6669 }
6670 skb_reserve(skb, TG3_RX_OFFSET(tp));
6671 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006672 * after the usage of the old DMA mapping.
6673 */
6674 smp_wmb();
6675
Eric Dumazet9205fd92011-11-18 06:47:01 +00006676 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006677
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006679 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 desc_idx, *post_ptr);
6681
Eric Dumazet9205fd92011-11-18 06:47:01 +00006682 skb = netdev_alloc_skb(tp->dev,
6683 len + TG3_RAW_IP_ALIGN);
6684 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 goto drop_it_no_recycle;
6686
Eric Dumazet9205fd92011-11-18 06:47:01 +00006687 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006689 memcpy(skb->data,
6690 data + TG3_RX_OFFSET(tp),
6691 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 }
6694
Eric Dumazet9205fd92011-11-18 06:47:01 +00006695 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006696 if (tstamp)
6697 tg3_hwclock_to_timestamp(tp, tstamp,
6698 skb_hwtstamps(skb));
6699
Michał Mirosławdc668912011-04-07 03:35:07 +00006700 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6702 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6703 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6704 skb->ip_summed = CHECKSUM_UNNECESSARY;
6705 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006706 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
6708 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006709
6710 if (len > (tp->dev->mtu + ETH_HLEN) &&
6711 skb->protocol != htons(ETH_P_8021Q)) {
6712 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006713 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006714 }
6715
Matt Carlson9dc7a112010-04-12 06:58:28 +00006716 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006717 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6718 __vlan_hwaccel_put_tag(skb,
6719 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006720
Matt Carlsonbf933c82011-01-25 15:58:49 +00006721 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 received++;
6724 budget--;
6725
6726next_pkt:
6727 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006728
6729 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006730 tpr->rx_std_prod_idx = std_prod_idx &
6731 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006732 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6733 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006734 work_mask &= ~RXD_OPAQUE_RING_STD;
6735 rx_std_posted = 0;
6736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006738 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006739 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006740
6741 /* Refresh hw_idx to see if there is new work */
6742 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006743 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006744 rmb();
6745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 }
6747
6748 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006749 tnapi->rx_rcb_ptr = sw_idx;
6750 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751
6752 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006753 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006754 /* Sync BD data before updating mailbox */
6755 wmb();
6756
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006757 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006758 tpr->rx_std_prod_idx = std_prod_idx &
6759 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006760 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6761 tpr->rx_std_prod_idx);
6762 }
6763 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006764 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6765 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006766 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6767 tpr->rx_jmb_prod_idx);
6768 }
6769 mmiowb();
6770 } else if (work_mask) {
6771 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6772 * updated before the producer indices can be updated.
6773 */
6774 smp_wmb();
6775
Matt Carlson2c49a442010-09-30 10:34:35 +00006776 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6777 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006778
Michael Chan7ae52892012-03-21 15:38:33 +00006779 if (tnapi != &tp->napi[1]) {
6780 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006781 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
6785 return received;
6786}
6787
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006788static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006791 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006792 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6793
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 if (sblk->status & SD_STATUS_LINK_CHG) {
6795 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006796 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006797 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006798 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006799 tw32_f(MAC_STATUS,
6800 (MAC_STATUS_SYNC_CHANGED |
6801 MAC_STATUS_CFG_CHANGED |
6802 MAC_STATUS_MI_COMPLETION |
6803 MAC_STATUS_LNKSTATE_CHANGED));
6804 udelay(40);
6805 } else
Joe Perches953c96e2013-04-09 10:18:14 +00006806 tg3_setup_phy(tp, false);
David S. Millerf47c11e2005-06-24 20:18:35 -07006807 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 }
6809 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006810}
6811
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006812static int tg3_rx_prodring_xfer(struct tg3 *tp,
6813 struct tg3_rx_prodring_set *dpr,
6814 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006815{
6816 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006817 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006818
6819 while (1) {
6820 src_prod_idx = spr->rx_std_prod_idx;
6821
6822 /* Make sure updates to the rx_std_buffers[] entries and the
6823 * standard producer index are seen in the correct order.
6824 */
6825 smp_rmb();
6826
6827 if (spr->rx_std_cons_idx == src_prod_idx)
6828 break;
6829
6830 if (spr->rx_std_cons_idx < src_prod_idx)
6831 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6832 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006833 cpycnt = tp->rx_std_ring_mask + 1 -
6834 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006835
Matt Carlson2c49a442010-09-30 10:34:35 +00006836 cpycnt = min(cpycnt,
6837 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006838
6839 si = spr->rx_std_cons_idx;
6840 di = dpr->rx_std_prod_idx;
6841
Matt Carlsone92967b2010-02-12 14:47:06 +00006842 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006843 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006844 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006845 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006846 break;
6847 }
6848 }
6849
6850 if (!cpycnt)
6851 break;
6852
6853 /* Ensure that updates to the rx_std_buffers ring and the
6854 * shadowed hardware producer ring from tg3_recycle_skb() are
6855 * ordered correctly WRT the skb check above.
6856 */
6857 smp_rmb();
6858
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006859 memcpy(&dpr->rx_std_buffers[di],
6860 &spr->rx_std_buffers[si],
6861 cpycnt * sizeof(struct ring_info));
6862
6863 for (i = 0; i < cpycnt; i++, di++, si++) {
6864 struct tg3_rx_buffer_desc *sbd, *dbd;
6865 sbd = &spr->rx_std[si];
6866 dbd = &dpr->rx_std[di];
6867 dbd->addr_hi = sbd->addr_hi;
6868 dbd->addr_lo = sbd->addr_lo;
6869 }
6870
Matt Carlson2c49a442010-09-30 10:34:35 +00006871 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6872 tp->rx_std_ring_mask;
6873 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6874 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006875 }
6876
6877 while (1) {
6878 src_prod_idx = spr->rx_jmb_prod_idx;
6879
6880 /* Make sure updates to the rx_jmb_buffers[] entries and
6881 * the jumbo producer index are seen in the correct order.
6882 */
6883 smp_rmb();
6884
6885 if (spr->rx_jmb_cons_idx == src_prod_idx)
6886 break;
6887
6888 if (spr->rx_jmb_cons_idx < src_prod_idx)
6889 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6890 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006891 cpycnt = tp->rx_jmb_ring_mask + 1 -
6892 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006893
6894 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006895 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006896
6897 si = spr->rx_jmb_cons_idx;
6898 di = dpr->rx_jmb_prod_idx;
6899
Matt Carlsone92967b2010-02-12 14:47:06 +00006900 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006901 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006902 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006903 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006904 break;
6905 }
6906 }
6907
6908 if (!cpycnt)
6909 break;
6910
6911 /* Ensure that updates to the rx_jmb_buffers ring and the
6912 * shadowed hardware producer ring from tg3_recycle_skb() are
6913 * ordered correctly WRT the skb check above.
6914 */
6915 smp_rmb();
6916
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006917 memcpy(&dpr->rx_jmb_buffers[di],
6918 &spr->rx_jmb_buffers[si],
6919 cpycnt * sizeof(struct ring_info));
6920
6921 for (i = 0; i < cpycnt; i++, di++, si++) {
6922 struct tg3_rx_buffer_desc *sbd, *dbd;
6923 sbd = &spr->rx_jmb[si].std;
6924 dbd = &dpr->rx_jmb[di].std;
6925 dbd->addr_hi = sbd->addr_hi;
6926 dbd->addr_lo = sbd->addr_lo;
6927 }
6928
Matt Carlson2c49a442010-09-30 10:34:35 +00006929 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6930 tp->rx_jmb_ring_mask;
6931 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6932 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006933 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006934
6935 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006936}
6937
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006938static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6939{
6940 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006943 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006944 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006945 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006946 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 }
6948
Matt Carlsonf891ea12012-04-24 13:37:01 +00006949 if (!tnapi->rx_rcb_prod_idx)
6950 return work_done;
6951
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 /* run RX thread, within the bounds set by NAPI.
6953 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006954 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006956 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006957 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958
Joe Perches63c3a662011-04-26 08:12:10 +00006959 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006960 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006961 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006962 u32 std_prod_idx = dpr->rx_std_prod_idx;
6963 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006964
Michael Chan7ae52892012-03-21 15:38:33 +00006965 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006966 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006967 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006968 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006969
6970 wmb();
6971
Matt Carlsone4af1af2010-02-12 14:47:05 +00006972 if (std_prod_idx != dpr->rx_std_prod_idx)
6973 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6974 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006975
Matt Carlsone4af1af2010-02-12 14:47:05 +00006976 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6977 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6978 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006979
6980 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006981
6982 if (err)
6983 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006984 }
6985
David S. Miller6f535762007-10-11 18:08:29 -07006986 return work_done;
6987}
David S. Millerf7383c22005-05-18 22:50:53 -07006988
Matt Carlsondb219972011-11-04 09:15:03 +00006989static inline void tg3_reset_task_schedule(struct tg3 *tp)
6990{
6991 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6992 schedule_work(&tp->reset_task);
6993}
6994
6995static inline void tg3_reset_task_cancel(struct tg3 *tp)
6996{
6997 cancel_work_sync(&tp->reset_task);
6998 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006999 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00007000}
7001
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007002static int tg3_poll_msix(struct napi_struct *napi, int budget)
7003{
7004 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7005 struct tg3 *tp = tnapi->tp;
7006 int work_done = 0;
7007 struct tg3_hw_status *sblk = tnapi->hw_status;
7008
7009 while (1) {
7010 work_done = tg3_poll_work(tnapi, work_done, budget);
7011
Joe Perches63c3a662011-04-26 08:12:10 +00007012 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007013 goto tx_recovery;
7014
7015 if (unlikely(work_done >= budget))
7016 break;
7017
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007018 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007019 * to tell the hw how much work has been processed,
7020 * so we must read it before checking for more work.
7021 */
7022 tnapi->last_tag = sblk->status_tag;
7023 tnapi->last_irq_tag = tnapi->last_tag;
7024 rmb();
7025
7026 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00007027 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
7028 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00007029
7030 /* This test here is not race free, but will reduce
7031 * the number of interrupts by looping again.
7032 */
7033 if (tnapi == &tp->napi[1] && tp->rx_refill)
7034 continue;
7035
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007036 napi_complete(napi);
7037 /* Reenable interrupts. */
7038 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00007039
7040 /* This test here is synchronized by napi_schedule()
7041 * and napi_complete() to close the race condition.
7042 */
7043 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
7044 tw32(HOSTCC_MODE, tp->coalesce_mode |
7045 HOSTCC_MODE_ENABLE |
7046 tnapi->coal_now);
7047 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007048 mmiowb();
7049 break;
7050 }
7051 }
7052
7053 return work_done;
7054
7055tx_recovery:
7056 /* work_done is guaranteed to be less than budget. */
7057 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007058 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007059 return work_done;
7060}
7061
Matt Carlsone64de4e2011-04-13 11:05:05 +00007062static void tg3_process_error(struct tg3 *tp)
7063{
7064 u32 val;
7065 bool real_error = false;
7066
Joe Perches63c3a662011-04-26 08:12:10 +00007067 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00007068 return;
7069
7070 /* Check Flow Attention register */
7071 val = tr32(HOSTCC_FLOW_ATTN);
7072 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
7073 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
7074 real_error = true;
7075 }
7076
7077 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
7078 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
7079 real_error = true;
7080 }
7081
7082 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
7083 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
7084 real_error = true;
7085 }
7086
7087 if (!real_error)
7088 return;
7089
7090 tg3_dump_state(tp);
7091
Joe Perches63c3a662011-04-26 08:12:10 +00007092 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00007093 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00007094}
7095
David S. Miller6f535762007-10-11 18:08:29 -07007096static int tg3_poll(struct napi_struct *napi, int budget)
7097{
Matt Carlson8ef04422009-08-28 14:01:37 +00007098 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
7099 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07007100 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00007101 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07007102
7103 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00007104 if (sblk->status & SD_STATUS_ERROR)
7105 tg3_process_error(tp);
7106
Matt Carlson35f2d7d2009-11-13 13:03:41 +00007107 tg3_poll_link(tp);
7108
Matt Carlson17375d22009-08-28 14:02:18 +00007109 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07007110
Joe Perches63c3a662011-04-26 08:12:10 +00007111 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07007112 goto tx_recovery;
7113
7114 if (unlikely(work_done >= budget))
7115 break;
7116
Joe Perches63c3a662011-04-26 08:12:10 +00007117 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00007118 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07007119 * to tell the hw how much work has been processed,
7120 * so we must read it before checking for more work.
7121 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007122 tnapi->last_tag = sblk->status_tag;
7123 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07007124 rmb();
7125 } else
7126 sblk->status &= ~SD_STATUS_UPDATED;
7127
Matt Carlson17375d22009-08-28 14:02:18 +00007128 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08007129 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00007130 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07007131 break;
7132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 }
7134
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007135 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07007136
7137tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07007138 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08007139 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00007140 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07007141 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142}
7143
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007144static void tg3_napi_disable(struct tg3 *tp)
7145{
7146 int i;
7147
7148 for (i = tp->irq_cnt - 1; i >= 0; i--)
7149 napi_disable(&tp->napi[i].napi);
7150}
7151
7152static void tg3_napi_enable(struct tg3 *tp)
7153{
7154 int i;
7155
7156 for (i = 0; i < tp->irq_cnt; i++)
7157 napi_enable(&tp->napi[i].napi);
7158}
7159
7160static void tg3_napi_init(struct tg3 *tp)
7161{
7162 int i;
7163
7164 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
7165 for (i = 1; i < tp->irq_cnt; i++)
7166 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
7167}
7168
7169static void tg3_napi_fini(struct tg3 *tp)
7170{
7171 int i;
7172
7173 for (i = 0; i < tp->irq_cnt; i++)
7174 netif_napi_del(&tp->napi[i].napi);
7175}
7176
7177static inline void tg3_netif_stop(struct tg3 *tp)
7178{
7179 tp->dev->trans_start = jiffies; /* prevent tx timeout */
7180 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007181 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007182 netif_tx_disable(tp->dev);
7183}
7184
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00007185/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007186static inline void tg3_netif_start(struct tg3 *tp)
7187{
Matt Carlsonbe947302012-12-03 19:36:57 +00007188 tg3_ptp_resume(tp);
7189
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007190 /* NOTE: unconditional netif_tx_wake_all_queues is only
7191 * appropriate so long as all callers are assured to
7192 * have free tx slots (such as after tg3_init_hw)
7193 */
7194 netif_tx_wake_all_queues(tp->dev);
7195
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00007196 if (tp->link_up)
7197 netif_carrier_on(tp->dev);
7198
Matt Carlson66cfd1b2010-09-30 10:34:30 +00007199 tg3_napi_enable(tp);
7200 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
7201 tg3_enable_ints(tp);
7202}
7203
David S. Millerf47c11e2005-06-24 20:18:35 -07007204static void tg3_irq_quiesce(struct tg3 *tp)
7205{
Matt Carlson4f125f42009-09-01 12:55:02 +00007206 int i;
7207
David S. Millerf47c11e2005-06-24 20:18:35 -07007208 BUG_ON(tp->irq_sync);
7209
7210 tp->irq_sync = 1;
7211 smp_mb();
7212
Matt Carlson4f125f42009-09-01 12:55:02 +00007213 for (i = 0; i < tp->irq_cnt; i++)
7214 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07007215}
7216
David S. Millerf47c11e2005-06-24 20:18:35 -07007217/* Fully shutdown all tg3 driver activity elsewhere in the system.
7218 * If irq_sync is non-zero, then the IRQ handler must be synchronized
7219 * with as well. Most of the time, this is not necessary except when
7220 * shutting down the device.
7221 */
7222static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
7223{
Michael Chan46966542007-07-11 19:47:19 -07007224 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07007225 if (irq_sync)
7226 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007227}
7228
7229static inline void tg3_full_unlock(struct tg3 *tp)
7230{
David S. Millerf47c11e2005-06-24 20:18:35 -07007231 spin_unlock_bh(&tp->lock);
7232}
7233
Michael Chanfcfa0a32006-03-20 22:28:41 -08007234/* One-shot MSI handler - Chip automatically disables interrupt
7235 * after sending MSI so driver doesn't have to do it.
7236 */
David Howells7d12e782006-10-05 14:55:46 +01007237static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08007238{
Matt Carlson09943a12009-08-28 14:01:57 +00007239 struct tg3_napi *tnapi = dev_id;
7240 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08007241
Matt Carlson898a56f2009-08-28 14:02:40 +00007242 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007243 if (tnapi->rx_rcb)
7244 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007245
7246 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007247 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08007248
7249 return IRQ_HANDLED;
7250}
7251
Michael Chan88b06bc22005-04-21 17:13:25 -07007252/* MSI ISR - No need to check for interrupt sharing and no need to
7253 * flush status block and interrupt mailbox. PCI ordering rules
7254 * guarantee that MSI will arrive after the status block.
7255 */
David Howells7d12e782006-10-05 14:55:46 +01007256static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07007257{
Matt Carlson09943a12009-08-28 14:01:57 +00007258 struct tg3_napi *tnapi = dev_id;
7259 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07007260
Matt Carlson898a56f2009-08-28 14:02:40 +00007261 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007262 if (tnapi->rx_rcb)
7263 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07007264 /*
David S. Millerfac9b832005-05-18 22:46:34 -07007265 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07007266 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07007267 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07007268 * NIC to stop sending us irqs, engaging "in-intr-handler"
7269 * event coalescing.
7270 */
Matt Carlson5b39de92011-08-31 11:44:50 +00007271 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07007272 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00007273 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07007274
Michael Chan88b06bc22005-04-21 17:13:25 -07007275 return IRQ_RETVAL(1);
7276}
7277
David Howells7d12e782006-10-05 14:55:46 +01007278static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279{
Matt Carlson09943a12009-08-28 14:01:57 +00007280 struct tg3_napi *tnapi = dev_id;
7281 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007282 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 unsigned int handled = 1;
7284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 /* In INTx mode, it is possible for the interrupt to arrive at
7286 * the CPU before the status block posted prior to the interrupt.
7287 * Reading the PCI State register will confirm whether the
7288 * interrupt is ours and will flush the status block.
7289 */
Michael Chand18edcb2007-03-24 20:57:11 -07007290 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00007291 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007292 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7293 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007294 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07007295 }
Michael Chand18edcb2007-03-24 20:57:11 -07007296 }
7297
7298 /*
7299 * Writing any value to intr-mbox-0 clears PCI INTA# and
7300 * chip-internal interrupt pending events.
7301 * Writing non-zero to intr-mbox-0 additional tells the
7302 * NIC to stop sending us irqs, engaging "in-intr-handler"
7303 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007304 *
7305 * Flush the mailbox to de-assert the IRQ immediately to prevent
7306 * spurious interrupts. The flush impacts performance but
7307 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007308 */
Michael Chanc04cb342007-05-07 00:26:15 -07007309 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07007310 if (tg3_irq_sync(tp))
7311 goto out;
7312 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00007313 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00007314 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00007315 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07007316 } else {
7317 /* No work, shared interrupt perhaps? re-enable
7318 * interrupts, and flush that PCI write
7319 */
7320 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
7321 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07007322 }
David S. Millerf47c11e2005-06-24 20:18:35 -07007323out:
David S. Millerfac9b832005-05-18 22:46:34 -07007324 return IRQ_RETVAL(handled);
7325}
7326
David Howells7d12e782006-10-05 14:55:46 +01007327static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07007328{
Matt Carlson09943a12009-08-28 14:01:57 +00007329 struct tg3_napi *tnapi = dev_id;
7330 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007331 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07007332 unsigned int handled = 1;
7333
David S. Millerfac9b832005-05-18 22:46:34 -07007334 /* In INTx mode, it is possible for the interrupt to arrive at
7335 * the CPU before the status block posted prior to the interrupt.
7336 * Reading the PCI State register will confirm whether the
7337 * interrupt is ours and will flush the status block.
7338 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007339 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00007340 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07007341 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
7342 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07007343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 }
Michael Chand18edcb2007-03-24 20:57:11 -07007345 }
7346
7347 /*
7348 * writing any value to intr-mbox-0 clears PCI INTA# and
7349 * chip-internal interrupt pending events.
7350 * writing non-zero to intr-mbox-0 additional tells the
7351 * NIC to stop sending us irqs, engaging "in-intr-handler"
7352 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07007353 *
7354 * Flush the mailbox to de-assert the IRQ immediately to prevent
7355 * spurious interrupts. The flush impacts performance but
7356 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07007357 */
Michael Chanc04cb342007-05-07 00:26:15 -07007358 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00007359
7360 /*
7361 * In a shared interrupt configuration, sometimes other devices'
7362 * interrupts will scream. We record the current status tag here
7363 * so that the above check can report that the screaming interrupts
7364 * are unhandled. Eventually they will be silenced.
7365 */
Matt Carlson898a56f2009-08-28 14:02:40 +00007366 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00007367
Michael Chand18edcb2007-03-24 20:57:11 -07007368 if (tg3_irq_sync(tp))
7369 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00007370
Matt Carlson72334482009-08-28 14:03:01 +00007371 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00007372
Matt Carlson09943a12009-08-28 14:01:57 +00007373 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00007374
David S. Millerf47c11e2005-06-24 20:18:35 -07007375out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 return IRQ_RETVAL(handled);
7377}
7378
Michael Chan79381092005-04-21 17:13:59 -07007379/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007380static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007381{
Matt Carlson09943a12009-08-28 14:01:57 +00007382 struct tg3_napi *tnapi = dev_id;
7383 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007384 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007385
Michael Chanf9804dd2005-09-27 12:13:10 -07007386 if ((sblk->status & SD_STATUS_UPDATED) ||
7387 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007388 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007389 return IRQ_RETVAL(1);
7390 }
7391 return IRQ_RETVAL(0);
7392}
7393
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394#ifdef CONFIG_NET_POLL_CONTROLLER
7395static void tg3_poll_controller(struct net_device *dev)
7396{
Matt Carlson4f125f42009-09-01 12:55:02 +00007397 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007398 struct tg3 *tp = netdev_priv(dev);
7399
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007400 if (tg3_irq_sync(tp))
7401 return;
7402
Matt Carlson4f125f42009-09-01 12:55:02 +00007403 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007404 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405}
7406#endif
7407
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408static void tg3_tx_timeout(struct net_device *dev)
7409{
7410 struct tg3 *tp = netdev_priv(dev);
7411
Michael Chanb0408752007-02-13 12:18:30 -08007412 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007413 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007414 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Matt Carlsondb219972011-11-04 09:15:03 +00007417 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418}
7419
Michael Chanc58ec932005-09-17 00:46:27 -07007420/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7421static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7422{
7423 u32 base = (u32) mapping & 0xffffffff;
7424
Eric Dumazet807540b2010-09-23 05:40:09 +00007425 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007426}
7427
Michael Chan72f2afb2006-03-06 19:28:35 -08007428/* Test for DMA addresses > 40-bit */
7429static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7430 int len)
7431{
7432#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007433 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007434 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007435 return 0;
7436#else
7437 return 0;
7438#endif
7439}
7440
Matt Carlsond1a3b732011-07-27 14:20:51 +00007441static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007442 dma_addr_t mapping, u32 len, u32 flags,
7443 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007444{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007445 txbd->addr_hi = ((u64) mapping >> 32);
7446 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7447 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7448 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
Matt Carlson84b67b22011-07-27 14:20:52 +00007451static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007452 dma_addr_t map, u32 len, u32 flags,
7453 u32 mss, u32 vlan)
7454{
7455 struct tg3 *tp = tnapi->tp;
7456 bool hwbug = false;
7457
7458 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007459 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007460
7461 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007462 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007463
7464 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007465 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007466
Matt Carlsona4cb4282011-12-14 11:09:58 +00007467 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007468 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007469 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007470 while (len > tp->dma_limit && *budget) {
7471 u32 frag_len = tp->dma_limit;
7472 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007473
Matt Carlsonb9e45482011-11-04 09:14:59 +00007474 /* Avoid the 8byte DMA problem */
7475 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007476 len += tp->dma_limit / 2;
7477 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007478 }
7479
Matt Carlsonb9e45482011-11-04 09:14:59 +00007480 tnapi->tx_buffers[*entry].fragmented = true;
7481
7482 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7483 frag_len, tmp_flag, mss, vlan);
7484 *budget -= 1;
7485 prvidx = *entry;
7486 *entry = NEXT_TX(*entry);
7487
Matt Carlsone31aa982011-07-27 14:20:53 +00007488 map += frag_len;
7489 }
7490
7491 if (len) {
7492 if (*budget) {
7493 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7494 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007495 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007496 *entry = NEXT_TX(*entry);
7497 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007498 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007499 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007500 }
7501 }
7502 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007503 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7504 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007505 *entry = NEXT_TX(*entry);
7506 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007507
7508 return hwbug;
7509}
7510
Matt Carlson0d681b22011-07-27 14:20:49 +00007511static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007512{
7513 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007514 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007515 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007516
Matt Carlson0d681b22011-07-27 14:20:49 +00007517 skb = txb->skb;
7518 txb->skb = NULL;
7519
Matt Carlson432aa7e2011-05-19 12:12:45 +00007520 pci_unmap_single(tnapi->tp->pdev,
7521 dma_unmap_addr(txb, mapping),
7522 skb_headlen(skb),
7523 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007524
7525 while (txb->fragmented) {
7526 txb->fragmented = false;
7527 entry = NEXT_TX(entry);
7528 txb = &tnapi->tx_buffers[entry];
7529 }
7530
Matt Carlsonba1142e2011-11-04 09:15:00 +00007531 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007532 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007533
7534 entry = NEXT_TX(entry);
7535 txb = &tnapi->tx_buffers[entry];
7536
7537 pci_unmap_page(tnapi->tp->pdev,
7538 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007539 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007540
7541 while (txb->fragmented) {
7542 txb->fragmented = false;
7543 entry = NEXT_TX(entry);
7544 txb = &tnapi->tx_buffers[entry];
7545 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007546 }
7547}
7548
Michael Chan72f2afb2006-03-06 19:28:35 -08007549/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007550static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007551 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007552 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007553 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007555 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007556 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007557 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007558 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559
Joe Perches41535772013-02-16 11:20:04 +00007560 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007561 new_skb = skb_copy(skb, GFP_ATOMIC);
7562 else {
7563 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7564
7565 new_skb = skb_copy_expand(skb,
7566 skb_headroom(skb) + more_headroom,
7567 skb_tailroom(skb), GFP_ATOMIC);
7568 }
7569
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007571 ret = -1;
7572 } else {
7573 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007574 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7575 PCI_DMA_TODEVICE);
7576 /* Make sure the mapping succeeded */
7577 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007578 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007579 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007580 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007581 u32 save_entry = *entry;
7582
Matt Carlson92cd3a12011-07-27 14:20:47 +00007583 base_flags |= TXD_FLAG_END;
7584
Matt Carlson84b67b22011-07-27 14:20:52 +00007585 tnapi->tx_buffers[*entry].skb = new_skb;
7586 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007587 mapping, new_addr);
7588
Matt Carlson84b67b22011-07-27 14:20:52 +00007589 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007590 new_skb->len, base_flags,
7591 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007592 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007593 dev_kfree_skb(new_skb);
7594 ret = -1;
7595 }
Michael Chanc58ec932005-09-17 00:46:27 -07007596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 }
7598
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007600 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602}
7603
Matt Carlson2ffcc982011-05-19 12:12:44 +00007604static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007605
7606/* Use GSO to workaround a rare TSO bug that may be triggered when the
7607 * TSO header is greater than 80 bytes.
7608 */
7609static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7610{
7611 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007612 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007613
7614 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007615 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007616 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007617
7618 /* netif_tx_stop_queue() must be done before checking
7619 * checking tx index in tg3_tx_avail() below, because in
7620 * tg3_tx(), we update tx index before checking for
7621 * netif_tx_queue_stopped().
7622 */
7623 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007624 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007625 return NETDEV_TX_BUSY;
7626
7627 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007628 }
7629
7630 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007631 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007632 goto tg3_tso_bug_end;
7633
7634 do {
7635 nskb = segs;
7636 segs = segs->next;
7637 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007638 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007639 } while (segs);
7640
7641tg3_tso_bug_end:
7642 dev_kfree_skb(skb);
7643
7644 return NETDEV_TX_OK;
7645}
Michael Chan52c0fd82006-06-29 20:15:54 -07007646
Michael Chan5a6f3072006-03-20 22:28:05 -08007647/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007648 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007649 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007650static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007651{
7652 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007653 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007654 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007655 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007656 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007657 struct tg3_napi *tnapi;
7658 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007659 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007660
Matt Carlson24f4efd2009-11-13 13:03:35 +00007661 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7662 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007663 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007664 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665
Matt Carlson84b67b22011-07-27 14:20:52 +00007666 budget = tg3_tx_avail(tnapi);
7667
Michael Chan00b70502006-06-17 21:58:45 -07007668 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007669 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007670 * interrupt. Furthermore, IRQ processing runs lockless so we have
7671 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007673 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007674 if (!netif_tx_queue_stopped(txq)) {
7675 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007676
7677 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007678 netdev_err(dev,
7679 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 return NETDEV_TX_BUSY;
7682 }
7683
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007684 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007686 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007688
Matt Carlsonbe98da62010-07-11 09:31:46 +00007689 mss = skb_shinfo(skb)->gso_size;
7690 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007691 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007692 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693
7694 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007695 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7696 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
Matt Carlson34195c32010-07-11 09:31:42 +00007698 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007699 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
Eric Dumazeta5a11952012-01-23 01:22:09 +00007701 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007702
Eric Dumazeta5a11952012-01-23 01:22:09 +00007703 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007704 iph->check = 0;
7705 iph->tot_len = htons(mss + hdr_len);
7706 }
7707
Michael Chan52c0fd82006-06-29 20:15:54 -07007708 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007709 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007710 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007711
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7713 TXD_FLAG_CPU_POST_DMA);
7714
Joe Perches63c3a662011-04-26 08:12:10 +00007715 if (tg3_flag(tp, HW_TSO_1) ||
7716 tg3_flag(tp, HW_TSO_2) ||
7717 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007718 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007719 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007720 } else
7721 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7722 iph->daddr, 0,
7723 IPPROTO_TCP,
7724 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
Joe Perches63c3a662011-04-26 08:12:10 +00007726 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007727 mss |= (hdr_len & 0xc) << 12;
7728 if (hdr_len & 0x10)
7729 base_flags |= 0x00000010;
7730 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007731 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007732 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007733 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007734 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007735 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 int tsflags;
7737
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007738 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007739 mss |= (tsflags << 11);
7740 }
7741 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007742 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 int tsflags;
7744
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007745 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746 base_flags |= tsflags << 12;
7747 }
7748 }
7749 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007750
Matt Carlson93a700a2011-08-31 11:44:54 +00007751 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7752 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7753 base_flags |= TXD_FLAG_JMB_PKT;
7754
Matt Carlson92cd3a12011-07-27 14:20:47 +00007755 if (vlan_tx_tag_present(skb)) {
7756 base_flags |= TXD_FLAG_VLAN;
7757 vlan = vlan_tx_tag_get(skb);
7758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007760 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7761 tg3_flag(tp, TX_TSTAMP_EN)) {
7762 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7763 base_flags |= TXD_FLAG_HWTSTAMP;
7764 }
7765
Alexander Duyckf4188d82009-12-02 16:48:38 +00007766 len = skb_headlen(skb);
7767
7768 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007769 if (pci_dma_mapping_error(tp->pdev, mapping))
7770 goto drop;
7771
David S. Miller90079ce2008-09-11 04:52:51 -07007772
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007773 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007774 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775
7776 would_hit_hwbug = 0;
7777
Joe Perches63c3a662011-04-26 08:12:10 +00007778 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007779 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
Matt Carlson84b67b22011-07-27 14:20:52 +00007781 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007782 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007783 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007784 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007785 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007786 u32 tmp_mss = mss;
7787
7788 if (!tg3_flag(tp, HW_TSO_1) &&
7789 !tg3_flag(tp, HW_TSO_2) &&
7790 !tg3_flag(tp, HW_TSO_3))
7791 tmp_mss = 0;
7792
Matt Carlsonc5665a52012-02-13 10:20:12 +00007793 /* Now loop through additional data
7794 * fragments, and queue them.
7795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 last = skb_shinfo(skb)->nr_frags - 1;
7797 for (i = 0; i <= last; i++) {
7798 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7799
Eric Dumazet9e903e02011-10-18 21:00:24 +00007800 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007801 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007802 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007804 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007805 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007806 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007807 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007808 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809
Matt Carlsonb9e45482011-11-04 09:14:59 +00007810 if (!budget ||
7811 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007812 len, base_flags |
7813 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007814 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007815 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007816 break;
7817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 }
7819 }
7820
7821 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007822 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
7824 /* If the workaround fails due to memory/mapping
7825 * failure, silently drop this packet.
7826 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007827 entry = tnapi->tx_prod;
7828 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007829 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007830 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007831 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 }
7833
Richard Cochrand515b452011-06-19 03:31:41 +00007834 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007835 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007836
Michael Chan6541b802012-03-04 14:48:14 +00007837 /* Sync BD data before updating mailbox */
7838 wmb();
7839
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007841 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007843 tnapi->tx_prod = entry;
7844 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007845 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007846
7847 /* netif_tx_stop_queue() must be done before checking
7848 * checking tx index in tg3_tx_avail() below, because in
7849 * tg3_tx(), we update tx index before checking for
7850 * netif_tx_queue_stopped().
7851 */
7852 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007853 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007854 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007857 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007859
7860dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007861 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007862 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007863drop:
7864 dev_kfree_skb(skb);
7865drop_nofree:
7866 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007867 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868}
7869
Matt Carlson6e01b202011-08-19 13:58:20 +00007870static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7871{
7872 if (enable) {
7873 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7874 MAC_MODE_PORT_MODE_MASK);
7875
7876 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7877
7878 if (!tg3_flag(tp, 5705_PLUS))
7879 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7880
7881 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7882 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7883 else
7884 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7885 } else {
7886 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7887
7888 if (tg3_flag(tp, 5705_PLUS) ||
7889 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007890 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007891 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7892 }
7893
7894 tw32(MAC_MODE, tp->mac_mode);
7895 udelay(40);
7896}
7897
Matt Carlson941ec902011-08-19 13:58:23 +00007898static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007899{
Matt Carlson941ec902011-08-19 13:58:23 +00007900 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007901
7902 tg3_phy_toggle_apd(tp, false);
Joe Perches953c96e2013-04-09 10:18:14 +00007903 tg3_phy_toggle_automdix(tp, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007904
Matt Carlson941ec902011-08-19 13:58:23 +00007905 if (extlpbk && tg3_phy_set_extloopbk(tp))
7906 return -EIO;
7907
7908 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007909 switch (speed) {
7910 case SPEED_10:
7911 break;
7912 case SPEED_100:
7913 bmcr |= BMCR_SPEED100;
7914 break;
7915 case SPEED_1000:
7916 default:
7917 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7918 speed = SPEED_100;
7919 bmcr |= BMCR_SPEED100;
7920 } else {
7921 speed = SPEED_1000;
7922 bmcr |= BMCR_SPEED1000;
7923 }
7924 }
7925
Matt Carlson941ec902011-08-19 13:58:23 +00007926 if (extlpbk) {
7927 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7928 tg3_readphy(tp, MII_CTRL1000, &val);
7929 val |= CTL1000_AS_MASTER |
7930 CTL1000_ENABLE_MASTER;
7931 tg3_writephy(tp, MII_CTRL1000, val);
7932 } else {
7933 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7934 MII_TG3_FET_PTEST_TRIM_2;
7935 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7936 }
7937 } else
7938 bmcr |= BMCR_LOOPBACK;
7939
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007940 tg3_writephy(tp, MII_BMCR, bmcr);
7941
7942 /* The write needs to be flushed for the FETs */
7943 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7944 tg3_readphy(tp, MII_BMCR, &bmcr);
7945
7946 udelay(40);
7947
7948 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007949 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007950 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007951 MII_TG3_FET_PTEST_FRC_TX_LINK |
7952 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7953
7954 /* The write needs to be flushed for the AC131 */
7955 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7956 }
7957
7958 /* Reset to prevent losing 1st rx packet intermittently */
7959 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7960 tg3_flag(tp, 5780_CLASS)) {
7961 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7962 udelay(10);
7963 tw32_f(MAC_RX_MODE, tp->rx_mode);
7964 }
7965
7966 mac_mode = tp->mac_mode &
7967 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7968 if (speed == SPEED_1000)
7969 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7970 else
7971 mac_mode |= MAC_MODE_PORT_MODE_MII;
7972
Joe Perches41535772013-02-16 11:20:04 +00007973 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007974 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7975
7976 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7977 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7978 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7979 mac_mode |= MAC_MODE_LINK_POLARITY;
7980
7981 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7982 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7983 }
7984
7985 tw32(MAC_MODE, mac_mode);
7986 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007987
7988 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007989}
7990
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007991static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007992{
7993 struct tg3 *tp = netdev_priv(dev);
7994
7995 if (features & NETIF_F_LOOPBACK) {
7996 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7997 return;
7998
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007999 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008000 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008001 netif_carrier_on(tp->dev);
8002 spin_unlock_bh(&tp->lock);
8003 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
8004 } else {
8005 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
8006 return;
8007
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008008 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00008009 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008010 /* Force link status check */
Joe Perches953c96e2013-04-09 10:18:14 +00008011 tg3_setup_phy(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008012 spin_unlock_bh(&tp->lock);
8013 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
8014 }
8015}
8016
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008017static netdev_features_t tg3_fix_features(struct net_device *dev,
8018 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00008019{
8020 struct tg3 *tp = netdev_priv(dev);
8021
Joe Perches63c3a662011-04-26 08:12:10 +00008022 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00008023 features &= ~NETIF_F_ALL_TSO;
8024
8025 return features;
8026}
8027
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008028static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008029{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00008030 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00008031
8032 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
8033 tg3_set_loopback(dev, features);
8034
8035 return 0;
8036}
8037
Matt Carlson21f581a2009-08-28 14:00:25 +00008038static void tg3_rx_prodring_free(struct tg3 *tp,
8039 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 int i;
8042
Matt Carlson8fea32b2010-09-15 08:59:58 +00008043 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008044 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008045 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008046 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008047 tp->rx_pkt_map_sz);
8048
Joe Perches63c3a662011-04-26 08:12:10 +00008049 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008050 for (i = tpr->rx_jmb_cons_idx;
8051 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00008052 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00008053 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008054 TG3_RX_JMB_MAP_SZ);
8055 }
8056 }
8057
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008058 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
Matt Carlson2c49a442010-09-30 10:34:35 +00008061 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008062 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008063 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008066 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00008067 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008068 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069 }
8070}
8071
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008072/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 *
8074 * The chip has been shut down and the driver detached from
8075 * the networking, so no interrupts or new tx packets will
8076 * end up in the driver. tp->{tx,}lock are held and thus
8077 * we may not sleep.
8078 */
Matt Carlson21f581a2009-08-28 14:00:25 +00008079static int tg3_rx_prodring_alloc(struct tg3 *tp,
8080 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081{
Matt Carlson287be122009-08-28 13:58:46 +00008082 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
Matt Carlsonb196c7e2009-11-13 13:03:50 +00008084 tpr->rx_std_cons_idx = 0;
8085 tpr->rx_std_prod_idx = 0;
8086 tpr->rx_jmb_cons_idx = 0;
8087 tpr->rx_jmb_prod_idx = 0;
8088
Matt Carlson8fea32b2010-09-15 08:59:58 +00008089 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008090 memset(&tpr->rx_std_buffers[0], 0,
8091 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00008092 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008093 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00008094 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008095 goto done;
8096 }
8097
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00008099 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100
Matt Carlson287be122009-08-28 13:58:46 +00008101 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00008103 tp->dev->mtu > ETH_DATA_LEN)
8104 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
8105 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07008106
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107 /* Initialize invariants of the rings, we only set this
8108 * stuff once. This works because the card does not
8109 * write into the rx buffer posting rings.
8110 */
Matt Carlson2c49a442010-09-30 10:34:35 +00008111 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 struct tg3_rx_buffer_desc *rxd;
8113
Matt Carlson21f581a2009-08-28 14:00:25 +00008114 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00008115 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
8117 rxd->opaque = (RXD_OPAQUE_RING_STD |
8118 (i << RXD_OPAQUE_INDEX_SHIFT));
8119 }
8120
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008121 /* Now allocate fresh SKBs for each rx ring. */
8122 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008123 unsigned int frag_size;
8124
8125 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
8126 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008127 netdev_warn(tp->dev,
8128 "Using a smaller RX standard ring. Only "
8129 "%d out of %d buffers were allocated "
8130 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008131 if (i == 0)
8132 goto initfail;
8133 tp->rx_pending = i;
8134 break;
8135 }
8136 }
8137
Joe Perches63c3a662011-04-26 08:12:10 +00008138 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008139 goto done;
8140
Matt Carlson2c49a442010-09-30 10:34:35 +00008141 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008142
Joe Perches63c3a662011-04-26 08:12:10 +00008143 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00008144 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Matt Carlson2c49a442010-09-30 10:34:35 +00008146 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00008147 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148
Matt Carlson0d86df82010-02-17 15:17:00 +00008149 rxd = &tpr->rx_jmb[i].std;
8150 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
8151 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
8152 RXD_FLAG_JUMBO;
8153 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
8154 (i << RXD_OPAQUE_INDEX_SHIFT));
8155 }
8156
8157 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00008158 unsigned int frag_size;
8159
8160 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
8161 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00008162 netdev_warn(tp->dev,
8163 "Using a smaller RX jumbo ring. Only %d "
8164 "out of %d buffers were allocated "
8165 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00008166 if (i == 0)
8167 goto initfail;
8168 tp->rx_jumbo_pending = i;
8169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170 }
8171 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008172
8173done:
Michael Chan32d8c572006-07-25 16:38:29 -07008174 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008175
8176initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00008177 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008179}
8180
Matt Carlson21f581a2009-08-28 14:00:25 +00008181static void tg3_rx_prodring_fini(struct tg3 *tp,
8182 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183{
Matt Carlson21f581a2009-08-28 14:00:25 +00008184 kfree(tpr->rx_std_buffers);
8185 tpr->rx_std_buffers = NULL;
8186 kfree(tpr->rx_jmb_buffers);
8187 tpr->rx_jmb_buffers = NULL;
8188 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008189 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
8190 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008191 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 }
Matt Carlson21f581a2009-08-28 14:00:25 +00008193 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008194 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
8195 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00008196 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008198}
8199
Matt Carlson21f581a2009-08-28 14:00:25 +00008200static int tg3_rx_prodring_init(struct tg3 *tp,
8201 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008202{
Matt Carlson2c49a442010-09-30 10:34:35 +00008203 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
8204 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008205 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008206 return -ENOMEM;
8207
Matt Carlson4bae65c2010-11-24 08:31:52 +00008208 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
8209 TG3_RX_STD_RING_BYTES(tp),
8210 &tpr->rx_std_mapping,
8211 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008212 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008213 goto err_out;
8214
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00008216 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00008217 GFP_KERNEL);
8218 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008219 goto err_out;
8220
Matt Carlson4bae65c2010-11-24 08:31:52 +00008221 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
8222 TG3_RX_JMB_RING_BYTES(tp),
8223 &tpr->rx_jmb_mapping,
8224 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00008225 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008226 goto err_out;
8227 }
8228
8229 return 0;
8230
8231err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00008232 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008233 return -ENOMEM;
8234}
8235
8236/* Free up pending packets in all rx/tx rings.
8237 *
8238 * The chip has been shut down and the driver detached from
8239 * the networking, so no interrupts or new tx packets will
8240 * end up in the driver. tp->{tx,}lock is not held and we are not
8241 * in an interrupt context and thus may sleep.
8242 */
8243static void tg3_free_rings(struct tg3 *tp)
8244{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008245 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008246
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008247 for (j = 0; j < tp->irq_cnt; j++) {
8248 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008249
Matt Carlson8fea32b2010-09-15 08:59:58 +00008250 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00008251
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008252 if (!tnapi->tx_buffers)
8253 continue;
8254
Matt Carlson0d681b22011-07-27 14:20:49 +00008255 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
8256 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008257
Matt Carlson0d681b22011-07-27 14:20:49 +00008258 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008259 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008260
Matt Carlsonba1142e2011-11-04 09:15:00 +00008261 tg3_tx_skb_unmap(tnapi, i,
8262 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008263
8264 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008265 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00008266 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008267 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008268}
8269
8270/* Initialize tx/rx rings for packet processing.
8271 *
8272 * The chip has been shut down and the driver detached from
8273 * the networking, so no interrupts or new tx packets will
8274 * end up in the driver. tp->{tx,}lock are held and thus
8275 * we may not sleep.
8276 */
8277static int tg3_init_rings(struct tg3 *tp)
8278{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008279 int i;
Matt Carlson72334482009-08-28 14:03:01 +00008280
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008281 /* Free up all the SKBs. */
8282 tg3_free_rings(tp);
8283
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008284 for (i = 0; i < tp->irq_cnt; i++) {
8285 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008286
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008287 tnapi->last_tag = 0;
8288 tnapi->last_irq_tag = 0;
8289 tnapi->hw_status->status = 0;
8290 tnapi->hw_status->status_tag = 0;
8291 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8292
8293 tnapi->tx_prod = 0;
8294 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008295 if (tnapi->tx_ring)
8296 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008297
8298 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00008299 if (tnapi->rx_rcb)
8300 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008301
Matt Carlson8fea32b2010-09-15 08:59:58 +00008302 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00008303 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008304 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00008305 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008306 }
Matt Carlson72334482009-08-28 14:03:01 +00008307
Matt Carlson2b2cdb62009-11-13 13:03:48 +00008308 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008309}
8310
Michael Chan49a359e2012-09-28 07:12:37 +00008311static void tg3_mem_tx_release(struct tg3 *tp)
8312{
8313 int i;
8314
8315 for (i = 0; i < tp->irq_max; i++) {
8316 struct tg3_napi *tnapi = &tp->napi[i];
8317
8318 if (tnapi->tx_ring) {
8319 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
8320 tnapi->tx_ring, tnapi->tx_desc_mapping);
8321 tnapi->tx_ring = NULL;
8322 }
8323
8324 kfree(tnapi->tx_buffers);
8325 tnapi->tx_buffers = NULL;
8326 }
8327}
8328
8329static int tg3_mem_tx_acquire(struct tg3 *tp)
8330{
8331 int i;
8332 struct tg3_napi *tnapi = &tp->napi[0];
8333
8334 /* If multivector TSS is enabled, vector 0 does not handle
8335 * tx interrupts. Don't allocate any resources for it.
8336 */
8337 if (tg3_flag(tp, ENABLE_TSS))
8338 tnapi++;
8339
8340 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
8341 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
8342 TG3_TX_RING_SIZE, GFP_KERNEL);
8343 if (!tnapi->tx_buffers)
8344 goto err_out;
8345
8346 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
8347 TG3_TX_RING_BYTES,
8348 &tnapi->tx_desc_mapping,
8349 GFP_KERNEL);
8350 if (!tnapi->tx_ring)
8351 goto err_out;
8352 }
8353
8354 return 0;
8355
8356err_out:
8357 tg3_mem_tx_release(tp);
8358 return -ENOMEM;
8359}
8360
8361static void tg3_mem_rx_release(struct tg3 *tp)
8362{
8363 int i;
8364
8365 for (i = 0; i < tp->irq_max; i++) {
8366 struct tg3_napi *tnapi = &tp->napi[i];
8367
8368 tg3_rx_prodring_fini(tp, &tnapi->prodring);
8369
8370 if (!tnapi->rx_rcb)
8371 continue;
8372
8373 dma_free_coherent(&tp->pdev->dev,
8374 TG3_RX_RCB_RING_BYTES(tp),
8375 tnapi->rx_rcb,
8376 tnapi->rx_rcb_mapping);
8377 tnapi->rx_rcb = NULL;
8378 }
8379}
8380
8381static int tg3_mem_rx_acquire(struct tg3 *tp)
8382{
8383 unsigned int i, limit;
8384
8385 limit = tp->rxq_cnt;
8386
8387 /* If RSS is enabled, we need a (dummy) producer ring
8388 * set on vector zero. This is the true hw prodring.
8389 */
8390 if (tg3_flag(tp, ENABLE_RSS))
8391 limit++;
8392
8393 for (i = 0; i < limit; i++) {
8394 struct tg3_napi *tnapi = &tp->napi[i];
8395
8396 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8397 goto err_out;
8398
8399 /* If multivector RSS is enabled, vector 0
8400 * does not handle rx or tx interrupts.
8401 * Don't allocate any resources for it.
8402 */
8403 if (!i && tg3_flag(tp, ENABLE_RSS))
8404 continue;
8405
8406 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8407 TG3_RX_RCB_RING_BYTES(tp),
8408 &tnapi->rx_rcb_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008409 GFP_KERNEL | __GFP_ZERO);
Michael Chan49a359e2012-09-28 07:12:37 +00008410 if (!tnapi->rx_rcb)
8411 goto err_out;
Michael Chan49a359e2012-09-28 07:12:37 +00008412 }
8413
8414 return 0;
8415
8416err_out:
8417 tg3_mem_rx_release(tp);
8418 return -ENOMEM;
8419}
8420
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008421/*
8422 * Must not be invoked with interrupt sources disabled and
8423 * the hardware shutdown down.
8424 */
8425static void tg3_free_consistent(struct tg3 *tp)
8426{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008427 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008428
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008429 for (i = 0; i < tp->irq_cnt; i++) {
8430 struct tg3_napi *tnapi = &tp->napi[i];
8431
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008432 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008433 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8434 tnapi->hw_status,
8435 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008436 tnapi->hw_status = NULL;
8437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008439
Michael Chan49a359e2012-09-28 07:12:37 +00008440 tg3_mem_rx_release(tp);
8441 tg3_mem_tx_release(tp);
8442
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008444 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8445 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 tp->hw_stats = NULL;
8447 }
8448}
8449
8450/*
8451 * Must not be invoked with interrupt sources disabled and
8452 * the hardware shutdown down. Can sleep.
8453 */
8454static int tg3_alloc_consistent(struct tg3 *tp)
8455{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008456 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008457
Matt Carlson4bae65c2010-11-24 08:31:52 +00008458 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8459 sizeof(struct tg3_hw_stats),
8460 &tp->stats_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008461 GFP_KERNEL | __GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 if (!tp->hw_stats)
8463 goto err_out;
8464
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008465 for (i = 0; i < tp->irq_cnt; i++) {
8466 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008467 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008468
Matt Carlson4bae65c2010-11-24 08:31:52 +00008469 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8470 TG3_HW_STATUS_SIZE,
8471 &tnapi->status_mapping,
Joe Perches1f9061d22013-03-15 07:23:58 +00008472 GFP_KERNEL | __GFP_ZERO);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008473 if (!tnapi->hw_status)
8474 goto err_out;
8475
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008476 sblk = tnapi->hw_status;
8477
Michael Chan49a359e2012-09-28 07:12:37 +00008478 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008479 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008480
Michael Chan49a359e2012-09-28 07:12:37 +00008481 /*
8482 * When RSS is enabled, the status block format changes
8483 * slightly. The "rx_jumbo_consumer", "reserved",
8484 * and "rx_mini_consumer" members get mapped to the
8485 * other three rx return ring producer indexes.
8486 */
8487 switch (i) {
8488 case 1:
8489 prodptr = &sblk->idx[0].rx_producer;
8490 break;
8491 case 2:
8492 prodptr = &sblk->rx_jumbo_consumer;
8493 break;
8494 case 3:
8495 prodptr = &sblk->reserved;
8496 break;
8497 case 4:
8498 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008499 break;
8500 }
Michael Chan49a359e2012-09-28 07:12:37 +00008501 tnapi->rx_rcb_prod_idx = prodptr;
8502 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008503 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008504 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008505 }
8506
Michael Chan49a359e2012-09-28 07:12:37 +00008507 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8508 goto err_out;
8509
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 return 0;
8511
8512err_out:
8513 tg3_free_consistent(tp);
8514 return -ENOMEM;
8515}
8516
8517#define MAX_WAIT_CNT 1000
8518
8519/* To stop a block, clear the enable bit and poll till it
8520 * clears. tp->lock is held.
8521 */
Joe Perches953c96e2013-04-09 10:18:14 +00008522static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523{
8524 unsigned int i;
8525 u32 val;
8526
Joe Perches63c3a662011-04-26 08:12:10 +00008527 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 switch (ofs) {
8529 case RCVLSC_MODE:
8530 case DMAC_MODE:
8531 case MBFREE_MODE:
8532 case BUFMGR_MODE:
8533 case MEMARB_MODE:
8534 /* We can't enable/disable these bits of the
8535 * 5705/5750, just say success.
8536 */
8537 return 0;
8538
8539 default:
8540 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 }
8543
8544 val = tr32(ofs);
8545 val &= ~enable_bit;
8546 tw32_f(ofs, val);
8547
8548 for (i = 0; i < MAX_WAIT_CNT; i++) {
8549 udelay(100);
8550 val = tr32(ofs);
8551 if ((val & enable_bit) == 0)
8552 break;
8553 }
8554
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008555 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008556 dev_err(&tp->pdev->dev,
8557 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8558 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 return -ENODEV;
8560 }
8561
8562 return 0;
8563}
8564
8565/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008566static int tg3_abort_hw(struct tg3 *tp, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567{
8568 int i, err;
8569
8570 tg3_disable_ints(tp);
8571
8572 tp->rx_mode &= ~RX_MODE_ENABLE;
8573 tw32_f(MAC_RX_MODE, tp->rx_mode);
8574 udelay(10);
8575
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008576 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8577 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8578 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8579 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8580 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8581 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008583 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8584 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8585 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8586 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8587 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8588 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8589 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
8591 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8592 tw32_f(MAC_MODE, tp->mac_mode);
8593 udelay(40);
8594
8595 tp->tx_mode &= ~TX_MODE_ENABLE;
8596 tw32_f(MAC_TX_MODE, tp->tx_mode);
8597
8598 for (i = 0; i < MAX_WAIT_CNT; i++) {
8599 udelay(100);
8600 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8601 break;
8602 }
8603 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008604 dev_err(&tp->pdev->dev,
8605 "%s timed out, TX_MODE_ENABLE will not clear "
8606 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008607 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 }
8609
Michael Chane6de8ad2005-05-05 14:42:41 -07008610 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008611 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8612 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
8614 tw32(FTQ_RESET, 0xffffffff);
8615 tw32(FTQ_RESET, 0x00000000);
8616
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008617 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8618 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008620 for (i = 0; i < tp->irq_cnt; i++) {
8621 struct tg3_napi *tnapi = &tp->napi[i];
8622 if (tnapi->hw_status)
8623 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625
Linus Torvalds1da177e2005-04-16 15:20:36 -07008626 return err;
8627}
8628
Michael Chanee6a99b2007-07-18 21:49:10 -07008629/* Save PCI command register before chip reset */
8630static void tg3_save_pci_state(struct tg3 *tp)
8631{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008632 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008633}
8634
8635/* Restore PCI state after chip reset */
8636static void tg3_restore_pci_state(struct tg3 *tp)
8637{
8638 u32 val;
8639
8640 /* Re-enable indirect register accesses. */
8641 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8642 tp->misc_host_ctrl);
8643
8644 /* Set MAX PCI retry to zero. */
8645 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008646 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008647 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008648 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008649 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008650 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008651 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008652 PCISTATE_ALLOW_APE_SHMEM_WR |
8653 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008654 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8655
Matt Carlson8a6eac92007-10-21 16:17:55 -07008656 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008657
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008658 if (!tg3_flag(tp, PCI_EXPRESS)) {
8659 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8660 tp->pci_cacheline_sz);
8661 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8662 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008663 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008664
Michael Chanee6a99b2007-07-18 21:49:10 -07008665 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008666 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008667 u16 pcix_cmd;
8668
8669 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8670 &pcix_cmd);
8671 pcix_cmd &= ~PCI_X_CMD_ERO;
8672 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8673 pcix_cmd);
8674 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008675
Joe Perches63c3a662011-04-26 08:12:10 +00008676 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008677
8678 /* Chip reset on 5780 will reset MSI enable bit,
8679 * so need to restore it.
8680 */
Joe Perches63c3a662011-04-26 08:12:10 +00008681 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008682 u16 ctrl;
8683
8684 pci_read_config_word(tp->pdev,
8685 tp->msi_cap + PCI_MSI_FLAGS,
8686 &ctrl);
8687 pci_write_config_word(tp->pdev,
8688 tp->msi_cap + PCI_MSI_FLAGS,
8689 ctrl | PCI_MSI_FLAGS_ENABLE);
8690 val = tr32(MSGINT_MODE);
8691 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8692 }
8693 }
8694}
8695
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696/* tp->lock is held. */
8697static int tg3_chip_reset(struct tg3 *tp)
8698{
8699 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008700 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008701 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702
David S. Millerf49639e2006-06-09 11:58:36 -07008703 tg3_nvram_lock(tp);
8704
Matt Carlson77b483f2008-08-15 14:07:24 -07008705 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8706
David S. Millerf49639e2006-06-09 11:58:36 -07008707 /* No matching tg3_nvram_unlock() after this because
8708 * chip reset below will undo the nvram lock.
8709 */
8710 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711
Michael Chanee6a99b2007-07-18 21:49:10 -07008712 /* GRC_MISC_CFG core clock reset will clear the memory
8713 * enable bit in PCI register 4 and the MSI enable bit
8714 * on some chips, so we save relevant registers here.
8715 */
8716 tg3_save_pci_state(tp);
8717
Joe Perches41535772013-02-16 11:20:04 +00008718 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008719 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008720 tw32(GRC_FASTBOOT_PC, 0);
8721
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722 /*
8723 * We must avoid the readl() that normally takes place.
8724 * It locks machines, causes machine checks, and other
8725 * fun things. So, temporarily disable the 5701
8726 * hardware workaround, while we do the reset.
8727 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008728 write_op = tp->write32;
8729 if (write_op == tg3_write_flush_reg32)
8730 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731
Michael Chand18edcb2007-03-24 20:57:11 -07008732 /* Prevent the irq handler from reading or writing PCI registers
8733 * during chip reset when the memory enable bit in the PCI command
8734 * register may be cleared. The chip does not generate interrupt
8735 * at this time, but the irq handler may still be called due to irq
8736 * sharing or irqpoll.
8737 */
Joe Perches63c3a662011-04-26 08:12:10 +00008738 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008739 for (i = 0; i < tp->irq_cnt; i++) {
8740 struct tg3_napi *tnapi = &tp->napi[i];
8741 if (tnapi->hw_status) {
8742 tnapi->hw_status->status = 0;
8743 tnapi->hw_status->status_tag = 0;
8744 }
8745 tnapi->last_tag = 0;
8746 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008747 }
Michael Chand18edcb2007-03-24 20:57:11 -07008748 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008749
8750 for (i = 0; i < tp->irq_cnt; i++)
8751 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008752
Joe Perches41535772013-02-16 11:20:04 +00008753 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008754 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8755 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8756 }
8757
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 /* do the reset */
8759 val = GRC_MISC_CFG_CORECLK_RESET;
8760
Joe Perches63c3a662011-04-26 08:12:10 +00008761 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008762 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008763 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008764 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008765 tr32(TG3_PCIE_PHY_TSTCTL) ==
8766 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8767 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8768
Joe Perches41535772013-02-16 11:20:04 +00008769 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008770 tw32(GRC_MISC_CFG, (1 << 29));
8771 val |= (1 << 29);
8772 }
8773 }
8774
Joe Perches41535772013-02-16 11:20:04 +00008775 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008776 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8777 tw32(GRC_VCPU_EXT_CTRL,
8778 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8779 }
8780
Matt Carlsonf37500d2010-08-02 11:25:59 +00008781 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008782 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008784
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 tw32(GRC_MISC_CFG, val);
8786
Michael Chan1ee582d2005-08-09 20:16:46 -07008787 /* restore 5701 hardware bug workaround write method */
8788 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789
8790 /* Unfortunately, we have to delay before the PCI read back.
8791 * Some 575X chips even will not respond to a PCI cfg access
8792 * when the reset command is given to the chip.
8793 *
8794 * How do these hardware designers expect things to work
8795 * properly if the PCI write is posted for a long period
8796 * of time? It is always necessary to have some method by
8797 * which a register read back can occur to push the write
8798 * out which does the reset.
8799 *
8800 * For most tg3 variants the trick below was working.
8801 * Ho hum...
8802 */
8803 udelay(120);
8804
8805 /* Flush PCI posted writes. The normal MMIO registers
8806 * are inaccessible at this time so this is the only
8807 * way to make this reliably (actually, this is no longer
8808 * the case, see above). I tried to use indirect
8809 * register read/write but this upset some 5701 variants.
8810 */
8811 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8812
8813 udelay(120);
8814
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008815 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008816 u16 val16;
8817
Joe Perches41535772013-02-16 11:20:04 +00008818 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008819 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 u32 cfg_val;
8821
8822 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008823 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 udelay(100);
8825
8826 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8827 pci_write_config_dword(tp->pdev, 0xc4,
8828 cfg_val | (1 << 15));
8829 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008830
Matt Carlsone7126992009-08-25 10:08:16 +00008831 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008832 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008833 /*
8834 * Older PCIe devices only support the 128 byte
8835 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008836 */
Joe Perches63c3a662011-04-26 08:12:10 +00008837 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008838 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8839 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008840
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008841 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008842 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008843 PCI_EXP_DEVSTA_CED |
8844 PCI_EXP_DEVSTA_NFED |
8845 PCI_EXP_DEVSTA_FED |
8846 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 }
8848
Michael Chanee6a99b2007-07-18 21:49:10 -07008849 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Joe Perches63c3a662011-04-26 08:12:10 +00008851 tg3_flag_clear(tp, CHIP_RESETTING);
8852 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008853
Michael Chanee6a99b2007-07-18 21:49:10 -07008854 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008855 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008856 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008857 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858
Joe Perches41535772013-02-16 11:20:04 +00008859 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 tg3_stop_fw(tp);
8861 tw32(0x5000, 0x400);
8862 }
8863
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008864 if (tg3_flag(tp, IS_SSB_CORE)) {
8865 /*
8866 * BCM4785: In order to avoid repercussions from using
8867 * potentially defective internal ROM, stop the Rx RISC CPU,
8868 * which is not required.
8869 */
8870 tg3_stop_fw(tp);
8871 tg3_halt_cpu(tp, RX_CPU_BASE);
8872 }
8873
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 tw32(GRC_MODE, tp->grc_mode);
8875
Joe Perches41535772013-02-16 11:20:04 +00008876 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008877 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878
8879 tw32(0xc4, val | (1 << 15));
8880 }
8881
8882 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008883 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008885 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8887 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8888 }
8889
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008890 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008891 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008892 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008893 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008894 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008895 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008897 val = 0;
8898
8899 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 udelay(40);
8901
Matt Carlson77b483f2008-08-15 14:07:24 -07008902 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8903
Michael Chan7a6f4362006-09-27 16:03:31 -07008904 err = tg3_poll_fw(tp);
8905 if (err)
8906 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Matt Carlson0a9140c2009-08-28 12:27:50 +00008908 tg3_mdio_start(tp);
8909
Joe Perches63c3a662011-04-26 08:12:10 +00008910 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008911 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8912 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008913 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008914 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
8916 tw32(0x7c00, val | (1 << 25));
8917 }
8918
Joe Perches41535772013-02-16 11:20:04 +00008919 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008920 val = tr32(TG3_CPMU_CLCK_ORIDE);
8921 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8922 }
8923
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008925 tg3_flag_clear(tp, ENABLE_ASF);
Nithin Sujir942d1af2013-04-09 08:48:07 +00008926 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
8927 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
8928
Joe Perches63c3a662011-04-26 08:12:10 +00008929 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8931 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8932 u32 nic_cfg;
8933
8934 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8935 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008936 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008937 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008938 if (tg3_flag(tp, 5750_PLUS))
8939 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Nithin Sujir942d1af2013-04-09 08:48:07 +00008940
8941 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &nic_cfg);
8942 if (nic_cfg & NIC_SRAM_1G_ON_VAUX_OK)
8943 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
8944 if (nic_cfg & NIC_SRAM_LNK_FLAP_AVOID)
8945 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008946 }
8947 }
8948
8949 return 0;
8950}
8951
Matt Carlson65ec6982012-02-28 23:33:37 +00008952static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8953static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008954
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00008956static int tg3_halt(struct tg3 *tp, int kind, bool silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008957{
8958 int err;
8959
8960 tg3_stop_fw(tp);
8961
Michael Chan944d9802005-05-29 14:57:48 -07008962 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008964 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 err = tg3_chip_reset(tp);
8966
Joe Perches953c96e2013-04-09 10:18:14 +00008967 __tg3_set_mac_addr(tp, false);
Matt Carlsondaba2a62009-04-20 06:58:52 +00008968
Michael Chan944d9802005-05-29 14:57:48 -07008969 tg3_write_sig_legacy(tp, kind);
8970 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971
Matt Carlson92feeab2011-12-08 14:40:14 +00008972 if (tp->hw_stats) {
8973 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008974 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008975 tg3_get_estats(tp, &tp->estats_prev);
8976
8977 /* And make sure the next sample is new data */
8978 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8979 }
8980
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 if (err)
8982 return err;
8983
8984 return 0;
8985}
8986
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987static int tg3_set_mac_addr(struct net_device *dev, void *p)
8988{
8989 struct tg3 *tp = netdev_priv(dev);
8990 struct sockaddr *addr = p;
Joe Perches953c96e2013-04-09 10:18:14 +00008991 int err = 0;
8992 bool skip_mac_1 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993
Michael Chanf9804dd2005-09-27 12:13:10 -07008994 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008995 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008996
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8998
Michael Chane75f7c92006-03-20 21:33:26 -08008999 if (!netif_running(dev))
9000 return 0;
9001
Joe Perches63c3a662011-04-26 08:12:10 +00009002 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07009003 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07009004
Michael Chan986e0ae2007-05-05 12:10:20 -07009005 addr0_high = tr32(MAC_ADDR_0_HIGH);
9006 addr0_low = tr32(MAC_ADDR_0_LOW);
9007 addr1_high = tr32(MAC_ADDR_1_HIGH);
9008 addr1_low = tr32(MAC_ADDR_1_LOW);
9009
9010 /* Skip MAC addr 1 if ASF is using it. */
9011 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
9012 !(addr1_high == 0 && addr1_low == 0))
Joe Perches953c96e2013-04-09 10:18:14 +00009013 skip_mac_1 = true;
Michael Chan58712ef2006-04-29 18:58:01 -07009014 }
Michael Chan986e0ae2007-05-05 12:10:20 -07009015 spin_lock_bh(&tp->lock);
9016 __tg3_set_mac_addr(tp, skip_mac_1);
9017 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018
Michael Chanb9ec6c12006-07-25 16:37:27 -07009019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020}
9021
9022/* tp->lock is held. */
9023static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
9024 dma_addr_t mapping, u32 maxlen_flags,
9025 u32 nic_addr)
9026{
9027 tg3_write_mem(tp,
9028 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
9029 ((u64) mapping >> 32));
9030 tg3_write_mem(tp,
9031 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
9032 ((u64) mapping & 0xffffffff));
9033 tg3_write_mem(tp,
9034 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
9035 maxlen_flags);
9036
Joe Perches63c3a662011-04-26 08:12:10 +00009037 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 tg3_write_mem(tp,
9039 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
9040 nic_addr);
9041}
9042
Michael Chana489b6d2012-09-28 07:12:39 +00009043
9044static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07009045{
Michael Chana489b6d2012-09-28 07:12:39 +00009046 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00009047
Joe Perches63c3a662011-04-26 08:12:10 +00009048 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009049 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
9050 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
9051 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00009052 } else {
9053 tw32(HOSTCC_TXCOL_TICKS, 0);
9054 tw32(HOSTCC_TXMAX_FRAMES, 0);
9055 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00009056
9057 for (; i < tp->txq_cnt; i++) {
9058 u32 reg;
9059
9060 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
9061 tw32(reg, ec->tx_coalesce_usecs);
9062 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
9063 tw32(reg, ec->tx_max_coalesced_frames);
9064 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
9065 tw32(reg, ec->tx_max_coalesced_frames_irq);
9066 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00009067 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009068
Michael Chana489b6d2012-09-28 07:12:39 +00009069 for (; i < tp->irq_max - 1; i++) {
9070 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
9071 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
9072 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9073 }
9074}
9075
9076static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
9077{
9078 int i = 0;
9079 u32 limit = tp->rxq_cnt;
9080
Joe Perches63c3a662011-04-26 08:12:10 +00009081 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00009082 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
9083 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
9084 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00009085 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00009086 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00009087 tw32(HOSTCC_RXCOL_TICKS, 0);
9088 tw32(HOSTCC_RXMAX_FRAMES, 0);
9089 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07009090 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00009091
Michael Chana489b6d2012-09-28 07:12:39 +00009092 for (; i < limit; i++) {
9093 u32 reg;
9094
9095 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
9096 tw32(reg, ec->rx_coalesce_usecs);
9097 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
9098 tw32(reg, ec->rx_max_coalesced_frames);
9099 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
9100 tw32(reg, ec->rx_max_coalesced_frames_irq);
9101 }
9102
9103 for (; i < tp->irq_max - 1; i++) {
9104 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
9105 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
9106 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
9107 }
9108}
9109
9110static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
9111{
9112 tg3_coal_tx_init(tp, ec);
9113 tg3_coal_rx_init(tp, ec);
9114
Joe Perches63c3a662011-04-26 08:12:10 +00009115 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07009116 u32 val = ec->stats_block_coalesce_usecs;
9117
Matt Carlsonb6080e12009-09-01 13:12:00 +00009118 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
9119 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
9120
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009121 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07009122 val = 0;
9123
9124 tw32(HOSTCC_STAT_COAL_TICKS, val);
9125 }
9126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127
9128/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00009129static void tg3_rings_reset(struct tg3 *tp)
9130{
9131 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009132 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009133 struct tg3_napi *tnapi = &tp->napi[0];
9134
9135 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009136 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009137 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009138 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00009139 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00009140 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009141 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00009142 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009143 else
9144 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9145
9146 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
9147 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
9148 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
9149 BDINFO_FLAGS_DISABLED);
9150
9151
9152 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00009153 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009154 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00009155 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009156 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00009157 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9158 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009159 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00009160 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
9161 else
9162 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9163
9164 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
9165 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
9166 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
9167 BDINFO_FLAGS_DISABLED);
9168
9169 /* Disable interrupts */
9170 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009171 tp->napi[0].chk_msi_cnt = 0;
9172 tp->napi[0].last_rx_cons = 0;
9173 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00009174
9175 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00009176 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00009177 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009178 tp->napi[i].tx_prod = 0;
9179 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00009180 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009181 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009182 tw32_rx_mbox(tp->napi[i].consmbox, 0);
9183 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00009184 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009185 tp->napi[i].last_rx_cons = 0;
9186 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009187 }
Joe Perches63c3a662011-04-26 08:12:10 +00009188 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00009189 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009190 } else {
9191 tp->napi[0].tx_prod = 0;
9192 tp->napi[0].tx_cons = 0;
9193 tw32_mailbox(tp->napi[0].prodmbox, 0);
9194 tw32_rx_mbox(tp->napi[0].consmbox, 0);
9195 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009196
9197 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00009198 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00009199 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
9200 for (i = 0; i < 16; i++)
9201 tw32_tx_mbox(mbox + i * 8, 0);
9202 }
9203
9204 txrcb = NIC_SRAM_SEND_RCB;
9205 rxrcb = NIC_SRAM_RCV_RET_RCB;
9206
9207 /* Clear status block in ram. */
9208 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9209
9210 /* Set status block DMA address */
9211 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9212 ((u64) tnapi->status_mapping >> 32));
9213 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9214 ((u64) tnapi->status_mapping & 0xffffffff));
9215
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009216 if (tnapi->tx_ring) {
9217 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9218 (TG3_TX_RING_SIZE <<
9219 BDINFO_FLAGS_MAXLEN_SHIFT),
9220 NIC_SRAM_TX_BUFFER_DESC);
9221 txrcb += TG3_BDINFO_SIZE;
9222 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009223
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009224 if (tnapi->rx_rcb) {
9225 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009226 (tp->rx_ret_ring_mask + 1) <<
9227 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009228 rxrcb += TG3_BDINFO_SIZE;
9229 }
9230
9231 stblk = HOSTCC_STATBLCK_RING1;
9232
9233 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
9234 u64 mapping = (u64)tnapi->status_mapping;
9235 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
9236 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
9237
9238 /* Clear status block in ram. */
9239 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
9240
Matt Carlson19cfaec2009-12-03 08:36:20 +00009241 if (tnapi->tx_ring) {
9242 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
9243 (TG3_TX_RING_SIZE <<
9244 BDINFO_FLAGS_MAXLEN_SHIFT),
9245 NIC_SRAM_TX_BUFFER_DESC);
9246 txrcb += TG3_BDINFO_SIZE;
9247 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009248
9249 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009250 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009251 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
9252
9253 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00009254 rxrcb += TG3_BDINFO_SIZE;
9255 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00009256}
9257
Matt Carlsoneb07a942011-04-20 07:57:36 +00009258static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
9259{
9260 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
9261
Joe Perches63c3a662011-04-26 08:12:10 +00009262 if (!tg3_flag(tp, 5750_PLUS) ||
9263 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009264 tg3_asic_rev(tp) == ASIC_REV_5750 ||
9265 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00009266 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009267 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00009268 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
9269 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009270 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
9271 else
9272 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
9273
9274 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
9275 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
9276
9277 val = min(nic_rep_thresh, host_rep_thresh);
9278 tw32(RCVBDI_STD_THRESH, val);
9279
Joe Perches63c3a662011-04-26 08:12:10 +00009280 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009281 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
9282
Joe Perches63c3a662011-04-26 08:12:10 +00009283 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009284 return;
9285
Matt Carlson513aa6e2011-11-21 15:01:18 +00009286 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00009287
9288 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
9289
9290 val = min(bdcache_maxcnt / 2, host_rep_thresh);
9291 tw32(RCVBDI_JUMBO_THRESH, val);
9292
Joe Perches63c3a662011-04-26 08:12:10 +00009293 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00009294 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
9295}
9296
Matt Carlsonccd5ba92012-02-13 10:20:08 +00009297static inline u32 calc_crc(unsigned char *buf, int len)
9298{
9299 u32 reg;
9300 u32 tmp;
9301 int j, k;
9302
9303 reg = 0xffffffff;
9304
9305 for (j = 0; j < len; j++) {
9306 reg ^= buf[j];
9307
9308 for (k = 0; k < 8; k++) {
9309 tmp = reg & 0x01;
9310
9311 reg >>= 1;
9312
9313 if (tmp)
9314 reg ^= 0xedb88320;
9315 }
9316 }
9317
9318 return ~reg;
9319}
9320
9321static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9322{
9323 /* accept or reject all multicast frames */
9324 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9325 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9326 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9327 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9328}
9329
9330static void __tg3_set_rx_mode(struct net_device *dev)
9331{
9332 struct tg3 *tp = netdev_priv(dev);
9333 u32 rx_mode;
9334
9335 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9336 RX_MODE_KEEP_VLAN_TAG);
9337
9338#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
9339 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9340 * flag clear.
9341 */
9342 if (!tg3_flag(tp, ENABLE_ASF))
9343 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9344#endif
9345
9346 if (dev->flags & IFF_PROMISC) {
9347 /* Promiscuous mode. */
9348 rx_mode |= RX_MODE_PROMISC;
9349 } else if (dev->flags & IFF_ALLMULTI) {
9350 /* Accept all multicast. */
9351 tg3_set_multi(tp, 1);
9352 } else if (netdev_mc_empty(dev)) {
9353 /* Reject all multicast. */
9354 tg3_set_multi(tp, 0);
9355 } else {
9356 /* Accept one or more multicast(s). */
9357 struct netdev_hw_addr *ha;
9358 u32 mc_filter[4] = { 0, };
9359 u32 regidx;
9360 u32 bit;
9361 u32 crc;
9362
9363 netdev_for_each_mc_addr(ha, dev) {
9364 crc = calc_crc(ha->addr, ETH_ALEN);
9365 bit = ~crc & 0x7f;
9366 regidx = (bit & 0x60) >> 5;
9367 bit &= 0x1f;
9368 mc_filter[regidx] |= (1 << bit);
9369 }
9370
9371 tw32(MAC_HASH_REG_0, mc_filter[0]);
9372 tw32(MAC_HASH_REG_1, mc_filter[1]);
9373 tw32(MAC_HASH_REG_2, mc_filter[2]);
9374 tw32(MAC_HASH_REG_3, mc_filter[3]);
9375 }
9376
9377 if (rx_mode != tp->rx_mode) {
9378 tp->rx_mode = rx_mode;
9379 tw32_f(MAC_RX_MODE, rx_mode);
9380 udelay(10);
9381 }
9382}
9383
Michael Chan91024262012-09-28 07:12:38 +00009384static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009385{
9386 int i;
9387
9388 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009389 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009390}
9391
9392static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009393{
9394 int i;
9395
9396 if (!tg3_flag(tp, SUPPORT_MSIX))
9397 return;
9398
Michael Chan0b3ba052012-11-14 14:44:29 +00009399 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009400 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009401 return;
9402 }
9403
9404 /* Validate table against current IRQ count */
9405 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009406 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009407 break;
9408 }
9409
9410 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009411 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009412}
9413
Matt Carlson90415472011-12-16 13:33:23 +00009414static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009415{
9416 int i = 0;
9417 u32 reg = MAC_RSS_INDIR_TBL_0;
9418
9419 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9420 u32 val = tp->rss_ind_tbl[i];
9421 i++;
9422 for (; i % 8; i++) {
9423 val <<= 4;
9424 val |= tp->rss_ind_tbl[i];
9425 }
9426 tw32(reg, val);
9427 reg += 4;
9428 }
9429}
9430
Matt Carlson2d31eca2009-09-01 12:53:31 +00009431/* tp->lock is held. */
Joe Perches953c96e2013-04-09 10:18:14 +00009432static int tg3_reset_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433{
9434 u32 val, rdmac_mode;
9435 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009436 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437
9438 tg3_disable_ints(tp);
9439
9440 tg3_stop_fw(tp);
9441
9442 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9443
Joe Perches63c3a662011-04-26 08:12:10 +00009444 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009445 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446
Matt Carlson699c0192010-12-06 08:28:51 +00009447 /* Enable MAC control of LPI */
9448 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009449 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
9450 TG3_CPMU_EEE_LNKIDL_UART_IDL;
Joe Perches41535772013-02-16 11:20:04 +00009451 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00009452 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
9453
9454 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00009455
9456 tw32_f(TG3_CPMU_EEE_CTRL,
9457 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9458
Matt Carlsona386b902010-12-06 08:28:53 +00009459 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9460 TG3_CPMU_EEEMD_LPI_IN_TX |
9461 TG3_CPMU_EEEMD_LPI_IN_RX |
9462 TG3_CPMU_EEEMD_EEE_ENABLE;
9463
Joe Perches41535772013-02-16 11:20:04 +00009464 if (tg3_asic_rev(tp) != ASIC_REV_5717)
Matt Carlsona386b902010-12-06 08:28:53 +00009465 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9466
Joe Perches63c3a662011-04-26 08:12:10 +00009467 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009468 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9469
9470 tw32_f(TG3_CPMU_EEE_MODE, val);
9471
9472 tw32_f(TG3_CPMU_EEE_DBTMR1,
9473 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9474 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9475
9476 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009477 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009478 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009479 }
9480
Nithin Sujirfdad8de2013-04-09 08:48:08 +00009481 if ((tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
9482 !(tp->phy_flags & TG3_PHYFLG_USER_CONFIGURED)) {
9483 tg3_phy_pull_config(tp);
9484 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
9485 }
9486
Matt Carlson603f1172010-02-12 14:47:10 +00009487 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009488 tg3_phy_reset(tp);
9489
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490 err = tg3_chip_reset(tp);
9491 if (err)
9492 return err;
9493
9494 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9495
Joe Perches41535772013-02-16 11:20:04 +00009496 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009497 val = tr32(TG3_CPMU_CTRL);
9498 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9499 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009500
9501 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9502 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9503 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9504 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9505
9506 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9507 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9508 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9509 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9510
9511 val = tr32(TG3_CPMU_HST_ACC);
9512 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9513 val |= CPMU_HST_ACC_MACCLK_6_25;
9514 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009515 }
9516
Joe Perches41535772013-02-16 11:20:04 +00009517 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009518 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9519 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9520 PCIE_PWR_MGMT_L1_THRESH_4MS;
9521 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009522
9523 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9524 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9525
9526 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009527
Matt Carlsonf40386c2009-11-02 14:24:02 +00009528 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9529 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009530 }
9531
Joe Perches63c3a662011-04-26 08:12:10 +00009532 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009533 u32 grc_mode = tr32(GRC_MODE);
9534
9535 /* Access the lower 1K of PL PCIE block registers. */
9536 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9537 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9538
9539 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9540 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9541 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9542
9543 tw32(GRC_MODE, grc_mode);
9544 }
9545
Matt Carlson55086ad2011-12-14 11:09:59 +00009546 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009547 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009548 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009549
Matt Carlson5093eed2010-11-24 08:31:45 +00009550 /* Access the lower 1K of PL PCIE block registers. */
9551 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9552 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009553
Matt Carlson5093eed2010-11-24 08:31:45 +00009554 val = tr32(TG3_PCIE_TLDLPL_PORT +
9555 TG3_PCIE_PL_LO_PHYCTL5);
9556 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9557 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009558
Matt Carlson5093eed2010-11-24 08:31:45 +00009559 tw32(GRC_MODE, grc_mode);
9560 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009561
Joe Perches41535772013-02-16 11:20:04 +00009562 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009563 u32 grc_mode;
9564
9565 /* Fix transmit hangs */
9566 val = tr32(TG3_CPMU_PADRNG_CTL);
9567 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9568 tw32(TG3_CPMU_PADRNG_CTL, val);
9569
9570 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009571
9572 /* Access the lower 1K of DL PCIE block registers. */
9573 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9574 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9575
9576 val = tr32(TG3_PCIE_TLDLPL_PORT +
9577 TG3_PCIE_DL_LO_FTSMAX);
9578 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9579 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9580 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9581
9582 tw32(GRC_MODE, grc_mode);
9583 }
9584
Matt Carlsona977dbe2010-04-12 06:58:26 +00009585 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9586 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9587 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9588 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009589 }
9590
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591 /* This works around an issue with Athlon chipsets on
9592 * B3 tigon3 silicon. This bit has no effect on any
9593 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009594 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 */
Joe Perches63c3a662011-04-26 08:12:10 +00009596 if (!tg3_flag(tp, CPMU_PRESENT)) {
9597 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009598 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9599 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601
Joe Perches41535772013-02-16 11:20:04 +00009602 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009603 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 val = tr32(TG3PCI_PCISTATE);
9605 val |= PCISTATE_RETRY_SAME_DMA;
9606 tw32(TG3PCI_PCISTATE, val);
9607 }
9608
Joe Perches63c3a662011-04-26 08:12:10 +00009609 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009610 /* Allow reads and writes to the
9611 * APE register and memory space.
9612 */
9613 val = tr32(TG3PCI_PCISTATE);
9614 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009615 PCISTATE_ALLOW_APE_SHMEM_WR |
9616 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009617 tw32(TG3PCI_PCISTATE, val);
9618 }
9619
Joe Perches41535772013-02-16 11:20:04 +00009620 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009621 /* Enable some hw fixes. */
9622 val = tr32(TG3PCI_MSI_DATA);
9623 val |= (1 << 26) | (1 << 28) | (1 << 29);
9624 tw32(TG3PCI_MSI_DATA, val);
9625 }
9626
9627 /* Descriptor ring init may make accesses to the
9628 * NIC SRAM area to setup the TX descriptors, so we
9629 * can only do this after the hardware has been
9630 * successfully reset.
9631 */
Michael Chan32d8c572006-07-25 16:38:29 -07009632 err = tg3_init_rings(tp);
9633 if (err)
9634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635
Joe Perches63c3a662011-04-26 08:12:10 +00009636 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009637 val = tr32(TG3PCI_DMA_RW_CTRL) &
9638 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009639 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009640 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009641 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009642 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9643 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009644 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009645 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009646 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9647 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009648 /* This value is determined during the probe time DMA
9649 * engine test, tg3_test_dma.
9650 */
9651 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653
9654 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9655 GRC_MODE_4X_NIC_SEND_RINGS |
9656 GRC_MODE_NO_TX_PHDR_CSUM |
9657 GRC_MODE_NO_RX_PHDR_CSUM);
9658 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009659
9660 /* Pseudo-header checksum is done by hardware logic and not
9661 * the offload processers, so make the chip do the pseudo-
9662 * header checksums on receive. For transmit it is more
9663 * convenient to do the pseudo-header checksum in software
9664 * as Linux does that on transmit for us in all cases.
9665 */
9666 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009668 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9669 if (tp->rxptpctl)
9670 tw32(TG3_RX_PTP_CTL,
9671 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9672
9673 if (tg3_flag(tp, PTP_CAPABLE))
9674 val |= GRC_MODE_TIME_SYNC_ENABLE;
9675
9676 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009677
9678 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9679 val = tr32(GRC_MISC_CFG);
9680 val &= ~0xff;
9681 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9682 tw32(GRC_MISC_CFG, val);
9683
9684 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009685 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009687 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009689 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9691 else
9692 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9693 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9694 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009695 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009696 int fw_len;
9697
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009698 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9700 tw32(BUFMGR_MB_POOL_ADDR,
9701 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9702 tw32(BUFMGR_MB_POOL_SIZE,
9703 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705
Michael Chan0f893dc2005-07-25 12:30:38 -07009706 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9708 tp->bufmgr_config.mbuf_read_dma_low_water);
9709 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9710 tp->bufmgr_config.mbuf_mac_rx_low_water);
9711 tw32(BUFMGR_MB_HIGH_WATER,
9712 tp->bufmgr_config.mbuf_high_water);
9713 } else {
9714 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9715 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9716 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9717 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9718 tw32(BUFMGR_MB_HIGH_WATER,
9719 tp->bufmgr_config.mbuf_high_water_jumbo);
9720 }
9721 tw32(BUFMGR_DMA_LOW_WATER,
9722 tp->bufmgr_config.dma_low_water);
9723 tw32(BUFMGR_DMA_HIGH_WATER,
9724 tp->bufmgr_config.dma_high_water);
9725
Matt Carlsond309a462010-09-30 10:34:31 +00009726 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009727 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009728 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009729 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9730 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9731 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009732 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009733 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009734 for (i = 0; i < 2000; i++) {
9735 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9736 break;
9737 udelay(10);
9738 }
9739 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009740 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741 return -ENODEV;
9742 }
9743
Joe Perches41535772013-02-16 11:20:04 +00009744 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009745 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009746
Matt Carlsoneb07a942011-04-20 07:57:36 +00009747 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
9749 /* Initialize TG3_BDINFO's at:
9750 * RCVDBDI_STD_BD: standard eth size rx ring
9751 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9752 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9753 *
9754 * like so:
9755 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9756 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9757 * ring attribute flags
9758 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9759 *
9760 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9761 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9762 *
9763 * The size of each ring is fixed in the firmware, but the location is
9764 * configurable.
9765 */
9766 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009767 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009769 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009770 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009771 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9772 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009774 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009775 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9777 BDINFO_FLAGS_DISABLED);
9778
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009779 /* Program the jumbo buffer descriptor ring control
9780 * blocks on those devices that have them.
9781 */
Joe Perches41535772013-02-16 11:20:04 +00009782 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009783 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
Joe Perches63c3a662011-04-26 08:12:10 +00009785 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009787 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009789 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009790 val = TG3_RX_JMB_RING_SIZE(tp) <<
9791 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009793 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009794 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009795 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009796 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009797 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9798 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 } else {
9800 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9801 BDINFO_FLAGS_DISABLED);
9802 }
9803
Joe Perches63c3a662011-04-26 08:12:10 +00009804 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009805 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009806 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9807 val |= (TG3_RX_STD_DMA_SZ << 2);
9808 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009809 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009810 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009811 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009812
9813 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
Matt Carlson411da642009-11-13 13:03:46 +00009815 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009816 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817
Joe Perches63c3a662011-04-26 08:12:10 +00009818 tpr->rx_jmb_prod_idx =
9819 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009820 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009821
Matt Carlson2d31eca2009-09-01 12:53:31 +00009822 tg3_rings_reset(tp);
9823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824 /* Initialize MAC address and backoff seed. */
Joe Perches953c96e2013-04-09 10:18:14 +00009825 __tg3_set_mac_addr(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826
9827 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009828 tw32(MAC_RX_MTU_SIZE,
9829 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830
9831 /* The slot time is changed by tg3_setup_phy if we
9832 * run at gigabit with half duplex.
9833 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009834 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9835 (6 << TX_LENGTHS_IPG_SHIFT) |
9836 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9837
Joe Perches41535772013-02-16 11:20:04 +00009838 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9839 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009840 val |= tr32(MAC_TX_LENGTHS) &
9841 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9842 TX_LENGTHS_CNT_DWN_VAL_MSK);
9843
9844 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845
9846 /* Receive rules. */
9847 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9848 tw32(RCVLPC_CONFIG, 0x0181);
9849
9850 /* Calculate RDMAC_MODE setting early, we need it to determine
9851 * the RCVLPC_STATE_ENABLE mask.
9852 */
9853 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9854 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9855 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9856 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9857 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009858
Joe Perches41535772013-02-16 11:20:04 +00009859 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009860 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9861
Joe Perches41535772013-02-16 11:20:04 +00009862 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9863 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9864 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009865 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9866 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9867 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9868
Joe Perches41535772013-02-16 11:20:04 +00009869 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9870 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009871 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009872 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009873 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9874 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009875 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009876 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9877 }
9878 }
9879
Joe Perches63c3a662011-04-26 08:12:10 +00009880 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009881 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9882
Joe Perches41535772013-02-16 11:20:04 +00009883 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009884 tp->dma_limit = 0;
9885 if (tp->dev->mtu <= ETH_DATA_LEN) {
9886 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9887 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9888 }
9889 }
9890
Joe Perches63c3a662011-04-26 08:12:10 +00009891 if (tg3_flag(tp, HW_TSO_1) ||
9892 tg3_flag(tp, HW_TSO_2) ||
9893 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009894 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9895
Matt Carlson108a6c12011-05-19 12:12:47 +00009896 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009897 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9898 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009899 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900
Joe Perches41535772013-02-16 11:20:04 +00009901 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9902 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009903 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9904
Joe Perches41535772013-02-16 11:20:04 +00009905 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9906 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9907 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9908 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009909 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009910 u32 tgtreg;
9911
Joe Perches41535772013-02-16 11:20:04 +00009912 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009913 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9914 else
9915 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9916
9917 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009918 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9919 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009920 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9921 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9922 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9923 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9924 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9925 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009926 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009927 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009928 }
9929
Joe Perches41535772013-02-16 11:20:04 +00009930 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9931 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9932 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009933 u32 tgtreg;
9934
Joe Perches41535772013-02-16 11:20:04 +00009935 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009936 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9937 else
9938 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9939
9940 val = tr32(tgtreg);
9941 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009942 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9943 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9944 }
9945
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009947 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009948 val = tr32(RCVLPC_STATS_ENABLE);
9949 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9950 tw32(RCVLPC_STATS_ENABLE, val);
9951 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009952 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 val = tr32(RCVLPC_STATS_ENABLE);
9954 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9955 tw32(RCVLPC_STATS_ENABLE, val);
9956 } else {
9957 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9958 }
9959 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9960 tw32(SNDDATAI_STATSENAB, 0xffffff);
9961 tw32(SNDDATAI_STATSCTRL,
9962 (SNDDATAI_SCTRL_ENABLE |
9963 SNDDATAI_SCTRL_FASTUPD));
9964
9965 /* Setup host coalescing engine. */
9966 tw32(HOSTCC_MODE, 0);
9967 for (i = 0; i < 2000; i++) {
9968 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9969 break;
9970 udelay(10);
9971 }
9972
Michael Chand244c892005-07-05 14:42:33 -07009973 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974
Joe Perches63c3a662011-04-26 08:12:10 +00009975 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976 /* Status/statistics block address. See tg3_timer,
9977 * the tg3_periodic_fetch_stats call there, and
9978 * tg3_get_stats to see how this works for 5705/5750 chips.
9979 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9981 ((u64) tp->stats_mapping >> 32));
9982 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9983 ((u64) tp->stats_mapping & 0xffffffff));
9984 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009985
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009987
9988 /* Clear statistics and status block memory areas */
9989 for (i = NIC_SRAM_STATS_BLK;
9990 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9991 i += sizeof(u32)) {
9992 tg3_write_mem(tp, i, 0);
9993 udelay(40);
9994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 }
9996
9997 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9998
9999 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
10000 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010001 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010002 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
10003
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010004 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
10005 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -070010006 /* reset to prevent losing 1st rx packet intermittently */
10007 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10008 udelay(10);
10009 }
10010
Matt Carlson3bda1252008-08-15 14:08:22 -070010011 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +000010012 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
10013 MAC_MODE_FHDE_ENABLE;
10014 if (tg3_flag(tp, ENABLE_APE))
10015 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +000010016 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010017 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010018 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070010019 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
10021 udelay(40);
10022
Michael Chan314fba32005-04-21 17:07:04 -070010023 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +000010024 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -070010025 * register to preserve the GPIO settings for LOMs. The GPIOs,
10026 * whether used as inputs or outputs, are set by boot code after
10027 * reset.
10028 */
Joe Perches63c3a662011-04-26 08:12:10 +000010029 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -070010030 u32 gpio_mask;
10031
Michael Chan9d26e212006-12-07 00:21:14 -080010032 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
10033 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
10034 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -070010035
Joe Perches41535772013-02-16 11:20:04 +000010036 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070010037 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
10038 GRC_LCLCTRL_GPIO_OUTPUT3;
10039
Joe Perches41535772013-02-16 11:20:04 +000010040 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -080010041 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
10042
Gary Zambranoaaf84462007-05-05 11:51:45 -070010043 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -070010044 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
10045
10046 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +000010047 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -080010048 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
10049 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -070010050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10052 udelay(100);
10053
Matt Carlsonc3b50032012-01-17 15:27:23 +000010054 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010055 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010056 val |= MSGINT_MODE_ENABLE;
10057 if (tp->irq_cnt > 1)
10058 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010059 if (!tg3_flag(tp, 1SHOT_MSI))
10060 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010061 tw32(MSGINT_MODE, val);
10062 }
10063
Joe Perches63c3a662011-04-26 08:12:10 +000010064 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
10066 udelay(40);
10067 }
10068
10069 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
10070 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
10071 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
10072 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
10073 WDMAC_MODE_LNGREAD_ENAB);
10074
Joe Perches41535772013-02-16 11:20:04 +000010075 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
10076 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000010077 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +000010078 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
10079 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080 /* nothing */
10081 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010082 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083 val |= WDMAC_MODE_RX_ACCEL;
10084 }
10085 }
10086
Michael Chand9ab5ad12006-03-20 22:27:35 -080010087 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +000010088 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -070010089 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -080010090
Joe Perches41535772013-02-16 11:20:04 +000010091 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +000010092 val |= WDMAC_MODE_BURST_ALL_DATA;
10093
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 tw32_f(WDMAC_MODE, val);
10095 udelay(40);
10096
Joe Perches63c3a662011-04-26 08:12:10 +000010097 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -070010098 u16 pcix_cmd;
10099
10100 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10101 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +000010102 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -070010103 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
10104 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +000010105 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -070010106 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
10107 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108 }
Matt Carlson9974a352007-10-07 23:27:28 -070010109 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
10110 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 }
10112
10113 tw32_f(RDMAC_MODE, rdmac_mode);
10114 udelay(40);
10115
Joe Perches41535772013-02-16 11:20:04 +000010116 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +000010117 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
10118 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
10119 break;
10120 }
10121 if (i < TG3_NUM_RDMA_CHANNELS) {
10122 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10123 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
10124 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10125 tg3_flag_set(tp, 5719_RDMA_BUG);
10126 }
10127 }
10128
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010130 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -070010132
Joe Perches41535772013-02-16 11:20:04 +000010133 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -070010134 tw32(SNDDATAC_MODE,
10135 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
10136 else
10137 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
10138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
10140 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010141 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +000010142 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +000010143 val |= RCVDBDI_MODE_LRG_RING_SZ;
10144 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +000010146 if (tg3_flag(tp, HW_TSO_1) ||
10147 tg3_flag(tp, HW_TSO_2) ||
10148 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010150 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010151 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010152 val |= SNDBDI_MODE_MULTI_TXQ_EN;
10153 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
10155
Joe Perches41535772013-02-16 11:20:04 +000010156 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 err = tg3_load_5701_a0_firmware_fix(tp);
10158 if (err)
10159 return err;
10160 }
10161
Nithin Sujirc4dab502013-03-06 17:02:34 +000010162 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
10163 /* Ignore any errors for the firmware download. If download
10164 * fails, the device will operate with EEE disabled
10165 */
10166 tg3_load_57766_firmware(tp);
10167 }
10168
Joe Perches63c3a662011-04-26 08:12:10 +000010169 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170 err = tg3_load_tso_firmware(tp);
10171 if (err)
10172 return err;
10173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174
10175 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010176
Joe Perches63c3a662011-04-26 08:12:10 +000010177 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000010178 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +000010179 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +000010180
Joe Perches41535772013-02-16 11:20:04 +000010181 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
10182 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +000010183 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
10184 tp->tx_mode &= ~val;
10185 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
10186 }
10187
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188 tw32_f(MAC_TX_MODE, tp->tx_mode);
10189 udelay(100);
10190
Joe Perches63c3a662011-04-26 08:12:10 +000010191 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010192 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010193
10194 /* Setup the "secret" hash key. */
10195 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
10196 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
10197 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
10198 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
10199 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
10200 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
10201 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
10202 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
10203 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
10204 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
10205 }
10206
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +000010208 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080010209 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
10210
Joe Perches63c3a662011-04-26 08:12:10 +000010211 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010212 tp->rx_mode |= RX_MODE_RSS_ENABLE |
10213 RX_MODE_RSS_ITBL_HASH_BITS_7 |
10214 RX_MODE_RSS_IPV6_HASH_EN |
10215 RX_MODE_RSS_TCP_IPV6_HASH_EN |
10216 RX_MODE_RSS_IPV4_HASH_EN |
10217 RX_MODE_RSS_TCP_IPV4_HASH_EN;
10218
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219 tw32_f(MAC_RX_MODE, tp->rx_mode);
10220 udelay(10);
10221
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 tw32(MAC_LED_CTRL, tp->led_ctrl);
10223
10224 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010225 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
10227 udelay(10);
10228 }
10229 tw32_f(MAC_RX_MODE, tp->rx_mode);
10230 udelay(10);
10231
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010232 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +000010233 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
10234 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235 /* Set drive transmission level to 1.2V */
10236 /* only if the signal pre-emphasis bit is not set */
10237 val = tr32(MAC_SERDES_CFG);
10238 val &= 0xfffff000;
10239 val |= 0x880;
10240 tw32(MAC_SERDES_CFG, val);
10241 }
Joe Perches41535772013-02-16 11:20:04 +000010242 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243 tw32(MAC_SERDES_CFG, 0x616000);
10244 }
10245
10246 /* Prevent chip from dropping frames when flow control
10247 * is enabled.
10248 */
Matt Carlson55086ad2011-12-14 11:09:59 +000010249 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +000010250 val = 1;
10251 else
10252 val = 2;
10253 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254
Joe Perches41535772013-02-16 11:20:04 +000010255 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010256 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +000010258 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259 }
10260
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010261 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010262 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -080010263 u32 tmp;
10264
10265 tmp = tr32(SERDES_RX_CTRL);
10266 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
10267 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
10268 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
10269 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
10270 }
10271
Joe Perches63c3a662011-04-26 08:12:10 +000010272 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010273 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +000010274 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275
Joe Perches953c96e2013-04-09 10:18:14 +000010276 err = tg3_setup_phy(tp, false);
Matt Carlsondd477002008-05-25 23:45:58 -070010277 if (err)
10278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010280 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10281 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -070010282 u32 tmp;
10283
10284 /* Clear CRC stats. */
10285 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
10286 tg3_writephy(tp, MII_TG3_TEST1,
10287 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010288 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -070010289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 }
10291 }
10292
10293 __tg3_set_rx_mode(tp->dev);
10294
10295 /* Initialize receive rules. */
10296 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
10297 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
10298 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
10299 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
10300
Joe Perches63c3a662011-04-26 08:12:10 +000010301 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 limit = 8;
10303 else
10304 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +000010305 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010306 limit -= 4;
10307 switch (limit) {
10308 case 16:
10309 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
10310 case 15:
10311 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
10312 case 14:
10313 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
10314 case 13:
10315 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
10316 case 12:
10317 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
10318 case 11:
10319 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
10320 case 10:
10321 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
10322 case 9:
10323 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
10324 case 8:
10325 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
10326 case 7:
10327 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
10328 case 6:
10329 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
10330 case 5:
10331 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
10332 case 4:
10333 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
10334 case 3:
10335 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
10336 case 2:
10337 case 1:
10338
10339 default:
10340 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070010341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010342
Joe Perches63c3a662011-04-26 08:12:10 +000010343 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -070010344 /* Write our heartbeat update interval to APE. */
10345 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
10346 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -070010347
Linus Torvalds1da177e2005-04-16 15:20:36 -070010348 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
10349
Linus Torvalds1da177e2005-04-16 15:20:36 -070010350 return 0;
10351}
10352
10353/* Called at device open time to get the chip ready for
10354 * packet processing. Invoked with tp->lock held.
10355 */
Joe Perches953c96e2013-04-09 10:18:14 +000010356static int tg3_init_hw(struct tg3 *tp, bool reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 tg3_switch_clocks(tp);
10359
10360 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
10361
Matt Carlson2f751b62008-08-04 23:17:34 -070010362 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363}
10364
Michael Chanaed93e02012-07-16 16:24:02 +000010365static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
10366{
10367 int i;
10368
10369 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
10370 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
10371
10372 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
10373 off += len;
10374
10375 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
10376 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
10377 memset(ocir, 0, TG3_OCIR_LEN);
10378 }
10379}
10380
10381/* sysfs attributes for hwmon */
10382static ssize_t tg3_show_temp(struct device *dev,
10383 struct device_attribute *devattr, char *buf)
10384{
10385 struct pci_dev *pdev = to_pci_dev(dev);
10386 struct net_device *netdev = pci_get_drvdata(pdev);
10387 struct tg3 *tp = netdev_priv(netdev);
10388 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
10389 u32 temperature;
10390
10391 spin_lock_bh(&tp->lock);
10392 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10393 sizeof(temperature));
10394 spin_unlock_bh(&tp->lock);
10395 return sprintf(buf, "%u\n", temperature);
10396}
10397
10398
10399static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10400 TG3_TEMP_SENSOR_OFFSET);
10401static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10402 TG3_TEMP_CAUTION_OFFSET);
10403static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10404 TG3_TEMP_MAX_OFFSET);
10405
10406static struct attribute *tg3_attributes[] = {
10407 &sensor_dev_attr_temp1_input.dev_attr.attr,
10408 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10409 &sensor_dev_attr_temp1_max.dev_attr.attr,
10410 NULL
10411};
10412
10413static const struct attribute_group tg3_group = {
10414 .attrs = tg3_attributes,
10415};
10416
Michael Chanaed93e02012-07-16 16:24:02 +000010417static void tg3_hwmon_close(struct tg3 *tp)
10418{
Michael Chanaed93e02012-07-16 16:24:02 +000010419 if (tp->hwmon_dev) {
10420 hwmon_device_unregister(tp->hwmon_dev);
10421 tp->hwmon_dev = NULL;
10422 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10423 }
Michael Chanaed93e02012-07-16 16:24:02 +000010424}
10425
10426static void tg3_hwmon_open(struct tg3 *tp)
10427{
Michael Chanaed93e02012-07-16 16:24:02 +000010428 int i, err;
10429 u32 size = 0;
10430 struct pci_dev *pdev = tp->pdev;
10431 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10432
10433 tg3_sd_scan_scratchpad(tp, ocirs);
10434
10435 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10436 if (!ocirs[i].src_data_length)
10437 continue;
10438
10439 size += ocirs[i].src_hdr_length;
10440 size += ocirs[i].src_data_length;
10441 }
10442
10443 if (!size)
10444 return;
10445
10446 /* Register hwmon sysfs hooks */
10447 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10448 if (err) {
10449 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10450 return;
10451 }
10452
10453 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10454 if (IS_ERR(tp->hwmon_dev)) {
10455 tp->hwmon_dev = NULL;
10456 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10457 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10458 }
Michael Chanaed93e02012-07-16 16:24:02 +000010459}
10460
10461
Linus Torvalds1da177e2005-04-16 15:20:36 -070010462#define TG3_STAT_ADD32(PSTAT, REG) \
10463do { u32 __val = tr32(REG); \
10464 (PSTAT)->low += __val; \
10465 if ((PSTAT)->low < __val) \
10466 (PSTAT)->high += 1; \
10467} while (0)
10468
10469static void tg3_periodic_fetch_stats(struct tg3 *tp)
10470{
10471 struct tg3_hw_stats *sp = tp->hw_stats;
10472
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010473 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474 return;
10475
10476 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10477 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10478 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10479 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10480 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10481 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10482 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10483 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10484 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10485 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10486 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10487 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10488 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010489 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10490 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10491 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10492 u32 val;
10493
10494 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10495 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10496 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10497 tg3_flag_clear(tp, 5719_RDMA_BUG);
10498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499
10500 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10501 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10502 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10503 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10504 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10505 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10506 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10507 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10508 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10509 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10510 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10511 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10512 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10513 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010514
10515 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010516 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10517 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10518 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010519 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10520 } else {
10521 u32 val = tr32(HOSTCC_FLOW_ATTN);
10522 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10523 if (val) {
10524 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10525 sp->rx_discards.low += val;
10526 if (sp->rx_discards.low < val)
10527 sp->rx_discards.high += 1;
10528 }
10529 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10530 }
Michael Chan463d3052006-05-22 16:36:27 -070010531 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532}
10533
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010534static void tg3_chk_missed_msi(struct tg3 *tp)
10535{
10536 u32 i;
10537
10538 for (i = 0; i < tp->irq_cnt; i++) {
10539 struct tg3_napi *tnapi = &tp->napi[i];
10540
10541 if (tg3_has_work(tnapi)) {
10542 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10543 tnapi->last_tx_cons == tnapi->tx_cons) {
10544 if (tnapi->chk_msi_cnt < 1) {
10545 tnapi->chk_msi_cnt++;
10546 return;
10547 }
Matt Carlson7f230732011-08-31 11:44:48 +000010548 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010549 }
10550 }
10551 tnapi->chk_msi_cnt = 0;
10552 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10553 tnapi->last_tx_cons = tnapi->tx_cons;
10554 }
10555}
10556
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557static void tg3_timer(unsigned long __opaque)
10558{
10559 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010560
Matt Carlson5b190622011-11-04 09:15:04 +000010561 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010562 goto restart_timer;
10563
David S. Millerf47c11e2005-06-24 20:18:35 -070010564 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565
Joe Perches41535772013-02-16 11:20:04 +000010566 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010567 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010568 tg3_chk_missed_msi(tp);
10569
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010570 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10571 /* BCM4785: Flush posted writes from GbE to host memory. */
10572 tr32(HOSTCC_MODE);
10573 }
10574
Joe Perches63c3a662011-04-26 08:12:10 +000010575 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010576 /* All of this garbage is because when using non-tagged
10577 * IRQ status the mailbox/status_block protocol the chip
10578 * uses with the cpu is race prone.
10579 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010580 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010581 tw32(GRC_LOCAL_CTRL,
10582 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10583 } else {
10584 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010585 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010587
David S. Millerfac9b832005-05-18 22:46:34 -070010588 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010589 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010590 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010591 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593 }
10594
Linus Torvalds1da177e2005-04-16 15:20:36 -070010595 /* This part only runs once per second. */
10596 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010597 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010598 tg3_periodic_fetch_stats(tp);
10599
Matt Carlsonb0c59432011-05-19 12:12:48 +000010600 if (tp->setlpicnt && !--tp->setlpicnt)
10601 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010602
Joe Perches63c3a662011-04-26 08:12:10 +000010603 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010604 u32 mac_stat;
10605 int phy_event;
10606
10607 mac_stat = tr32(MAC_STATUS);
10608
10609 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010610 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10612 phy_event = 1;
10613 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10614 phy_event = 1;
10615
10616 if (phy_event)
Joe Perches953c96e2013-04-09 10:18:14 +000010617 tg3_setup_phy(tp, false);
Joe Perches63c3a662011-04-26 08:12:10 +000010618 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619 u32 mac_stat = tr32(MAC_STATUS);
10620 int need_setup = 0;
10621
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010622 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010623 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10624 need_setup = 1;
10625 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010626 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010627 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10628 MAC_STATUS_SIGNAL_DET))) {
10629 need_setup = 1;
10630 }
10631 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010632 if (!tp->serdes_counter) {
10633 tw32_f(MAC_MODE,
10634 (tp->mac_mode &
10635 ~MAC_MODE_PORT_MODE_MASK));
10636 udelay(40);
10637 tw32_f(MAC_MODE, tp->mac_mode);
10638 udelay(40);
10639 }
Joe Perches953c96e2013-04-09 10:18:14 +000010640 tg3_setup_phy(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010641 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010642 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010643 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010644 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010646
10647 tp->timer_counter = tp->timer_multiplier;
10648 }
10649
Michael Chan130b8e42006-09-27 16:00:40 -070010650 /* Heartbeat is only sent once every 2 seconds.
10651 *
10652 * The heartbeat is to tell the ASF firmware that the host
10653 * driver is still alive. In the event that the OS crashes,
10654 * ASF needs to reset the hardware to free up the FIFO space
10655 * that may be filled with rx packets destined for the host.
10656 * If the FIFO is full, ASF will no longer function properly.
10657 *
10658 * Unintended resets have been reported on real time kernels
10659 * where the timer doesn't run on time. Netpoll will also have
10660 * same problem.
10661 *
10662 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10663 * to check the ring condition when the heartbeat is expiring
10664 * before doing the reset. This will prevent most unintended
10665 * resets.
10666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010668 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010669 tg3_wait_for_event_ack(tp);
10670
Michael Chanbbadf502006-04-06 21:46:34 -070010671 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010672 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010673 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010674 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10675 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010676
10677 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678 }
10679 tp->asf_counter = tp->asf_multiplier;
10680 }
10681
David S. Millerf47c11e2005-06-24 20:18:35 -070010682 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010683
Michael Chanf475f162006-03-27 23:20:14 -080010684restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010685 tp->timer.expires = jiffies + tp->timer_offset;
10686 add_timer(&tp->timer);
10687}
10688
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010689static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010690{
10691 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010692 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010693 !tg3_flag(tp, 57765_CLASS))
10694 tp->timer_offset = HZ;
10695 else
10696 tp->timer_offset = HZ / 10;
10697
10698 BUG_ON(tp->timer_offset > HZ);
10699
10700 tp->timer_multiplier = (HZ / tp->timer_offset);
10701 tp->asf_multiplier = (HZ / tp->timer_offset) *
10702 TG3_FW_UPDATE_FREQ_SEC;
10703
10704 init_timer(&tp->timer);
10705 tp->timer.data = (unsigned long) tp;
10706 tp->timer.function = tg3_timer;
10707}
10708
10709static void tg3_timer_start(struct tg3 *tp)
10710{
10711 tp->asf_counter = tp->asf_multiplier;
10712 tp->timer_counter = tp->timer_multiplier;
10713
10714 tp->timer.expires = jiffies + tp->timer_offset;
10715 add_timer(&tp->timer);
10716}
10717
10718static void tg3_timer_stop(struct tg3 *tp)
10719{
10720 del_timer_sync(&tp->timer);
10721}
10722
10723/* Restart hardware after configuration changes, self-test, etc.
10724 * Invoked with tp->lock held.
10725 */
Joe Perches953c96e2013-04-09 10:18:14 +000010726static int tg3_restart_hw(struct tg3 *tp, bool reset_phy)
Matt Carlson21f76382012-02-22 12:35:21 +000010727 __releases(tp->lock)
10728 __acquires(tp->lock)
10729{
10730 int err;
10731
10732 err = tg3_init_hw(tp, reset_phy);
10733 if (err) {
10734 netdev_err(tp->dev,
10735 "Failed to re-initialize device, aborting\n");
10736 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10737 tg3_full_unlock(tp);
10738 tg3_timer_stop(tp);
10739 tp->irq_sync = 0;
10740 tg3_napi_enable(tp);
10741 dev_close(tp->dev);
10742 tg3_full_lock(tp, 0);
10743 }
10744 return err;
10745}
10746
10747static void tg3_reset_task(struct work_struct *work)
10748{
10749 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10750 int err;
10751
10752 tg3_full_lock(tp, 0);
10753
10754 if (!netif_running(tp->dev)) {
10755 tg3_flag_clear(tp, RESET_TASK_PENDING);
10756 tg3_full_unlock(tp);
10757 return;
10758 }
10759
10760 tg3_full_unlock(tp);
10761
10762 tg3_phy_stop(tp);
10763
10764 tg3_netif_stop(tp);
10765
10766 tg3_full_lock(tp, 1);
10767
10768 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10769 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10770 tp->write32_rx_mbox = tg3_write_flush_reg32;
10771 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10772 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10773 }
10774
10775 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Joe Perches953c96e2013-04-09 10:18:14 +000010776 err = tg3_init_hw(tp, true);
Matt Carlson21f76382012-02-22 12:35:21 +000010777 if (err)
10778 goto out;
10779
10780 tg3_netif_start(tp);
10781
10782out:
10783 tg3_full_unlock(tp);
10784
10785 if (!err)
10786 tg3_phy_start(tp);
10787
10788 tg3_flag_clear(tp, RESET_TASK_PENDING);
10789}
10790
Matt Carlson4f125f42009-09-01 12:55:02 +000010791static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010792{
David Howells7d12e782006-10-05 14:55:46 +010010793 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010794 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010795 char *name;
10796 struct tg3_napi *tnapi = &tp->napi[irq_num];
10797
10798 if (tp->irq_cnt == 1)
10799 name = tp->dev->name;
10800 else {
10801 name = &tnapi->irq_lbl[0];
10802 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10803 name[IFNAMSIZ-1] = 0;
10804 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010805
Joe Perches63c3a662011-04-26 08:12:10 +000010806 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010807 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010808 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010809 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010810 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010811 } else {
10812 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010813 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010814 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010815 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010816 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010817
10818 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010819}
10820
Michael Chan79381092005-04-21 17:13:59 -070010821static int tg3_test_interrupt(struct tg3 *tp)
10822{
Matt Carlson09943a12009-08-28 14:01:57 +000010823 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010824 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010825 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010826 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010827
Michael Chand4bc3922005-05-29 14:59:20 -070010828 if (!netif_running(dev))
10829 return -ENODEV;
10830
Michael Chan79381092005-04-21 17:13:59 -070010831 tg3_disable_ints(tp);
10832
Matt Carlson4f125f42009-09-01 12:55:02 +000010833 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010834
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010835 /*
10836 * Turn off MSI one shot mode. Otherwise this test has no
10837 * observable way to know whether the interrupt was delivered.
10838 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010839 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010840 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10841 tw32(MSGINT_MODE, val);
10842 }
10843
Matt Carlson4f125f42009-09-01 12:55:02 +000010844 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010845 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010846 if (err)
10847 return err;
10848
Matt Carlson898a56f2009-08-28 14:02:40 +000010849 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010850 tg3_enable_ints(tp);
10851
10852 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010853 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010854
10855 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010856 u32 int_mbox, misc_host_ctrl;
10857
Matt Carlson898a56f2009-08-28 14:02:40 +000010858 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010859 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10860
10861 if ((int_mbox != 0) ||
10862 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10863 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010864 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010865 }
10866
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010867 if (tg3_flag(tp, 57765_PLUS) &&
10868 tnapi->hw_status->status_tag != tnapi->last_tag)
10869 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10870
Michael Chan79381092005-04-21 17:13:59 -070010871 msleep(10);
10872 }
10873
10874 tg3_disable_ints(tp);
10875
Matt Carlson4f125f42009-09-01 12:55:02 +000010876 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010877
Matt Carlson4f125f42009-09-01 12:55:02 +000010878 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010879
10880 if (err)
10881 return err;
10882
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010883 if (intr_ok) {
10884 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010885 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010886 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10887 tw32(MSGINT_MODE, val);
10888 }
Michael Chan79381092005-04-21 17:13:59 -070010889 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010890 }
Michael Chan79381092005-04-21 17:13:59 -070010891
10892 return -EIO;
10893}
10894
10895/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10896 * successfully restored
10897 */
10898static int tg3_test_msi(struct tg3 *tp)
10899{
Michael Chan79381092005-04-21 17:13:59 -070010900 int err;
10901 u16 pci_cmd;
10902
Joe Perches63c3a662011-04-26 08:12:10 +000010903 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010904 return 0;
10905
10906 /* Turn off SERR reporting in case MSI terminates with Master
10907 * Abort.
10908 */
10909 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10910 pci_write_config_word(tp->pdev, PCI_COMMAND,
10911 pci_cmd & ~PCI_COMMAND_SERR);
10912
10913 err = tg3_test_interrupt(tp);
10914
10915 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10916
10917 if (!err)
10918 return 0;
10919
10920 /* other failures */
10921 if (err != -EIO)
10922 return err;
10923
10924 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010925 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10926 "to INTx mode. Please report this failure to the PCI "
10927 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010928
Matt Carlson4f125f42009-09-01 12:55:02 +000010929 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010930
Michael Chan79381092005-04-21 17:13:59 -070010931 pci_disable_msi(tp->pdev);
10932
Joe Perches63c3a662011-04-26 08:12:10 +000010933 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010934 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010935
Matt Carlson4f125f42009-09-01 12:55:02 +000010936 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010937 if (err)
10938 return err;
10939
10940 /* Need to reset the chip because the MSI cycle may have terminated
10941 * with Master Abort.
10942 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010943 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010944
Michael Chan944d9802005-05-29 14:57:48 -070010945 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000010946 err = tg3_init_hw(tp, true);
Michael Chan79381092005-04-21 17:13:59 -070010947
David S. Millerf47c11e2005-06-24 20:18:35 -070010948 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010949
10950 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010951 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010952
10953 return err;
10954}
10955
Matt Carlson9e9fd122009-01-19 16:57:45 -080010956static int tg3_request_firmware(struct tg3 *tp)
10957{
Nithin Sujir77997ea2013-03-06 17:02:32 +000010958 const struct tg3_firmware_hdr *fw_hdr;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010959
10960 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010961 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10962 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010963 return -ENOENT;
10964 }
10965
Nithin Sujir77997ea2013-03-06 17:02:32 +000010966 fw_hdr = (struct tg3_firmware_hdr *)tp->fw->data;
Matt Carlson9e9fd122009-01-19 16:57:45 -080010967
10968 /* Firmware blob starts with version numbers, followed by
10969 * start address and _full_ length including BSS sections
10970 * (which must be longer than the actual data, of course
10971 */
10972
Nithin Sujir77997ea2013-03-06 17:02:32 +000010973 tp->fw_len = be32_to_cpu(fw_hdr->len); /* includes bss */
10974 if (tp->fw_len < (tp->fw->size - TG3_FW_HDR_LEN)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010975 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10976 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010977 release_firmware(tp->fw);
10978 tp->fw = NULL;
10979 return -EINVAL;
10980 }
10981
10982 /* We no longer need firmware; we have it. */
10983 tp->fw_needed = NULL;
10984 return 0;
10985}
10986
Michael Chan91024262012-09-28 07:12:38 +000010987static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010988{
Michael Chan91024262012-09-28 07:12:38 +000010989 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010990
Michael Chan91024262012-09-28 07:12:38 +000010991 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010992 /* We want as many rx rings enabled as there are cpus.
10993 * In multiqueue MSI-X mode, the first MSI-X vector
10994 * only deals with link interrupts, etc, so we add
10995 * one to the number of vectors we are requesting.
10996 */
Michael Chan91024262012-09-28 07:12:38 +000010997 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010998 }
Matt Carlson679563f2009-09-01 12:55:46 +000010999
Michael Chan91024262012-09-28 07:12:38 +000011000 return irq_cnt;
11001}
11002
11003static bool tg3_enable_msix(struct tg3 *tp)
11004{
11005 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070011006 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000011007
Michael Chan09681692012-09-28 07:12:42 +000011008 tp->txq_cnt = tp->txq_req;
11009 tp->rxq_cnt = tp->rxq_req;
11010 if (!tp->rxq_cnt)
11011 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000011012 if (tp->rxq_cnt > tp->rxq_max)
11013 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000011014
11015 /* Disable multiple TX rings by default. Simple round-robin hardware
11016 * scheduling of the TX rings can cause starvation of rings with
11017 * small packets when other rings have TSO or jumbo packets.
11018 */
11019 if (!tp->txq_req)
11020 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000011021
11022 tp->irq_cnt = tg3_irq_count(tp);
11023
Matt Carlson679563f2009-09-01 12:55:46 +000011024 for (i = 0; i < tp->irq_max; i++) {
11025 msix_ent[i].entry = i;
11026 msix_ent[i].vector = 0;
11027 }
11028
11029 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011030 if (rc < 0) {
11031 return false;
11032 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000011033 if (pci_enable_msix(tp->pdev, msix_ent, rc))
11034 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000011035 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
11036 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000011037 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000011038 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000011039 if (tp->txq_cnt)
11040 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000011041 }
11042
11043 for (i = 0; i < tp->irq_max; i++)
11044 tp->napi[i].irq_vec = msix_ent[i].vector;
11045
Michael Chan49a359e2012-09-28 07:12:37 +000011046 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011047 pci_disable_msix(tp->pdev);
11048 return false;
11049 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000011050
Michael Chan91024262012-09-28 07:12:38 +000011051 if (tp->irq_cnt == 1)
11052 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000011053
Michael Chan91024262012-09-28 07:12:38 +000011054 tg3_flag_set(tp, ENABLE_RSS);
11055
11056 if (tp->txq_cnt > 1)
11057 tg3_flag_set(tp, ENABLE_TSS);
11058
11059 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000011060
Matt Carlson679563f2009-09-01 12:55:46 +000011061 return true;
11062}
11063
Matt Carlson07b01732009-08-28 14:01:15 +000011064static void tg3_ints_init(struct tg3 *tp)
11065{
Joe Perches63c3a662011-04-26 08:12:10 +000011066 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
11067 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000011068 /* All MSI supporting chips should support tagged
11069 * status. Assert that this is the case.
11070 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000011071 netdev_warn(tp->dev,
11072 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000011073 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000011074 }
Matt Carlson4f125f42009-09-01 12:55:02 +000011075
Joe Perches63c3a662011-04-26 08:12:10 +000011076 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
11077 tg3_flag_set(tp, USING_MSIX);
11078 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
11079 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000011080
Joe Perches63c3a662011-04-26 08:12:10 +000011081 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011082 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000011083 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000011084 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000011085 if (!tg3_flag(tp, 1SHOT_MSI))
11086 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000011087 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
11088 }
11089defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000011090 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000011091 tp->irq_cnt = 1;
11092 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000011093 }
11094
11095 if (tp->irq_cnt == 1) {
11096 tp->txq_cnt = 1;
11097 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070011098 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070011099 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000011100 }
Matt Carlson07b01732009-08-28 14:01:15 +000011101}
11102
11103static void tg3_ints_fini(struct tg3 *tp)
11104{
Joe Perches63c3a662011-04-26 08:12:10 +000011105 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000011106 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011107 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000011108 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000011109 tg3_flag_clear(tp, USING_MSI);
11110 tg3_flag_clear(tp, USING_MSIX);
11111 tg3_flag_clear(tp, ENABLE_RSS);
11112 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000011113}
11114
Matt Carlsonbe947302012-12-03 19:36:57 +000011115static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
11116 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117{
Michael Chand8f4cd32012-09-28 07:12:40 +000011118 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000011119 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011120
Matt Carlson679563f2009-09-01 12:55:46 +000011121 /*
11122 * Setup interrupts first so we know how
11123 * many NAPI resources to allocate
11124 */
11125 tg3_ints_init(tp);
11126
Matt Carlson90415472011-12-16 13:33:23 +000011127 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000011128
Linus Torvalds1da177e2005-04-16 15:20:36 -070011129 /* The placement of this call is tied
11130 * to the setup and use of Host TX descriptors.
11131 */
11132 err = tg3_alloc_consistent(tp);
11133 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011134 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011136 tg3_napi_init(tp);
11137
Matt Carlsonfed97812009-09-01 13:10:19 +000011138 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070011139
Matt Carlson4f125f42009-09-01 12:55:02 +000011140 for (i = 0; i < tp->irq_cnt; i++) {
11141 struct tg3_napi *tnapi = &tp->napi[i];
11142 err = tg3_request_irq(tp, i);
11143 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000011144 for (i--; i >= 0; i--) {
11145 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000011146 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000011147 }
11148 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000011149 }
11150 }
Matt Carlson07b01732009-08-28 14:01:15 +000011151
David S. Millerf47c11e2005-06-24 20:18:35 -070011152 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011153
Michael Chand8f4cd32012-09-28 07:12:40 +000011154 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011155 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070011156 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011157 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011158 }
11159
David S. Millerf47c11e2005-06-24 20:18:35 -070011160 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011161
Matt Carlson07b01732009-08-28 14:01:15 +000011162 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000011163 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164
Michael Chand8f4cd32012-09-28 07:12:40 +000011165 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070011166 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070011167
Michael Chan79381092005-04-21 17:13:59 -070011168 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070011169 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070011170 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070011171 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070011172 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070011173
Matt Carlson679563f2009-09-01 12:55:46 +000011174 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070011175 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080011176
Joe Perches63c3a662011-04-26 08:12:10 +000011177 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011178 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011179
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000011180 tw32(PCIE_TRANSACTION_CFG,
11181 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080011182 }
Michael Chan79381092005-04-21 17:13:59 -070011183 }
11184
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011185 tg3_phy_start(tp);
11186
Michael Chanaed93e02012-07-16 16:24:02 +000011187 tg3_hwmon_open(tp);
11188
David S. Millerf47c11e2005-06-24 20:18:35 -070011189 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011190
Matt Carlson21f76382012-02-22 12:35:21 +000011191 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011192 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011193 tg3_enable_ints(tp);
11194
Matt Carlsonbe947302012-12-03 19:36:57 +000011195 if (init)
11196 tg3_ptp_init(tp);
11197 else
11198 tg3_ptp_resume(tp);
11199
11200
David S. Millerf47c11e2005-06-24 20:18:35 -070011201 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202
Matt Carlsonfe5f5782009-09-01 13:09:39 +000011203 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011204
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000011205 /*
11206 * Reset loopback feature if it was turned on while the device was down
11207 * make sure that it's installed properly now.
11208 */
11209 if (dev->features & NETIF_F_LOOPBACK)
11210 tg3_set_loopback(dev, dev->features);
11211
Linus Torvalds1da177e2005-04-16 15:20:36 -070011212 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000011213
Matt Carlson679563f2009-09-01 12:55:46 +000011214err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000011215 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11216 struct tg3_napi *tnapi = &tp->napi[i];
11217 free_irq(tnapi->irq_vec, tnapi);
11218 }
Matt Carlson07b01732009-08-28 14:01:15 +000011219
Matt Carlson679563f2009-09-01 12:55:46 +000011220err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000011221 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011222 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000011223 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000011224
11225err_out1:
11226 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011227
Matt Carlson07b01732009-08-28 14:01:15 +000011228 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011229}
11230
Michael Chan65138592012-09-28 07:12:41 +000011231static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011232{
Matt Carlson4f125f42009-09-01 12:55:02 +000011233 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011234
Matt Carlsondb219972011-11-04 09:15:03 +000011235 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000011236 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011237
Matt Carlson21f76382012-02-22 12:35:21 +000011238 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011239
Michael Chanaed93e02012-07-16 16:24:02 +000011240 tg3_hwmon_close(tp);
11241
Matt Carlson24bb4fb2009-10-05 17:55:29 +000011242 tg3_phy_stop(tp);
11243
David S. Millerf47c11e2005-06-24 20:18:35 -070011244 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011245
11246 tg3_disable_ints(tp);
11247
Michael Chan944d9802005-05-29 14:57:48 -070011248 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011249 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000011250 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011251
David S. Millerf47c11e2005-06-24 20:18:35 -070011252 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011253
Matt Carlson4f125f42009-09-01 12:55:02 +000011254 for (i = tp->irq_cnt - 1; i >= 0; i--) {
11255 struct tg3_napi *tnapi = &tp->napi[i];
11256 free_irq(tnapi->irq_vec, tnapi);
11257 }
Matt Carlson07b01732009-08-28 14:01:15 +000011258
11259 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011260
Matt Carlson66cfd1b2010-09-30 10:34:30 +000011261 tg3_napi_fini(tp);
11262
Linus Torvalds1da177e2005-04-16 15:20:36 -070011263 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000011264}
11265
Michael Chand8f4cd32012-09-28 07:12:40 +000011266static int tg3_open(struct net_device *dev)
11267{
11268 struct tg3 *tp = netdev_priv(dev);
11269 int err;
11270
11271 if (tp->fw_needed) {
11272 err = tg3_request_firmware(tp);
Nithin Sujirc4dab502013-03-06 17:02:34 +000011273 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
11274 if (err) {
11275 netdev_warn(tp->dev, "EEE capability disabled\n");
11276 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11277 } else if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP)) {
11278 netdev_warn(tp->dev, "EEE capability restored\n");
11279 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
11280 }
11281 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000011282 if (err)
11283 return err;
11284 } else if (err) {
11285 netdev_warn(tp->dev, "TSO capability disabled\n");
11286 tg3_flag_clear(tp, TSO_CAPABLE);
11287 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
11288 netdev_notice(tp->dev, "TSO capability restored\n");
11289 tg3_flag_set(tp, TSO_CAPABLE);
11290 }
11291 }
11292
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011293 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000011294
11295 err = tg3_power_up(tp);
11296 if (err)
11297 return err;
11298
11299 tg3_full_lock(tp, 0);
11300
11301 tg3_disable_ints(tp);
11302 tg3_flag_clear(tp, INIT_COMPLETE);
11303
11304 tg3_full_unlock(tp);
11305
Nithin Sujir942d1af2013-04-09 08:48:07 +000011306 err = tg3_start(tp,
11307 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN),
11308 true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000011309 if (err) {
11310 tg3_frob_aux_power(tp, false);
11311 pci_set_power_state(tp->pdev, PCI_D3hot);
11312 }
Matt Carlsonbe947302012-12-03 19:36:57 +000011313
Matt Carlson7d41e492012-12-03 19:36:58 +000011314 if (tg3_flag(tp, PTP_CAPABLE)) {
11315 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
11316 &tp->pdev->dev);
11317 if (IS_ERR(tp->ptp_clock))
11318 tp->ptp_clock = NULL;
11319 }
11320
Linus Torvalds1da177e2005-04-16 15:20:36 -070011321 return err;
11322}
11323
11324static int tg3_close(struct net_device *dev)
11325{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011326 struct tg3 *tp = netdev_priv(dev);
11327
Matt Carlsonbe947302012-12-03 19:36:57 +000011328 tg3_ptp_fini(tp);
11329
Michael Chan65138592012-09-28 07:12:41 +000011330 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011331
11332 /* Clear stats across close / open calls */
11333 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
11334 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011335
11336 tg3_power_down(tp);
11337
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011338 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011339
11340 return 0;
11341}
11342
11343static inline u64 get_stat64(tg3_stat64_t *val)
11344{
11345 return ((u64)val->high << 32) | ((u64)val->low);
11346}
11347
11348static u64 tg3_calc_crc_errors(struct tg3 *tp)
11349{
11350 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11351
11352 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000011353 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
11354 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011355 u32 val;
11356
11357 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
11358 tg3_writephy(tp, MII_TG3_TEST1,
11359 val | MII_TG3_TEST1_CRC_EN);
11360 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
11361 } else
11362 val = 0;
11363
11364 tp->phy_crc_errors += val;
11365
11366 return tp->phy_crc_errors;
11367 }
11368
11369 return get_stat64(&hw_stats->rx_fcs_errors);
11370}
11371
11372#define ESTAT_ADD(member) \
11373 estats->member = old_estats->member + \
11374 get_stat64(&hw_stats->member)
11375
11376static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
11377{
11378 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
11379 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11380
11381 ESTAT_ADD(rx_octets);
11382 ESTAT_ADD(rx_fragments);
11383 ESTAT_ADD(rx_ucast_packets);
11384 ESTAT_ADD(rx_mcast_packets);
11385 ESTAT_ADD(rx_bcast_packets);
11386 ESTAT_ADD(rx_fcs_errors);
11387 ESTAT_ADD(rx_align_errors);
11388 ESTAT_ADD(rx_xon_pause_rcvd);
11389 ESTAT_ADD(rx_xoff_pause_rcvd);
11390 ESTAT_ADD(rx_mac_ctrl_rcvd);
11391 ESTAT_ADD(rx_xoff_entered);
11392 ESTAT_ADD(rx_frame_too_long_errors);
11393 ESTAT_ADD(rx_jabbers);
11394 ESTAT_ADD(rx_undersize_packets);
11395 ESTAT_ADD(rx_in_length_errors);
11396 ESTAT_ADD(rx_out_length_errors);
11397 ESTAT_ADD(rx_64_or_less_octet_packets);
11398 ESTAT_ADD(rx_65_to_127_octet_packets);
11399 ESTAT_ADD(rx_128_to_255_octet_packets);
11400 ESTAT_ADD(rx_256_to_511_octet_packets);
11401 ESTAT_ADD(rx_512_to_1023_octet_packets);
11402 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11403 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11404 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11405 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11406 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11407
11408 ESTAT_ADD(tx_octets);
11409 ESTAT_ADD(tx_collisions);
11410 ESTAT_ADD(tx_xon_sent);
11411 ESTAT_ADD(tx_xoff_sent);
11412 ESTAT_ADD(tx_flow_control);
11413 ESTAT_ADD(tx_mac_errors);
11414 ESTAT_ADD(tx_single_collisions);
11415 ESTAT_ADD(tx_mult_collisions);
11416 ESTAT_ADD(tx_deferred);
11417 ESTAT_ADD(tx_excessive_collisions);
11418 ESTAT_ADD(tx_late_collisions);
11419 ESTAT_ADD(tx_collide_2times);
11420 ESTAT_ADD(tx_collide_3times);
11421 ESTAT_ADD(tx_collide_4times);
11422 ESTAT_ADD(tx_collide_5times);
11423 ESTAT_ADD(tx_collide_6times);
11424 ESTAT_ADD(tx_collide_7times);
11425 ESTAT_ADD(tx_collide_8times);
11426 ESTAT_ADD(tx_collide_9times);
11427 ESTAT_ADD(tx_collide_10times);
11428 ESTAT_ADD(tx_collide_11times);
11429 ESTAT_ADD(tx_collide_12times);
11430 ESTAT_ADD(tx_collide_13times);
11431 ESTAT_ADD(tx_collide_14times);
11432 ESTAT_ADD(tx_collide_15times);
11433 ESTAT_ADD(tx_ucast_packets);
11434 ESTAT_ADD(tx_mcast_packets);
11435 ESTAT_ADD(tx_bcast_packets);
11436 ESTAT_ADD(tx_carrier_sense_errors);
11437 ESTAT_ADD(tx_discards);
11438 ESTAT_ADD(tx_errors);
11439
11440 ESTAT_ADD(dma_writeq_full);
11441 ESTAT_ADD(dma_write_prioq_full);
11442 ESTAT_ADD(rxbds_empty);
11443 ESTAT_ADD(rx_discards);
11444 ESTAT_ADD(rx_errors);
11445 ESTAT_ADD(rx_threshold_hit);
11446
11447 ESTAT_ADD(dma_readq_full);
11448 ESTAT_ADD(dma_read_prioq_full);
11449 ESTAT_ADD(tx_comp_queue_full);
11450
11451 ESTAT_ADD(ring_set_send_prod_index);
11452 ESTAT_ADD(ring_status_update);
11453 ESTAT_ADD(nic_irqs);
11454 ESTAT_ADD(nic_avoided_irqs);
11455 ESTAT_ADD(nic_tx_threshold_hit);
11456
Matt Carlson4452d092011-05-19 12:12:51 +000011457 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011458}
11459
Matt Carlson65ec6982012-02-28 23:33:37 +000011460static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011461{
Eric Dumazet511d2222010-07-07 20:44:24 +000011462 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011463 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11464
Linus Torvalds1da177e2005-04-16 15:20:36 -070011465 stats->rx_packets = old_stats->rx_packets +
11466 get_stat64(&hw_stats->rx_ucast_packets) +
11467 get_stat64(&hw_stats->rx_mcast_packets) +
11468 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011469
Linus Torvalds1da177e2005-04-16 15:20:36 -070011470 stats->tx_packets = old_stats->tx_packets +
11471 get_stat64(&hw_stats->tx_ucast_packets) +
11472 get_stat64(&hw_stats->tx_mcast_packets) +
11473 get_stat64(&hw_stats->tx_bcast_packets);
11474
11475 stats->rx_bytes = old_stats->rx_bytes +
11476 get_stat64(&hw_stats->rx_octets);
11477 stats->tx_bytes = old_stats->tx_bytes +
11478 get_stat64(&hw_stats->tx_octets);
11479
11480 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011481 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011482 stats->tx_errors = old_stats->tx_errors +
11483 get_stat64(&hw_stats->tx_errors) +
11484 get_stat64(&hw_stats->tx_mac_errors) +
11485 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11486 get_stat64(&hw_stats->tx_discards);
11487
11488 stats->multicast = old_stats->multicast +
11489 get_stat64(&hw_stats->rx_mcast_packets);
11490 stats->collisions = old_stats->collisions +
11491 get_stat64(&hw_stats->tx_collisions);
11492
11493 stats->rx_length_errors = old_stats->rx_length_errors +
11494 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11495 get_stat64(&hw_stats->rx_undersize_packets);
11496
11497 stats->rx_over_errors = old_stats->rx_over_errors +
11498 get_stat64(&hw_stats->rxbds_empty);
11499 stats->rx_frame_errors = old_stats->rx_frame_errors +
11500 get_stat64(&hw_stats->rx_align_errors);
11501 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11502 get_stat64(&hw_stats->tx_discards);
11503 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11504 get_stat64(&hw_stats->tx_carrier_sense_errors);
11505
11506 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011507 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011508
John W. Linville4f63b872005-09-12 14:43:18 -070011509 stats->rx_missed_errors = old_stats->rx_missed_errors +
11510 get_stat64(&hw_stats->rx_discards);
11511
Eric Dumazetb0057c52010-10-10 19:55:52 +000011512 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011513 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011514}
11515
Linus Torvalds1da177e2005-04-16 15:20:36 -070011516static int tg3_get_regs_len(struct net_device *dev)
11517{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011518 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011519}
11520
11521static void tg3_get_regs(struct net_device *dev,
11522 struct ethtool_regs *regs, void *_p)
11523{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011524 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011525
11526 regs->version = 0;
11527
Matt Carlson97bd8e42011-04-13 11:05:04 +000011528 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011529
Matt Carlson80096062010-08-02 11:26:06 +000011530 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011531 return;
11532
David S. Millerf47c11e2005-06-24 20:18:35 -070011533 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011534
Matt Carlson97bd8e42011-04-13 11:05:04 +000011535 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011536
David S. Millerf47c11e2005-06-24 20:18:35 -070011537 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538}
11539
11540static int tg3_get_eeprom_len(struct net_device *dev)
11541{
11542 struct tg3 *tp = netdev_priv(dev);
11543
11544 return tp->nvram_size;
11545}
11546
Linus Torvalds1da177e2005-04-16 15:20:36 -070011547static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11548{
11549 struct tg3 *tp = netdev_priv(dev);
11550 int ret;
11551 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011552 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011553 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554
Joe Perches63c3a662011-04-26 08:12:10 +000011555 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011556 return -EINVAL;
11557
Matt Carlson80096062010-08-02 11:26:06 +000011558 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011559 return -EAGAIN;
11560
Linus Torvalds1da177e2005-04-16 15:20:36 -070011561 offset = eeprom->offset;
11562 len = eeprom->len;
11563 eeprom->len = 0;
11564
11565 eeprom->magic = TG3_EEPROM_MAGIC;
11566
11567 if (offset & 3) {
11568 /* adjustments to start on required 4 byte boundary */
11569 b_offset = offset & 3;
11570 b_count = 4 - b_offset;
11571 if (b_count > len) {
11572 /* i.e. offset=1 len=2 */
11573 b_count = len;
11574 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011575 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576 if (ret)
11577 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011578 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011579 len -= b_count;
11580 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011581 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011582 }
11583
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011584 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011585 pd = &data[eeprom->len];
11586 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011587 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011588 if (ret) {
11589 eeprom->len += i;
11590 return ret;
11591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592 memcpy(pd + i, &val, 4);
11593 }
11594 eeprom->len += i;
11595
11596 if (len & 3) {
11597 /* read last bytes not ending on 4 byte boundary */
11598 pd = &data[eeprom->len];
11599 b_count = len & 3;
11600 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011601 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602 if (ret)
11603 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011604 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011605 eeprom->len += b_count;
11606 }
11607 return 0;
11608}
11609
Linus Torvalds1da177e2005-04-16 15:20:36 -070011610static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11611{
11612 struct tg3 *tp = netdev_priv(dev);
11613 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011614 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011615 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011616 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617
Matt Carlson80096062010-08-02 11:26:06 +000011618 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011619 return -EAGAIN;
11620
Joe Perches63c3a662011-04-26 08:12:10 +000011621 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011622 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011623 return -EINVAL;
11624
11625 offset = eeprom->offset;
11626 len = eeprom->len;
11627
11628 if ((b_offset = (offset & 3))) {
11629 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011630 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631 if (ret)
11632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011633 len += b_offset;
11634 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011635 if (len < 4)
11636 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011637 }
11638
11639 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011640 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011641 /* adjustments to end on required 4 byte boundary */
11642 odd_len = 1;
11643 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011644 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011645 if (ret)
11646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011647 }
11648
11649 buf = data;
11650 if (b_offset || odd_len) {
11651 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011652 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011653 return -ENOMEM;
11654 if (b_offset)
11655 memcpy(buf, &start, 4);
11656 if (odd_len)
11657 memcpy(buf+len-4, &end, 4);
11658 memcpy(buf + b_offset, data, eeprom->len);
11659 }
11660
11661 ret = tg3_nvram_write_block(tp, offset, len, buf);
11662
11663 if (buf != data)
11664 kfree(buf);
11665
11666 return ret;
11667}
11668
11669static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11670{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011671 struct tg3 *tp = netdev_priv(dev);
11672
Joe Perches63c3a662011-04-26 08:12:10 +000011673 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011674 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011675 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011676 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011677 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11678 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011679 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011680
Linus Torvalds1da177e2005-04-16 15:20:36 -070011681 cmd->supported = (SUPPORTED_Autoneg);
11682
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011683 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684 cmd->supported |= (SUPPORTED_1000baseT_Half |
11685 SUPPORTED_1000baseT_Full);
11686
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011687 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688 cmd->supported |= (SUPPORTED_100baseT_Half |
11689 SUPPORTED_100baseT_Full |
11690 SUPPORTED_10baseT_Half |
11691 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011692 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011693 cmd->port = PORT_TP;
11694 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011695 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011696 cmd->port = PORT_FIBRE;
11697 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011698
Linus Torvalds1da177e2005-04-16 15:20:36 -070011699 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011700 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11701 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11702 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11703 cmd->advertising |= ADVERTISED_Pause;
11704 } else {
11705 cmd->advertising |= ADVERTISED_Pause |
11706 ADVERTISED_Asym_Pause;
11707 }
11708 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11709 cmd->advertising |= ADVERTISED_Asym_Pause;
11710 }
11711 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011712 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011713 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011715 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011716 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11717 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11718 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11719 else
11720 cmd->eth_tp_mdix = ETH_TP_MDI;
11721 }
Matt Carlson64c22182010-10-14 10:37:44 +000011722 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011723 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11724 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011725 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 }
Matt Carlson882e9792009-09-01 13:21:36 +000011727 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011728 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011729 cmd->autoneg = tp->link_config.autoneg;
11730 cmd->maxtxpkt = 0;
11731 cmd->maxrxpkt = 0;
11732 return 0;
11733}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011734
Linus Torvalds1da177e2005-04-16 15:20:36 -070011735static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11736{
11737 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011738 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011739
Joe Perches63c3a662011-04-26 08:12:10 +000011740 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011741 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011742 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011743 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011744 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11745 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011746 }
11747
Matt Carlson7e5856b2009-02-25 14:23:01 +000011748 if (cmd->autoneg != AUTONEG_ENABLE &&
11749 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011750 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011751
11752 if (cmd->autoneg == AUTONEG_DISABLE &&
11753 cmd->duplex != DUPLEX_FULL &&
11754 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011755 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011756
Matt Carlson7e5856b2009-02-25 14:23:01 +000011757 if (cmd->autoneg == AUTONEG_ENABLE) {
11758 u32 mask = ADVERTISED_Autoneg |
11759 ADVERTISED_Pause |
11760 ADVERTISED_Asym_Pause;
11761
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011762 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011763 mask |= ADVERTISED_1000baseT_Half |
11764 ADVERTISED_1000baseT_Full;
11765
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011766 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011767 mask |= ADVERTISED_100baseT_Half |
11768 ADVERTISED_100baseT_Full |
11769 ADVERTISED_10baseT_Half |
11770 ADVERTISED_10baseT_Full |
11771 ADVERTISED_TP;
11772 else
11773 mask |= ADVERTISED_FIBRE;
11774
11775 if (cmd->advertising & ~mask)
11776 return -EINVAL;
11777
11778 mask &= (ADVERTISED_1000baseT_Half |
11779 ADVERTISED_1000baseT_Full |
11780 ADVERTISED_100baseT_Half |
11781 ADVERTISED_100baseT_Full |
11782 ADVERTISED_10baseT_Half |
11783 ADVERTISED_10baseT_Full);
11784
11785 cmd->advertising &= mask;
11786 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011787 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011788 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011789 return -EINVAL;
11790
11791 if (cmd->duplex != DUPLEX_FULL)
11792 return -EINVAL;
11793 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011794 if (speed != SPEED_100 &&
11795 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011796 return -EINVAL;
11797 }
11798 }
11799
David S. Millerf47c11e2005-06-24 20:18:35 -070011800 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801
11802 tp->link_config.autoneg = cmd->autoneg;
11803 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011804 tp->link_config.advertising = (cmd->advertising |
11805 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011806 tp->link_config.speed = SPEED_UNKNOWN;
11807 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808 } else {
11809 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011810 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011812 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011813
Nithin Sujirfdad8de2013-04-09 08:48:08 +000011814 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
11815
Nithin Sujirce20f162013-04-09 08:48:04 +000011816 tg3_warn_mgmt_link_flap(tp);
11817
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818 if (netif_running(dev))
Joe Perches953c96e2013-04-09 10:18:14 +000011819 tg3_setup_phy(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011820
David S. Millerf47c11e2005-06-24 20:18:35 -070011821 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011822
Linus Torvalds1da177e2005-04-16 15:20:36 -070011823 return 0;
11824}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011825
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11827{
11828 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011829
Rick Jones68aad782011-11-07 13:29:27 +000011830 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11831 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11832 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11833 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011835
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11837{
11838 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011839
Joe Perches63c3a662011-04-26 08:12:10 +000011840 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011841 wol->supported = WAKE_MAGIC;
11842 else
11843 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011845 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011846 wol->wolopts = WAKE_MAGIC;
11847 memset(&wol->sopass, 0, sizeof(wol->sopass));
11848}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011849
Linus Torvalds1da177e2005-04-16 15:20:36 -070011850static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11851{
11852 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011853 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011854
Linus Torvalds1da177e2005-04-16 15:20:36 -070011855 if (wol->wolopts & ~WAKE_MAGIC)
11856 return -EINVAL;
11857 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011858 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011859 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011860
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011861 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11862
David S. Millerf47c11e2005-06-24 20:18:35 -070011863 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011864 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011865 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011866 else
Joe Perches63c3a662011-04-26 08:12:10 +000011867 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011868 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011869
Linus Torvalds1da177e2005-04-16 15:20:36 -070011870 return 0;
11871}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011872
Linus Torvalds1da177e2005-04-16 15:20:36 -070011873static u32 tg3_get_msglevel(struct net_device *dev)
11874{
11875 struct tg3 *tp = netdev_priv(dev);
11876 return tp->msg_enable;
11877}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011878
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879static void tg3_set_msglevel(struct net_device *dev, u32 value)
11880{
11881 struct tg3 *tp = netdev_priv(dev);
11882 tp->msg_enable = value;
11883}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011884
Linus Torvalds1da177e2005-04-16 15:20:36 -070011885static int tg3_nway_reset(struct net_device *dev)
11886{
11887 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011888 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011889
Linus Torvalds1da177e2005-04-16 15:20:36 -070011890 if (!netif_running(dev))
11891 return -EAGAIN;
11892
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011893 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011894 return -EINVAL;
11895
Nithin Sujirce20f162013-04-09 08:48:04 +000011896 tg3_warn_mgmt_link_flap(tp);
11897
Joe Perches63c3a662011-04-26 08:12:10 +000011898 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011899 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011900 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011901 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011902 } else {
11903 u32 bmcr;
11904
11905 spin_lock_bh(&tp->lock);
11906 r = -EINVAL;
11907 tg3_readphy(tp, MII_BMCR, &bmcr);
11908 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11909 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011910 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011911 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11912 BMCR_ANENABLE);
11913 r = 0;
11914 }
11915 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011917
Linus Torvalds1da177e2005-04-16 15:20:36 -070011918 return r;
11919}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011920
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11922{
11923 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011924
Matt Carlson2c49a442010-09-30 10:34:35 +000011925 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011926 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011927 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011928 else
11929 ering->rx_jumbo_max_pending = 0;
11930
11931 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011932
11933 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011934 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011935 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11936 else
11937 ering->rx_jumbo_pending = 0;
11938
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011939 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011941
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11943{
11944 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011945 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011946
Matt Carlson2c49a442010-09-30 10:34:35 +000011947 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11948 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011949 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11950 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011951 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011952 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011953 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011954
Michael Chanbbe832c2005-06-24 20:20:04 -070011955 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011956 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011957 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011958 irq_sync = 1;
11959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011960
Michael Chanbbe832c2005-06-24 20:20:04 -070011961 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011962
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963 tp->rx_pending = ering->rx_pending;
11964
Joe Perches63c3a662011-04-26 08:12:10 +000011965 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011966 tp->rx_pending > 63)
11967 tp->rx_pending = 63;
11968 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011969
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011970 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011971 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011972
11973 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011974 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000011975 err = tg3_restart_hw(tp, false);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011976 if (!err)
11977 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978 }
11979
David S. Millerf47c11e2005-06-24 20:18:35 -070011980 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011981
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011982 if (irq_sync && !err)
11983 tg3_phy_start(tp);
11984
Michael Chanb9ec6c12006-07-25 16:37:27 -070011985 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011986}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011987
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11989{
11990 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011991
Joe Perches63c3a662011-04-26 08:12:10 +000011992 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011993
Matt Carlson4a2db502011-12-08 14:40:17 +000011994 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011995 epause->rx_pause = 1;
11996 else
11997 epause->rx_pause = 0;
11998
Matt Carlson4a2db502011-12-08 14:40:17 +000011999 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080012000 epause->tx_pause = 1;
12001 else
12002 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012004
Linus Torvalds1da177e2005-04-16 15:20:36 -070012005static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
12006{
12007 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012008 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012009
Nithin Sujirce20f162013-04-09 08:48:04 +000012010 if (tp->link_config.autoneg == AUTONEG_ENABLE)
12011 tg3_warn_mgmt_link_flap(tp);
12012
Joe Perches63c3a662011-04-26 08:12:10 +000012013 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000012014 u32 newadv;
12015 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012016
Matt Carlson27121682010-02-17 15:16:57 +000012017 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012018
Matt Carlson27121682010-02-17 15:16:57 +000012019 if (!(phydev->supported & SUPPORTED_Pause) ||
12020 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000012021 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000012022 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012023
Matt Carlson27121682010-02-17 15:16:57 +000012024 tp->link_config.flowctrl = 0;
12025 if (epause->rx_pause) {
12026 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012027
Matt Carlson27121682010-02-17 15:16:57 +000012028 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080012029 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000012030 newadv = ADVERTISED_Pause;
12031 } else
12032 newadv = ADVERTISED_Pause |
12033 ADVERTISED_Asym_Pause;
12034 } else if (epause->tx_pause) {
12035 tp->link_config.flowctrl |= FLOW_CTRL_TX;
12036 newadv = ADVERTISED_Asym_Pause;
12037 } else
12038 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012039
Matt Carlson27121682010-02-17 15:16:57 +000012040 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012041 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012042 else
Joe Perches63c3a662011-04-26 08:12:10 +000012043 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000012044
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012045 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000012046 u32 oldadv = phydev->advertising &
12047 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
12048 if (oldadv != newadv) {
12049 phydev->advertising &=
12050 ~(ADVERTISED_Pause |
12051 ADVERTISED_Asym_Pause);
12052 phydev->advertising |= newadv;
12053 if (phydev->autoneg) {
12054 /*
12055 * Always renegotiate the link to
12056 * inform our link partner of our
12057 * flow control settings, even if the
12058 * flow control is forced. Let
12059 * tg3_adjust_link() do the final
12060 * flow control setup.
12061 */
12062 return phy_start_aneg(phydev);
12063 }
12064 }
12065
12066 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012067 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000012068 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012069 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000012070 ~(ADVERTISED_Pause |
12071 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000012072 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012073 }
12074 } else {
12075 int irq_sync = 0;
12076
12077 if (netif_running(dev)) {
12078 tg3_netif_stop(tp);
12079 irq_sync = 1;
12080 }
12081
12082 tg3_full_lock(tp, irq_sync);
12083
12084 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000012085 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012086 else
Joe Perches63c3a662011-04-26 08:12:10 +000012087 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012088 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012089 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012090 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012091 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012092 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080012093 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012094 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080012095 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012096
12097 if (netif_running(dev)) {
12098 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches953c96e2013-04-09 10:18:14 +000012099 err = tg3_restart_hw(tp, false);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012100 if (!err)
12101 tg3_netif_start(tp);
12102 }
12103
12104 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012106
Nithin Sujirfdad8de2013-04-09 08:48:08 +000012107 tp->phy_flags |= TG3_PHYFLG_USER_CONFIGURED;
12108
Michael Chanb9ec6c12006-07-25 16:37:27 -070012109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012110}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012111
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012112static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012114 switch (sset) {
12115 case ETH_SS_TEST:
12116 return TG3_NUM_TEST;
12117 case ETH_SS_STATS:
12118 return TG3_NUM_STATS;
12119 default:
12120 return -EOPNOTSUPP;
12121 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070012122}
12123
Matt Carlson90415472011-12-16 13:33:23 +000012124static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
12125 u32 *rules __always_unused)
12126{
12127 struct tg3 *tp = netdev_priv(dev);
12128
12129 if (!tg3_flag(tp, SUPPORT_MSIX))
12130 return -EOPNOTSUPP;
12131
12132 switch (info->cmd) {
12133 case ETHTOOL_GRXRINGS:
12134 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000012135 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000012136 else {
12137 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000012138 if (info->data > TG3_RSS_MAX_NUM_QS)
12139 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000012140 }
12141
12142 /* The first interrupt vector only
12143 * handles link interrupts.
12144 */
12145 info->data -= 1;
12146 return 0;
12147
12148 default:
12149 return -EOPNOTSUPP;
12150 }
12151}
12152
12153static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
12154{
12155 u32 size = 0;
12156 struct tg3 *tp = netdev_priv(dev);
12157
12158 if (tg3_flag(tp, SUPPORT_MSIX))
12159 size = TG3_RSS_INDIR_TBL_SIZE;
12160
12161 return size;
12162}
12163
12164static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
12165{
12166 struct tg3 *tp = netdev_priv(dev);
12167 int i;
12168
12169 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12170 indir[i] = tp->rss_ind_tbl[i];
12171
12172 return 0;
12173}
12174
12175static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
12176{
12177 struct tg3 *tp = netdev_priv(dev);
12178 size_t i;
12179
12180 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
12181 tp->rss_ind_tbl[i] = indir[i];
12182
12183 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
12184 return 0;
12185
12186 /* It is legal to write the indirection
12187 * table while the device is running.
12188 */
12189 tg3_full_lock(tp, 0);
12190 tg3_rss_write_indir_tbl(tp);
12191 tg3_full_unlock(tp);
12192
12193 return 0;
12194}
12195
Michael Chan09681692012-09-28 07:12:42 +000012196static void tg3_get_channels(struct net_device *dev,
12197 struct ethtool_channels *channel)
12198{
12199 struct tg3 *tp = netdev_priv(dev);
12200 u32 deflt_qs = netif_get_num_default_rss_queues();
12201
12202 channel->max_rx = tp->rxq_max;
12203 channel->max_tx = tp->txq_max;
12204
12205 if (netif_running(dev)) {
12206 channel->rx_count = tp->rxq_cnt;
12207 channel->tx_count = tp->txq_cnt;
12208 } else {
12209 if (tp->rxq_req)
12210 channel->rx_count = tp->rxq_req;
12211 else
12212 channel->rx_count = min(deflt_qs, tp->rxq_max);
12213
12214 if (tp->txq_req)
12215 channel->tx_count = tp->txq_req;
12216 else
12217 channel->tx_count = min(deflt_qs, tp->txq_max);
12218 }
12219}
12220
12221static int tg3_set_channels(struct net_device *dev,
12222 struct ethtool_channels *channel)
12223{
12224 struct tg3 *tp = netdev_priv(dev);
12225
12226 if (!tg3_flag(tp, SUPPORT_MSIX))
12227 return -EOPNOTSUPP;
12228
12229 if (channel->rx_count > tp->rxq_max ||
12230 channel->tx_count > tp->txq_max)
12231 return -EINVAL;
12232
12233 tp->rxq_req = channel->rx_count;
12234 tp->txq_req = channel->tx_count;
12235
12236 if (!netif_running(dev))
12237 return 0;
12238
12239 tg3_stop(tp);
12240
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012241 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000012242
Matt Carlsonbe947302012-12-03 19:36:57 +000012243 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000012244
12245 return 0;
12246}
12247
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012248static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012249{
12250 switch (stringset) {
12251 case ETH_SS_STATS:
12252 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
12253 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070012254 case ETH_SS_TEST:
12255 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
12256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012257 default:
12258 WARN_ON(1); /* we need a WARN() */
12259 break;
12260 }
12261}
12262
stephen hemminger81b87092011-04-04 08:43:50 +000012263static int tg3_set_phys_id(struct net_device *dev,
12264 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070012265{
12266 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070012267
12268 if (!netif_running(tp->dev))
12269 return -EAGAIN;
12270
stephen hemminger81b87092011-04-04 08:43:50 +000012271 switch (state) {
12272 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000012273 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070012274
stephen hemminger81b87092011-04-04 08:43:50 +000012275 case ETHTOOL_ID_ON:
12276 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12277 LED_CTRL_1000MBPS_ON |
12278 LED_CTRL_100MBPS_ON |
12279 LED_CTRL_10MBPS_ON |
12280 LED_CTRL_TRAFFIC_OVERRIDE |
12281 LED_CTRL_TRAFFIC_BLINK |
12282 LED_CTRL_TRAFFIC_LED);
12283 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012284
stephen hemminger81b87092011-04-04 08:43:50 +000012285 case ETHTOOL_ID_OFF:
12286 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
12287 LED_CTRL_TRAFFIC_OVERRIDE);
12288 break;
Michael Chan4009a932005-09-05 17:52:54 -070012289
stephen hemminger81b87092011-04-04 08:43:50 +000012290 case ETHTOOL_ID_INACTIVE:
12291 tw32(MAC_LED_CTRL, tp->led_ctrl);
12292 break;
Michael Chan4009a932005-09-05 17:52:54 -070012293 }
stephen hemminger81b87092011-04-04 08:43:50 +000012294
Michael Chan4009a932005-09-05 17:52:54 -070012295 return 0;
12296}
12297
Matt Carlsonde6f31e2010-04-12 06:58:30 +000012298static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299 struct ethtool_stats *estats, u64 *tmp_stats)
12300{
12301 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000012302
Matt Carlsonb546e462012-02-13 15:20:09 +000012303 if (tp->hw_stats)
12304 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
12305 else
12306 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012307}
12308
Matt Carlson535a4902011-07-20 10:20:56 +000012309static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000012310{
12311 int i;
12312 __be32 *buf;
12313 u32 offset = 0, len = 0;
12314 u32 magic, val;
12315
Joe Perches63c3a662011-04-26 08:12:10 +000012316 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000012317 return NULL;
12318
12319 if (magic == TG3_EEPROM_MAGIC) {
12320 for (offset = TG3_NVM_DIR_START;
12321 offset < TG3_NVM_DIR_END;
12322 offset += TG3_NVM_DIRENT_SIZE) {
12323 if (tg3_nvram_read(tp, offset, &val))
12324 return NULL;
12325
12326 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
12327 TG3_NVM_DIRTYPE_EXTVPD)
12328 break;
12329 }
12330
12331 if (offset != TG3_NVM_DIR_END) {
12332 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
12333 if (tg3_nvram_read(tp, offset + 4, &offset))
12334 return NULL;
12335
12336 offset = tg3_nvram_logical_addr(tp, offset);
12337 }
12338 }
12339
12340 if (!offset || !len) {
12341 offset = TG3_NVM_VPD_OFF;
12342 len = TG3_NVM_VPD_LEN;
12343 }
12344
12345 buf = kmalloc(len, GFP_KERNEL);
12346 if (buf == NULL)
12347 return NULL;
12348
12349 if (magic == TG3_EEPROM_MAGIC) {
12350 for (i = 0; i < len; i += 4) {
12351 /* The data is in little-endian format in NVRAM.
12352 * Use the big-endian read routines to preserve
12353 * the byte order as it exists in NVRAM.
12354 */
12355 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
12356 goto error;
12357 }
12358 } else {
12359 u8 *ptr;
12360 ssize_t cnt;
12361 unsigned int pos = 0;
12362
12363 ptr = (u8 *)&buf[0];
12364 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
12365 cnt = pci_read_vpd(tp->pdev, pos,
12366 len - pos, ptr);
12367 if (cnt == -ETIMEDOUT || cnt == -EINTR)
12368 cnt = 0;
12369 else if (cnt < 0)
12370 goto error;
12371 }
12372 if (pos != len)
12373 goto error;
12374 }
12375
Matt Carlson535a4902011-07-20 10:20:56 +000012376 *vpdlen = len;
12377
Matt Carlsonc3e94502011-04-13 11:05:08 +000012378 return buf;
12379
12380error:
12381 kfree(buf);
12382 return NULL;
12383}
12384
Michael Chan566f86a2005-05-29 14:56:58 -070012385#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080012386#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
12387#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
12388#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000012389#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
12390#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000012391#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070012392#define NVRAM_SELFBOOT_HW_SIZE 0x20
12393#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070012394
12395static int tg3_test_nvram(struct tg3 *tp)
12396{
Matt Carlson535a4902011-07-20 10:20:56 +000012397 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012398 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010012399 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070012400
Joe Perches63c3a662011-04-26 08:12:10 +000012401 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000012402 return 0;
12403
Matt Carlsone4f34112009-02-25 14:25:00 +000012404 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012405 return -EIO;
12406
Michael Chan1b277772006-03-20 22:27:48 -080012407 if (magic == TG3_EEPROM_MAGIC)
12408 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070012409 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080012410 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
12411 TG3_EEPROM_SB_FORMAT_1) {
12412 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
12413 case TG3_EEPROM_SB_REVISION_0:
12414 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12415 break;
12416 case TG3_EEPROM_SB_REVISION_2:
12417 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12418 break;
12419 case TG3_EEPROM_SB_REVISION_3:
12420 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12421 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012422 case TG3_EEPROM_SB_REVISION_4:
12423 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12424 break;
12425 case TG3_EEPROM_SB_REVISION_5:
12426 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12427 break;
12428 case TG3_EEPROM_SB_REVISION_6:
12429 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12430 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012431 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012432 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012433 }
12434 } else
Michael Chan1b277772006-03-20 22:27:48 -080012435 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012436 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12437 size = NVRAM_SELFBOOT_HW_SIZE;
12438 else
Michael Chan1b277772006-03-20 22:27:48 -080012439 return -EIO;
12440
12441 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012442 if (buf == NULL)
12443 return -ENOMEM;
12444
Michael Chan1b277772006-03-20 22:27:48 -080012445 err = -EIO;
12446 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012447 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12448 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012449 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012450 }
Michael Chan1b277772006-03-20 22:27:48 -080012451 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012452 goto out;
12453
Michael Chan1b277772006-03-20 22:27:48 -080012454 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012455 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012456 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012457 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012458 u8 *buf8 = (u8 *) buf, csum8 = 0;
12459
Al Virob9fc7dc2007-12-17 22:59:57 -080012460 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012461 TG3_EEPROM_SB_REVISION_2) {
12462 /* For rev 2, the csum doesn't include the MBA. */
12463 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12464 csum8 += buf8[i];
12465 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12466 csum8 += buf8[i];
12467 } else {
12468 for (i = 0; i < size; i++)
12469 csum8 += buf8[i];
12470 }
Michael Chan1b277772006-03-20 22:27:48 -080012471
Adrian Bunkad96b482006-04-05 22:21:04 -070012472 if (csum8 == 0) {
12473 err = 0;
12474 goto out;
12475 }
12476
12477 err = -EIO;
12478 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012479 }
Michael Chan566f86a2005-05-29 14:56:58 -070012480
Al Virob9fc7dc2007-12-17 22:59:57 -080012481 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012482 TG3_EEPROM_MAGIC_HW) {
12483 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012484 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012485 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012486
12487 /* Separate the parity bits and the data bytes. */
12488 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12489 if ((i == 0) || (i == 8)) {
12490 int l;
12491 u8 msk;
12492
12493 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12494 parity[k++] = buf8[i] & msk;
12495 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012496 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012497 int l;
12498 u8 msk;
12499
12500 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12501 parity[k++] = buf8[i] & msk;
12502 i++;
12503
12504 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12505 parity[k++] = buf8[i] & msk;
12506 i++;
12507 }
12508 data[j++] = buf8[i];
12509 }
12510
12511 err = -EIO;
12512 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12513 u8 hw8 = hweight8(data[i]);
12514
12515 if ((hw8 & 0x1) && parity[i])
12516 goto out;
12517 else if (!(hw8 & 0x1) && !parity[i])
12518 goto out;
12519 }
12520 err = 0;
12521 goto out;
12522 }
12523
Matt Carlson01c3a392011-03-09 16:58:20 +000012524 err = -EIO;
12525
Michael Chan566f86a2005-05-29 14:56:58 -070012526 /* Bootstrap checksum at offset 0x10 */
12527 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012528 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012529 goto out;
12530
12531 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12532 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012533 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012534 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012535
Matt Carlsonc3e94502011-04-13 11:05:08 +000012536 kfree(buf);
12537
Matt Carlson535a4902011-07-20 10:20:56 +000012538 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012539 if (!buf)
12540 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012541
Matt Carlson535a4902011-07-20 10:20:56 +000012542 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012543 if (i > 0) {
12544 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12545 if (j < 0)
12546 goto out;
12547
Matt Carlson535a4902011-07-20 10:20:56 +000012548 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012549 goto out;
12550
12551 i += PCI_VPD_LRDT_TAG_SIZE;
12552 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12553 PCI_VPD_RO_KEYWORD_CHKSUM);
12554 if (j > 0) {
12555 u8 csum8 = 0;
12556
12557 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12558
12559 for (i = 0; i <= j; i++)
12560 csum8 += ((u8 *)buf)[i];
12561
12562 if (csum8)
12563 goto out;
12564 }
12565 }
12566
Michael Chan566f86a2005-05-29 14:56:58 -070012567 err = 0;
12568
12569out:
12570 kfree(buf);
12571 return err;
12572}
12573
Michael Chanca430072005-05-29 14:57:23 -070012574#define TG3_SERDES_TIMEOUT_SEC 2
12575#define TG3_COPPER_TIMEOUT_SEC 6
12576
12577static int tg3_test_link(struct tg3 *tp)
12578{
12579 int i, max;
12580
12581 if (!netif_running(tp->dev))
12582 return -ENODEV;
12583
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012584 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012585 max = TG3_SERDES_TIMEOUT_SEC;
12586 else
12587 max = TG3_COPPER_TIMEOUT_SEC;
12588
12589 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012590 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012591 return 0;
12592
12593 if (msleep_interruptible(1000))
12594 break;
12595 }
12596
12597 return -EIO;
12598}
12599
Michael Chana71116d2005-05-29 14:58:11 -070012600/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012601static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012602{
Michael Chanb16250e2006-09-27 16:10:14 -070012603 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012604 u32 offset, read_mask, write_mask, val, save_val, read_val;
12605 static struct {
12606 u16 offset;
12607 u16 flags;
12608#define TG3_FL_5705 0x1
12609#define TG3_FL_NOT_5705 0x2
12610#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012611#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012612 u32 read_mask;
12613 u32 write_mask;
12614 } reg_tbl[] = {
12615 /* MAC Control Registers */
12616 { MAC_MODE, TG3_FL_NOT_5705,
12617 0x00000000, 0x00ef6f8c },
12618 { MAC_MODE, TG3_FL_5705,
12619 0x00000000, 0x01ef6b8c },
12620 { MAC_STATUS, TG3_FL_NOT_5705,
12621 0x03800107, 0x00000000 },
12622 { MAC_STATUS, TG3_FL_5705,
12623 0x03800100, 0x00000000 },
12624 { MAC_ADDR_0_HIGH, 0x0000,
12625 0x00000000, 0x0000ffff },
12626 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012627 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012628 { MAC_RX_MTU_SIZE, 0x0000,
12629 0x00000000, 0x0000ffff },
12630 { MAC_TX_MODE, 0x0000,
12631 0x00000000, 0x00000070 },
12632 { MAC_TX_LENGTHS, 0x0000,
12633 0x00000000, 0x00003fff },
12634 { MAC_RX_MODE, TG3_FL_NOT_5705,
12635 0x00000000, 0x000007fc },
12636 { MAC_RX_MODE, TG3_FL_5705,
12637 0x00000000, 0x000007dc },
12638 { MAC_HASH_REG_0, 0x0000,
12639 0x00000000, 0xffffffff },
12640 { MAC_HASH_REG_1, 0x0000,
12641 0x00000000, 0xffffffff },
12642 { MAC_HASH_REG_2, 0x0000,
12643 0x00000000, 0xffffffff },
12644 { MAC_HASH_REG_3, 0x0000,
12645 0x00000000, 0xffffffff },
12646
12647 /* Receive Data and Receive BD Initiator Control Registers. */
12648 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12649 0x00000000, 0xffffffff },
12650 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12651 0x00000000, 0xffffffff },
12652 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12653 0x00000000, 0x00000003 },
12654 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12655 0x00000000, 0xffffffff },
12656 { RCVDBDI_STD_BD+0, 0x0000,
12657 0x00000000, 0xffffffff },
12658 { RCVDBDI_STD_BD+4, 0x0000,
12659 0x00000000, 0xffffffff },
12660 { RCVDBDI_STD_BD+8, 0x0000,
12661 0x00000000, 0xffff0002 },
12662 { RCVDBDI_STD_BD+0xc, 0x0000,
12663 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012664
Michael Chana71116d2005-05-29 14:58:11 -070012665 /* Receive BD Initiator Control Registers. */
12666 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12667 0x00000000, 0xffffffff },
12668 { RCVBDI_STD_THRESH, TG3_FL_5705,
12669 0x00000000, 0x000003ff },
12670 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12671 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012672
Michael Chana71116d2005-05-29 14:58:11 -070012673 /* Host Coalescing Control Registers. */
12674 { HOSTCC_MODE, TG3_FL_NOT_5705,
12675 0x00000000, 0x00000004 },
12676 { HOSTCC_MODE, TG3_FL_5705,
12677 0x00000000, 0x000000f6 },
12678 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12679 0x00000000, 0xffffffff },
12680 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12681 0x00000000, 0x000003ff },
12682 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12683 0x00000000, 0xffffffff },
12684 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12685 0x00000000, 0x000003ff },
12686 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12687 0x00000000, 0xffffffff },
12688 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12689 0x00000000, 0x000000ff },
12690 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12691 0x00000000, 0xffffffff },
12692 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12693 0x00000000, 0x000000ff },
12694 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12695 0x00000000, 0xffffffff },
12696 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12697 0x00000000, 0xffffffff },
12698 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12699 0x00000000, 0xffffffff },
12700 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12701 0x00000000, 0x000000ff },
12702 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12703 0x00000000, 0xffffffff },
12704 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12705 0x00000000, 0x000000ff },
12706 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12707 0x00000000, 0xffffffff },
12708 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12709 0x00000000, 0xffffffff },
12710 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12711 0x00000000, 0xffffffff },
12712 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12713 0x00000000, 0xffffffff },
12714 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12715 0x00000000, 0xffffffff },
12716 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12717 0xffffffff, 0x00000000 },
12718 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12719 0xffffffff, 0x00000000 },
12720
12721 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012722 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012723 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012724 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012725 0x00000000, 0x007fffff },
12726 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12727 0x00000000, 0x0000003f },
12728 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12729 0x00000000, 0x000001ff },
12730 { BUFMGR_MB_HIGH_WATER, 0x0000,
12731 0x00000000, 0x000001ff },
12732 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12733 0xffffffff, 0x00000000 },
12734 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12735 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012736
Michael Chana71116d2005-05-29 14:58:11 -070012737 /* Mailbox Registers */
12738 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12739 0x00000000, 0x000001ff },
12740 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12741 0x00000000, 0x000001ff },
12742 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12743 0x00000000, 0x000007ff },
12744 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12745 0x00000000, 0x000001ff },
12746
12747 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12748 };
12749
Michael Chanb16250e2006-09-27 16:10:14 -070012750 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012751 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012752 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012753 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012754 is_5750 = 1;
12755 }
Michael Chana71116d2005-05-29 14:58:11 -070012756
12757 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12758 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12759 continue;
12760
12761 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12762 continue;
12763
Joe Perches63c3a662011-04-26 08:12:10 +000012764 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012765 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12766 continue;
12767
Michael Chanb16250e2006-09-27 16:10:14 -070012768 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12769 continue;
12770
Michael Chana71116d2005-05-29 14:58:11 -070012771 offset = (u32) reg_tbl[i].offset;
12772 read_mask = reg_tbl[i].read_mask;
12773 write_mask = reg_tbl[i].write_mask;
12774
12775 /* Save the original register content */
12776 save_val = tr32(offset);
12777
12778 /* Determine the read-only value. */
12779 read_val = save_val & read_mask;
12780
12781 /* Write zero to the register, then make sure the read-only bits
12782 * are not changed and the read/write bits are all zeros.
12783 */
12784 tw32(offset, 0);
12785
12786 val = tr32(offset);
12787
12788 /* Test the read-only and read/write bits. */
12789 if (((val & read_mask) != read_val) || (val & write_mask))
12790 goto out;
12791
12792 /* Write ones to all the bits defined by RdMask and WrMask, then
12793 * make sure the read-only bits are not changed and the
12794 * read/write bits are all ones.
12795 */
12796 tw32(offset, read_mask | write_mask);
12797
12798 val = tr32(offset);
12799
12800 /* Test the read-only bits. */
12801 if ((val & read_mask) != read_val)
12802 goto out;
12803
12804 /* Test the read/write bits. */
12805 if ((val & write_mask) != write_mask)
12806 goto out;
12807
12808 tw32(offset, save_val);
12809 }
12810
12811 return 0;
12812
12813out:
Michael Chan9f88f292006-12-07 00:22:54 -080012814 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012815 netdev_err(tp->dev,
12816 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012817 tw32(offset, save_val);
12818 return -EIO;
12819}
12820
Michael Chan7942e1d2005-05-29 14:58:36 -070012821static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12822{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012823 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012824 int i;
12825 u32 j;
12826
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012827 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012828 for (j = 0; j < len; j += 4) {
12829 u32 val;
12830
12831 tg3_write_mem(tp, offset + j, test_pattern[i]);
12832 tg3_read_mem(tp, offset + j, &val);
12833 if (val != test_pattern[i])
12834 return -EIO;
12835 }
12836 }
12837 return 0;
12838}
12839
12840static int tg3_test_memory(struct tg3 *tp)
12841{
12842 static struct mem_entry {
12843 u32 offset;
12844 u32 len;
12845 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012846 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012847 { 0x00002000, 0x1c000},
12848 { 0xffffffff, 0x00000}
12849 }, mem_tbl_5705[] = {
12850 { 0x00000100, 0x0000c},
12851 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012852 { 0x00004000, 0x00800},
12853 { 0x00006000, 0x01000},
12854 { 0x00008000, 0x02000},
12855 { 0x00010000, 0x0e000},
12856 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012857 }, mem_tbl_5755[] = {
12858 { 0x00000200, 0x00008},
12859 { 0x00004000, 0x00800},
12860 { 0x00006000, 0x00800},
12861 { 0x00008000, 0x02000},
12862 { 0x00010000, 0x0c000},
12863 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012864 }, mem_tbl_5906[] = {
12865 { 0x00000200, 0x00008},
12866 { 0x00004000, 0x00400},
12867 { 0x00006000, 0x00400},
12868 { 0x00008000, 0x01000},
12869 { 0x00010000, 0x01000},
12870 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012871 }, mem_tbl_5717[] = {
12872 { 0x00000200, 0x00008},
12873 { 0x00010000, 0x0a000},
12874 { 0x00020000, 0x13c00},
12875 { 0xffffffff, 0x00000}
12876 }, mem_tbl_57765[] = {
12877 { 0x00000200, 0x00008},
12878 { 0x00004000, 0x00800},
12879 { 0x00006000, 0x09800},
12880 { 0x00010000, 0x0a000},
12881 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012882 };
12883 struct mem_entry *mem_tbl;
12884 int err = 0;
12885 int i;
12886
Joe Perches63c3a662011-04-26 08:12:10 +000012887 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012888 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012889 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012890 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012891 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012892 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012893 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012894 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012895 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012896 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012897 mem_tbl = mem_tbl_5705;
12898 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012899 mem_tbl = mem_tbl_570x;
12900
12901 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012902 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12903 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012904 break;
12905 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012906
Michael Chan7942e1d2005-05-29 14:58:36 -070012907 return err;
12908}
12909
Matt Carlsonbb158d62011-04-25 12:42:47 +000012910#define TG3_TSO_MSS 500
12911
12912#define TG3_TSO_IP_HDR_LEN 20
12913#define TG3_TSO_TCP_HDR_LEN 20
12914#define TG3_TSO_TCP_OPT_LEN 12
12915
12916static const u8 tg3_tso_header[] = {
129170x08, 0x00,
129180x45, 0x00, 0x00, 0x00,
129190x00, 0x00, 0x40, 0x00,
129200x40, 0x06, 0x00, 0x00,
129210x0a, 0x00, 0x00, 0x01,
129220x0a, 0x00, 0x00, 0x02,
129230x0d, 0x00, 0xe0, 0x00,
129240x00, 0x00, 0x01, 0x00,
129250x00, 0x00, 0x02, 0x00,
129260x80, 0x10, 0x10, 0x00,
129270x14, 0x09, 0x00, 0x00,
129280x01, 0x01, 0x08, 0x0a,
129290x11, 0x11, 0x11, 0x11,
129300x11, 0x11, 0x11, 0x11,
12931};
Michael Chan9f40dea2005-09-05 17:53:06 -070012932
Matt Carlson28a45952011-08-19 13:58:22 +000012933static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012934{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012935 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012936 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012937 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012938 struct sk_buff *skb;
12939 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012940 dma_addr_t map;
12941 int num_pkts, tx_len, rx_len, i, err;
12942 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012943 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012944 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012945
Matt Carlsonc8873402010-02-12 14:47:11 +000012946 tnapi = &tp->napi[0];
12947 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012948 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012949 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012950 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012951 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012952 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012953 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012954 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012955
Michael Chanc76949a2005-05-29 14:58:59 -070012956 err = -EIO;
12957
Matt Carlson4852a862011-04-13 11:05:07 +000012958 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012959 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012960 if (!skb)
12961 return -ENOMEM;
12962
Michael Chanc76949a2005-05-29 14:58:59 -070012963 tx_data = skb_put(skb, tx_len);
12964 memcpy(tx_data, tp->dev->dev_addr, 6);
12965 memset(tx_data + 6, 0x0, 8);
12966
Matt Carlson4852a862011-04-13 11:05:07 +000012967 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012968
Matt Carlson28a45952011-08-19 13:58:22 +000012969 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012970 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12971
12972 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12973 TG3_TSO_TCP_OPT_LEN;
12974
12975 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12976 sizeof(tg3_tso_header));
12977 mss = TG3_TSO_MSS;
12978
12979 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12980 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12981
12982 /* Set the total length field in the IP header */
12983 iph->tot_len = htons((u16)(mss + hdr_len));
12984
12985 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12986 TXD_FLAG_CPU_POST_DMA);
12987
Joe Perches63c3a662011-04-26 08:12:10 +000012988 if (tg3_flag(tp, HW_TSO_1) ||
12989 tg3_flag(tp, HW_TSO_2) ||
12990 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012991 struct tcphdr *th;
12992 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12993 th = (struct tcphdr *)&tx_data[val];
12994 th->check = 0;
12995 } else
12996 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12997
Joe Perches63c3a662011-04-26 08:12:10 +000012998 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012999 mss |= (hdr_len & 0xc) << 12;
13000 if (hdr_len & 0x10)
13001 base_flags |= 0x00000010;
13002 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000013003 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013004 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000013005 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000013006 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013007 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
13008 } else {
13009 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
13010 }
13011
13012 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
13013 } else {
13014 num_pkts = 1;
13015 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000013016
13017 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
13018 tx_len > VLAN_ETH_FRAME_LEN)
13019 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013020 }
13021
13022 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070013023 tx_data[i] = (u8) (i & 0xff);
13024
Alexander Duyckf4188d82009-12-02 16:48:38 +000013025 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
13026 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000013027 dev_kfree_skb(skb);
13028 return -EIO;
13029 }
Michael Chanc76949a2005-05-29 14:58:59 -070013030
Matt Carlson0d681b22011-07-27 14:20:49 +000013031 val = tnapi->tx_prod;
13032 tnapi->tx_buffers[val].skb = skb;
13033 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
13034
Michael Chanc76949a2005-05-29 14:58:59 -070013035 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013036 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013037
13038 udelay(10);
13039
Matt Carlson898a56f2009-08-28 14:02:40 +000013040 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070013041
Matt Carlson84b67b22011-07-27 14:20:52 +000013042 budget = tg3_tx_avail(tnapi);
13043 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000013044 base_flags | TXD_FLAG_END, mss, 0)) {
13045 tnapi->tx_buffers[val].skb = NULL;
13046 dev_kfree_skb(skb);
13047 return -EIO;
13048 }
Michael Chanc76949a2005-05-29 14:58:59 -070013049
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013050 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070013051
Michael Chan6541b802012-03-04 14:48:14 +000013052 /* Sync BD data before updating mailbox */
13053 wmb();
13054
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013055 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
13056 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070013057
13058 udelay(10);
13059
Matt Carlson303fc922009-11-02 14:27:34 +000013060 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
13061 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070013062 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000013063 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070013064
13065 udelay(10);
13066
Matt Carlson898a56f2009-08-28 14:02:40 +000013067 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
13068 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013069 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070013070 (rx_idx == (rx_start_idx + num_pkts)))
13071 break;
13072 }
13073
Matt Carlsonba1142e2011-11-04 09:15:00 +000013074 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070013075 dev_kfree_skb(skb);
13076
Matt Carlsonf3f3f272009-08-28 14:03:21 +000013077 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070013078 goto out;
13079
13080 if (rx_idx != rx_start_idx + num_pkts)
13081 goto out;
13082
Matt Carlsonbb158d62011-04-25 12:42:47 +000013083 val = data_off;
13084 while (rx_idx != rx_start_idx) {
13085 desc = &rnapi->rx_rcb[rx_start_idx++];
13086 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
13087 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070013088
Matt Carlsonbb158d62011-04-25 12:42:47 +000013089 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
13090 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000013091 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070013092
Matt Carlsonbb158d62011-04-25 12:42:47 +000013093 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
13094 - ETH_FCS_LEN;
13095
Matt Carlson28a45952011-08-19 13:58:22 +000013096 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013097 if (rx_len != tx_len)
13098 goto out;
13099
13100 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
13101 if (opaque_key != RXD_OPAQUE_RING_STD)
13102 goto out;
13103 } else {
13104 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
13105 goto out;
13106 }
13107 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
13108 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000013109 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000013110 goto out;
13111 }
13112
13113 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013114 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013115 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
13116 mapping);
13117 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013118 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000013119 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
13120 mapping);
13121 } else
Matt Carlson4852a862011-04-13 11:05:07 +000013122 goto out;
13123
Matt Carlsonbb158d62011-04-25 12:42:47 +000013124 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
13125 PCI_DMA_FROMDEVICE);
13126
Eric Dumazet9205fd92011-11-18 06:47:01 +000013127 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000013128 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000013129 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000013130 goto out;
13131 }
Matt Carlson4852a862011-04-13 11:05:07 +000013132 }
13133
Michael Chanc76949a2005-05-29 14:58:59 -070013134 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013135
Eric Dumazet9205fd92011-11-18 06:47:01 +000013136 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070013137out:
13138 return err;
13139}
13140
Matt Carlson00c266b2011-04-25 12:42:46 +000013141#define TG3_STD_LOOPBACK_FAILED 1
13142#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000013143#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000013144#define TG3_LOOPBACK_FAILED \
13145 (TG3_STD_LOOPBACK_FAILED | \
13146 TG3_JMB_LOOPBACK_FAILED | \
13147 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000013148
Matt Carlson941ec902011-08-19 13:58:23 +000013149static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070013150{
Matt Carlson28a45952011-08-19 13:58:22 +000013151 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000013152 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000013153 u32 jmb_pkt_sz = 9000;
13154
13155 if (tp->dma_limit)
13156 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070013157
Matt Carlsonab789042011-01-25 15:58:54 +000013158 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
13159 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
13160
Matt Carlson28a45952011-08-19 13:58:22 +000013161 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013162 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13163 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013164 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013165 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000013166 goto done;
13167 }
13168
Joe Perches953c96e2013-04-09 10:18:14 +000013169 err = tg3_reset_hw(tp, true);
Matt Carlsonab789042011-01-25 15:58:54 +000013170 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013171 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
13172 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013173 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013174 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000013175 goto done;
13176 }
Michael Chan9f40dea2005-09-05 17:53:06 -070013177
Joe Perches63c3a662011-04-26 08:12:10 +000013178 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000013179 int i;
13180
13181 /* Reroute all rx packets to the 1st queue */
13182 for (i = MAC_RSS_INDIR_TBL_0;
13183 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
13184 tw32(i, 0x0);
13185 }
13186
Matt Carlson6e01b202011-08-19 13:58:20 +000013187 /* HW errata - mac loopback fails in some cases on 5780.
13188 * Normal traffic and PHY loopback are not affected by
13189 * errata. Also, the MAC loopback test is deprecated for
13190 * all newer ASIC revisions.
13191 */
Joe Perches41535772013-02-16 11:20:04 +000013192 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000013193 !tg3_flag(tp, CPMU_PRESENT)) {
13194 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070013195
Matt Carlson28a45952011-08-19 13:58:22 +000013196 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013197 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013198
13199 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013200 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013201 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000013202
13203 tg3_mac_loopback(tp, false);
13204 }
Matt Carlson4852a862011-04-13 11:05:07 +000013205
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013206 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013207 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013208 int i;
13209
Matt Carlson941ec902011-08-19 13:58:23 +000013210 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013211
13212 /* Wait for link */
13213 for (i = 0; i < 100; i++) {
13214 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
13215 break;
13216 mdelay(1);
13217 }
13218
Matt Carlson28a45952011-08-19 13:58:22 +000013219 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013220 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013221 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000013222 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013223 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000013224 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013225 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013226 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070013227
Matt Carlson941ec902011-08-19 13:58:23 +000013228 if (do_extlpbk) {
13229 tg3_phy_lpbk_set(tp, 0, true);
13230
13231 /* All link indications report up, but the hardware
13232 * isn't really ready for about 20 msec. Double it
13233 * to be sure.
13234 */
13235 mdelay(40);
13236
13237 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013238 data[TG3_EXT_LOOPB_TEST] |=
13239 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013240 if (tg3_flag(tp, TSO_CAPABLE) &&
13241 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013242 data[TG3_EXT_LOOPB_TEST] |=
13243 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013244 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000013245 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013246 data[TG3_EXT_LOOPB_TEST] |=
13247 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000013248 }
13249
Matt Carlson5e5a7f32011-08-19 13:58:21 +000013250 /* Re-enable gphy autopowerdown. */
13251 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
13252 tg3_phy_toggle_apd(tp, true);
13253 }
Matt Carlson6833c042008-11-21 17:18:59 -080013254
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013255 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
13256 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000013257
Matt Carlsonab789042011-01-25 15:58:54 +000013258done:
13259 tp->phy_flags |= eee_cap;
13260
Michael Chan9f40dea2005-09-05 17:53:06 -070013261 return err;
13262}
13263
Michael Chan4cafd3f2005-05-29 14:56:34 -070013264static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
13265 u64 *data)
13266{
Michael Chan566f86a2005-05-29 14:56:58 -070013267 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000013268 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070013269
Matt Carlsonbed98292011-07-13 09:27:29 +000013270 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
13271 tg3_power_up(tp)) {
13272 etest->flags |= ETH_TEST_FL_FAILED;
13273 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
13274 return;
13275 }
Michael Chanbc1c7562006-03-20 17:48:03 -080013276
Michael Chan566f86a2005-05-29 14:56:58 -070013277 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
13278
13279 if (tg3_test_nvram(tp) != 0) {
13280 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013281 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070013282 }
Matt Carlson941ec902011-08-19 13:58:23 +000013283 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070013284 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013285 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070013286 }
Michael Chana71116d2005-05-29 14:58:11 -070013287 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013288 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070013289
Michael Chanbbe832c2005-06-24 20:20:04 -070013290 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013291 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070013292 tg3_netif_stop(tp);
13293 irq_sync = 1;
13294 }
13295
13296 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070013297 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080013298 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013299 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000013300 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070013301 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080013302 if (!err)
13303 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013304
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013305 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080013306 tg3_phy_reset(tp);
13307
Michael Chana71116d2005-05-29 14:58:11 -070013308 if (tg3_test_registers(tp) != 0) {
13309 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013310 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070013311 }
Matt Carlson28a45952011-08-19 13:58:22 +000013312
Michael Chan7942e1d2005-05-29 14:58:36 -070013313 if (tg3_test_memory(tp) != 0) {
13314 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013315 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070013316 }
Matt Carlson28a45952011-08-19 13:58:22 +000013317
Matt Carlson941ec902011-08-19 13:58:23 +000013318 if (doextlpbk)
13319 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
13320
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013321 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070013322 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070013323
David S. Millerf47c11e2005-06-24 20:18:35 -070013324 tg3_full_unlock(tp);
13325
Michael Chand4bc3922005-05-29 14:59:20 -070013326 if (tg3_test_interrupt(tp) != 0) {
13327 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000013328 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070013329 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013330
13331 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070013332
Michael Chana71116d2005-05-29 14:58:11 -070013333 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13334 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013335 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000013336 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013337 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070013338 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013339 }
David S. Millerf47c11e2005-06-24 20:18:35 -070013340
13341 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013342
13343 if (irq_sync && !err2)
13344 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070013345 }
Matt Carlson80096062010-08-02 11:26:06 +000013346 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013347 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080013348
Michael Chan4cafd3f2005-05-29 14:56:34 -070013349}
13350
Matt Carlson0a633ac2012-12-03 19:36:59 +000013351static int tg3_hwtstamp_ioctl(struct net_device *dev,
13352 struct ifreq *ifr, int cmd)
13353{
13354 struct tg3 *tp = netdev_priv(dev);
13355 struct hwtstamp_config stmpconf;
13356
13357 if (!tg3_flag(tp, PTP_CAPABLE))
13358 return -EINVAL;
13359
13360 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
13361 return -EFAULT;
13362
13363 if (stmpconf.flags)
13364 return -EINVAL;
13365
13366 switch (stmpconf.tx_type) {
13367 case HWTSTAMP_TX_ON:
13368 tg3_flag_set(tp, TX_TSTAMP_EN);
13369 break;
13370 case HWTSTAMP_TX_OFF:
13371 tg3_flag_clear(tp, TX_TSTAMP_EN);
13372 break;
13373 default:
13374 return -ERANGE;
13375 }
13376
13377 switch (stmpconf.rx_filter) {
13378 case HWTSTAMP_FILTER_NONE:
13379 tp->rxptpctl = 0;
13380 break;
13381 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13382 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13383 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
13384 break;
13385 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13386 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13387 TG3_RX_PTP_CTL_SYNC_EVNT;
13388 break;
13389 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13390 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
13391 TG3_RX_PTP_CTL_DELAY_REQ;
13392 break;
13393 case HWTSTAMP_FILTER_PTP_V2_EVENT:
13394 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13395 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13396 break;
13397 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13398 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13399 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13400 break;
13401 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13402 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13403 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
13404 break;
13405 case HWTSTAMP_FILTER_PTP_V2_SYNC:
13406 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13407 TG3_RX_PTP_CTL_SYNC_EVNT;
13408 break;
13409 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13410 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13411 TG3_RX_PTP_CTL_SYNC_EVNT;
13412 break;
13413 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13414 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13415 TG3_RX_PTP_CTL_SYNC_EVNT;
13416 break;
13417 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13418 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13419 TG3_RX_PTP_CTL_DELAY_REQ;
13420 break;
13421 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13422 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13423 TG3_RX_PTP_CTL_DELAY_REQ;
13424 break;
13425 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13426 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13427 TG3_RX_PTP_CTL_DELAY_REQ;
13428 break;
13429 default:
13430 return -ERANGE;
13431 }
13432
13433 if (netif_running(dev) && tp->rxptpctl)
13434 tw32(TG3_RX_PTP_CTL,
13435 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13436
13437 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13438 -EFAULT : 0;
13439}
13440
Linus Torvalds1da177e2005-04-16 15:20:36 -070013441static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13442{
13443 struct mii_ioctl_data *data = if_mii(ifr);
13444 struct tg3 *tp = netdev_priv(dev);
13445 int err;
13446
Joe Perches63c3a662011-04-26 08:12:10 +000013447 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013448 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013449 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013450 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013451 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013452 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013453 }
13454
Matt Carlson33f401a2010-04-05 10:19:27 +000013455 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013456 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013457 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013458
13459 /* fallthru */
13460 case SIOCGMIIREG: {
13461 u32 mii_regval;
13462
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013463 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013464 break; /* We have no PHY */
13465
Matt Carlson34eea5a2011-04-20 07:57:38 +000013466 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013467 return -EAGAIN;
13468
David S. Millerf47c11e2005-06-24 20:18:35 -070013469 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013470 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13471 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013472 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013473
13474 data->val_out = mii_regval;
13475
13476 return err;
13477 }
13478
13479 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013480 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013481 break; /* We have no PHY */
13482
Matt Carlson34eea5a2011-04-20 07:57:38 +000013483 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013484 return -EAGAIN;
13485
David S. Millerf47c11e2005-06-24 20:18:35 -070013486 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013487 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13488 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013489 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013490
13491 return err;
13492
Matt Carlson0a633ac2012-12-03 19:36:59 +000013493 case SIOCSHWTSTAMP:
13494 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13495
Linus Torvalds1da177e2005-04-16 15:20:36 -070013496 default:
13497 /* do nothing */
13498 break;
13499 }
13500 return -EOPNOTSUPP;
13501}
13502
David S. Miller15f98502005-05-18 22:49:26 -070013503static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13504{
13505 struct tg3 *tp = netdev_priv(dev);
13506
13507 memcpy(ec, &tp->coal, sizeof(*ec));
13508 return 0;
13509}
13510
Michael Chand244c892005-07-05 14:42:33 -070013511static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13512{
13513 struct tg3 *tp = netdev_priv(dev);
13514 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13515 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13516
Joe Perches63c3a662011-04-26 08:12:10 +000013517 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013518 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13519 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13520 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13521 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13522 }
13523
13524 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13525 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13526 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13527 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13528 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13529 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13530 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13531 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13532 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13533 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13534 return -EINVAL;
13535
13536 /* No rx interrupts will be generated if both are zero */
13537 if ((ec->rx_coalesce_usecs == 0) &&
13538 (ec->rx_max_coalesced_frames == 0))
13539 return -EINVAL;
13540
13541 /* No tx interrupts will be generated if both are zero */
13542 if ((ec->tx_coalesce_usecs == 0) &&
13543 (ec->tx_max_coalesced_frames == 0))
13544 return -EINVAL;
13545
13546 /* Only copy relevant parameters, ignore all others. */
13547 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13548 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13549 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13550 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13551 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13552 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13553 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13554 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13555 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13556
13557 if (netif_running(dev)) {
13558 tg3_full_lock(tp, 0);
13559 __tg3_set_coalesce(tp, &tp->coal);
13560 tg3_full_unlock(tp);
13561 }
13562 return 0;
13563}
13564
Jeff Garzik7282d492006-09-13 14:30:00 -040013565static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013566 .get_settings = tg3_get_settings,
13567 .set_settings = tg3_set_settings,
13568 .get_drvinfo = tg3_get_drvinfo,
13569 .get_regs_len = tg3_get_regs_len,
13570 .get_regs = tg3_get_regs,
13571 .get_wol = tg3_get_wol,
13572 .set_wol = tg3_set_wol,
13573 .get_msglevel = tg3_get_msglevel,
13574 .set_msglevel = tg3_set_msglevel,
13575 .nway_reset = tg3_nway_reset,
13576 .get_link = ethtool_op_get_link,
13577 .get_eeprom_len = tg3_get_eeprom_len,
13578 .get_eeprom = tg3_get_eeprom,
13579 .set_eeprom = tg3_set_eeprom,
13580 .get_ringparam = tg3_get_ringparam,
13581 .set_ringparam = tg3_set_ringparam,
13582 .get_pauseparam = tg3_get_pauseparam,
13583 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013584 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013585 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013586 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013587 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013588 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013589 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013590 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013591 .get_rxnfc = tg3_get_rxnfc,
13592 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13593 .get_rxfh_indir = tg3_get_rxfh_indir,
13594 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013595 .get_channels = tg3_get_channels,
13596 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013597 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013598};
13599
David S. Millerb4017c52012-03-01 17:57:40 -050013600static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13601 struct rtnl_link_stats64 *stats)
13602{
13603 struct tg3 *tp = netdev_priv(dev);
13604
David S. Millerb4017c52012-03-01 17:57:40 -050013605 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013606 if (!tp->hw_stats) {
13607 spin_unlock_bh(&tp->lock);
13608 return &tp->net_stats_prev;
13609 }
13610
David S. Millerb4017c52012-03-01 17:57:40 -050013611 tg3_get_nstats(tp, stats);
13612 spin_unlock_bh(&tp->lock);
13613
13614 return stats;
13615}
13616
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013617static void tg3_set_rx_mode(struct net_device *dev)
13618{
13619 struct tg3 *tp = netdev_priv(dev);
13620
13621 if (!netif_running(dev))
13622 return;
13623
13624 tg3_full_lock(tp, 0);
13625 __tg3_set_rx_mode(dev);
13626 tg3_full_unlock(tp);
13627}
13628
Matt Carlsonfaf16272012-02-13 10:20:07 +000013629static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13630 int new_mtu)
13631{
13632 dev->mtu = new_mtu;
13633
13634 if (new_mtu > ETH_DATA_LEN) {
13635 if (tg3_flag(tp, 5780_CLASS)) {
13636 netdev_update_features(dev);
13637 tg3_flag_clear(tp, TSO_CAPABLE);
13638 } else {
13639 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13640 }
13641 } else {
13642 if (tg3_flag(tp, 5780_CLASS)) {
13643 tg3_flag_set(tp, TSO_CAPABLE);
13644 netdev_update_features(dev);
13645 }
13646 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13647 }
13648}
13649
13650static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13651{
13652 struct tg3 *tp = netdev_priv(dev);
Joe Perches953c96e2013-04-09 10:18:14 +000013653 int err;
13654 bool reset_phy = false;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013655
13656 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13657 return -EINVAL;
13658
13659 if (!netif_running(dev)) {
13660 /* We'll just catch it later when the
13661 * device is up'd.
13662 */
13663 tg3_set_mtu(dev, tp, new_mtu);
13664 return 0;
13665 }
13666
13667 tg3_phy_stop(tp);
13668
13669 tg3_netif_stop(tp);
13670
13671 tg3_full_lock(tp, 1);
13672
13673 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13674
13675 tg3_set_mtu(dev, tp, new_mtu);
13676
Michael Chan2fae5e32012-03-04 14:48:15 +000013677 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13678 * breaks all requests to 256 bytes.
13679 */
Joe Perches41535772013-02-16 11:20:04 +000013680 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Joe Perches953c96e2013-04-09 10:18:14 +000013681 reset_phy = true;
Michael Chan2fae5e32012-03-04 14:48:15 +000013682
13683 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013684
13685 if (!err)
13686 tg3_netif_start(tp);
13687
13688 tg3_full_unlock(tp);
13689
13690 if (!err)
13691 tg3_phy_start(tp);
13692
13693 return err;
13694}
13695
13696static const struct net_device_ops tg3_netdev_ops = {
13697 .ndo_open = tg3_open,
13698 .ndo_stop = tg3_close,
13699 .ndo_start_xmit = tg3_start_xmit,
13700 .ndo_get_stats64 = tg3_get_stats64,
13701 .ndo_validate_addr = eth_validate_addr,
13702 .ndo_set_rx_mode = tg3_set_rx_mode,
13703 .ndo_set_mac_address = tg3_set_mac_addr,
13704 .ndo_do_ioctl = tg3_ioctl,
13705 .ndo_tx_timeout = tg3_tx_timeout,
13706 .ndo_change_mtu = tg3_change_mtu,
13707 .ndo_fix_features = tg3_fix_features,
13708 .ndo_set_features = tg3_set_features,
13709#ifdef CONFIG_NET_POLL_CONTROLLER
13710 .ndo_poll_controller = tg3_poll_controller,
13711#endif
13712};
13713
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013714static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013715{
Michael Chan1b277772006-03-20 22:27:48 -080013716 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013717
13718 tp->nvram_size = EEPROM_CHIP_SIZE;
13719
Matt Carlsone4f34112009-02-25 14:25:00 +000013720 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013721 return;
13722
Michael Chanb16250e2006-09-27 16:10:14 -070013723 if ((magic != TG3_EEPROM_MAGIC) &&
13724 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13725 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013726 return;
13727
13728 /*
13729 * Size the chip by reading offsets at increasing powers of two.
13730 * When we encounter our validation signature, we know the addressing
13731 * has wrapped around, and thus have our chip size.
13732 */
Michael Chan1b277772006-03-20 22:27:48 -080013733 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734
13735 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013736 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013737 return;
13738
Michael Chan18201802006-03-20 22:29:15 -080013739 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013740 break;
13741
13742 cursize <<= 1;
13743 }
13744
13745 tp->nvram_size = cursize;
13746}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013747
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013748static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749{
13750 u32 val;
13751
Joe Perches63c3a662011-04-26 08:12:10 +000013752 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013753 return;
13754
13755 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013756 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013757 tg3_get_eeprom_size(tp);
13758 return;
13759 }
13760
Matt Carlson6d348f22009-02-25 14:25:52 +000013761 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013762 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013763 /* This is confusing. We want to operate on the
13764 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13765 * call will read from NVRAM and byteswap the data
13766 * according to the byteswapping settings for all
13767 * other register accesses. This ensures the data we
13768 * want will always reside in the lower 16-bits.
13769 * However, the data in NVRAM is in LE format, which
13770 * means the data from the NVRAM read will always be
13771 * opposite the endianness of the CPU. The 16-bit
13772 * byteswap then brings the data to CPU endianness.
13773 */
13774 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013775 return;
13776 }
13777 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013778 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013779}
13780
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013781static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013782{
13783 u32 nvcfg1;
13784
13785 nvcfg1 = tr32(NVRAM_CFG1);
13786 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013787 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013788 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013789 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13790 tw32(NVRAM_CFG1, nvcfg1);
13791 }
13792
Joe Perches41535772013-02-16 11:20:04 +000013793 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013794 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013795 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013796 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13797 tp->nvram_jedecnum = JEDEC_ATMEL;
13798 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013799 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013800 break;
13801 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13802 tp->nvram_jedecnum = JEDEC_ATMEL;
13803 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13804 break;
13805 case FLASH_VENDOR_ATMEL_EEPROM:
13806 tp->nvram_jedecnum = JEDEC_ATMEL;
13807 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013808 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013809 break;
13810 case FLASH_VENDOR_ST:
13811 tp->nvram_jedecnum = JEDEC_ST;
13812 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013814 break;
13815 case FLASH_VENDOR_SAIFUN:
13816 tp->nvram_jedecnum = JEDEC_SAIFUN;
13817 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13818 break;
13819 case FLASH_VENDOR_SST_SMALL:
13820 case FLASH_VENDOR_SST_LARGE:
13821 tp->nvram_jedecnum = JEDEC_SST;
13822 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824 }
Matt Carlson8590a602009-08-28 12:29:16 +000013825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013826 tp->nvram_jedecnum = JEDEC_ATMEL;
13827 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013828 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013829 }
13830}
13831
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013832static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013833{
13834 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13835 case FLASH_5752PAGE_SIZE_256:
13836 tp->nvram_pagesize = 256;
13837 break;
13838 case FLASH_5752PAGE_SIZE_512:
13839 tp->nvram_pagesize = 512;
13840 break;
13841 case FLASH_5752PAGE_SIZE_1K:
13842 tp->nvram_pagesize = 1024;
13843 break;
13844 case FLASH_5752PAGE_SIZE_2K:
13845 tp->nvram_pagesize = 2048;
13846 break;
13847 case FLASH_5752PAGE_SIZE_4K:
13848 tp->nvram_pagesize = 4096;
13849 break;
13850 case FLASH_5752PAGE_SIZE_264:
13851 tp->nvram_pagesize = 264;
13852 break;
13853 case FLASH_5752PAGE_SIZE_528:
13854 tp->nvram_pagesize = 528;
13855 break;
13856 }
13857}
13858
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013859static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013860{
13861 u32 nvcfg1;
13862
13863 nvcfg1 = tr32(NVRAM_CFG1);
13864
Michael Chane6af3012005-04-21 17:12:05 -070013865 /* NVRAM protection for TPM */
13866 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013867 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013868
Michael Chan361b4ac2005-04-21 17:11:21 -070013869 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013870 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13871 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13872 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013873 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013874 break;
13875 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13876 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013877 tg3_flag_set(tp, NVRAM_BUFFERED);
13878 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013879 break;
13880 case FLASH_5752VENDOR_ST_M45PE10:
13881 case FLASH_5752VENDOR_ST_M45PE20:
13882 case FLASH_5752VENDOR_ST_M45PE40:
13883 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013884 tg3_flag_set(tp, NVRAM_BUFFERED);
13885 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013886 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013887 }
13888
Joe Perches63c3a662011-04-26 08:12:10 +000013889 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013890 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013891 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013892 /* For eeprom, set pagesize to maximum eeprom size */
13893 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13894
13895 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13896 tw32(NVRAM_CFG1, nvcfg1);
13897 }
13898}
13899
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013900static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013901{
Matt Carlson989a9d22007-05-05 11:51:05 -070013902 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013903
13904 nvcfg1 = tr32(NVRAM_CFG1);
13905
13906 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013907 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013908 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013909 protect = 1;
13910 }
Michael Chand3c7b882006-03-23 01:28:25 -080013911
Matt Carlson989a9d22007-05-05 11:51:05 -070013912 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13913 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013914 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13915 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13916 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13917 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13918 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013919 tg3_flag_set(tp, NVRAM_BUFFERED);
13920 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013921 tp->nvram_pagesize = 264;
13922 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13923 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13924 tp->nvram_size = (protect ? 0x3e200 :
13925 TG3_NVRAM_SIZE_512KB);
13926 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13927 tp->nvram_size = (protect ? 0x1f200 :
13928 TG3_NVRAM_SIZE_256KB);
13929 else
13930 tp->nvram_size = (protect ? 0x1f200 :
13931 TG3_NVRAM_SIZE_128KB);
13932 break;
13933 case FLASH_5752VENDOR_ST_M45PE10:
13934 case FLASH_5752VENDOR_ST_M45PE20:
13935 case FLASH_5752VENDOR_ST_M45PE40:
13936 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013937 tg3_flag_set(tp, NVRAM_BUFFERED);
13938 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013939 tp->nvram_pagesize = 256;
13940 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13941 tp->nvram_size = (protect ?
13942 TG3_NVRAM_SIZE_64KB :
13943 TG3_NVRAM_SIZE_128KB);
13944 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13945 tp->nvram_size = (protect ?
13946 TG3_NVRAM_SIZE_64KB :
13947 TG3_NVRAM_SIZE_256KB);
13948 else
13949 tp->nvram_size = (protect ?
13950 TG3_NVRAM_SIZE_128KB :
13951 TG3_NVRAM_SIZE_512KB);
13952 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013953 }
13954}
13955
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013956static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013957{
13958 u32 nvcfg1;
13959
13960 nvcfg1 = tr32(NVRAM_CFG1);
13961
13962 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013963 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13964 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13965 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13966 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13967 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013968 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013969 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013970
Matt Carlson8590a602009-08-28 12:29:16 +000013971 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13972 tw32(NVRAM_CFG1, nvcfg1);
13973 break;
13974 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13975 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13976 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13977 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13978 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013979 tg3_flag_set(tp, NVRAM_BUFFERED);
13980 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013981 tp->nvram_pagesize = 264;
13982 break;
13983 case FLASH_5752VENDOR_ST_M45PE10:
13984 case FLASH_5752VENDOR_ST_M45PE20:
13985 case FLASH_5752VENDOR_ST_M45PE40:
13986 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013987 tg3_flag_set(tp, NVRAM_BUFFERED);
13988 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013989 tp->nvram_pagesize = 256;
13990 break;
Michael Chan1b277772006-03-20 22:27:48 -080013991 }
13992}
13993
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013994static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013995{
13996 u32 nvcfg1, protect = 0;
13997
13998 nvcfg1 = tr32(NVRAM_CFG1);
13999
14000 /* NVRAM protection for TPM */
14001 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014002 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070014003 protect = 1;
14004 }
14005
14006 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
14007 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014008 case FLASH_5761VENDOR_ATMEL_ADB021D:
14009 case FLASH_5761VENDOR_ATMEL_ADB041D:
14010 case FLASH_5761VENDOR_ATMEL_ADB081D:
14011 case FLASH_5761VENDOR_ATMEL_ADB161D:
14012 case FLASH_5761VENDOR_ATMEL_MDB021D:
14013 case FLASH_5761VENDOR_ATMEL_MDB041D:
14014 case FLASH_5761VENDOR_ATMEL_MDB081D:
14015 case FLASH_5761VENDOR_ATMEL_MDB161D:
14016 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014017 tg3_flag_set(tp, NVRAM_BUFFERED);
14018 tg3_flag_set(tp, FLASH);
14019 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000014020 tp->nvram_pagesize = 256;
14021 break;
14022 case FLASH_5761VENDOR_ST_A_M45PE20:
14023 case FLASH_5761VENDOR_ST_A_M45PE40:
14024 case FLASH_5761VENDOR_ST_A_M45PE80:
14025 case FLASH_5761VENDOR_ST_A_M45PE16:
14026 case FLASH_5761VENDOR_ST_M_M45PE20:
14027 case FLASH_5761VENDOR_ST_M_M45PE40:
14028 case FLASH_5761VENDOR_ST_M_M45PE80:
14029 case FLASH_5761VENDOR_ST_M_M45PE16:
14030 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, NVRAM_BUFFERED);
14032 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000014033 tp->nvram_pagesize = 256;
14034 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014035 }
14036
14037 if (protect) {
14038 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
14039 } else {
14040 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000014041 case FLASH_5761VENDOR_ATMEL_ADB161D:
14042 case FLASH_5761VENDOR_ATMEL_MDB161D:
14043 case FLASH_5761VENDOR_ST_A_M45PE16:
14044 case FLASH_5761VENDOR_ST_M_M45PE16:
14045 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
14046 break;
14047 case FLASH_5761VENDOR_ATMEL_ADB081D:
14048 case FLASH_5761VENDOR_ATMEL_MDB081D:
14049 case FLASH_5761VENDOR_ST_A_M45PE80:
14050 case FLASH_5761VENDOR_ST_M_M45PE80:
14051 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14052 break;
14053 case FLASH_5761VENDOR_ATMEL_ADB041D:
14054 case FLASH_5761VENDOR_ATMEL_MDB041D:
14055 case FLASH_5761VENDOR_ST_A_M45PE40:
14056 case FLASH_5761VENDOR_ST_M_M45PE40:
14057 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14058 break;
14059 case FLASH_5761VENDOR_ATMEL_ADB021D:
14060 case FLASH_5761VENDOR_ATMEL_MDB021D:
14061 case FLASH_5761VENDOR_ST_A_M45PE20:
14062 case FLASH_5761VENDOR_ST_M_M45PE20:
14063 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14064 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070014065 }
14066 }
14067}
14068
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014069static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070014070{
14071 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014072 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070014073 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14074}
14075
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014076static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080014077{
14078 u32 nvcfg1;
14079
14080 nvcfg1 = tr32(NVRAM_CFG1);
14081
14082 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14083 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
14084 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
14085 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014086 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080014087 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14088
14089 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14090 tw32(NVRAM_CFG1, nvcfg1);
14091 return;
14092 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14093 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14094 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14095 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14096 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14097 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14098 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14099 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014100 tg3_flag_set(tp, NVRAM_BUFFERED);
14101 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014102
14103 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14104 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
14105 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
14106 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
14107 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14108 break;
14109 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
14110 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
14111 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14112 break;
14113 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
14114 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
14115 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14116 break;
14117 }
14118 break;
14119 case FLASH_5752VENDOR_ST_M45PE10:
14120 case FLASH_5752VENDOR_ST_M45PE20:
14121 case FLASH_5752VENDOR_ST_M45PE40:
14122 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014123 tg3_flag_set(tp, NVRAM_BUFFERED);
14124 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080014125
14126 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14127 case FLASH_5752VENDOR_ST_M45PE10:
14128 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14129 break;
14130 case FLASH_5752VENDOR_ST_M45PE20:
14131 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14132 break;
14133 case FLASH_5752VENDOR_ST_M45PE40:
14134 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14135 break;
14136 }
14137 break;
14138 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080014140 return;
14141 }
14142
Matt Carlsona1b950d2009-09-01 13:20:17 +000014143 tg3_nvram_get_pagesize(tp, nvcfg1);
14144 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014145 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014146}
14147
14148
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014149static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014150{
14151 u32 nvcfg1;
14152
14153 nvcfg1 = tr32(NVRAM_CFG1);
14154
14155 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14156 case FLASH_5717VENDOR_ATMEL_EEPROM:
14157 case FLASH_5717VENDOR_MICRO_EEPROM:
14158 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014160 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14161
14162 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14163 tw32(NVRAM_CFG1, nvcfg1);
14164 return;
14165 case FLASH_5717VENDOR_ATMEL_MDB011D:
14166 case FLASH_5717VENDOR_ATMEL_ADB011B:
14167 case FLASH_5717VENDOR_ATMEL_ADB011D:
14168 case FLASH_5717VENDOR_ATMEL_MDB021D:
14169 case FLASH_5717VENDOR_ATMEL_ADB021B:
14170 case FLASH_5717VENDOR_ATMEL_ADB021D:
14171 case FLASH_5717VENDOR_ATMEL_45USPT:
14172 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_set(tp, NVRAM_BUFFERED);
14174 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014175
14176 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14177 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014178 /* Detect size with tg3_nvram_get_size() */
14179 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014180 case FLASH_5717VENDOR_ATMEL_ADB021B:
14181 case FLASH_5717VENDOR_ATMEL_ADB021D:
14182 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14183 break;
14184 default:
14185 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14186 break;
14187 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014188 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014189 case FLASH_5717VENDOR_ST_M_M25PE10:
14190 case FLASH_5717VENDOR_ST_A_M25PE10:
14191 case FLASH_5717VENDOR_ST_M_M45PE10:
14192 case FLASH_5717VENDOR_ST_A_M45PE10:
14193 case FLASH_5717VENDOR_ST_M_M25PE20:
14194 case FLASH_5717VENDOR_ST_A_M25PE20:
14195 case FLASH_5717VENDOR_ST_M_M45PE20:
14196 case FLASH_5717VENDOR_ST_A_M45PE20:
14197 case FLASH_5717VENDOR_ST_25USPT:
14198 case FLASH_5717VENDOR_ST_45USPT:
14199 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014200 tg3_flag_set(tp, NVRAM_BUFFERED);
14201 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014202
14203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
14204 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014205 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000014206 /* Detect size with tg3_nvram_get_size() */
14207 break;
14208 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000014209 case FLASH_5717VENDOR_ST_A_M45PE20:
14210 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14211 break;
14212 default:
14213 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
14214 break;
14215 }
Matt Carlson321d32a2008-11-21 17:22:19 -080014216 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014217 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014218 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000014219 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080014220 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000014221
14222 tg3_nvram_get_pagesize(tp, nvcfg1);
14223 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014225}
14226
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014227static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014228{
14229 u32 nvcfg1, nvmpinstrp;
14230
14231 nvcfg1 = tr32(NVRAM_CFG1);
14232 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
14233
Joe Perches41535772013-02-16 11:20:04 +000014234 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014235 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
14236 tg3_flag_set(tp, NO_NVRAM);
14237 return;
14238 }
14239
14240 switch (nvmpinstrp) {
14241 case FLASH_5762_EEPROM_HD:
14242 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014243 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014244 case FLASH_5762_EEPROM_LD:
14245 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030014246 break;
Michael Chanf6334bb2013-04-09 08:48:02 +000014247 case FLASH_5720VENDOR_M_ST_M45PE20:
14248 /* This pinstrap supports multiple sizes, so force it
14249 * to read the actual size from location 0xf0.
14250 */
14251 nvmpinstrp = FLASH_5720VENDOR_ST_45USPT;
14252 break;
Michael Chanc86a8562013-01-06 12:51:08 +000014253 }
14254 }
14255
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014256 switch (nvmpinstrp) {
14257 case FLASH_5720_EEPROM_HD:
14258 case FLASH_5720_EEPROM_LD:
14259 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014260 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014261
14262 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
14263 tw32(NVRAM_CFG1, nvcfg1);
14264 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
14265 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
14266 else
14267 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
14268 return;
14269 case FLASH_5720VENDOR_M_ATMEL_DB011D:
14270 case FLASH_5720VENDOR_A_ATMEL_DB011B:
14271 case FLASH_5720VENDOR_A_ATMEL_DB011D:
14272 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14273 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14274 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14275 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14276 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14277 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14278 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14279 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14280 case FLASH_5720VENDOR_ATMEL_45USPT:
14281 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014282 tg3_flag_set(tp, NVRAM_BUFFERED);
14283 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014284
14285 switch (nvmpinstrp) {
14286 case FLASH_5720VENDOR_M_ATMEL_DB021D:
14287 case FLASH_5720VENDOR_A_ATMEL_DB021B:
14288 case FLASH_5720VENDOR_A_ATMEL_DB021D:
14289 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14290 break;
14291 case FLASH_5720VENDOR_M_ATMEL_DB041D:
14292 case FLASH_5720VENDOR_A_ATMEL_DB041B:
14293 case FLASH_5720VENDOR_A_ATMEL_DB041D:
14294 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14295 break;
14296 case FLASH_5720VENDOR_M_ATMEL_DB081D:
14297 case FLASH_5720VENDOR_A_ATMEL_DB081D:
14298 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14299 break;
14300 default:
Joe Perches41535772013-02-16 11:20:04 +000014301 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014302 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014303 break;
14304 }
14305 break;
14306 case FLASH_5720VENDOR_M_ST_M25PE10:
14307 case FLASH_5720VENDOR_M_ST_M45PE10:
14308 case FLASH_5720VENDOR_A_ST_M25PE10:
14309 case FLASH_5720VENDOR_A_ST_M45PE10:
14310 case FLASH_5720VENDOR_M_ST_M25PE20:
14311 case FLASH_5720VENDOR_M_ST_M45PE20:
14312 case FLASH_5720VENDOR_A_ST_M25PE20:
14313 case FLASH_5720VENDOR_A_ST_M45PE20:
14314 case FLASH_5720VENDOR_M_ST_M25PE40:
14315 case FLASH_5720VENDOR_M_ST_M45PE40:
14316 case FLASH_5720VENDOR_A_ST_M25PE40:
14317 case FLASH_5720VENDOR_A_ST_M45PE40:
14318 case FLASH_5720VENDOR_M_ST_M25PE80:
14319 case FLASH_5720VENDOR_M_ST_M45PE80:
14320 case FLASH_5720VENDOR_A_ST_M25PE80:
14321 case FLASH_5720VENDOR_A_ST_M45PE80:
14322 case FLASH_5720VENDOR_ST_25USPT:
14323 case FLASH_5720VENDOR_ST_45USPT:
14324 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000014325 tg3_flag_set(tp, NVRAM_BUFFERED);
14326 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014327
14328 switch (nvmpinstrp) {
14329 case FLASH_5720VENDOR_M_ST_M25PE20:
14330 case FLASH_5720VENDOR_M_ST_M45PE20:
14331 case FLASH_5720VENDOR_A_ST_M25PE20:
14332 case FLASH_5720VENDOR_A_ST_M45PE20:
14333 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
14334 break;
14335 case FLASH_5720VENDOR_M_ST_M25PE40:
14336 case FLASH_5720VENDOR_M_ST_M45PE40:
14337 case FLASH_5720VENDOR_A_ST_M25PE40:
14338 case FLASH_5720VENDOR_A_ST_M45PE40:
14339 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
14340 break;
14341 case FLASH_5720VENDOR_M_ST_M25PE80:
14342 case FLASH_5720VENDOR_M_ST_M45PE80:
14343 case FLASH_5720VENDOR_A_ST_M25PE80:
14344 case FLASH_5720VENDOR_A_ST_M45PE80:
14345 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
14346 break;
14347 default:
Joe Perches41535772013-02-16 11:20:04 +000014348 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000014349 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014350 break;
14351 }
14352 break;
14353 default:
Joe Perches63c3a662011-04-26 08:12:10 +000014354 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014355 return;
14356 }
14357
14358 tg3_nvram_get_pagesize(tp, nvcfg1);
14359 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000014360 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000014361
Joe Perches41535772013-02-16 11:20:04 +000014362 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000014363 u32 val;
14364
14365 if (tg3_nvram_read(tp, 0, &val))
14366 return;
14367
14368 if (val != TG3_EEPROM_MAGIC &&
14369 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
14370 tg3_flag_set(tp, NO_NVRAM);
14371 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014372}
14373
Linus Torvalds1da177e2005-04-16 15:20:36 -070014374/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014375static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014376{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014377 if (tg3_flag(tp, IS_SSB_CORE)) {
14378 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
14379 tg3_flag_clear(tp, NVRAM);
14380 tg3_flag_clear(tp, NVRAM_BUFFERED);
14381 tg3_flag_set(tp, NO_NVRAM);
14382 return;
14383 }
14384
Linus Torvalds1da177e2005-04-16 15:20:36 -070014385 tw32_f(GRC_EEPROM_ADDR,
14386 (EEPROM_ADDR_FSM_RESET |
14387 (EEPROM_DEFAULT_CLOCK_PERIOD <<
14388 EEPROM_ADDR_CLKPERD_SHIFT)));
14389
Michael Chan9d57f012006-12-07 00:23:25 -080014390 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014391
14392 /* Enable seeprom accesses. */
14393 tw32_f(GRC_LOCAL_CTRL,
14394 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
14395 udelay(100);
14396
Joe Perches41535772013-02-16 11:20:04 +000014397 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14398 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000014399 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014400
Michael Chanec41c7d2006-01-17 02:40:55 -080014401 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014402 netdev_warn(tp->dev,
14403 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000014404 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080014405 return;
14406 }
Michael Chane6af3012005-04-21 17:12:05 -070014407 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014408
Matt Carlson989a9d22007-05-05 11:51:05 -070014409 tp->nvram_size = 0;
14410
Joe Perches41535772013-02-16 11:20:04 +000014411 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070014412 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014413 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080014414 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014415 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
14416 tg3_asic_rev(tp) == ASIC_REV_5784 ||
14417 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080014418 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014419 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070014420 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014421 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014422 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014423 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014424 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014425 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014426 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14427 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014428 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014429 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14430 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014431 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014432 else
14433 tg3_get_nvram_info(tp);
14434
Matt Carlson989a9d22007-05-05 11:51:05 -070014435 if (tp->nvram_size == 0)
14436 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014437
Michael Chane6af3012005-04-21 17:12:05 -070014438 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014439 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014440
14441 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014442 tg3_flag_clear(tp, NVRAM);
14443 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014444
14445 tg3_get_eeprom_size(tp);
14446 }
14447}
14448
Linus Torvalds1da177e2005-04-16 15:20:36 -070014449struct subsys_tbl_ent {
14450 u16 subsys_vendor, subsys_devid;
14451 u32 phy_id;
14452};
14453
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014454static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014455 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014456 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014457 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014458 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014459 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014460 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014461 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014462 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14463 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14464 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014465 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014466 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014467 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014468 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14469 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14470 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014471 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014472 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014473 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014474 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014475 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014476 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014477 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478
14479 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014480 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014481 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014482 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014483 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014484 { TG3PCI_SUBVENDOR_ID_3COM,
14485 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14486 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014487 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014488 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014489 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490
14491 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014492 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014493 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014494 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014495 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014496 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014497 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014498 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014499 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500
14501 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014502 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014503 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014504 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014505 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014506 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14507 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14508 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014509 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014510 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014511 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512
14513 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014514 { TG3PCI_SUBVENDOR_ID_IBM,
14515 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014516};
14517
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014518static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519{
14520 int i;
14521
14522 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14523 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14524 tp->pdev->subsystem_vendor) &&
14525 (subsys_id_to_phy_id[i].subsys_devid ==
14526 tp->pdev->subsystem_device))
14527 return &subsys_id_to_phy_id[i];
14528 }
14529 return NULL;
14530}
14531
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014532static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014535
Matt Carlson79eb6902010-02-17 15:17:03 +000014536 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014537 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14538
Gary Zambranoa85feb82007-05-05 11:52:19 -070014539 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014540 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14541 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014542
Joe Perches41535772013-02-16 11:20:04 +000014543 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014544 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014545 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14546 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014547 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014548 val = tr32(VCPU_CFGSHDW);
14549 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014550 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014551 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014552 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014553 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014554 device_set_wakeup_enable(&tp->pdev->dev, true);
14555 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014556 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014557 }
14558
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14560 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14561 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014562 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014563 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
14565 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14566 tp->nic_sram_data_cfg = nic_cfg;
14567
14568 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14569 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014570 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14571 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14572 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014573 (ver > 0) && (ver < 0x100))
14574 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14575
Joe Perches41535772013-02-16 11:20:04 +000014576 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014577 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14578
Linus Torvalds1da177e2005-04-16 15:20:36 -070014579 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14580 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14581 eeprom_phy_serdes = 1;
14582
14583 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14584 if (nic_phy_id != 0) {
14585 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14586 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14587
14588 eeprom_phy_id = (id1 >> 16) << 10;
14589 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14590 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14591 } else
14592 eeprom_phy_id = 0;
14593
Michael Chan7d0c41e2005-04-21 17:06:20 -070014594 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014595 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014596 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014597 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014598 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014599 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014600 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014601
Joe Perches63c3a662011-04-26 08:12:10 +000014602 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014603 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14604 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014605 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014606 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14607
14608 switch (led_cfg) {
14609 default:
14610 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14611 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14612 break;
14613
14614 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14615 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14616 break;
14617
14618 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14619 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014620
14621 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14622 * read on some older 5700/5701 bootcode.
14623 */
Joe Perches41535772013-02-16 11:20:04 +000014624 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14625 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014626 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14627
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 break;
14629
14630 case SHASTA_EXT_LED_SHARED:
14631 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014632 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14633 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14635 LED_CTRL_MODE_PHY_2);
14636 break;
14637
14638 case SHASTA_EXT_LED_MAC:
14639 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14640 break;
14641
14642 case SHASTA_EXT_LED_COMBO:
14643 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014644 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014645 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14646 LED_CTRL_MODE_PHY_2);
14647 break;
14648
Stephen Hemminger855e1112008-04-16 16:37:28 -070014649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014650
Joe Perches41535772013-02-16 11:20:04 +000014651 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14652 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14654 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14655
Joe Perches41535772013-02-16 11:20:04 +000014656 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014657 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014658
Michael Chan9d26e212006-12-07 00:21:14 -080014659 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014660 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014661 if ((tp->pdev->subsystem_vendor ==
14662 PCI_VENDOR_ID_ARIMA) &&
14663 (tp->pdev->subsystem_device == 0x205a ||
14664 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014665 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014666 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014667 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14668 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014670
14671 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014672 tg3_flag_set(tp, ENABLE_ASF);
14673 if (tg3_flag(tp, 5750_PLUS))
14674 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014676
14677 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014678 tg3_flag(tp, 5750_PLUS))
14679 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014680
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014681 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014682 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014683 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014684
Joe Perches63c3a662011-04-26 08:12:10 +000014685 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014686 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014687 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014688 device_set_wakeup_enable(&tp->pdev->dev, true);
14689 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014690
Linus Torvalds1da177e2005-04-16 15:20:36 -070014691 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014692 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014693
14694 /* serdes signal pre-emphasis in register 0x590 set by */
14695 /* bootcode if bit 18 is set */
14696 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014697 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014698
Joe Perches63c3a662011-04-26 08:12:10 +000014699 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014700 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14701 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014702 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014703 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014704
Nithin Sujir942d1af2013-04-09 08:48:07 +000014705 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014706 u32 cfg3;
14707
14708 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014709 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
14710 !tg3_flag(tp, 57765_PLUS) &&
14711 (cfg3 & NIC_SRAM_ASPM_DEBOUNCE))
Joe Perches63c3a662011-04-26 08:12:10 +000014712 tg3_flag_set(tp, ASPM_WORKAROUND);
Nithin Sujir942d1af2013-04-09 08:48:07 +000014713 if (cfg3 & NIC_SRAM_LNK_FLAP_AVOID)
14714 tp->phy_flags |= TG3_PHYFLG_KEEP_LINK_ON_PWRDN;
14715 if (cfg3 & NIC_SRAM_1G_ON_VAUX_OK)
14716 tp->phy_flags |= TG3_PHYFLG_1G_ON_VAUX_OK;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014717 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014718
Matt Carlson14417062010-02-17 15:16:59 +000014719 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014720 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014721 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014722 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014723 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014724 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014725 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014726done:
Joe Perches63c3a662011-04-26 08:12:10 +000014727 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014728 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014729 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014730 else
14731 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014732}
14733
Michael Chanc86a8562013-01-06 12:51:08 +000014734static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14735{
14736 int i, err;
14737 u32 val2, off = offset * 8;
14738
14739 err = tg3_nvram_lock(tp);
14740 if (err)
14741 return err;
14742
14743 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14744 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14745 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14746 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14747 udelay(10);
14748
14749 for (i = 0; i < 100; i++) {
14750 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14751 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14752 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14753 break;
14754 }
14755 udelay(10);
14756 }
14757
14758 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14759
14760 tg3_nvram_unlock(tp);
14761 if (val2 & APE_OTP_STATUS_CMD_DONE)
14762 return 0;
14763
14764 return -EBUSY;
14765}
14766
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014767static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014768{
14769 int i;
14770 u32 val;
14771
14772 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14773 tw32(OTP_CTRL, cmd);
14774
14775 /* Wait for up to 1 ms for command to execute. */
14776 for (i = 0; i < 100; i++) {
14777 val = tr32(OTP_STATUS);
14778 if (val & OTP_STATUS_CMD_DONE)
14779 break;
14780 udelay(10);
14781 }
14782
14783 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14784}
14785
14786/* Read the gphy configuration from the OTP region of the chip. The gphy
14787 * configuration is a 32-bit value that straddles the alignment boundary.
14788 * We do two 32-bit reads and then shift and merge the results.
14789 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014790static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014791{
14792 u32 bhalf_otp, thalf_otp;
14793
14794 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14795
14796 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14797 return 0;
14798
14799 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14800
14801 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14802 return 0;
14803
14804 thalf_otp = tr32(OTP_READ_DATA);
14805
14806 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14807
14808 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14809 return 0;
14810
14811 bhalf_otp = tr32(OTP_READ_DATA);
14812
14813 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14814}
14815
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014816static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014817{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014818 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014819
14820 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14821 adv |= ADVERTISED_1000baseT_Half |
14822 ADVERTISED_1000baseT_Full;
14823
14824 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14825 adv |= ADVERTISED_100baseT_Half |
14826 ADVERTISED_100baseT_Full |
14827 ADVERTISED_10baseT_Half |
14828 ADVERTISED_10baseT_Full |
14829 ADVERTISED_TP;
14830 else
14831 adv |= ADVERTISED_FIBRE;
14832
14833 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014834 tp->link_config.speed = SPEED_UNKNOWN;
14835 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014836 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014837 tp->link_config.active_speed = SPEED_UNKNOWN;
14838 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014839
14840 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014841}
14842
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014843static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014844{
14845 u32 hw_phy_id_1, hw_phy_id_2;
14846 u32 hw_phy_id, hw_phy_id_masked;
14847 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014848
Matt Carlsone256f8a2011-03-09 16:58:24 +000014849 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014850 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014851 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14852
Michael Chan8151ad52012-07-29 19:15:41 +000014853 if (tg3_flag(tp, ENABLE_APE)) {
14854 switch (tp->pci_fn) {
14855 case 0:
14856 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14857 break;
14858 case 1:
14859 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14860 break;
14861 case 2:
14862 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14863 break;
14864 case 3:
14865 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14866 break;
14867 }
14868 }
14869
Nithin Sujir942d1af2013-04-09 08:48:07 +000014870 if (!tg3_flag(tp, ENABLE_ASF) &&
14871 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
14872 !(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14873 tp->phy_flags &= ~(TG3_PHYFLG_1G_ON_VAUX_OK |
14874 TG3_PHYFLG_KEEP_LINK_ON_PWRDN);
14875
Joe Perches63c3a662011-04-26 08:12:10 +000014876 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014877 return tg3_phy_init(tp);
14878
Linus Torvalds1da177e2005-04-16 15:20:36 -070014879 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014880 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014881 */
14882 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014883 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014884 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885 } else {
14886 /* Now read the physical PHY_ID from the chip and verify
14887 * that it is sane. If it doesn't look good, we fall back
14888 * to either the hard-coded table based PHY_ID and failing
14889 * that the value found in the eeprom area.
14890 */
14891 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14892 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14893
14894 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14895 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14896 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14897
Matt Carlson79eb6902010-02-17 15:17:03 +000014898 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014899 }
14900
Matt Carlson79eb6902010-02-17 15:17:03 +000014901 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014902 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014903 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014904 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014905 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014906 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014907 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014908 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014909 /* Do nothing, phy ID already set up in
14910 * tg3_get_eeprom_hw_cfg().
14911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912 } else {
14913 struct subsys_tbl_ent *p;
14914
14915 /* No eeprom signature? Try the hardcoded
14916 * subsys device table.
14917 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014918 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014919 if (p) {
14920 tp->phy_id = p->phy_id;
14921 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14922 /* For now we saw the IDs 0xbc050cd0,
14923 * 0xbc050f80 and 0xbc050c30 on devices
14924 * connected to an BCM4785 and there are
14925 * probably more. Just assume that the phy is
14926 * supported when it is connected to a SSB core
14927 * for now.
14928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014931
Linus Torvalds1da177e2005-04-16 15:20:36 -070014932 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014933 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014934 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935 }
14936 }
14937
Matt Carlsona6b68da2010-12-06 08:28:52 +000014938 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014939 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14940 tg3_asic_rev(tp) == ASIC_REV_5720 ||
Nithin Sujirc4dab502013-03-06 17:02:34 +000014941 tg3_asic_rev(tp) == ASIC_REV_57766 ||
Joe Perches41535772013-02-16 11:20:04 +000014942 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14943 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14944 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14945 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
14946 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014947 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14948
Matt Carlsone256f8a2011-03-09 16:58:24 +000014949 tg3_phy_init_link_config(tp);
14950
Nithin Sujir942d1af2013-04-09 08:48:07 +000014951 if (!(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN) &&
14952 !(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014953 !tg3_flag(tp, ENABLE_APE) &&
14954 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014955 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014956
14957 tg3_readphy(tp, MII_BMSR, &bmsr);
14958 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14959 (bmsr & BMSR_LSTATUS))
14960 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014961
Linus Torvalds1da177e2005-04-16 15:20:36 -070014962 err = tg3_phy_reset(tp);
14963 if (err)
14964 return err;
14965
Matt Carlson42b64a42011-05-19 12:12:49 +000014966 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014967
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014968 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014969 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14970 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014971
14972 tg3_writephy(tp, MII_BMCR,
14973 BMCR_ANENABLE | BMCR_ANRESTART);
14974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014975 }
14976
14977skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014978 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979 err = tg3_init_5401phy_dsp(tp);
14980 if (err)
14981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014982
Linus Torvalds1da177e2005-04-16 15:20:36 -070014983 err = tg3_init_5401phy_dsp(tp);
14984 }
14985
Linus Torvalds1da177e2005-04-16 15:20:36 -070014986 return err;
14987}
14988
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014989static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014991 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014992 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014993 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014994 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995
Matt Carlson535a4902011-07-20 10:20:56 +000014996 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014997 if (!vpd_data)
14998 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999
Matt Carlson535a4902011-07-20 10:20:56 +000015000 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000015001 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000015003
15004 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
15005 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
15006 i += PCI_VPD_LRDT_TAG_SIZE;
15007
Matt Carlson535a4902011-07-20 10:20:56 +000015008 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015009 goto out_not_found;
15010
Matt Carlson184b8902010-04-05 10:19:25 +000015011 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15012 PCI_VPD_RO_KEYWORD_MFR_ID);
15013 if (j > 0) {
15014 len = pci_vpd_info_field_size(&vpd_data[j]);
15015
15016 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15017 if (j + len > block_end || len != 4 ||
15018 memcmp(&vpd_data[j], "1028", 4))
15019 goto partno;
15020
15021 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15022 PCI_VPD_RO_KEYWORD_VENDOR0);
15023 if (j < 0)
15024 goto partno;
15025
15026 len = pci_vpd_info_field_size(&vpd_data[j]);
15027
15028 j += PCI_VPD_INFO_FLD_HDR_SIZE;
15029 if (j + len > block_end)
15030 goto partno;
15031
Kees Cook715230a2013-03-27 06:40:50 +000015032 if (len >= sizeof(tp->fw_ver))
15033 len = sizeof(tp->fw_ver) - 1;
15034 memset(tp->fw_ver, 0, sizeof(tp->fw_ver));
15035 snprintf(tp->fw_ver, sizeof(tp->fw_ver), "%.*s bc ", len,
15036 &vpd_data[j]);
Matt Carlson184b8902010-04-05 10:19:25 +000015037 }
15038
15039partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000015040 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
15041 PCI_VPD_RO_KEYWORD_PARTNO);
15042 if (i < 0)
15043 goto out_not_found;
15044
15045 len = pci_vpd_info_field_size(&vpd_data[i]);
15046
15047 i += PCI_VPD_INFO_FLD_HDR_SIZE;
15048 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000015049 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000015050 goto out_not_found;
15051
15052 memcpy(tp->board_part_number, &vpd_data[i], len);
15053
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015055 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000015056 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000015057 return;
15058
15059out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000015060 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000015061 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15062 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000015063 strcpy(tp->board_part_number, "BCM5717");
15064 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
15065 strcpy(tp->board_part_number, "BCM5718");
15066 else
15067 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015068 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015069 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
15070 strcpy(tp->board_part_number, "BCM57780");
15071 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
15072 strcpy(tp->board_part_number, "BCM57760");
15073 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
15074 strcpy(tp->board_part_number, "BCM57790");
15075 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
15076 strcpy(tp->board_part_number, "BCM57788");
15077 else
15078 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015079 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000015080 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
15081 strcpy(tp->board_part_number, "BCM57761");
15082 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
15083 strcpy(tp->board_part_number, "BCM57765");
15084 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
15085 strcpy(tp->board_part_number, "BCM57781");
15086 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
15087 strcpy(tp->board_part_number, "BCM57785");
15088 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
15089 strcpy(tp->board_part_number, "BCM57791");
15090 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
15091 strcpy(tp->board_part_number, "BCM57795");
15092 else
15093 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015094 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000015095 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
15096 strcpy(tp->board_part_number, "BCM57762");
15097 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
15098 strcpy(tp->board_part_number, "BCM57766");
15099 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
15100 strcpy(tp->board_part_number, "BCM57782");
15101 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15102 strcpy(tp->board_part_number, "BCM57786");
15103 else
15104 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000015105 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015106 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000015107 } else {
15108nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070015109 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000015110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015111}
15112
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015113static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070015114{
15115 u32 val;
15116
Matt Carlsone4f34112009-02-25 14:25:00 +000015117 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015118 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015119 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015120 val != 0)
15121 return 0;
15122
15123 return 1;
15124}
15125
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015126static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015127{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015128 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000015129 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015130 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015131
15132 if (tg3_nvram_read(tp, 0xc, &offset) ||
15133 tg3_nvram_read(tp, 0x4, &start))
15134 return;
15135
15136 offset = tg3_nvram_logical_addr(tp, offset);
15137
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015138 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015139 return;
15140
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015141 if ((val & 0xfc000000) == 0x0c000000) {
15142 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000015143 return;
15144
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015145 if (val == 0)
15146 newver = true;
15147 }
15148
Matt Carlson75f99362010-04-05 10:19:24 +000015149 dst_off = strlen(tp->fw_ver);
15150
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015151 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000015152 if (TG3_VER_SIZE - dst_off < 16 ||
15153 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015154 return;
15155
15156 offset = offset + ver_offset - start;
15157 for (i = 0; i < 16; i += 4) {
15158 __be32 v;
15159 if (tg3_nvram_read_be32(tp, offset + i, &v))
15160 return;
15161
Matt Carlson75f99362010-04-05 10:19:24 +000015162 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000015163 }
15164 } else {
15165 u32 major, minor;
15166
15167 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
15168 return;
15169
15170 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
15171 TG3_NVM_BCVER_MAJSFT;
15172 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000015173 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
15174 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015175 }
15176}
15177
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015178static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015179{
15180 u32 val, major, minor;
15181
15182 /* Use native endian representation */
15183 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
15184 return;
15185
15186 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
15187 TG3_NVM_HWSB_CFG1_MAJSFT;
15188 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
15189 TG3_NVM_HWSB_CFG1_MINSFT;
15190
15191 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
15192}
15193
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015194static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080015195{
15196 u32 offset, major, minor, build;
15197
Matt Carlson75f99362010-04-05 10:19:24 +000015198 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015199
15200 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
15201 return;
15202
15203 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
15204 case TG3_EEPROM_SB_REVISION_0:
15205 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
15206 break;
15207 case TG3_EEPROM_SB_REVISION_2:
15208 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
15209 break;
15210 case TG3_EEPROM_SB_REVISION_3:
15211 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
15212 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000015213 case TG3_EEPROM_SB_REVISION_4:
15214 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
15215 break;
15216 case TG3_EEPROM_SB_REVISION_5:
15217 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
15218 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000015219 case TG3_EEPROM_SB_REVISION_6:
15220 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
15221 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015222 default:
15223 return;
15224 }
15225
Matt Carlsone4f34112009-02-25 14:25:00 +000015226 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080015227 return;
15228
15229 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
15230 TG3_EEPROM_SB_EDH_BLD_SHFT;
15231 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
15232 TG3_EEPROM_SB_EDH_MAJ_SHFT;
15233 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
15234
15235 if (minor > 99 || build > 26)
15236 return;
15237
Matt Carlson75f99362010-04-05 10:19:24 +000015238 offset = strlen(tp->fw_ver);
15239 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
15240 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080015241
15242 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000015243 offset = strlen(tp->fw_ver);
15244 if (offset < TG3_VER_SIZE - 1)
15245 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080015246 }
15247}
15248
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015249static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080015250{
15251 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015252 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070015253
15254 for (offset = TG3_NVM_DIR_START;
15255 offset < TG3_NVM_DIR_END;
15256 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000015257 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015258 return;
15259
15260 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
15261 break;
15262 }
15263
15264 if (offset == TG3_NVM_DIR_END)
15265 return;
15266
Joe Perches63c3a662011-04-26 08:12:10 +000015267 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015268 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000015269 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015270 return;
15271
Matt Carlsone4f34112009-02-25 14:25:00 +000015272 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070015273 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000015274 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015275 return;
15276
15277 offset += val - start;
15278
Matt Carlsonacd9c112009-02-25 14:26:33 +000015279 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015280
Matt Carlsonacd9c112009-02-25 14:26:33 +000015281 tp->fw_ver[vlen++] = ',';
15282 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070015283
15284 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000015285 __be32 v;
15286 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070015287 return;
15288
Al Virob9fc7dc2007-12-17 22:59:57 -080015289 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015290
Matt Carlsonacd9c112009-02-25 14:26:33 +000015291 if (vlen > TG3_VER_SIZE - sizeof(v)) {
15292 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015293 break;
15294 }
15295
Matt Carlsonacd9c112009-02-25 14:26:33 +000015296 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
15297 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070015298 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000015299}
15300
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015301static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000015302{
Matt Carlson7fd76442009-02-25 14:27:20 +000015303 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000015304
15305 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
15306 if (apedata != APE_SEG_SIG_MAGIC)
15307 return;
15308
15309 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
15310 if (!(apedata & APE_FW_STATUS_READY))
15311 return;
15312
Michael Chan165f4d12012-07-16 16:23:59 +000015313 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
15314 tg3_flag_set(tp, APE_HAS_NCSI);
15315}
15316
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015317static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000015318{
15319 int vlen;
15320 u32 apedata;
15321 char *fwtype;
15322
Matt Carlson7fd76442009-02-25 14:27:20 +000015323 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
15324
Michael Chan165f4d12012-07-16 16:23:59 +000015325 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000015326 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000015327 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
15328 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000015329 else
Matt Carlsonecc79642010-08-02 11:26:01 +000015330 fwtype = "DASH";
15331
Matt Carlson7fd76442009-02-25 14:27:20 +000015332 vlen = strlen(tp->fw_ver);
15333
Matt Carlsonecc79642010-08-02 11:26:01 +000015334 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
15335 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000015336 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
15337 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
15338 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
15339 (apedata & APE_FW_VERSION_BLDMSK));
15340}
15341
Michael Chanc86a8562013-01-06 12:51:08 +000015342static void tg3_read_otp_ver(struct tg3 *tp)
15343{
15344 u32 val, val2;
15345
Joe Perches41535772013-02-16 11:20:04 +000015346 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015347 return;
15348
15349 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
15350 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
15351 TG3_OTP_MAGIC0_VALID(val)) {
15352 u64 val64 = (u64) val << 32 | val2;
15353 u32 ver = 0;
15354 int i, vlen;
15355
15356 for (i = 0; i < 7; i++) {
15357 if ((val64 & 0xff) == 0)
15358 break;
15359 ver = val64 & 0xff;
15360 val64 >>= 8;
15361 }
15362 vlen = strlen(tp->fw_ver);
15363 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
15364 }
15365}
15366
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015367static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000015368{
15369 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000015370 bool vpd_vers = false;
15371
15372 if (tp->fw_ver[0] != 0)
15373 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000015374
Joe Perches63c3a662011-04-26 08:12:10 +000015375 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000015376 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000015377 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000015378 return;
15379 }
15380
Matt Carlsonacd9c112009-02-25 14:26:33 +000015381 if (tg3_nvram_read(tp, 0, &val))
15382 return;
15383
15384 if (val == TG3_EEPROM_MAGIC)
15385 tg3_read_bc_ver(tp);
15386 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
15387 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000015388 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
15389 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000015390
Michael Chan165f4d12012-07-16 16:23:59 +000015391 if (tg3_flag(tp, ENABLE_ASF)) {
15392 if (tg3_flag(tp, ENABLE_APE)) {
15393 tg3_probe_ncsi(tp);
15394 if (!vpd_vers)
15395 tg3_read_dash_ver(tp);
15396 } else if (!vpd_vers) {
15397 tg3_read_mgmtfw_ver(tp);
15398 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000015399 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070015400
15401 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080015402}
15403
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015404static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
15405{
Joe Perches63c3a662011-04-26 08:12:10 +000015406 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015407 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000015408 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000015409 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015410 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000015411 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015412}
15413
Matt Carlson41434702011-03-09 16:58:22 +000015414static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015415 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
15416 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
15417 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
15418 { },
15419};
15420
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015421static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000015422{
15423 struct pci_dev *peer;
15424 unsigned int func, devnr = tp->pdev->devfn & ~7;
15425
15426 for (func = 0; func < 8; func++) {
15427 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15428 if (peer && peer != tp->pdev)
15429 break;
15430 pci_dev_put(peer);
15431 }
15432 /* 5704 can be configured in single-port mode, set peer to
15433 * tp->pdev in that case.
15434 */
15435 if (!peer) {
15436 peer = tp->pdev;
15437 return peer;
15438 }
15439
15440 /*
15441 * We don't need to keep the refcount elevated; there's no way
15442 * to remove one half of this device without removing the other
15443 */
15444 pci_dev_put(peer);
15445
15446 return peer;
15447}
15448
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015449static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015450{
15451 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015452 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015453 u32 reg;
15454
15455 /* All devices that use the alternate
15456 * ASIC REV location have a CPMU.
15457 */
15458 tg3_flag_set(tp, CPMU_PRESENT);
15459
15460 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015461 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015462 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15463 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015464 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15465 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15466 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15467 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015468 reg = TG3PCI_GEN2_PRODID_ASICREV;
15469 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15470 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15471 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15472 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15473 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15476 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15477 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15479 reg = TG3PCI_GEN15_PRODID_ASICREV;
15480 else
15481 reg = TG3PCI_PRODID_ASICREV;
15482
15483 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15484 }
15485
15486 /* Wrong chip ID in 5752 A0. This code can be removed later
15487 * as A0 is not in production.
15488 */
Joe Perches41535772013-02-16 11:20:04 +000015489 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015490 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15491
Joe Perches41535772013-02-16 11:20:04 +000015492 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015493 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15494
Joe Perches41535772013-02-16 11:20:04 +000015495 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15496 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15497 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015498 tg3_flag_set(tp, 5717_PLUS);
15499
Joe Perches41535772013-02-16 11:20:04 +000015500 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15501 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015502 tg3_flag_set(tp, 57765_CLASS);
15503
Michael Chanc65a17f2013-01-06 12:51:07 +000015504 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015505 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015506 tg3_flag_set(tp, 57765_PLUS);
15507
15508 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015509 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15510 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15511 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15512 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15513 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15514 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015515 tg3_flag(tp, 57765_PLUS))
15516 tg3_flag_set(tp, 5755_PLUS);
15517
Joe Perches41535772013-02-16 11:20:04 +000015518 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15519 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015520 tg3_flag_set(tp, 5780_CLASS);
15521
Joe Perches41535772013-02-16 11:20:04 +000015522 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15523 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15524 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015525 tg3_flag(tp, 5755_PLUS) ||
15526 tg3_flag(tp, 5780_CLASS))
15527 tg3_flag_set(tp, 5750_PLUS);
15528
Joe Perches41535772013-02-16 11:20:04 +000015529 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015530 tg3_flag(tp, 5750_PLUS))
15531 tg3_flag_set(tp, 5705_PLUS);
15532}
15533
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015534static bool tg3_10_100_only_device(struct tg3 *tp,
15535 const struct pci_device_id *ent)
15536{
15537 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15538
Joe Perches41535772013-02-16 11:20:04 +000015539 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15540 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015541 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15542 return true;
15543
15544 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015545 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015546 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15547 return true;
15548 } else {
15549 return true;
15550 }
15551 }
15552
15553 return false;
15554}
15555
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015556static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015557{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015559 u32 pci_state_reg, grc_misc_cfg;
15560 u32 val;
15561 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015562 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015563
Linus Torvalds1da177e2005-04-16 15:20:36 -070015564 /* Force memory write invalidate off. If we leave it on,
15565 * then on 5700_BX chips we have to enable a workaround.
15566 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15567 * to match the cacheline size. The Broadcom driver have this
15568 * workaround but turns MWI off all the times so never uses
15569 * it. This seems to suggest that the workaround is insufficient.
15570 */
15571 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15572 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15573 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15574
Matt Carlson16821282011-07-13 09:27:28 +000015575 /* Important! -- Make sure register accesses are byteswapped
15576 * correctly. Also, for those chips that require it, make
15577 * sure that indirect register accesses are enabled before
15578 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015579 */
15580 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15581 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015582 tp->misc_host_ctrl |= (misc_ctrl_reg &
15583 MISC_HOST_CTRL_CHIPREV);
15584 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15585 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015586
Matt Carlson42b123b2012-02-13 15:20:13 +000015587 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015588
Michael Chan68929142005-08-09 20:17:14 -070015589 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15590 * we need to disable memory and use config. cycles
15591 * only to access all registers. The 5702/03 chips
15592 * can mistakenly decode the special cycles from the
15593 * ICH chipsets as memory write cycles, causing corruption
15594 * of register and memory space. Only certain ICH bridges
15595 * will drive special cycles with non-zero data during the
15596 * address phase which can fall within the 5703's address
15597 * range. This is not an ICH bug as the PCI spec allows
15598 * non-zero address during special cycles. However, only
15599 * these ICH bridges are known to drive non-zero addresses
15600 * during special cycles.
15601 *
15602 * Since special cycles do not cross PCI bridges, we only
15603 * enable this workaround if the 5703 is on the secondary
15604 * bus of these ICH bridges.
15605 */
Joe Perches41535772013-02-16 11:20:04 +000015606 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15607 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015608 static struct tg3_dev_id {
15609 u32 vendor;
15610 u32 device;
15611 u32 rev;
15612 } ich_chipsets[] = {
15613 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15614 PCI_ANY_ID },
15615 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15616 PCI_ANY_ID },
15617 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15618 0xa },
15619 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15620 PCI_ANY_ID },
15621 { },
15622 };
15623 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15624 struct pci_dev *bridge = NULL;
15625
15626 while (pci_id->vendor != 0) {
15627 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15628 bridge);
15629 if (!bridge) {
15630 pci_id++;
15631 continue;
15632 }
15633 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015634 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015635 continue;
15636 }
15637 if (bridge->subordinate &&
15638 (bridge->subordinate->number ==
15639 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015640 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015641 pci_dev_put(bridge);
15642 break;
15643 }
15644 }
15645 }
15646
Joe Perches41535772013-02-16 11:20:04 +000015647 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015648 static struct tg3_dev_id {
15649 u32 vendor;
15650 u32 device;
15651 } bridge_chipsets[] = {
15652 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15653 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15654 { },
15655 };
15656 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15657 struct pci_dev *bridge = NULL;
15658
15659 while (pci_id->vendor != 0) {
15660 bridge = pci_get_device(pci_id->vendor,
15661 pci_id->device,
15662 bridge);
15663 if (!bridge) {
15664 pci_id++;
15665 continue;
15666 }
15667 if (bridge->subordinate &&
15668 (bridge->subordinate->number <=
15669 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015670 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015671 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015672 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015673 pci_dev_put(bridge);
15674 break;
15675 }
15676 }
15677 }
15678
Michael Chan4a29cc22006-03-19 13:21:12 -080015679 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15680 * DMA addresses > 40-bit. This bridge may have other additional
15681 * 57xx devices behind it in some 4-port NIC designs for example.
15682 * Any tg3 device found behind the bridge will also need the 40-bit
15683 * DMA workaround.
15684 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015685 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015686 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015687 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015688 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015689 struct pci_dev *bridge = NULL;
15690
15691 do {
15692 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15693 PCI_DEVICE_ID_SERVERWORKS_EPB,
15694 bridge);
15695 if (bridge && bridge->subordinate &&
15696 (bridge->subordinate->number <=
15697 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015698 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015699 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015700 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015701 pci_dev_put(bridge);
15702 break;
15703 }
15704 } while (bridge);
15705 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015706
Joe Perches41535772013-02-16 11:20:04 +000015707 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15708 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015709 tp->pdev_peer = tg3_find_peer(tp);
15710
Matt Carlson507399f2009-11-13 13:03:37 +000015711 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015712 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015713 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015714 else if (tg3_flag(tp, 57765_PLUS))
15715 tg3_flag_set(tp, HW_TSO_3);
15716 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015717 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015718 tg3_flag_set(tp, HW_TSO_2);
15719 else if (tg3_flag(tp, 5750_PLUS)) {
15720 tg3_flag_set(tp, HW_TSO_1);
15721 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015722 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15723 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015724 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015725 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15726 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15727 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Matt Carlson1caf13e2013-03-06 17:02:29 +000015728 tg3_flag_set(tp, FW_TSO);
15729 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015730 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015731 tp->fw_needed = FIRMWARE_TG3TSO5;
15732 else
15733 tp->fw_needed = FIRMWARE_TG3TSO;
15734 }
15735
Matt Carlsondabc5c62011-05-19 12:12:52 +000015736 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015737 if (tg3_flag(tp, HW_TSO_1) ||
15738 tg3_flag(tp, HW_TSO_2) ||
15739 tg3_flag(tp, HW_TSO_3) ||
Matt Carlson1caf13e2013-03-06 17:02:29 +000015740 tg3_flag(tp, FW_TSO)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015741 /* For firmware TSO, assume ASF is disabled.
15742 * We'll disable TSO later if we discover ASF
15743 * is enabled in tg3_get_eeprom_hw_cfg().
15744 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015745 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015746 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015747 tg3_flag_clear(tp, TSO_CAPABLE);
15748 tg3_flag_clear(tp, TSO_BUG);
15749 tp->fw_needed = NULL;
15750 }
15751
Joe Perches41535772013-02-16 11:20:04 +000015752 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015753 tp->fw_needed = FIRMWARE_TG3;
15754
Nithin Sujirc4dab502013-03-06 17:02:34 +000015755 if (tg3_asic_rev(tp) == ASIC_REV_57766)
15756 tp->fw_needed = FIRMWARE_TG357766;
15757
Matt Carlson507399f2009-11-13 13:03:37 +000015758 tp->irq_max = 1;
15759
Joe Perches63c3a662011-04-26 08:12:10 +000015760 if (tg3_flag(tp, 5750_PLUS)) {
15761 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015762 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15763 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15764 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15765 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015766 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015767 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015768
Joe Perches63c3a662011-04-26 08:12:10 +000015769 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015770 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015771 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773
Joe Perches63c3a662011-04-26 08:12:10 +000015774 if (tg3_flag(tp, 57765_PLUS)) {
15775 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015776 tp->irq_max = TG3_IRQ_MAX_VECS;
15777 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015778 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015779
Michael Chan91024262012-09-28 07:12:38 +000015780 tp->txq_max = 1;
15781 tp->rxq_max = 1;
15782 if (tp->irq_max > 1) {
15783 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15784 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15785
Joe Perches41535772013-02-16 11:20:04 +000015786 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15787 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015788 tp->txq_max = tp->irq_max - 1;
15789 }
15790
Matt Carlsonb7abee62012-06-07 12:56:54 +000015791 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015792 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015793 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015794
Joe Perches41535772013-02-16 11:20:04 +000015795 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015796 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015797
Joe Perches41535772013-02-16 11:20:04 +000015798 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15799 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15800 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15801 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015802 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015803
Joe Perches63c3a662011-04-26 08:12:10 +000015804 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015805 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015806 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015807
Joe Perches63c3a662011-04-26 08:12:10 +000015808 if (!tg3_flag(tp, 5705_PLUS) ||
15809 tg3_flag(tp, 5780_CLASS) ||
15810 tg3_flag(tp, USE_JUMBO_BDFLAG))
15811 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015812
Matt Carlson52f44902008-11-21 17:17:04 -080015813 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15814 &pci_state_reg);
15815
Jon Mason708ebb3a2011-06-27 12:56:50 +000015816 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015817 u16 lnkctl;
15818
Joe Perches63c3a662011-04-26 08:12:10 +000015819 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015820
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015821 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015822 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015823 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015824 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015825 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015826 }
Joe Perches41535772013-02-16 11:20:04 +000015827 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15828 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15829 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15830 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015831 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015832 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015833 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015834 }
Joe Perches41535772013-02-16 11:20:04 +000015835 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015836 /* BCM5785 devices are effectively PCIe devices, and should
15837 * follow PCIe codepaths, but do not have a PCIe capabilities
15838 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015839 */
Joe Perches63c3a662011-04-26 08:12:10 +000015840 tg3_flag_set(tp, PCI_EXPRESS);
15841 } else if (!tg3_flag(tp, 5705_PLUS) ||
15842 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015843 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15844 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015845 dev_err(&tp->pdev->dev,
15846 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015847 return -EIO;
15848 }
15849
15850 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015851 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015853
Michael Chan399de502005-10-03 14:02:39 -070015854 /* If we have an AMD 762 or VIA K8T800 chipset, write
15855 * reordering to the mailbox registers done by the host
15856 * controller can cause major troubles. We read back from
15857 * every mailbox register write to force the writes to be
15858 * posted to the chip in order.
15859 */
Matt Carlson41434702011-03-09 16:58:22 +000015860 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015861 !tg3_flag(tp, PCI_EXPRESS))
15862 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015863
Matt Carlson69fc4052008-12-21 20:19:57 -080015864 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15865 &tp->pci_cacheline_sz);
15866 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15867 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015868 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015869 tp->pci_lat_timer < 64) {
15870 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015871 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15872 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015873 }
15874
Matt Carlson16821282011-07-13 09:27:28 +000015875 /* Important! -- It is critical that the PCI-X hw workaround
15876 * situation is decided before the first MMIO register access.
15877 */
Joe Perches41535772013-02-16 11:20:04 +000015878 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015879 /* 5700 BX chips need to have their TX producer index
15880 * mailboxes written twice to workaround a bug.
15881 */
Joe Perches63c3a662011-04-26 08:12:10 +000015882 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015883
Matt Carlson52f44902008-11-21 17:17:04 -080015884 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015885 *
15886 * The workaround is to use indirect register accesses
15887 * for all chip writes not to mailbox registers.
15888 */
Joe Perches63c3a662011-04-26 08:12:10 +000015889 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015890 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891
Joe Perches63c3a662011-04-26 08:12:10 +000015892 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015893
15894 /* The chip can have it's power management PCI config
15895 * space registers clobbered due to this bug.
15896 * So explicitly force the chip into D0 here.
15897 */
Matt Carlson9974a352007-10-07 23:27:28 -070015898 pci_read_config_dword(tp->pdev,
15899 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015900 &pm_reg);
15901 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15902 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015903 pci_write_config_dword(tp->pdev,
15904 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015905 pm_reg);
15906
15907 /* Also, force SERR#/PERR# in PCI command. */
15908 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15909 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15910 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15911 }
15912 }
15913
Linus Torvalds1da177e2005-04-16 15:20:36 -070015914 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015915 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015916 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015917 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015918
15919 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015920 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015921 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15922 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15923 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15924 }
15925
Michael Chan1ee582d2005-08-09 20:16:46 -070015926 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015927 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015928 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015929 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015930 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015931 tp->write32_tx_mbox = tg3_write32;
15932 tp->write32_rx_mbox = tg3_write32;
15933
15934 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015935 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015936 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015937 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015938 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015939 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015940 /*
15941 * Back to back register writes can cause problems on these
15942 * chips, the workaround is to read back all reg writes
15943 * except those to mailbox regs.
15944 *
15945 * See tg3_write_indirect_reg32().
15946 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015947 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015948 }
15949
Joe Perches63c3a662011-04-26 08:12:10 +000015950 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015951 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015952 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015953 tp->write32_rx_mbox = tg3_write_flush_reg32;
15954 }
Michael Chan20094932005-08-09 20:16:32 -070015955
Joe Perches63c3a662011-04-26 08:12:10 +000015956 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015957 tp->read32 = tg3_read_indirect_reg32;
15958 tp->write32 = tg3_write_indirect_reg32;
15959 tp->read32_mbox = tg3_read_indirect_mbox;
15960 tp->write32_mbox = tg3_write_indirect_mbox;
15961 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15962 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15963
15964 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015965 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015966
15967 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15968 pci_cmd &= ~PCI_COMMAND_MEMORY;
15969 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15970 }
Joe Perches41535772013-02-16 11:20:04 +000015971 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015972 tp->read32_mbox = tg3_read32_mbox_5906;
15973 tp->write32_mbox = tg3_write32_mbox_5906;
15974 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15975 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15976 }
Michael Chan68929142005-08-09 20:17:14 -070015977
Michael Chanbbadf502006-04-06 21:46:34 -070015978 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015979 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000015980 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15981 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015982 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015983
Matt Carlson16821282011-07-13 09:27:28 +000015984 /* The memory arbiter has to be enabled in order for SRAM accesses
15985 * to succeed. Normally on powerup the tg3 chip firmware will make
15986 * sure it is enabled, but other entities such as system netboot
15987 * code might disable it.
15988 */
15989 val = tr32(MEMARB_MODE);
15990 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15991
Matt Carlson9dc5e342011-11-04 09:15:02 +000015992 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000015993 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015994 tg3_flag(tp, 5780_CLASS)) {
15995 if (tg3_flag(tp, PCIX_MODE)) {
15996 pci_read_config_dword(tp->pdev,
15997 tp->pcix_cap + PCI_X_STATUS,
15998 &val);
15999 tp->pci_fn = val & 0x7;
16000 }
Joe Perches41535772013-02-16 11:20:04 +000016001 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16002 tg3_asic_rev(tp) == ASIC_REV_5719 ||
16003 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000016004 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000016005 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
16006 val = tr32(TG3_CPMU_STATUS);
16007
Joe Perches41535772013-02-16 11:20:04 +000016008 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000016009 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
16010 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000016011 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
16012 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000016013 }
16014
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016015 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
16016 tp->write32_tx_mbox = tg3_write_flush_reg32;
16017 tp->write32_rx_mbox = tg3_write_flush_reg32;
16018 }
16019
Michael Chan7d0c41e2005-04-21 17:06:20 -070016020 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000016021 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070016022 * determined before calling tg3_set_power_state() so that
16023 * we know whether or not to switch out of Vaux power.
16024 * When the flag is set, it means that GPIO1 is used for eeprom
16025 * write protect and also implies that it is a LOM where GPIOs
16026 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040016027 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070016028 tg3_get_eeprom_hw_cfg(tp);
16029
Matt Carlson1caf13e2013-03-06 17:02:29 +000016030 if (tg3_flag(tp, FW_TSO) && tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000016031 tg3_flag_clear(tp, TSO_CAPABLE);
16032 tg3_flag_clear(tp, TSO_BUG);
16033 tp->fw_needed = NULL;
16034 }
16035
Joe Perches63c3a662011-04-26 08:12:10 +000016036 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070016037 /* Allow reads and writes to the
16038 * APE register and memory space.
16039 */
16040 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000016041 PCISTATE_ALLOW_APE_SHMEM_WR |
16042 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016043 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
16044 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000016045
16046 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016047 }
16048
Matt Carlson16821282011-07-13 09:27:28 +000016049 /* Set up tp->grc_local_ctrl before calling
16050 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
16051 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070016052 * It is also used as eeprom write protect on LOMs.
16053 */
16054 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000016055 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016056 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070016057 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
16058 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070016059 /* Unused GPIO3 must be driven as output on 5752 because there
16060 * are no pull-up resistors on unused GPIO pins.
16061 */
Joe Perches41535772013-02-16 11:20:04 +000016062 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070016063 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070016064
Joe Perches41535772013-02-16 11:20:04 +000016065 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16066 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000016067 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080016068 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
16069
Matt Carlson8d519ab2009-04-20 06:58:01 +000016070 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16071 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016072 /* Turn off the debug UART. */
16073 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000016074 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070016075 /* Keep VMain power. */
16076 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
16077 GRC_LCLCTRL_GPIO_OUTPUT0;
16078 }
16079
Joe Perches41535772013-02-16 11:20:04 +000016080 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000016081 tp->grc_local_ctrl |=
16082 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
16083
Matt Carlson16821282011-07-13 09:27:28 +000016084 /* Switch out of Vaux if it is a NIC */
16085 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016086
Linus Torvalds1da177e2005-04-16 15:20:36 -070016087 /* Derive initial jumbo mode from MTU assigned in
16088 * ether_setup() via the alloc_etherdev() call
16089 */
Joe Perches63c3a662011-04-26 08:12:10 +000016090 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
16091 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016092
16093 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000016094 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16095 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16096 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16097 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000016098 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016099 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000016100 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016101 }
16102
Joe Perches41535772013-02-16 11:20:04 +000016103 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016104 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000016105
Linus Torvalds1da177e2005-04-16 15:20:36 -070016106 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000016107 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16108 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
16109 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
16110 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016111 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
16112 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
16113 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016114
Joe Perches41535772013-02-16 11:20:04 +000016115 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
16116 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016117 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000016118 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016119 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016120
Joe Perches63c3a662011-04-26 08:12:10 +000016121 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016122 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000016123 tg3_asic_rev(tp) != ASIC_REV_5785 &&
16124 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016125 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000016126 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
16127 tg3_asic_rev(tp) == ASIC_REV_5787 ||
16128 tg3_asic_rev(tp) == ASIC_REV_5784 ||
16129 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080016130 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
16131 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016132 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080016133 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016134 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080016135 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016136 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070016137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016138
Joe Perches41535772013-02-16 11:20:04 +000016139 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16140 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070016141 tp->phy_otp = tg3_read_otp_phycfg(tp);
16142 if (tp->phy_otp == 0)
16143 tp->phy_otp = TG3_OTP_DEFAULT;
16144 }
16145
Joe Perches63c3a662011-04-26 08:12:10 +000016146 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070016147 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
16148 else
16149 tp->mi_mode = MAC_MI_MODE_BASE;
16150
Linus Torvalds1da177e2005-04-16 15:20:36 -070016151 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000016152 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
16153 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016154 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
16155
Matt Carlson4d958472011-04-20 07:57:35 +000016156 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000016157 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
16158 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
16159 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000016160 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
16161 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
16162 }
16163
Joe Perches41535772013-02-16 11:20:04 +000016164 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
16165 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000016166 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070016167
Matt Carlson158d7ab2008-05-29 01:37:54 -070016168 err = tg3_mdio_init(tp);
16169 if (err)
16170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016171
16172 /* Initialize data/descriptor byte/word swapping. */
16173 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000016174 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
16175 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000016176 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
16177 GRC_MODE_WORD_SWAP_B2HRX_DATA |
16178 GRC_MODE_B2HRX_ENABLE |
16179 GRC_MODE_HTX2B_ENABLE |
16180 GRC_MODE_HOST_STACKUP);
16181 else
16182 val &= GRC_MODE_HOST_STACKUP;
16183
Linus Torvalds1da177e2005-04-16 15:20:36 -070016184 tw32(GRC_MODE, val | tp->grc_mode);
16185
16186 tg3_switch_clocks(tp);
16187
16188 /* Clear this out for sanity. */
16189 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
16190
16191 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
16192 &pci_state_reg);
16193 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016194 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000016195 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
16196 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
16197 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
16198 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016199 void __iomem *sram_base;
16200
16201 /* Write some dummy words into the SRAM status block
16202 * area, see if it reads back correctly. If the return
16203 * value is bad, force enable the PCIX workaround.
16204 */
16205 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
16206
16207 writel(0x00000000, sram_base);
16208 writel(0x00000000, sram_base + 4);
16209 writel(0xffffffff, sram_base + 4);
16210 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000016211 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016212 }
16213 }
16214
16215 udelay(50);
16216 tg3_nvram_init(tp);
16217
Nithin Sujirc4dab502013-03-06 17:02:34 +000016218 /* If the device has an NVRAM, no need to load patch firmware */
16219 if (tg3_asic_rev(tp) == ASIC_REV_57766 &&
16220 !tg3_flag(tp, NO_NVRAM))
16221 tp->fw_needed = NULL;
16222
Linus Torvalds1da177e2005-04-16 15:20:36 -070016223 grc_misc_cfg = tr32(GRC_MISC_CFG);
16224 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
16225
Joe Perches41535772013-02-16 11:20:04 +000016226 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016227 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
16228 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000016229 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016230
Joe Perches63c3a662011-04-26 08:12:10 +000016231 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000016232 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016233 tg3_flag_set(tp, TAGGED_STATUS);
16234 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070016235 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
16236 HOSTCC_MODE_CLRTICK_TXBD);
16237
16238 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
16239 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
16240 tp->misc_host_ctrl);
16241 }
16242
Matt Carlson3bda1252008-08-15 14:08:22 -070016243 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000016244 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000016245 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070016246 else
Matt Carlson6e01b202011-08-19 13:58:20 +000016247 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070016248
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016249 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016250 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016251
16252 err = tg3_phy_probe(tp);
16253 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016254 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016255 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016256 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016257 }
16258
Matt Carlson184b8902010-04-05 10:19:25 +000016259 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080016260 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016261
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016262 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
16263 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016264 } else {
Joe Perches41535772013-02-16 11:20:04 +000016265 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016266 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016267 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016268 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016269 }
16270
16271 /* 5700 {AX,BX} chips have a broken status block link
16272 * change bit implementation, so we must use the
16273 * status register in those cases.
16274 */
Joe Perches41535772013-02-16 11:20:04 +000016275 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000016276 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016277 else
Joe Perches63c3a662011-04-26 08:12:10 +000016278 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016279
16280 /* The led_ctrl is set during tg3_phy_probe, here we might
16281 * have to force the link status polling mechanism based
16282 * upon subsystem IDs.
16283 */
16284 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000016285 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016286 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
16287 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000016288 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016289 }
16290
16291 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016292 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000016293 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016294 else
Joe Perches63c3a662011-04-26 08:12:10 +000016295 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016296
Eric Dumazet9205fd92011-11-18 06:47:01 +000016297 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016298 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000016299 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016300 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000016301 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016302#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000016303 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000016304#endif
16305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016306
Matt Carlson2c49a442010-09-30 10:34:35 +000016307 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
16308 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000016309 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
16310
Matt Carlson2c49a442010-09-30 10:34:35 +000016311 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070016312
16313 /* Increment the rx prod index on the rx std ring by at most
16314 * 8 for these chips to workaround hw errata.
16315 */
Joe Perches41535772013-02-16 11:20:04 +000016316 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
16317 tg3_asic_rev(tp) == ASIC_REV_5752 ||
16318 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070016319 tp->rx_std_max_post = 8;
16320
Joe Perches63c3a662011-04-26 08:12:10 +000016321 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070016322 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
16323 PCIE_PWR_MGMT_L1_THRESH_MSK;
16324
Linus Torvalds1da177e2005-04-16 15:20:36 -070016325 return err;
16326}
16327
David S. Miller49b6e95f2007-03-29 01:38:42 -070016328#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016329static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016330{
16331 struct net_device *dev = tp->dev;
16332 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016333 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070016334 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070016335 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016336
David S. Miller49b6e95f2007-03-29 01:38:42 -070016337 addr = of_get_property(dp, "local-mac-address", &len);
16338 if (addr && len == 6) {
16339 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070016340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016341 }
16342 return -ENODEV;
16343}
16344
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016345static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016346{
16347 struct net_device *dev = tp->dev;
16348
16349 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
16350 return 0;
16351}
16352#endif
16353
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016354static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016355{
16356 struct net_device *dev = tp->dev;
16357 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080016358 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016359 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016360
David S. Miller49b6e95f2007-03-29 01:38:42 -070016361#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016362 if (!tg3_get_macaddr_sparc(tp))
16363 return 0;
16364#endif
16365
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016366 if (tg3_flag(tp, IS_SSB_CORE)) {
16367 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
16368 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
16369 return 0;
16370 }
16371
Linus Torvalds1da177e2005-04-16 15:20:36 -070016372 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000016373 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000016374 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016375 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
16376 mac_offset = 0xcc;
16377 if (tg3_nvram_lock(tp))
16378 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
16379 else
16380 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000016381 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000016382 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000016383 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000016384 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000016385 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000016386 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070016387 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016388
16389 /* First try to get it from MAC address mailbox. */
16390 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
16391 if ((hi >> 16) == 0x484b) {
16392 dev->dev_addr[0] = (hi >> 8) & 0xff;
16393 dev->dev_addr[1] = (hi >> 0) & 0xff;
16394
16395 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
16396 dev->dev_addr[2] = (lo >> 24) & 0xff;
16397 dev->dev_addr[3] = (lo >> 16) & 0xff;
16398 dev->dev_addr[4] = (lo >> 8) & 0xff;
16399 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016400
Michael Chan008652b2006-03-27 23:14:53 -080016401 /* Some old bootcode may report a 0 MAC address in SRAM */
16402 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
16403 }
16404 if (!addr_ok) {
16405 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000016406 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000016407 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000016408 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070016409 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
16410 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080016411 }
16412 /* Finally just fetch it out of the MAC control regs. */
16413 else {
16414 hi = tr32(MAC_ADDR_0_HIGH);
16415 lo = tr32(MAC_ADDR_0_LOW);
16416
16417 dev->dev_addr[5] = lo & 0xff;
16418 dev->dev_addr[4] = (lo >> 8) & 0xff;
16419 dev->dev_addr[3] = (lo >> 16) & 0xff;
16420 dev->dev_addr[2] = (lo >> 24) & 0xff;
16421 dev->dev_addr[1] = hi & 0xff;
16422 dev->dev_addr[0] = (hi >> 8) & 0xff;
16423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016424 }
16425
16426 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070016427#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070016428 if (!tg3_get_default_macaddr_sparc(tp))
16429 return 0;
16430#endif
16431 return -EINVAL;
16432 }
16433 return 0;
16434}
16435
David S. Miller59e6b432005-05-18 22:50:10 -070016436#define BOUNDARY_SINGLE_CACHELINE 1
16437#define BOUNDARY_MULTI_CACHELINE 2
16438
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016439static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070016440{
16441 int cacheline_size;
16442 u8 byte;
16443 int goal;
16444
16445 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16446 if (byte == 0)
16447 cacheline_size = 1024;
16448 else
16449 cacheline_size = (int) byte * 4;
16450
16451 /* On 5703 and later chips, the boundary bits have no
16452 * effect.
16453 */
Joe Perches41535772013-02-16 11:20:04 +000016454 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16455 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016456 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016457 goto out;
16458
16459#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16460 goal = BOUNDARY_MULTI_CACHELINE;
16461#else
16462#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16463 goal = BOUNDARY_SINGLE_CACHELINE;
16464#else
16465 goal = 0;
16466#endif
16467#endif
16468
Joe Perches63c3a662011-04-26 08:12:10 +000016469 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016470 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16471 goto out;
16472 }
16473
David S. Miller59e6b432005-05-18 22:50:10 -070016474 if (!goal)
16475 goto out;
16476
16477 /* PCI controllers on most RISC systems tend to disconnect
16478 * when a device tries to burst across a cache-line boundary.
16479 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16480 *
16481 * Unfortunately, for PCI-E there are only limited
16482 * write-side controls for this, and thus for reads
16483 * we will still get the disconnects. We'll also waste
16484 * these PCI cycles for both read and write for chips
16485 * other than 5700 and 5701 which do not implement the
16486 * boundary bits.
16487 */
Joe Perches63c3a662011-04-26 08:12:10 +000016488 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016489 switch (cacheline_size) {
16490 case 16:
16491 case 32:
16492 case 64:
16493 case 128:
16494 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16495 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16496 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16497 } else {
16498 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16499 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16500 }
16501 break;
16502
16503 case 256:
16504 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16505 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16506 break;
16507
16508 default:
16509 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16510 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16511 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016512 }
Joe Perches63c3a662011-04-26 08:12:10 +000016513 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016514 switch (cacheline_size) {
16515 case 16:
16516 case 32:
16517 case 64:
16518 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16519 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16520 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16521 break;
16522 }
16523 /* fallthrough */
16524 case 128:
16525 default:
16526 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16527 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16528 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016529 }
David S. Miller59e6b432005-05-18 22:50:10 -070016530 } else {
16531 switch (cacheline_size) {
16532 case 16:
16533 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16534 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16535 DMA_RWCTRL_WRITE_BNDRY_16);
16536 break;
16537 }
16538 /* fallthrough */
16539 case 32:
16540 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16541 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16542 DMA_RWCTRL_WRITE_BNDRY_32);
16543 break;
16544 }
16545 /* fallthrough */
16546 case 64:
16547 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16548 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16549 DMA_RWCTRL_WRITE_BNDRY_64);
16550 break;
16551 }
16552 /* fallthrough */
16553 case 128:
16554 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16555 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16556 DMA_RWCTRL_WRITE_BNDRY_128);
16557 break;
16558 }
16559 /* fallthrough */
16560 case 256:
16561 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16562 DMA_RWCTRL_WRITE_BNDRY_256);
16563 break;
16564 case 512:
16565 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16566 DMA_RWCTRL_WRITE_BNDRY_512);
16567 break;
16568 case 1024:
16569 default:
16570 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16571 DMA_RWCTRL_WRITE_BNDRY_1024);
16572 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016573 }
David S. Miller59e6b432005-05-18 22:50:10 -070016574 }
16575
16576out:
16577 return val;
16578}
16579
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016580static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
Joe Perches953c96e2013-04-09 10:18:14 +000016581 int size, bool to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016582{
16583 struct tg3_internal_buffer_desc test_desc;
16584 u32 sram_dma_descs;
16585 int i, ret;
16586
16587 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16588
16589 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16590 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16591 tw32(RDMAC_STATUS, 0);
16592 tw32(WDMAC_STATUS, 0);
16593
16594 tw32(BUFMGR_MODE, 0);
16595 tw32(FTQ_RESET, 0);
16596
16597 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16598 test_desc.addr_lo = buf_dma & 0xffffffff;
16599 test_desc.nic_mbuf = 0x00002100;
16600 test_desc.len = size;
16601
16602 /*
16603 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16604 * the *second* time the tg3 driver was getting loaded after an
16605 * initial scan.
16606 *
16607 * Broadcom tells me:
16608 * ...the DMA engine is connected to the GRC block and a DMA
16609 * reset may affect the GRC block in some unpredictable way...
16610 * The behavior of resets to individual blocks has not been tested.
16611 *
16612 * Broadcom noted the GRC reset will also reset all sub-components.
16613 */
16614 if (to_device) {
16615 test_desc.cqid_sqid = (13 << 8) | 2;
16616
16617 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16618 udelay(40);
16619 } else {
16620 test_desc.cqid_sqid = (16 << 8) | 7;
16621
16622 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16623 udelay(40);
16624 }
16625 test_desc.flags = 0x00000005;
16626
16627 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16628 u32 val;
16629
16630 val = *(((u32 *)&test_desc) + i);
16631 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16632 sram_dma_descs + (i * sizeof(u32)));
16633 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16634 }
16635 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16636
Matt Carlson859a588792010-04-05 10:19:28 +000016637 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016638 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016639 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016640 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016641
16642 ret = -ENODEV;
16643 for (i = 0; i < 40; i++) {
16644 u32 val;
16645
16646 if (to_device)
16647 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16648 else
16649 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16650 if ((val & 0xffff) == sram_dma_descs) {
16651 ret = 0;
16652 break;
16653 }
16654
16655 udelay(100);
16656 }
16657
16658 return ret;
16659}
16660
David S. Millerded73402005-05-23 13:59:47 -070016661#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016662
Matt Carlson41434702011-03-09 16:58:22 +000016663static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016664 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16665 { },
16666};
16667
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016668static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016669{
16670 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016671 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016672 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016673
Matt Carlson4bae65c2010-11-24 08:31:52 +000016674 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16675 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016676 if (!buf) {
16677 ret = -ENOMEM;
16678 goto out_nofree;
16679 }
16680
16681 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16682 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16683
David S. Miller59e6b432005-05-18 22:50:10 -070016684 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016685
Joe Perches63c3a662011-04-26 08:12:10 +000016686 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016687 goto out;
16688
Joe Perches63c3a662011-04-26 08:12:10 +000016689 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016690 /* DMA read watermark not used on PCIE */
16691 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016692 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016693 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16694 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016695 tp->dma_rwctrl |= 0x003f0000;
16696 else
16697 tp->dma_rwctrl |= 0x003f000f;
16698 } else {
Joe Perches41535772013-02-16 11:20:04 +000016699 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16700 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016702 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016703
Michael Chan4a29cc22006-03-19 13:21:12 -080016704 /* If the 5704 is behind the EPB bridge, we can
16705 * do the less restrictive ONE_DMA workaround for
16706 * better performance.
16707 */
Joe Perches63c3a662011-04-26 08:12:10 +000016708 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016709 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016710 tp->dma_rwctrl |= 0x8000;
16711 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016712 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16713
Joe Perches41535772013-02-16 11:20:04 +000016714 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016715 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016716 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016717 tp->dma_rwctrl |=
16718 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16719 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16720 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016721 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016722 /* 5780 always in PCIX mode */
16723 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016724 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016725 /* 5714 always in PCIX mode */
16726 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016727 } else {
16728 tp->dma_rwctrl |= 0x001b000f;
16729 }
16730 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016731 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16732 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016733
Joe Perches41535772013-02-16 11:20:04 +000016734 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16735 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016736 tp->dma_rwctrl &= 0xfffffff0;
16737
Joe Perches41535772013-02-16 11:20:04 +000016738 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16739 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016740 /* Remove this if it causes problems for some boards. */
16741 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16742
16743 /* On 5700/5701 chips, we need to set this bit.
16744 * Otherwise the chip will issue cacheline transactions
16745 * to streamable DMA memory with not all the byte
16746 * enables turned on. This is an error on several
16747 * RISC PCI controllers, in particular sparc64.
16748 *
16749 * On 5703/5704 chips, this bit has been reassigned
16750 * a different meaning. In particular, it is used
16751 * on those chips to enable a PCI-X workaround.
16752 */
16753 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16754 }
16755
16756 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16757
16758#if 0
16759 /* Unneeded, already done by tg3_get_invariants. */
16760 tg3_switch_clocks(tp);
16761#endif
16762
Joe Perches41535772013-02-16 11:20:04 +000016763 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16764 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016765 goto out;
16766
David S. Miller59e6b432005-05-18 22:50:10 -070016767 /* It is best to perform DMA test with maximum write burst size
16768 * to expose the 5700/5701 write DMA bug.
16769 */
16770 saved_dma_rwctrl = tp->dma_rwctrl;
16771 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16772 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16773
Linus Torvalds1da177e2005-04-16 15:20:36 -070016774 while (1) {
16775 u32 *p = buf, i;
16776
16777 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16778 p[i] = i;
16779
16780 /* Send the buffer to the chip. */
Joe Perches953c96e2013-04-09 10:18:14 +000016781 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016782 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016783 dev_err(&tp->pdev->dev,
16784 "%s: Buffer write failed. err = %d\n",
16785 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016786 break;
16787 }
16788
16789#if 0
16790 /* validate data reached card RAM correctly. */
16791 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16792 u32 val;
16793 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16794 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016795 dev_err(&tp->pdev->dev,
16796 "%s: Buffer corrupted on device! "
16797 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016798 /* ret = -ENODEV here? */
16799 }
16800 p[i] = 0;
16801 }
16802#endif
16803 /* Now read it back. */
Joe Perches953c96e2013-04-09 10:18:14 +000016804 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016805 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016806 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16807 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016808 break;
16809 }
16810
16811 /* Verify it. */
16812 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16813 if (p[i] == i)
16814 continue;
16815
David S. Miller59e6b432005-05-18 22:50:10 -070016816 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16817 DMA_RWCTRL_WRITE_BNDRY_16) {
16818 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016819 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16820 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16821 break;
16822 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016823 dev_err(&tp->pdev->dev,
16824 "%s: Buffer corrupted on read back! "
16825 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016826 ret = -ENODEV;
16827 goto out;
16828 }
16829 }
16830
16831 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16832 /* Success. */
16833 ret = 0;
16834 break;
16835 }
16836 }
David S. Miller59e6b432005-05-18 22:50:10 -070016837 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16838 DMA_RWCTRL_WRITE_BNDRY_16) {
16839 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016840 * now look for chipsets that are known to expose the
16841 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016842 */
Matt Carlson41434702011-03-09 16:58:22 +000016843 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016844 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16845 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016846 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016847 /* Safe to use the calculated DMA boundary. */
16848 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016849 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016850
David S. Miller59e6b432005-05-18 22:50:10 -070016851 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016853
16854out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016855 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016856out_nofree:
16857 return ret;
16858}
16859
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016860static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016861{
Joe Perches63c3a662011-04-26 08:12:10 +000016862 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016863 tp->bufmgr_config.mbuf_read_dma_low_water =
16864 DEFAULT_MB_RDMA_LOW_WATER_5705;
16865 tp->bufmgr_config.mbuf_mac_rx_low_water =
16866 DEFAULT_MB_MACRX_LOW_WATER_57765;
16867 tp->bufmgr_config.mbuf_high_water =
16868 DEFAULT_MB_HIGH_WATER_57765;
16869
16870 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16871 DEFAULT_MB_RDMA_LOW_WATER_5705;
16872 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16873 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16874 tp->bufmgr_config.mbuf_high_water_jumbo =
16875 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016876 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016877 tp->bufmgr_config.mbuf_read_dma_low_water =
16878 DEFAULT_MB_RDMA_LOW_WATER_5705;
16879 tp->bufmgr_config.mbuf_mac_rx_low_water =
16880 DEFAULT_MB_MACRX_LOW_WATER_5705;
16881 tp->bufmgr_config.mbuf_high_water =
16882 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016883 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016884 tp->bufmgr_config.mbuf_mac_rx_low_water =
16885 DEFAULT_MB_MACRX_LOW_WATER_5906;
16886 tp->bufmgr_config.mbuf_high_water =
16887 DEFAULT_MB_HIGH_WATER_5906;
16888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016889
Michael Chanfdfec1722005-07-25 12:31:48 -070016890 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16891 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16892 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16893 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16894 tp->bufmgr_config.mbuf_high_water_jumbo =
16895 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16896 } else {
16897 tp->bufmgr_config.mbuf_read_dma_low_water =
16898 DEFAULT_MB_RDMA_LOW_WATER;
16899 tp->bufmgr_config.mbuf_mac_rx_low_water =
16900 DEFAULT_MB_MACRX_LOW_WATER;
16901 tp->bufmgr_config.mbuf_high_water =
16902 DEFAULT_MB_HIGH_WATER;
16903
16904 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16905 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16906 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16907 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16908 tp->bufmgr_config.mbuf_high_water_jumbo =
16909 DEFAULT_MB_HIGH_WATER_JUMBO;
16910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016911
16912 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16913 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16914}
16915
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016916static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016917{
Matt Carlson79eb6902010-02-17 15:17:03 +000016918 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16919 case TG3_PHY_ID_BCM5400: return "5400";
16920 case TG3_PHY_ID_BCM5401: return "5401";
16921 case TG3_PHY_ID_BCM5411: return "5411";
16922 case TG3_PHY_ID_BCM5701: return "5701";
16923 case TG3_PHY_ID_BCM5703: return "5703";
16924 case TG3_PHY_ID_BCM5704: return "5704";
16925 case TG3_PHY_ID_BCM5705: return "5705";
16926 case TG3_PHY_ID_BCM5750: return "5750";
16927 case TG3_PHY_ID_BCM5752: return "5752";
16928 case TG3_PHY_ID_BCM5714: return "5714";
16929 case TG3_PHY_ID_BCM5780: return "5780";
16930 case TG3_PHY_ID_BCM5755: return "5755";
16931 case TG3_PHY_ID_BCM5787: return "5787";
16932 case TG3_PHY_ID_BCM5784: return "5784";
16933 case TG3_PHY_ID_BCM5756: return "5722/5756";
16934 case TG3_PHY_ID_BCM5906: return "5906";
16935 case TG3_PHY_ID_BCM5761: return "5761";
16936 case TG3_PHY_ID_BCM5718C: return "5718C";
16937 case TG3_PHY_ID_BCM5718S: return "5718S";
16938 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016939 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016940 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016941 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016942 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016943 case 0: return "serdes";
16944 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016946}
16947
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016948static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016949{
Joe Perches63c3a662011-04-26 08:12:10 +000016950 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016951 strcpy(str, "PCI Express");
16952 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016953 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016954 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16955
16956 strcpy(str, "PCIX:");
16957
16958 if ((clock_ctrl == 7) ||
16959 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16960 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16961 strcat(str, "133MHz");
16962 else if (clock_ctrl == 0)
16963 strcat(str, "33MHz");
16964 else if (clock_ctrl == 2)
16965 strcat(str, "50MHz");
16966 else if (clock_ctrl == 4)
16967 strcat(str, "66MHz");
16968 else if (clock_ctrl == 6)
16969 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016970 } else {
16971 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016972 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016973 strcat(str, "66MHz");
16974 else
16975 strcat(str, "33MHz");
16976 }
Joe Perches63c3a662011-04-26 08:12:10 +000016977 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016978 strcat(str, ":32-bit");
16979 else
16980 strcat(str, ":64-bit");
16981 return str;
16982}
16983
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016984static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016985{
16986 struct ethtool_coalesce *ec = &tp->coal;
16987
16988 memset(ec, 0, sizeof(*ec));
16989 ec->cmd = ETHTOOL_GCOALESCE;
16990 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16991 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16992 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16993 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16994 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16995 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16996 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16997 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16998 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16999
17000 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
17001 HOSTCC_MODE_CLRTICK_TXBD)) {
17002 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
17003 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
17004 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
17005 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
17006 }
Michael Chand244c892005-07-05 14:42:33 -070017007
Joe Perches63c3a662011-04-26 08:12:10 +000017008 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070017009 ec->rx_coalesce_usecs_irq = 0;
17010 ec->tx_coalesce_usecs_irq = 0;
17011 ec->stats_block_coalesce_usecs = 0;
17012 }
David S. Miller15f98502005-05-18 22:49:26 -070017013}
17014
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017015static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017016 const struct pci_device_id *ent)
17017{
Linus Torvalds1da177e2005-04-16 15:20:36 -070017018 struct net_device *dev;
17019 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000017020 int i, err, pm_cap;
17021 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070017022 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080017023 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000017024 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017025
Joe Perches05dbe002010-02-17 19:44:19 +000017026 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017027
17028 err = pci_enable_device(pdev);
17029 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017030 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017031 return err;
17032 }
17033
Linus Torvalds1da177e2005-04-16 15:20:36 -070017034 err = pci_request_regions(pdev, DRV_MODULE_NAME);
17035 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000017036 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017037 goto err_out_disable_pdev;
17038 }
17039
17040 pci_set_master(pdev);
17041
17042 /* Find power-management capability. */
17043 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
17044 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000017045 dev_err(&pdev->dev,
17046 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017047 err = -EIO;
17048 goto err_out_free_res;
17049 }
17050
Matt Carlson16821282011-07-13 09:27:28 +000017051 err = pci_set_power_state(pdev, PCI_D0);
17052 if (err) {
17053 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
17054 goto err_out_free_res;
17055 }
17056
Matt Carlsonfe5f5782009-09-01 13:09:39 +000017057 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017058 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070017059 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000017060 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017061 }
17062
Linus Torvalds1da177e2005-04-16 15:20:36 -070017063 SET_NETDEV_DEV(dev, &pdev->dev);
17064
Linus Torvalds1da177e2005-04-16 15:20:36 -070017065 tp = netdev_priv(dev);
17066 tp->pdev = pdev;
17067 tp->dev = dev;
17068 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017069 tp->rx_mode = TG3_DEF_RX_MODE;
17070 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000017071 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070017072
Linus Torvalds1da177e2005-04-16 15:20:36 -070017073 if (tg3_debug > 0)
17074 tp->msg_enable = tg3_debug;
17075 else
17076 tp->msg_enable = TG3_DEF_MSG_ENABLE;
17077
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000017078 if (pdev_is_ssb_gige_core(pdev)) {
17079 tg3_flag_set(tp, IS_SSB_CORE);
17080 if (ssb_gige_must_flush_posted_writes(pdev))
17081 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
17082 if (ssb_gige_one_dma_at_once(pdev))
17083 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
17084 if (ssb_gige_have_roboswitch(pdev))
17085 tg3_flag_set(tp, ROBOSWITCH);
17086 if (ssb_gige_is_rgmii(pdev))
17087 tg3_flag_set(tp, RGMII_MODE);
17088 }
17089
Linus Torvalds1da177e2005-04-16 15:20:36 -070017090 /* The word/byte swap controls here control register access byte
17091 * swapping. DMA data byte swapping is controlled in the GRC_MODE
17092 * setting below.
17093 */
17094 tp->misc_host_ctrl =
17095 MISC_HOST_CTRL_MASK_PCI_INT |
17096 MISC_HOST_CTRL_WORD_SWAP |
17097 MISC_HOST_CTRL_INDIR_ACCESS |
17098 MISC_HOST_CTRL_PCISTATE_RW;
17099
17100 /* The NONFRM (non-frame) byte/word swap controls take effect
17101 * on descriptor entries, anything which isn't packet data.
17102 *
17103 * The StrongARM chips on the board (one for tx, one for rx)
17104 * are running in big-endian mode.
17105 */
17106 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
17107 GRC_MODE_WSWAP_NONFRM_DATA);
17108#ifdef __BIG_ENDIAN
17109 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
17110#endif
17111 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017112 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000017113 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017114
Matt Carlsond5fe4882008-11-21 17:20:32 -080017115 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010017116 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017117 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017118 err = -ENOMEM;
17119 goto err_out_free_dev;
17120 }
17121
Matt Carlsonc9cab242011-07-13 09:27:27 +000017122 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
17123 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
17124 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
17125 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
17126 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000017127 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000017128 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
17129 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000017130 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
17131 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
17132 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
17133 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000017134 tg3_flag_set(tp, ENABLE_APE);
17135 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
17136 if (!tp->aperegs) {
17137 dev_err(&pdev->dev,
17138 "Cannot map APE registers, aborting\n");
17139 err = -ENOMEM;
17140 goto err_out_iounmap;
17141 }
17142 }
17143
Linus Torvalds1da177e2005-04-16 15:20:36 -070017144 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
17145 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017146
Linus Torvalds1da177e2005-04-16 15:20:36 -070017147 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017148 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000017149 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017150 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017151
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000017152 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017153 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017154 dev_err(&pdev->dev,
17155 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017156 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017157 }
17158
Michael Chan4a29cc22006-03-19 13:21:12 -080017159 /* The EPB bridge inside 5714, 5715, and 5780 and any
17160 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080017161 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
17162 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
17163 * do DMA address check in tg3_start_xmit().
17164 */
Joe Perches63c3a662011-04-26 08:12:10 +000017165 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070017166 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000017167 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070017168 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080017169#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070017170 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017171#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080017172 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070017173 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080017174
17175 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070017176 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080017177 err = pci_set_dma_mask(pdev, dma_mask);
17178 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000017179 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080017180 err = pci_set_consistent_dma_mask(pdev,
17181 persist_dma_mask);
17182 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017183 dev_err(&pdev->dev, "Unable to obtain 64 bit "
17184 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017185 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017186 }
17187 }
17188 }
Yang Hongyang284901a2009-04-06 19:01:15 -070017189 if (err || dma_mask == DMA_BIT_MASK(32)) {
17190 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080017191 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017192 dev_err(&pdev->dev,
17193 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017194 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080017195 }
17196 }
17197
Michael Chanfdfec1722005-07-25 12:31:48 -070017198 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017199
Matt Carlson0da06062011-05-19 12:12:53 +000017200 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
17201
17202 /* 5700 B0 chips do not support checksumming correctly due
17203 * to hardware bugs.
17204 */
Joe Perches41535772013-02-16 11:20:04 +000017205 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000017206 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
17207
17208 if (tg3_flag(tp, 5755_PLUS))
17209 features |= NETIF_F_IPV6_CSUM;
17210 }
17211
Michael Chan4e3a7aa2006-03-20 17:47:44 -080017212 /* TSO is on by default on chips that support hardware TSO.
17213 * Firmware TSO on older chips gives lower performance, so it
17214 * is off by default, but can be enabled using ethtool.
17215 */
Joe Perches63c3a662011-04-26 08:12:10 +000017216 if ((tg3_flag(tp, HW_TSO_1) ||
17217 tg3_flag(tp, HW_TSO_2) ||
17218 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000017219 (features & NETIF_F_IP_CSUM))
17220 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000017221 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000017222 if (features & NETIF_F_IPV6_CSUM)
17223 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000017224 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000017225 tg3_asic_rev(tp) == ASIC_REV_5761 ||
17226 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
17227 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
17228 tg3_asic_rev(tp) == ASIC_REV_5785 ||
17229 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000017230 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070017231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017232
Matt Carlsond542fe22011-05-19 16:02:43 +000017233 dev->features |= features;
17234 dev->vlan_features |= features;
17235
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017236 /*
17237 * Add loopback capability only for a subset of devices that support
17238 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
17239 * loopback for the remaining devices.
17240 */
Joe Perches41535772013-02-16 11:20:04 +000017241 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017242 !tg3_flag(tp, CPMU_PRESENT))
17243 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000017244 features |= NETIF_F_LOOPBACK;
17245
Matt Carlson0da06062011-05-19 12:12:53 +000017246 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000017247
Joe Perches41535772013-02-16 11:20:04 +000017248 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000017249 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070017250 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000017251 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252 tp->rx_pending = 63;
17253 }
17254
Linus Torvalds1da177e2005-04-16 15:20:36 -070017255 err = tg3_get_device_address(tp);
17256 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017257 dev_err(&pdev->dev,
17258 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000017259 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070017260 }
17261
Matt Carlsonc88864d2007-11-12 21:07:01 -080017262 /*
17263 * Reset chip in case UNDI or EFI driver did not shutdown
17264 * DMA self test will enable WDMAC and we'll see (spurious)
17265 * pending DMA on the PCI bus at that point.
17266 */
17267 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
17268 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
17269 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
17270 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
17271 }
17272
17273 err = tg3_test_dma(tp);
17274 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017275 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080017276 goto err_out_apeunmap;
17277 }
17278
Matt Carlson78f90dc2009-11-13 13:03:42 +000017279 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
17280 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
17281 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000017282 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000017283 struct tg3_napi *tnapi = &tp->napi[i];
17284
17285 tnapi->tp = tp;
17286 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
17287
17288 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000017289 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017290 intmbx += 0x8;
17291 else
17292 intmbx += 0x4;
17293
17294 tnapi->consmbox = rcvmbx;
17295 tnapi->prodmbox = sndmbx;
17296
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017297 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000017298 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000017299 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000017300 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000017301
Joe Perches63c3a662011-04-26 08:12:10 +000017302 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000017303 break;
17304
17305 /*
17306 * If we support MSIX, we'll be using RSS. If we're using
17307 * RSS, the first vector only handles link interrupts and the
17308 * remaining vectors handle rx and tx interrupts. Reuse the
17309 * mailbox values for the next iteration. The values we setup
17310 * above are still useful for the single vectored mode.
17311 */
17312 if (!i)
17313 continue;
17314
17315 rcvmbx += 0x8;
17316
17317 if (sndmbx & 0x4)
17318 sndmbx -= 0x4;
17319 else
17320 sndmbx += 0xc;
17321 }
17322
Matt Carlsonc88864d2007-11-12 21:07:01 -080017323 tg3_init_coal(tp);
17324
Michael Chanc49a1562006-12-17 17:07:29 -080017325 pci_set_drvdata(pdev, dev);
17326
Joe Perches41535772013-02-16 11:20:04 +000017327 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
17328 tg3_asic_rev(tp) == ASIC_REV_5720 ||
17329 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000017330 tg3_flag_set(tp, PTP_CAPABLE);
17331
Matt Carlsoncd0d7222011-07-13 09:27:33 +000017332 if (tg3_flag(tp, 5717_PLUS)) {
17333 /* Resume a low-power mode */
17334 tg3_frob_aux_power(tp, false);
17335 }
17336
Matt Carlson21f76382012-02-22 12:35:21 +000017337 tg3_timer_init(tp);
17338
Michael Chan402e1392013-02-14 12:13:41 +000017339 tg3_carrier_off(tp);
17340
Linus Torvalds1da177e2005-04-16 15:20:36 -070017341 err = register_netdev(dev);
17342 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000017343 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070017344 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017345 }
17346
Joe Perches05dbe002010-02-17 19:44:19 +000017347 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
17348 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000017349 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000017350 tg3_bus_string(tp, str),
17351 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017352
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017353 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000017354 struct phy_device *phydev;
17355 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000017356 netdev_info(dev,
17357 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000017358 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017359 } else {
17360 char *ethtype;
17361
17362 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
17363 ethtype = "10/100Base-TX";
17364 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
17365 ethtype = "1000Base-SX";
17366 else
17367 ethtype = "10/100/1000Base-T";
17368
Matt Carlson5129c3a2010-04-05 10:19:23 +000017369 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000017370 "(WireSpeed[%d], EEE[%d])\n",
17371 tg3_phy_string(tp), ethtype,
17372 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
17373 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017374 }
Matt Carlsondf59c942008-11-03 16:52:56 -080017375
Joe Perches05dbe002010-02-17 19:44:19 +000017376 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000017377 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017378 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000017379 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000017380 tg3_flag(tp, ENABLE_ASF) != 0,
17381 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000017382 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
17383 tp->dma_rwctrl,
17384 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
17385 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017386
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017387 pci_save_state(pdev);
17388
Linus Torvalds1da177e2005-04-16 15:20:36 -070017389 return 0;
17390
Matt Carlson0d3031d2007-10-10 18:02:43 -070017391err_out_apeunmap:
17392 if (tp->aperegs) {
17393 iounmap(tp->aperegs);
17394 tp->aperegs = NULL;
17395 }
17396
Linus Torvalds1da177e2005-04-16 15:20:36 -070017397err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070017398 if (tp->regs) {
17399 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017400 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017402
17403err_out_free_dev:
17404 free_netdev(dev);
17405
Matt Carlson16821282011-07-13 09:27:28 +000017406err_out_power_down:
17407 pci_set_power_state(pdev, PCI_D3hot);
17408
Linus Torvalds1da177e2005-04-16 15:20:36 -070017409err_out_free_res:
17410 pci_release_regions(pdev);
17411
17412err_out_disable_pdev:
17413 pci_disable_device(pdev);
17414 pci_set_drvdata(pdev, NULL);
17415 return err;
17416}
17417
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017418static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017419{
17420 struct net_device *dev = pci_get_drvdata(pdev);
17421
17422 if (dev) {
17423 struct tg3 *tp = netdev_priv(dev);
17424
Jesper Juhle3c55302012-04-09 22:50:15 +020017425 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080017426
Matt Carlsondb219972011-11-04 09:15:03 +000017427 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017428
David S. Miller1805b2f2011-10-24 18:18:09 -040017429 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017430 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070017431 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017432 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070017433
Linus Torvalds1da177e2005-04-16 15:20:36 -070017434 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070017435 if (tp->aperegs) {
17436 iounmap(tp->aperegs);
17437 tp->aperegs = NULL;
17438 }
Michael Chan68929142005-08-09 20:17:14 -070017439 if (tp->regs) {
17440 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070017441 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070017442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070017443 free_netdev(dev);
17444 pci_release_regions(pdev);
17445 pci_disable_device(pdev);
17446 pci_set_drvdata(pdev, NULL);
17447 }
17448}
17449
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017450#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017451static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017452{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017453 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017454 struct net_device *dev = pci_get_drvdata(pdev);
17455 struct tg3 *tp = netdev_priv(dev);
17456 int err;
17457
17458 if (!netif_running(dev))
17459 return 0;
17460
Matt Carlsondb219972011-11-04 09:15:03 +000017461 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017462 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017463 tg3_netif_stop(tp);
17464
Matt Carlson21f76382012-02-22 12:35:21 +000017465 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017466
David S. Millerf47c11e2005-06-24 20:18:35 -070017467 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017468 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017469 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017470
17471 netif_device_detach(dev);
17472
David S. Millerf47c11e2005-06-24 20:18:35 -070017473 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017474 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017475 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017476 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017477
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017478 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017479 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017480 int err2;
17481
David S. Millerf47c11e2005-06-24 20:18:35 -070017482 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017483
Joe Perches63c3a662011-04-26 08:12:10 +000017484 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017485 err2 = tg3_restart_hw(tp, true);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017486 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017488
Matt Carlson21f76382012-02-22 12:35:21 +000017489 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017490
17491 netif_device_attach(dev);
17492 tg3_netif_start(tp);
17493
Michael Chanb9ec6c12006-07-25 16:37:27 -070017494out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017495 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017496
17497 if (!err2)
17498 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017499 }
17500
17501 return err;
17502}
17503
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017504static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017505{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017506 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017507 struct net_device *dev = pci_get_drvdata(pdev);
17508 struct tg3 *tp = netdev_priv(dev);
17509 int err;
17510
17511 if (!netif_running(dev))
17512 return 0;
17513
Linus Torvalds1da177e2005-04-16 15:20:36 -070017514 netif_device_attach(dev);
17515
David S. Millerf47c11e2005-06-24 20:18:35 -070017516 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017517
Joe Perches63c3a662011-04-26 08:12:10 +000017518 tg3_flag_set(tp, INIT_COMPLETE);
Nithin Sujir942d1af2013-04-09 08:48:07 +000017519 err = tg3_restart_hw(tp,
17520 !(tp->phy_flags & TG3_PHYFLG_KEEP_LINK_ON_PWRDN));
Michael Chanb9ec6c12006-07-25 16:37:27 -070017521 if (err)
17522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017523
Matt Carlson21f76382012-02-22 12:35:21 +000017524 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017525
Linus Torvalds1da177e2005-04-16 15:20:36 -070017526 tg3_netif_start(tp);
17527
Michael Chanb9ec6c12006-07-25 16:37:27 -070017528out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017529 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017530
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017531 if (!err)
17532 tg3_phy_start(tp);
17533
Michael Chanb9ec6c12006-07-25 16:37:27 -070017534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017535}
17536
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017537static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017538#define TG3_PM_OPS (&tg3_pm_ops)
17539
17540#else
17541
17542#define TG3_PM_OPS NULL
17543
17544#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017545
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017546/**
17547 * tg3_io_error_detected - called when PCI error is detected
17548 * @pdev: Pointer to PCI device
17549 * @state: The current pci connection state
17550 *
17551 * This function is called after a PCI bus error affecting
17552 * this device has been detected.
17553 */
17554static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17555 pci_channel_state_t state)
17556{
17557 struct net_device *netdev = pci_get_drvdata(pdev);
17558 struct tg3 *tp = netdev_priv(netdev);
17559 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17560
17561 netdev_info(netdev, "PCI I/O error detected\n");
17562
17563 rtnl_lock();
17564
17565 if (!netif_running(netdev))
17566 goto done;
17567
17568 tg3_phy_stop(tp);
17569
17570 tg3_netif_stop(tp);
17571
Matt Carlson21f76382012-02-22 12:35:21 +000017572 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017573
17574 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017575 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017576
17577 netif_device_detach(netdev);
17578
17579 /* Clean up software state, even if MMIO is blocked */
17580 tg3_full_lock(tp, 0);
17581 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17582 tg3_full_unlock(tp);
17583
17584done:
17585 if (state == pci_channel_io_perm_failure)
17586 err = PCI_ERS_RESULT_DISCONNECT;
17587 else
17588 pci_disable_device(pdev);
17589
17590 rtnl_unlock();
17591
17592 return err;
17593}
17594
17595/**
17596 * tg3_io_slot_reset - called after the pci bus has been reset.
17597 * @pdev: Pointer to PCI device
17598 *
17599 * Restart the card from scratch, as if from a cold-boot.
17600 * At this point, the card has exprienced a hard reset,
17601 * followed by fixups by BIOS, and has its config space
17602 * set up identically to what it was at cold boot.
17603 */
17604static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17605{
17606 struct net_device *netdev = pci_get_drvdata(pdev);
17607 struct tg3 *tp = netdev_priv(netdev);
17608 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17609 int err;
17610
17611 rtnl_lock();
17612
17613 if (pci_enable_device(pdev)) {
17614 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17615 goto done;
17616 }
17617
17618 pci_set_master(pdev);
17619 pci_restore_state(pdev);
17620 pci_save_state(pdev);
17621
17622 if (!netif_running(netdev)) {
17623 rc = PCI_ERS_RESULT_RECOVERED;
17624 goto done;
17625 }
17626
17627 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017628 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017629 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017630
17631 rc = PCI_ERS_RESULT_RECOVERED;
17632
17633done:
17634 rtnl_unlock();
17635
17636 return rc;
17637}
17638
17639/**
17640 * tg3_io_resume - called when traffic can start flowing again.
17641 * @pdev: Pointer to PCI device
17642 *
17643 * This callback is called when the error recovery driver tells
17644 * us that its OK to resume normal operation.
17645 */
17646static void tg3_io_resume(struct pci_dev *pdev)
17647{
17648 struct net_device *netdev = pci_get_drvdata(pdev);
17649 struct tg3 *tp = netdev_priv(netdev);
17650 int err;
17651
17652 rtnl_lock();
17653
17654 if (!netif_running(netdev))
17655 goto done;
17656
17657 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017658 tg3_flag_set(tp, INIT_COMPLETE);
Joe Perches953c96e2013-04-09 10:18:14 +000017659 err = tg3_restart_hw(tp, true);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017660 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017661 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017662 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17663 goto done;
17664 }
17665
17666 netif_device_attach(netdev);
17667
Matt Carlson21f76382012-02-22 12:35:21 +000017668 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017669
17670 tg3_netif_start(tp);
17671
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017672 tg3_full_unlock(tp);
17673
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017674 tg3_phy_start(tp);
17675
17676done:
17677 rtnl_unlock();
17678}
17679
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017680static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017681 .error_detected = tg3_io_error_detected,
17682 .slot_reset = tg3_io_slot_reset,
17683 .resume = tg3_io_resume
17684};
17685
Linus Torvalds1da177e2005-04-16 15:20:36 -070017686static struct pci_driver tg3_driver = {
17687 .name = DRV_MODULE_NAME,
17688 .id_table = tg3_pci_tbl,
17689 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017690 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017691 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017692 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017693};
17694
17695static int __init tg3_init(void)
17696{
Jeff Garzik29917622006-08-19 17:48:59 -040017697 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017698}
17699
17700static void __exit tg3_cleanup(void)
17701{
17702 pci_unregister_driver(&tg3_driver);
17703}
17704
17705module_init(tg3_init);
17706module_exit(tg3_cleanup);