blob: 93729f9423588de05fa4d5e25dbcef99438fd17d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Nithin Nayak Sujirb681b652013-01-06 12:51:10 +00007 * Copyright (C) 2005-2013 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000047#include <linux/ssb/ssb_driver_gige.h>
Michael Chanaed93e02012-07-16 16:24:02 +000048#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000056#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Matt Carlsonbe947302012-12-03 19:36:57 +000058#include <uapi/linux/net_tstamp.h>
59#include <linux/ptp_clock_kernel.h>
60
David S. Miller49b6e95f2007-03-29 01:38:42 -070061#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070063#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Matt Carlson63532392008-11-03 16:49:57 -080066#define BAR_0 0
67#define BAR_2 2
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "tg3.h"
70
Joe Perches63c3a662011-04-26 08:12:10 +000071/* Functions & macros to verify TG3_FLAGS types */
72
73static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 return test_bit(flag, bits);
76}
77
78static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 set_bit(flag, bits);
81}
82
83static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
84{
85 clear_bit(flag, bits);
86}
87
88#define tg3_flag(tp, flag) \
89 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
90#define tg3_flag_set(tp, flag) \
91 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
92#define tg3_flag_clear(tp, flag) \
93 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000096#define TG3_MAJ_NUM 3
Michael Chand8871992013-02-14 12:13:42 +000097#define TG3_MIN_NUM 130
Matt Carlson6867c842010-07-11 09:31:44 +000098#define DRV_MODULE_VERSION \
99 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chand8871992013-02-14 12:13:42 +0000100#define DRV_MODULE_RELDATE "February 14, 2013"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000102#define RESET_KIND_SHUTDOWN 0
103#define RESET_KIND_INIT 1
104#define RESET_KIND_SUSPEND 2
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_MODE 0
107#define TG3_DEF_TX_MODE 0
108#define TG3_DEF_MSG_ENABLE \
109 (NETIF_MSG_DRV | \
110 NETIF_MSG_PROBE | \
111 NETIF_MSG_LINK | \
112 NETIF_MSG_TIMER | \
113 NETIF_MSG_IFDOWN | \
114 NETIF_MSG_IFUP | \
115 NETIF_MSG_RX_ERR | \
116 NETIF_MSG_TX_ERR)
117
Matt Carlson520b2752011-06-13 13:39:02 +0000118#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* length of time before we decide the hardware is borked,
121 * and dev->tx_timeout() should be called to fix the problem
122 */
Joe Perches63c3a662011-04-26 08:12:10 +0000123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define TG3_TX_TIMEOUT (5 * HZ)
125
126/* hardware minimum and maximum for a single frame's data payload */
127#define TG3_MIN_MTU 60
128#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000129 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* These numbers seem to be hard coded in the NIC firmware somehow.
132 * You can't change the ring sizes, but you can change where you place
133 * them in the NIC onboard memory.
134 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000135#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000136 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000137 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000139#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000140 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000141 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142#define TG3_DEF_RX_JUMBO_RING_PENDING 100
143
144/* Do not place this n-ring entries value into the tp struct itself,
145 * we really want to expose these constants to GCC so that modulo et
146 * al. operations are done with shifts and masks instead of with
147 * hw multiply/modulo instructions. Another solution would be to
148 * replace things like '% foo' with '& (foo - 1)'.
149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151#define TG3_TX_RING_SIZE 512
152#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
153
Matt Carlson2c49a442010-09-30 10:34:35 +0000154#define TG3_RX_STD_RING_BYTES(tp) \
155 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
156#define TG3_RX_JMB_RING_BYTES(tp) \
157 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
158#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000159 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
161 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
163
Matt Carlson287be122009-08-28 13:58:46 +0000164#define TG3_DMA_BYTE_ENAB 64
165
166#define TG3_RX_STD_DMA_SZ 1536
167#define TG3_RX_JMB_DMA_SZ 9046
168
169#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
170
171#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
172#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Matt Carlson2c49a442010-09-30 10:34:35 +0000174#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
175 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000176
Matt Carlson2c49a442010-09-30 10:34:35 +0000177#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
178 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000179
Matt Carlsond2757fc2010-04-12 06:58:27 +0000180/* Due to a hardware bug, the 5701 can only DMA to memory addresses
181 * that are at least dword aligned when used in PCIX mode. The driver
182 * works around this bug by double copying the packet. This workaround
183 * is built into the normal double copy length check for efficiency.
184 *
185 * However, the double copy is only necessary on those architectures
186 * where unaligned memory accesses are inefficient. For those architectures
187 * where unaligned memory accesses incur little penalty, we can reintegrate
188 * the 5701 in the normal rx path. Doing so saves a device structure
189 * dereference by hardcoding the double copy threshold in place.
190 */
191#define TG3_RX_COPY_THRESHOLD 256
192#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
193 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
194#else
195 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
196#endif
197
Matt Carlson81389f52011-08-31 11:44:49 +0000198#if (NET_IP_ALIGN != 0)
199#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
200#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000201#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000202#endif
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000205#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000206#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000207#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Matt Carlsonad829262008-11-21 17:16:16 -0800209#define TG3_RAW_IP_ALIGN 2
210
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000211#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000212#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000213
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800214#define FIRMWARE_TG3 "tigon/tg3.bin"
215#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
216#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
217
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500218static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000219 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
222MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
223MODULE_LICENSE("GPL");
224MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800225MODULE_FIRMWARE(FIRMWARE_TG3);
226MODULE_FIRMWARE(FIRMWARE_TG3TSO);
227MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
230module_param(tg3_debug, int, 0);
231MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
232
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000233#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
234#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
235
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000236static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
256 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
257 TG3_DRV_DATA_FLAG_5705_10_100},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
259 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
260 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
263 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
264 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5750)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
271 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
277 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000285 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
286 PCI_VENDOR_ID_LENOVO,
287 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
288 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
291 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000310 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
311 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
312 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
313 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
314 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
315 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
317 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
319 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
327 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
329 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
331 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000333 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000334 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Matt Carlsond3f677a2013-02-14 14:27:51 +0000335 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57766)},
Michael Chanc86a8562013-01-06 12:51:08 +0000336 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5762)},
337 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5725)},
338 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5727)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700339 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
340 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
341 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
342 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
343 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
344 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
345 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000346 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700347 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
350MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
351
Andreas Mohr50da8592006-08-14 23:54:30 -0700352static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000354} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 { "rx_octets" },
356 { "rx_fragments" },
357 { "rx_ucast_packets" },
358 { "rx_mcast_packets" },
359 { "rx_bcast_packets" },
360 { "rx_fcs_errors" },
361 { "rx_align_errors" },
362 { "rx_xon_pause_rcvd" },
363 { "rx_xoff_pause_rcvd" },
364 { "rx_mac_ctrl_rcvd" },
365 { "rx_xoff_entered" },
366 { "rx_frame_too_long_errors" },
367 { "rx_jabbers" },
368 { "rx_undersize_packets" },
369 { "rx_in_length_errors" },
370 { "rx_out_length_errors" },
371 { "rx_64_or_less_octet_packets" },
372 { "rx_65_to_127_octet_packets" },
373 { "rx_128_to_255_octet_packets" },
374 { "rx_256_to_511_octet_packets" },
375 { "rx_512_to_1023_octet_packets" },
376 { "rx_1024_to_1522_octet_packets" },
377 { "rx_1523_to_2047_octet_packets" },
378 { "rx_2048_to_4095_octet_packets" },
379 { "rx_4096_to_8191_octet_packets" },
380 { "rx_8192_to_9022_octet_packets" },
381
382 { "tx_octets" },
383 { "tx_collisions" },
384
385 { "tx_xon_sent" },
386 { "tx_xoff_sent" },
387 { "tx_flow_control" },
388 { "tx_mac_errors" },
389 { "tx_single_collisions" },
390 { "tx_mult_collisions" },
391 { "tx_deferred" },
392 { "tx_excessive_collisions" },
393 { "tx_late_collisions" },
394 { "tx_collide_2times" },
395 { "tx_collide_3times" },
396 { "tx_collide_4times" },
397 { "tx_collide_5times" },
398 { "tx_collide_6times" },
399 { "tx_collide_7times" },
400 { "tx_collide_8times" },
401 { "tx_collide_9times" },
402 { "tx_collide_10times" },
403 { "tx_collide_11times" },
404 { "tx_collide_12times" },
405 { "tx_collide_13times" },
406 { "tx_collide_14times" },
407 { "tx_collide_15times" },
408 { "tx_ucast_packets" },
409 { "tx_mcast_packets" },
410 { "tx_bcast_packets" },
411 { "tx_carrier_sense_errors" },
412 { "tx_discards" },
413 { "tx_errors" },
414
415 { "dma_writeq_full" },
416 { "dma_write_prioq_full" },
417 { "rxbds_empty" },
418 { "rx_discards" },
419 { "rx_errors" },
420 { "rx_threshold_hit" },
421
422 { "dma_readq_full" },
423 { "dma_read_prioq_full" },
424 { "tx_comp_queue_full" },
425
426 { "ring_set_send_prod_index" },
427 { "ring_status_update" },
428 { "nic_irqs" },
429 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000430 { "nic_tx_threshold_hit" },
431
432 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Matt Carlson48fa55a2011-04-13 11:05:06 +0000435#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000436#define TG3_NVRAM_TEST 0
437#define TG3_LINK_TEST 1
438#define TG3_REGISTER_TEST 2
439#define TG3_MEMORY_TEST 3
440#define TG3_MAC_LOOPB_TEST 4
441#define TG3_PHY_LOOPB_TEST 5
442#define TG3_EXT_LOOPB_TEST 6
443#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000444
445
Andreas Mohr50da8592006-08-14 23:54:30 -0700446static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700447 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000448} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000449 [TG3_NVRAM_TEST] = { "nvram test (online) " },
450 [TG3_LINK_TEST] = { "link test (online) " },
451 [TG3_REGISTER_TEST] = { "register test (offline)" },
452 [TG3_MEMORY_TEST] = { "memory test (offline)" },
453 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
454 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
455 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
456 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700457};
458
Matt Carlson48fa55a2011-04-13 11:05:06 +0000459#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
460
461
Michael Chanb401e9e2005-12-19 16:27:04 -0800462static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
463{
464 writel(val, tp->regs + off);
465}
466
467static u32 tg3_read32(struct tg3 *tp, u32 off)
468{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000469 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800470}
471
Matt Carlson0d3031d2007-10-10 18:02:43 -0700472static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
473{
474 writel(val, tp->aperegs + off);
475}
476
477static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
478{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000479 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
483{
Michael Chan68929142005-08-09 20:17:14 -0700484 unsigned long flags;
485
486 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700487 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
488 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700489 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700490}
491
492static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
493{
494 writel(val, tp->regs + off);
495 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Michael Chan68929142005-08-09 20:17:14 -0700498static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
499{
500 unsigned long flags;
501 u32 val;
502
503 spin_lock_irqsave(&tp->indirect_lock, flags);
504 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
505 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
506 spin_unlock_irqrestore(&tp->indirect_lock, flags);
507 return val;
508}
509
510static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
511{
512 unsigned long flags;
513
514 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
515 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
516 TG3_64BIT_REG_LOW, val);
517 return;
518 }
Matt Carlson66711e62009-11-13 13:03:49 +0000519 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700520 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
521 TG3_64BIT_REG_LOW, val);
522 return;
523 }
524
525 spin_lock_irqsave(&tp->indirect_lock, flags);
526 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
527 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
528 spin_unlock_irqrestore(&tp->indirect_lock, flags);
529
530 /* In indirect mode when disabling interrupts, we also need
531 * to clear the interrupt bit in the GRC local ctrl register.
532 */
533 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
534 (val == 0x1)) {
535 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
536 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
537 }
538}
539
540static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
541{
542 unsigned long flags;
543 u32 val;
544
545 spin_lock_irqsave(&tp->indirect_lock, flags);
546 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
547 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
548 spin_unlock_irqrestore(&tp->indirect_lock, flags);
549 return val;
550}
551
Michael Chanb401e9e2005-12-19 16:27:04 -0800552/* usec_wait specifies the wait time in usec when writing to certain registers
553 * where it is unsafe to read back the register without some delay.
554 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
555 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
556 */
557static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Joe Perches63c3a662011-04-26 08:12:10 +0000559 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800560 /* Non-posted methods */
561 tp->write32(tp, off, val);
562 else {
563 /* Posted method */
564 tg3_write32(tp, off, val);
565 if (usec_wait)
566 udelay(usec_wait);
567 tp->read32(tp, off);
568 }
569 /* Wait again after the read for the posted method to guarantee that
570 * the wait time is met.
571 */
572 if (usec_wait)
573 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Michael Chan09ee9292005-08-09 20:17:00 -0700576static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
577{
578 tp->write32_mbox(tp, off, val);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000579 if (tg3_flag(tp, FLUSH_POSTED_WRITES) ||
580 (!tg3_flag(tp, MBOX_WRITE_REORDER) &&
581 !tg3_flag(tp, ICH_WORKAROUND)))
Michael Chan68929142005-08-09 20:17:14 -0700582 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700583}
584
Michael Chan20094932005-08-09 20:16:32 -0700585static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 void __iomem *mbox = tp->regs + off;
588 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000589 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 writel(val, mbox);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +0000591 if (tg3_flag(tp, MBOX_WRITE_REORDER) ||
592 tg3_flag(tp, FLUSH_POSTED_WRITES))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 readl(mbox);
594}
595
Michael Chanb5d37722006-09-27 16:06:21 -0700596static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
597{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000598 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700599}
600
601static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
602{
603 writel(val, tp->regs + off + GRCMBOX_BASE);
604}
605
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000606#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700607#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000608#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
609#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
610#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700611
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000612#define tw32(reg, val) tp->write32(tp, reg, val)
613#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
614#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
615#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
618{
Michael Chan68929142005-08-09 20:17:14 -0700619 unsigned long flags;
620
Joe Perches41535772013-02-16 11:20:04 +0000621 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700622 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
623 return;
624
Michael Chan68929142005-08-09 20:17:14 -0700625 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000626 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700627 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
628 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Michael Chanbbadf502006-04-06 21:46:34 -0700630 /* Always leave this as zero. */
631 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
632 } else {
633 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
634 tw32_f(TG3PCI_MEM_WIN_DATA, val);
635
636 /* Always leave this as zero. */
637 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
638 }
Michael Chan68929142005-08-09 20:17:14 -0700639 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
643{
Michael Chan68929142005-08-09 20:17:14 -0700644 unsigned long flags;
645
Joe Perches41535772013-02-16 11:20:04 +0000646 if (tg3_asic_rev(tp) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700647 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
648 *val = 0;
649 return;
650 }
651
Michael Chan68929142005-08-09 20:17:14 -0700652 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000653 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700654 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
655 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Michael Chanbbadf502006-04-06 21:46:34 -0700657 /* Always leave this as zero. */
658 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
659 } else {
660 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
661 *val = tr32(TG3PCI_MEM_WIN_DATA);
662
663 /* Always leave this as zero. */
664 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
665 }
Michael Chan68929142005-08-09 20:17:14 -0700666 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669static void tg3_ape_lock_init(struct tg3 *tp)
670{
671 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000672 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000673
Joe Perches41535772013-02-16 11:20:04 +0000674 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000675 regbase = TG3_APE_LOCK_GRANT;
676 else
677 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678
679 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000680 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
681 switch (i) {
682 case TG3_APE_LOCK_PHY0:
683 case TG3_APE_LOCK_PHY1:
684 case TG3_APE_LOCK_PHY2:
685 case TG3_APE_LOCK_PHY3:
686 bit = APE_LOCK_GRANT_DRIVER;
687 break;
688 default:
689 if (!tp->pci_fn)
690 bit = APE_LOCK_GRANT_DRIVER;
691 else
692 bit = 1 << tp->pci_fn;
693 }
694 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000695 }
696
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697}
698
699static int tg3_ape_lock(struct tg3 *tp, int locknum)
700{
701 int i, off;
702 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000703 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700704
Joe Perches63c3a662011-04-26 08:12:10 +0000705 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706 return 0;
707
708 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000709 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000710 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000712 case TG3_APE_LOCK_GRC:
713 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000714 if (!tp->pci_fn)
715 bit = APE_LOCK_REQ_DRIVER;
716 else
717 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000718 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000719 case TG3_APE_LOCK_PHY0:
720 case TG3_APE_LOCK_PHY1:
721 case TG3_APE_LOCK_PHY2:
722 case TG3_APE_LOCK_PHY3:
723 bit = APE_LOCK_REQ_DRIVER;
724 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000725 default:
726 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700727 }
728
Joe Perches41535772013-02-16 11:20:04 +0000729 if (tg3_asic_rev(tp) == ASIC_REV_5761) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000730 req = TG3_APE_LOCK_REQ;
731 gnt = TG3_APE_LOCK_GRANT;
732 } else {
733 req = TG3_APE_PER_LOCK_REQ;
734 gnt = TG3_APE_PER_LOCK_GRANT;
735 }
736
Matt Carlson0d3031d2007-10-10 18:02:43 -0700737 off = 4 * locknum;
738
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000739 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700740
741 /* Wait for up to 1 millisecond to acquire lock. */
742 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000743 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000744 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700745 break;
746 udelay(10);
747 }
748
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000749 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700750 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000751 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700752 ret = -EBUSY;
753 }
754
755 return ret;
756}
757
758static void tg3_ape_unlock(struct tg3 *tp, int locknum)
759{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000760 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700761
Joe Perches63c3a662011-04-26 08:12:10 +0000762 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700763 return;
764
765 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000766 case TG3_APE_LOCK_GPIO:
Joe Perches41535772013-02-16 11:20:04 +0000767 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000768 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000769 case TG3_APE_LOCK_GRC:
770 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000771 if (!tp->pci_fn)
772 bit = APE_LOCK_GRANT_DRIVER;
773 else
774 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000775 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000776 case TG3_APE_LOCK_PHY0:
777 case TG3_APE_LOCK_PHY1:
778 case TG3_APE_LOCK_PHY2:
779 case TG3_APE_LOCK_PHY3:
780 bit = APE_LOCK_GRANT_DRIVER;
781 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000782 default:
783 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700784 }
785
Joe Perches41535772013-02-16 11:20:04 +0000786 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000787 gnt = TG3_APE_LOCK_GRANT;
788 else
789 gnt = TG3_APE_PER_LOCK_GRANT;
790
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000791 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700792}
793
Matt Carlsonb65a3722012-07-16 16:24:00 +0000794static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000795{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000796 u32 apedata;
797
Matt Carlsonb65a3722012-07-16 16:24:00 +0000798 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000799 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000800 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000801
802 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000803 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
804 break;
805
Matt Carlsonb65a3722012-07-16 16:24:00 +0000806 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
807
808 udelay(10);
809 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000810 }
811
Matt Carlsonb65a3722012-07-16 16:24:00 +0000812 return timeout_us ? 0 : -EBUSY;
813}
814
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000815static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
816{
817 u32 i, apedata;
818
819 for (i = 0; i < timeout_us / 10; i++) {
820 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
821
822 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
823 break;
824
825 udelay(10);
826 }
827
828 return i == timeout_us / 10;
829}
830
Michael Chan86449942012-10-02 20:31:14 -0700831static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
832 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000833{
834 int err;
835 u32 i, bufoff, msgoff, maxlen, apedata;
836
837 if (!tg3_flag(tp, APE_HAS_NCSI))
838 return 0;
839
840 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
841 if (apedata != APE_SEG_SIG_MAGIC)
842 return -ENODEV;
843
844 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
845 if (!(apedata & APE_FW_STATUS_READY))
846 return -EAGAIN;
847
848 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
849 TG3_APE_SHMEM_BASE;
850 msgoff = bufoff + 2 * sizeof(u32);
851 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
852
853 while (len) {
854 u32 length;
855
856 /* Cap xfer sizes to scratchpad limits. */
857 length = (len > maxlen) ? maxlen : len;
858 len -= length;
859
860 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
861 if (!(apedata & APE_FW_STATUS_READY))
862 return -EAGAIN;
863
864 /* Wait for up to 1 msec for APE to service previous event. */
865 err = tg3_ape_event_lock(tp, 1000);
866 if (err)
867 return err;
868
869 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
870 APE_EVENT_STATUS_SCRTCHPD_READ |
871 APE_EVENT_STATUS_EVENT_PENDING;
872 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
873
874 tg3_ape_write32(tp, bufoff, base_off);
875 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
876
877 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
878 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
879
880 base_off += length;
881
882 if (tg3_ape_wait_for_event(tp, 30000))
883 return -EAGAIN;
884
885 for (i = 0; length; i += 4, length -= 4) {
886 u32 val = tg3_ape_read32(tp, msgoff + i);
887 memcpy(data, &val, sizeof(u32));
888 data++;
889 }
890 }
891
892 return 0;
893}
894
Matt Carlsonb65a3722012-07-16 16:24:00 +0000895static int tg3_ape_send_event(struct tg3 *tp, u32 event)
896{
897 int err;
898 u32 apedata;
899
900 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
901 if (apedata != APE_SEG_SIG_MAGIC)
902 return -EAGAIN;
903
904 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
905 if (!(apedata & APE_FW_STATUS_READY))
906 return -EAGAIN;
907
908 /* Wait for up to 1 millisecond for APE to service previous event. */
909 err = tg3_ape_event_lock(tp, 1000);
910 if (err)
911 return err;
912
913 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
914 event | APE_EVENT_STATUS_EVENT_PENDING);
915
916 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
917 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
918
919 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000920}
921
922static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
923{
924 u32 event;
925 u32 apedata;
926
927 if (!tg3_flag(tp, ENABLE_APE))
928 return;
929
930 switch (kind) {
931 case RESET_KIND_INIT:
932 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
933 APE_HOST_SEG_SIG_MAGIC);
934 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
935 APE_HOST_SEG_LEN_MAGIC);
936 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
937 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
938 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
939 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
940 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
941 APE_HOST_BEHAV_NO_PHYLOCK);
942 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
943 TG3_APE_HOST_DRVR_STATE_START);
944
945 event = APE_EVENT_STATUS_STATE_START;
946 break;
947 case RESET_KIND_SHUTDOWN:
948 /* With the interface we are currently using,
949 * APE does not track driver state. Wiping
950 * out the HOST SEGMENT SIGNATURE forces
951 * the APE to assume OS absent status.
952 */
953 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
954
955 if (device_may_wakeup(&tp->pdev->dev) &&
956 tg3_flag(tp, WOL_ENABLE)) {
957 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
958 TG3_APE_HOST_WOL_SPEED_AUTO);
959 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
960 } else
961 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
962
963 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
964
965 event = APE_EVENT_STATUS_STATE_UNLOAD;
966 break;
967 case RESET_KIND_SUSPEND:
968 event = APE_EVENT_STATUS_STATE_SUSPEND;
969 break;
970 default:
971 return;
972 }
973
974 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
975
976 tg3_ape_send_event(tp, event);
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979static void tg3_disable_ints(struct tg3 *tp)
980{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000981 int i;
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 tw32(TG3PCI_MISC_HOST_CTRL,
984 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000985 for (i = 0; i < tp->irq_max; i++)
986 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989static void tg3_enable_ints(struct tg3 *tp)
990{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000992
Michael Chanbbe832c2005-06-24 20:20:04 -0700993 tp->irq_sync = 0;
994 wmb();
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 tw32(TG3PCI_MISC_HOST_CTRL,
997 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000998
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000999 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001000 for (i = 0; i < tp->irq_cnt; i++) {
1001 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001002
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001003 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +00001004 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001005 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
1006
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001007 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +00001008 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001009
1010 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001011 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001012 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1013 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1014 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001015 tw32(HOSTCC_MODE, tp->coal_now);
1016
1017 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Matt Carlson17375d22009-08-28 14:02:18 +00001020static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001021{
Matt Carlson17375d22009-08-28 14:02:18 +00001022 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001023 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001024 unsigned int work_exists = 0;
1025
1026 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001027 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001028 if (sblk->status & SD_STATUS_LINK_CHG)
1029 work_exists = 1;
1030 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001031
1032 /* check for TX work to do */
1033 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1034 work_exists = 1;
1035
1036 /* check for RX work to do */
1037 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001038 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001039 work_exists = 1;
1040
1041 return work_exists;
1042}
1043
Matt Carlson17375d22009-08-28 14:02:18 +00001044/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001045 * similar to tg3_enable_ints, but it accurately determines whether there
1046 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001047 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Matt Carlson17375d22009-08-28 14:02:18 +00001049static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Matt Carlson17375d22009-08-28 14:02:18 +00001051 struct tg3 *tp = tnapi->tp;
1052
Matt Carlson898a56f2009-08-28 14:02:40 +00001053 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 mmiowb();
1055
David S. Millerfac9b832005-05-18 22:46:34 -07001056 /* When doing tagged status, this work check is unnecessary.
1057 * The last_tag we write above tells the chip which piece of
1058 * work we've completed.
1059 */
Joe Perches63c3a662011-04-26 08:12:10 +00001060 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001061 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001062 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065static void tg3_switch_clocks(struct tg3 *tp)
1066{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001067 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 orig_clock_ctrl;
1069
Joe Perches63c3a662011-04-26 08:12:10 +00001070 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001071 return;
1072
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001073 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 orig_clock_ctrl = clock_ctrl;
1076 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1077 CLOCK_CTRL_CLKRUN_OENABLE |
1078 0x1f);
1079 tp->pci_clock_ctrl = clock_ctrl;
1080
Joe Perches63c3a662011-04-26 08:12:10 +00001081 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001083 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1084 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001087 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1088 clock_ctrl |
1089 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1090 40);
1091 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1092 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1093 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001095 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
1098#define PHY_BUSY_LOOPS 5000
1099
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001100static int __tg3_readphy(struct tg3 *tp, unsigned int phy_addr, int reg,
1101 u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 u32 frame_val;
1104 unsigned int loops;
1105 int ret;
1106
1107 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1108 tw32_f(MAC_MI_MODE,
1109 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1110 udelay(80);
1111 }
1112
Michael Chan8151ad52012-07-29 19:15:41 +00001113 tg3_ape_lock(tp, tp->phy_ape_lock);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *val = 0x0;
1116
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001117 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 MI_COM_PHY_ADDR_MASK);
1119 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1120 MI_COM_REG_ADDR_MASK);
1121 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 tw32_f(MAC_MI_COM, frame_val);
1124
1125 loops = PHY_BUSY_LOOPS;
1126 while (loops != 0) {
1127 udelay(10);
1128 frame_val = tr32(MAC_MI_COM);
1129
1130 if ((frame_val & MI_COM_BUSY) == 0) {
1131 udelay(5);
1132 frame_val = tr32(MAC_MI_COM);
1133 break;
1134 }
1135 loops -= 1;
1136 }
1137
1138 ret = -EBUSY;
1139 if (loops != 0) {
1140 *val = frame_val & MI_COM_DATA_MASK;
1141 ret = 0;
1142 }
1143
1144 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1145 tw32_f(MAC_MI_MODE, tp->mi_mode);
1146 udelay(80);
1147 }
1148
Michael Chan8151ad52012-07-29 19:15:41 +00001149 tg3_ape_unlock(tp, tp->phy_ape_lock);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return ret;
1152}
1153
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001154static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1155{
1156 return __tg3_readphy(tp, tp->phy_addr, reg, val);
1157}
1158
1159static int __tg3_writephy(struct tg3 *tp, unsigned int phy_addr, int reg,
1160 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 u32 frame_val;
1163 unsigned int loops;
1164 int ret;
1165
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001166 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001167 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001168 return 0;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1171 tw32_f(MAC_MI_MODE,
1172 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1173 udelay(80);
1174 }
1175
Michael Chan8151ad52012-07-29 19:15:41 +00001176 tg3_ape_lock(tp, tp->phy_ape_lock);
1177
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001178 frame_val = ((phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 MI_COM_PHY_ADDR_MASK);
1180 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1181 MI_COM_REG_ADDR_MASK);
1182 frame_val |= (val & MI_COM_DATA_MASK);
1183 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 tw32_f(MAC_MI_COM, frame_val);
1186
1187 loops = PHY_BUSY_LOOPS;
1188 while (loops != 0) {
1189 udelay(10);
1190 frame_val = tr32(MAC_MI_COM);
1191 if ((frame_val & MI_COM_BUSY) == 0) {
1192 udelay(5);
1193 frame_val = tr32(MAC_MI_COM);
1194 break;
1195 }
1196 loops -= 1;
1197 }
1198
1199 ret = -EBUSY;
1200 if (loops != 0)
1201 ret = 0;
1202
1203 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1204 tw32_f(MAC_MI_MODE, tp->mi_mode);
1205 udelay(80);
1206 }
1207
Michael Chan8151ad52012-07-29 19:15:41 +00001208 tg3_ape_unlock(tp, tp->phy_ape_lock);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return ret;
1211}
1212
Hauke Mehrtens5c358042013-02-07 05:37:38 +00001213static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1214{
1215 return __tg3_writephy(tp, tp->phy_addr, reg, val);
1216}
1217
Matt Carlsonb0988c12011-04-20 07:57:39 +00001218static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1219{
1220 int err;
1221
1222 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1223 if (err)
1224 goto done;
1225
1226 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1227 if (err)
1228 goto done;
1229
1230 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1231 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1232 if (err)
1233 goto done;
1234
1235 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1236
1237done:
1238 return err;
1239}
1240
1241static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1242{
1243 int err;
1244
1245 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1246 if (err)
1247 goto done;
1248
1249 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1250 if (err)
1251 goto done;
1252
1253 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1254 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1255 if (err)
1256 goto done;
1257
1258 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1259
1260done:
1261 return err;
1262}
1263
1264static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1265{
1266 int err;
1267
1268 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1269 if (!err)
1270 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1271
1272 return err;
1273}
1274
1275static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1276{
1277 int err;
1278
1279 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1280 if (!err)
1281 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1282
1283 return err;
1284}
1285
Matt Carlson15ee95c2011-04-20 07:57:40 +00001286static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1287{
1288 int err;
1289
1290 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1291 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1292 MII_TG3_AUXCTL_SHDWSEL_MISC);
1293 if (!err)
1294 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1295
1296 return err;
1297}
1298
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001299static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1300{
1301 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1302 set |= MII_TG3_AUXCTL_MISC_WREN;
1303
1304 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1305}
1306
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001307static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1308{
1309 u32 val;
1310 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001311
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001312 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1313
1314 if (err)
1315 return err;
1316 if (enable)
1317
1318 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1319 else
1320 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1321
1322 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1323 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1324
1325 return err;
1326}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001327
Matt Carlson95e28692008-05-25 23:44:14 -07001328static int tg3_bmcr_reset(struct tg3 *tp)
1329{
1330 u32 phy_control;
1331 int limit, err;
1332
1333 /* OK, reset it, and poll the BMCR_RESET bit until it
1334 * clears or we time out.
1335 */
1336 phy_control = BMCR_RESET;
1337 err = tg3_writephy(tp, MII_BMCR, phy_control);
1338 if (err != 0)
1339 return -EBUSY;
1340
1341 limit = 5000;
1342 while (limit--) {
1343 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1344 if (err != 0)
1345 return -EBUSY;
1346
1347 if ((phy_control & BMCR_RESET) == 0) {
1348 udelay(40);
1349 break;
1350 }
1351 udelay(10);
1352 }
Roel Kluind4675b52009-02-12 16:33:27 -08001353 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001354 return -EBUSY;
1355
1356 return 0;
1357}
1358
Matt Carlson158d7ab2008-05-29 01:37:54 -07001359static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1360{
Francois Romieu3d165432009-01-19 16:56:50 -08001361 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001362 u32 val;
1363
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001364 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001365
1366 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001367 val = -EIO;
1368
1369 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001370
1371 return val;
1372}
1373
1374static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1375{
Francois Romieu3d165432009-01-19 16:56:50 -08001376 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001377 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001378
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001379 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001380
1381 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001382 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001383
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001384 spin_unlock_bh(&tp->lock);
1385
1386 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001387}
1388
1389static int tg3_mdio_reset(struct mii_bus *bp)
1390{
1391 return 0;
1392}
1393
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001394static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001395{
1396 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001397 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001398
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001399 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001400 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001401 case PHY_ID_BCM50610:
1402 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001403 val = MAC_PHYCFG2_50610_LED_MODES;
1404 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001405 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001406 val = MAC_PHYCFG2_AC131_LED_MODES;
1407 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001408 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001409 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1410 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001411 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001412 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1413 break;
1414 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001415 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001416 }
1417
1418 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1419 tw32(MAC_PHYCFG2, val);
1420
1421 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001422 val &= ~(MAC_PHYCFG1_RGMII_INT |
1423 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1424 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001425 tw32(MAC_PHYCFG1, val);
1426
1427 return;
1428 }
1429
Joe Perches63c3a662011-04-26 08:12:10 +00001430 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001431 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1432 MAC_PHYCFG2_FMODE_MASK_MASK |
1433 MAC_PHYCFG2_GMODE_MASK_MASK |
1434 MAC_PHYCFG2_ACT_MASK_MASK |
1435 MAC_PHYCFG2_QUAL_MASK_MASK |
1436 MAC_PHYCFG2_INBAND_ENABLE;
1437
1438 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001439
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001440 val = tr32(MAC_PHYCFG1);
1441 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1442 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001443 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1444 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001445 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001446 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001447 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1448 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001449 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1450 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1451 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001452
Matt Carlsona9daf362008-05-25 23:49:44 -07001453 val = tr32(MAC_EXT_RGMII_MODE);
1454 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1455 MAC_RGMII_MODE_RX_QUALITY |
1456 MAC_RGMII_MODE_RX_ACTIVITY |
1457 MAC_RGMII_MODE_RX_ENG_DET |
1458 MAC_RGMII_MODE_TX_ENABLE |
1459 MAC_RGMII_MODE_TX_LOWPWR |
1460 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1462 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001463 val |= MAC_RGMII_MODE_RX_INT_B |
1464 MAC_RGMII_MODE_RX_QUALITY |
1465 MAC_RGMII_MODE_RX_ACTIVITY |
1466 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001467 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001468 val |= MAC_RGMII_MODE_TX_ENABLE |
1469 MAC_RGMII_MODE_TX_LOWPWR |
1470 MAC_RGMII_MODE_TX_RESET;
1471 }
1472 tw32(MAC_EXT_RGMII_MODE, val);
1473}
1474
Matt Carlson158d7ab2008-05-29 01:37:54 -07001475static void tg3_mdio_start(struct tg3 *tp)
1476{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001477 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1478 tw32_f(MAC_MI_MODE, tp->mi_mode);
1479 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001480
Joe Perches63c3a662011-04-26 08:12:10 +00001481 if (tg3_flag(tp, MDIOBUS_INITED) &&
Joe Perches41535772013-02-16 11:20:04 +00001482 tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9ea48182010-02-17 15:17:01 +00001483 tg3_mdio_config_5785(tp);
1484}
1485
1486static int tg3_mdio_init(struct tg3 *tp)
1487{
1488 int i;
1489 u32 reg;
1490 struct phy_device *phydev;
1491
Joe Perches63c3a662011-04-26 08:12:10 +00001492 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001493 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001494
Matt Carlson69f11c92011-07-13 09:27:30 +00001495 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001496
Joe Perches41535772013-02-16 11:20:04 +00001497 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0)
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001498 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1499 else
1500 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1501 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001502 if (is_serdes)
1503 tp->phy_addr += 7;
1504 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001505 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001506
Matt Carlson158d7ab2008-05-29 01:37:54 -07001507 tg3_mdio_start(tp);
1508
Joe Perches63c3a662011-04-26 08:12:10 +00001509 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001510 return 0;
1511
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001512 tp->mdio_bus = mdiobus_alloc();
1513 if (tp->mdio_bus == NULL)
1514 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001515
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001516 tp->mdio_bus->name = "tg3 mdio bus";
1517 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001518 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001519 tp->mdio_bus->priv = tp;
1520 tp->mdio_bus->parent = &tp->pdev->dev;
1521 tp->mdio_bus->read = &tg3_mdio_read;
1522 tp->mdio_bus->write = &tg3_mdio_write;
1523 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001524 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001525 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001526
1527 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001528 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001529
1530 /* The bus registration will look for all the PHYs on the mdio bus.
1531 * Unfortunately, it does not ensure the PHY is powered up before
1532 * accessing the PHY ID registers. A chip reset is the
1533 * quickest way to bring the device back to an operational state..
1534 */
1535 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1536 tg3_bmcr_reset(tp);
1537
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001538 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001539 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001540 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001541 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001542 return i;
1543 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001544
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001545 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001546
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001547 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001548 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001549 mdiobus_unregister(tp->mdio_bus);
1550 mdiobus_free(tp->mdio_bus);
1551 return -ENODEV;
1552 }
1553
1554 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001555 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001556 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001557 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001558 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001559 case PHY_ID_BCM50610:
1560 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001561 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001562 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001563 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001564 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001565 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001566 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001567 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001568 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001569 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001570 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001571 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001572 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001573 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001574 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001575 case PHY_ID_RTL8201E:
1576 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001577 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001578 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001579 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001580 break;
1581 }
1582
Joe Perches63c3a662011-04-26 08:12:10 +00001583 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001584
Joe Perches41535772013-02-16 11:20:04 +00001585 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001586 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001587
1588 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001589}
1590
1591static void tg3_mdio_fini(struct tg3 *tp)
1592{
Joe Perches63c3a662011-04-26 08:12:10 +00001593 if (tg3_flag(tp, MDIOBUS_INITED)) {
1594 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001595 mdiobus_unregister(tp->mdio_bus);
1596 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001597 }
1598}
1599
Matt Carlson95e28692008-05-25 23:44:14 -07001600/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001601static inline void tg3_generate_fw_event(struct tg3 *tp)
1602{
1603 u32 val;
1604
1605 val = tr32(GRC_RX_CPU_EVENT);
1606 val |= GRC_RX_CPU_DRIVER_EVENT;
1607 tw32_f(GRC_RX_CPU_EVENT, val);
1608
1609 tp->last_event_jiffies = jiffies;
1610}
1611
1612#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1613
1614/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001615static void tg3_wait_for_event_ack(struct tg3 *tp)
1616{
1617 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001618 unsigned int delay_cnt;
1619 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001620
Matt Carlson4ba526c2008-08-15 14:10:04 -07001621 /* If enough time has passed, no wait is necessary. */
1622 time_remain = (long)(tp->last_event_jiffies + 1 +
1623 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1624 (long)jiffies;
1625 if (time_remain < 0)
1626 return;
1627
1628 /* Check if we can shorten the wait time. */
1629 delay_cnt = jiffies_to_usecs(time_remain);
1630 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1631 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1632 delay_cnt = (delay_cnt >> 3) + 1;
1633
1634 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001635 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1636 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001637 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001638 }
1639}
1640
1641/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001642static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001643{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001644 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001645
1646 val = 0;
1647 if (!tg3_readphy(tp, MII_BMCR, &reg))
1648 val = reg << 16;
1649 if (!tg3_readphy(tp, MII_BMSR, &reg))
1650 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001651 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001652
1653 val = 0;
1654 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1655 val = reg << 16;
1656 if (!tg3_readphy(tp, MII_LPA, &reg))
1657 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001658 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001659
1660 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001661 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001662 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1663 val = reg << 16;
1664 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1665 val |= (reg & 0xffff);
1666 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001667 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001668
1669 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1670 val = reg << 16;
1671 else
1672 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001673 *data++ = val;
1674}
1675
1676/* tp->lock is held. */
1677static void tg3_ump_link_report(struct tg3 *tp)
1678{
1679 u32 data[4];
1680
1681 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1682 return;
1683
1684 tg3_phy_gather_ump_data(tp, data);
1685
1686 tg3_wait_for_event_ack(tp);
1687
1688 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1689 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1690 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1691 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1692 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1693 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001694
Matt Carlson4ba526c2008-08-15 14:10:04 -07001695 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001696}
1697
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001698/* tp->lock is held. */
1699static void tg3_stop_fw(struct tg3 *tp)
1700{
1701 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1702 /* Wait for RX cpu to ACK the previous event. */
1703 tg3_wait_for_event_ack(tp);
1704
1705 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1706
1707 tg3_generate_fw_event(tp);
1708
1709 /* Wait for RX cpu to ACK this event. */
1710 tg3_wait_for_event_ack(tp);
1711 }
1712}
1713
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001714/* tp->lock is held. */
1715static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1716{
1717 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1718 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1719
1720 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1721 switch (kind) {
1722 case RESET_KIND_INIT:
1723 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1724 DRV_STATE_START);
1725 break;
1726
1727 case RESET_KIND_SHUTDOWN:
1728 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1729 DRV_STATE_UNLOAD);
1730 break;
1731
1732 case RESET_KIND_SUSPEND:
1733 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1734 DRV_STATE_SUSPEND);
1735 break;
1736
1737 default:
1738 break;
1739 }
1740 }
1741
1742 if (kind == RESET_KIND_INIT ||
1743 kind == RESET_KIND_SUSPEND)
1744 tg3_ape_driver_state_change(tp, kind);
1745}
1746
1747/* tp->lock is held. */
1748static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1749{
1750 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1751 switch (kind) {
1752 case RESET_KIND_INIT:
1753 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1754 DRV_STATE_START_DONE);
1755 break;
1756
1757 case RESET_KIND_SHUTDOWN:
1758 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1759 DRV_STATE_UNLOAD_DONE);
1760 break;
1761
1762 default:
1763 break;
1764 }
1765 }
1766
1767 if (kind == RESET_KIND_SHUTDOWN)
1768 tg3_ape_driver_state_change(tp, kind);
1769}
1770
1771/* tp->lock is held. */
1772static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1773{
1774 if (tg3_flag(tp, ENABLE_ASF)) {
1775 switch (kind) {
1776 case RESET_KIND_INIT:
1777 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1778 DRV_STATE_START);
1779 break;
1780
1781 case RESET_KIND_SHUTDOWN:
1782 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1783 DRV_STATE_UNLOAD);
1784 break;
1785
1786 case RESET_KIND_SUSPEND:
1787 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1788 DRV_STATE_SUSPEND);
1789 break;
1790
1791 default:
1792 break;
1793 }
1794 }
1795}
1796
1797static int tg3_poll_fw(struct tg3 *tp)
1798{
1799 int i;
1800 u32 val;
1801
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00001802 if (tg3_flag(tp, IS_SSB_CORE)) {
1803 /* We don't use firmware. */
1804 return 0;
1805 }
1806
Joe Perches41535772013-02-16 11:20:04 +00001807 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001808 /* Wait up to 20ms for init done. */
1809 for (i = 0; i < 200; i++) {
1810 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1811 return 0;
1812 udelay(100);
1813 }
1814 return -ENODEV;
1815 }
1816
1817 /* Wait for firmware initialization to complete. */
1818 for (i = 0; i < 100000; i++) {
1819 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1820 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1821 break;
1822 udelay(10);
1823 }
1824
1825 /* Chip might not be fitted with firmware. Some Sun onboard
1826 * parts are configured like that. So don't signal the timeout
1827 * of the above loop as an error, but do report the lack of
1828 * running firmware once.
1829 */
1830 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1831 tg3_flag_set(tp, NO_FWARE_REPORTED);
1832
1833 netdev_info(tp->dev, "No firmware running\n");
1834 }
1835
Joe Perches41535772013-02-16 11:20:04 +00001836 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001837 /* The 57765 A0 needs a little more
1838 * time to do some important work.
1839 */
1840 mdelay(10);
1841 }
1842
1843 return 0;
1844}
1845
Matt Carlson95e28692008-05-25 23:44:14 -07001846static void tg3_link_report(struct tg3 *tp)
1847{
1848 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001849 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001850 tg3_ump_link_report(tp);
1851 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001852 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1853 (tp->link_config.active_speed == SPEED_1000 ?
1854 1000 :
1855 (tp->link_config.active_speed == SPEED_100 ?
1856 100 : 10)),
1857 (tp->link_config.active_duplex == DUPLEX_FULL ?
1858 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001859
Joe Perches05dbe002010-02-17 19:44:19 +00001860 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1861 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1862 "on" : "off",
1863 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1864 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001865
1866 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1867 netdev_info(tp->dev, "EEE is %s\n",
1868 tp->setlpicnt ? "enabled" : "disabled");
1869
Matt Carlson95e28692008-05-25 23:44:14 -07001870 tg3_ump_link_report(tp);
1871 }
Nithin Sujir84421b92013-03-08 08:01:24 +00001872
1873 tp->link_up = netif_carrier_ok(tp->dev);
Matt Carlson95e28692008-05-25 23:44:14 -07001874}
1875
Matt Carlson95e28692008-05-25 23:44:14 -07001876static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1877{
1878 u16 miireg;
1879
Steve Glendinninge18ce342008-12-16 02:00:00 -08001880 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001881 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001882 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001883 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001884 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001885 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1886 else
1887 miireg = 0;
1888
1889 return miireg;
1890}
1891
Matt Carlson95e28692008-05-25 23:44:14 -07001892static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1893{
1894 u8 cap = 0;
1895
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001896 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1897 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1898 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1899 if (lcladv & ADVERTISE_1000XPAUSE)
1900 cap = FLOW_CTRL_RX;
1901 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001902 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001903 }
1904
1905 return cap;
1906}
1907
Matt Carlsonf51f3562008-05-25 23:45:08 -07001908static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001909{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001910 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001911 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001912 u32 old_rx_mode = tp->rx_mode;
1913 u32 old_tx_mode = tp->tx_mode;
1914
Joe Perches63c3a662011-04-26 08:12:10 +00001915 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001916 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001917 else
1918 autoneg = tp->link_config.autoneg;
1919
Joe Perches63c3a662011-04-26 08:12:10 +00001920 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001921 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001922 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001923 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001924 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001925 } else
1926 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001927
Matt Carlsonf51f3562008-05-25 23:45:08 -07001928 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001929
Steve Glendinninge18ce342008-12-16 02:00:00 -08001930 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001931 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1932 else
1933 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1934
Matt Carlsonf51f3562008-05-25 23:45:08 -07001935 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001936 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001937
Steve Glendinninge18ce342008-12-16 02:00:00 -08001938 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001939 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1940 else
1941 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1942
Matt Carlsonf51f3562008-05-25 23:45:08 -07001943 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001944 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001945}
1946
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001947static void tg3_adjust_link(struct net_device *dev)
1948{
1949 u8 oldflowctrl, linkmesg = 0;
1950 u32 mac_mode, lcl_adv, rmt_adv;
1951 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001952 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001953
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001954 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001955
1956 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1957 MAC_MODE_HALF_DUPLEX);
1958
1959 oldflowctrl = tp->link_config.active_flowctrl;
1960
1961 if (phydev->link) {
1962 lcl_adv = 0;
1963 rmt_adv = 0;
1964
1965 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1966 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001967 else if (phydev->speed == SPEED_1000 ||
Joe Perches41535772013-02-16 11:20:04 +00001968 tg3_asic_rev(tp) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001969 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001970 else
1971 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001972
1973 if (phydev->duplex == DUPLEX_HALF)
1974 mac_mode |= MAC_MODE_HALF_DUPLEX;
1975 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001976 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001977 tp->link_config.flowctrl);
1978
1979 if (phydev->pause)
1980 rmt_adv = LPA_PAUSE_CAP;
1981 if (phydev->asym_pause)
1982 rmt_adv |= LPA_PAUSE_ASYM;
1983 }
1984
1985 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1986 } else
1987 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1988
1989 if (mac_mode != tp->mac_mode) {
1990 tp->mac_mode = mac_mode;
1991 tw32_f(MAC_MODE, tp->mac_mode);
1992 udelay(40);
1993 }
1994
Joe Perches41535772013-02-16 11:20:04 +00001995 if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001996 if (phydev->speed == SPEED_10)
1997 tw32(MAC_MI_STAT,
1998 MAC_MI_STAT_10MBPS_MODE |
1999 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2000 else
2001 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2002 }
2003
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002004 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
2005 tw32(MAC_TX_LENGTHS,
2006 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2007 (6 << TX_LENGTHS_IPG_SHIFT) |
2008 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
2009 else
2010 tw32(MAC_TX_LENGTHS,
2011 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2012 (6 << TX_LENGTHS_IPG_SHIFT) |
2013 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
2014
Matt Carlson34655ad2012-02-22 12:35:18 +00002015 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002016 phydev->speed != tp->link_config.active_speed ||
2017 phydev->duplex != tp->link_config.active_duplex ||
2018 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002019 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002020
Matt Carlson34655ad2012-02-22 12:35:18 +00002021 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002022 tp->link_config.active_speed = phydev->speed;
2023 tp->link_config.active_duplex = phydev->duplex;
2024
Matt Carlson24bb4fb2009-10-05 17:55:29 +00002025 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002026
2027 if (linkmesg)
2028 tg3_link_report(tp);
2029}
2030
2031static int tg3_phy_init(struct tg3 *tp)
2032{
2033 struct phy_device *phydev;
2034
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002035 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002036 return 0;
2037
2038 /* Bring the PHY back to a known state. */
2039 tg3_bmcr_reset(tp);
2040
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002041 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002042
2043 /* Attach the MAC to the PHY. */
Florian Fainellif9a8f832013-01-14 00:52:52 +00002044 phydev = phy_connect(tp->dev, dev_name(&phydev->dev),
2045 tg3_adjust_link, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002046 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002047 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002048 return PTR_ERR(phydev);
2049 }
2050
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002051 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002052 switch (phydev->interface) {
2053 case PHY_INTERFACE_MODE_GMII:
2054 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002055 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002056 phydev->supported &= (PHY_GBIT_FEATURES |
2057 SUPPORTED_Pause |
2058 SUPPORTED_Asym_Pause);
2059 break;
2060 }
2061 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002062 case PHY_INTERFACE_MODE_MII:
2063 phydev->supported &= (PHY_BASIC_FEATURES |
2064 SUPPORTED_Pause |
2065 SUPPORTED_Asym_Pause);
2066 break;
2067 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002068 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002069 return -EINVAL;
2070 }
2071
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002072 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002073
2074 phydev->advertising = phydev->supported;
2075
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002076 return 0;
2077}
2078
2079static void tg3_phy_start(struct tg3 *tp)
2080{
2081 struct phy_device *phydev;
2082
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002083 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002084 return;
2085
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002086 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002087
Matt Carlson80096062010-08-02 11:26:06 +00002088 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2089 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002090 phydev->speed = tp->link_config.speed;
2091 phydev->duplex = tp->link_config.duplex;
2092 phydev->autoneg = tp->link_config.autoneg;
2093 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002094 }
2095
2096 phy_start(phydev);
2097
2098 phy_start_aneg(phydev);
2099}
2100
2101static void tg3_phy_stop(struct tg3 *tp)
2102{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002103 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002104 return;
2105
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002106 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002107}
2108
2109static void tg3_phy_fini(struct tg3 *tp)
2110{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002111 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002112 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002113 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002114 }
2115}
2116
Matt Carlson941ec902011-08-19 13:58:23 +00002117static int tg3_phy_set_extloopbk(struct tg3 *tp)
2118{
2119 int err;
2120 u32 val;
2121
2122 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2123 return 0;
2124
2125 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2126 /* Cannot do read-modify-write on 5401 */
2127 err = tg3_phy_auxctl_write(tp,
2128 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2129 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2130 0x4c20);
2131 goto done;
2132 }
2133
2134 err = tg3_phy_auxctl_read(tp,
2135 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2136 if (err)
2137 return err;
2138
2139 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2140 err = tg3_phy_auxctl_write(tp,
2141 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2142
2143done:
2144 return err;
2145}
2146
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002147static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2148{
2149 u32 phytest;
2150
2151 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2152 u32 phy;
2153
2154 tg3_writephy(tp, MII_TG3_FET_TEST,
2155 phytest | MII_TG3_FET_SHADOW_EN);
2156 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2157 if (enable)
2158 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2159 else
2160 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2161 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2162 }
2163 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2164 }
2165}
2166
Matt Carlson6833c042008-11-21 17:18:59 -08002167static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2168{
2169 u32 reg;
2170
Joe Perches63c3a662011-04-26 08:12:10 +00002171 if (!tg3_flag(tp, 5705_PLUS) ||
2172 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002173 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002174 return;
2175
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002176 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002177 tg3_phy_fet_toggle_apd(tp, enable);
2178 return;
2179 }
2180
Matt Carlson6833c042008-11-21 17:18:59 -08002181 reg = MII_TG3_MISC_SHDW_WREN |
2182 MII_TG3_MISC_SHDW_SCR5_SEL |
2183 MII_TG3_MISC_SHDW_SCR5_LPED |
2184 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2185 MII_TG3_MISC_SHDW_SCR5_SDTL |
2186 MII_TG3_MISC_SHDW_SCR5_C125OE;
Joe Perches41535772013-02-16 11:20:04 +00002187 if (tg3_asic_rev(tp) != ASIC_REV_5784 || !enable)
Matt Carlson6833c042008-11-21 17:18:59 -08002188 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2189
2190 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2191
2192
2193 reg = MII_TG3_MISC_SHDW_WREN |
2194 MII_TG3_MISC_SHDW_APD_SEL |
2195 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2196 if (enable)
2197 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2198
2199 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2200}
2201
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002202static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2203{
2204 u32 phy;
2205
Joe Perches63c3a662011-04-26 08:12:10 +00002206 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002207 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002208 return;
2209
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002210 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002211 u32 ephy;
2212
Matt Carlson535ef6e2009-08-25 10:09:36 +00002213 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2214 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2215
2216 tg3_writephy(tp, MII_TG3_FET_TEST,
2217 ephy | MII_TG3_FET_SHADOW_EN);
2218 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002219 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002220 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002221 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002222 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2223 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002224 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002225 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002226 }
2227 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002228 int ret;
2229
2230 ret = tg3_phy_auxctl_read(tp,
2231 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2232 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002233 if (enable)
2234 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2235 else
2236 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002237 tg3_phy_auxctl_write(tp,
2238 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002239 }
2240 }
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243static void tg3_phy_set_wirespeed(struct tg3 *tp)
2244{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002245 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 u32 val;
2247
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002248 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return;
2250
Matt Carlson15ee95c2011-04-20 07:57:40 +00002251 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2252 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002253 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2254 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002257static void tg3_phy_apply_otp(struct tg3 *tp)
2258{
2259 u32 otp, phy;
2260
2261 if (!tp->phy_otp)
2262 return;
2263
2264 otp = tp->phy_otp;
2265
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002266 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002267 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002268
2269 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2270 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2271 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2272
2273 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2274 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2275 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2276
2277 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2278 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2279 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2280
2281 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2282 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2283
2284 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2285 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2286
2287 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2288 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2289 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2290
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002291 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002292}
2293
Matt Carlson52b02d02010-10-14 10:37:41 +00002294static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2295{
2296 u32 val;
2297
2298 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2299 return;
2300
2301 tp->setlpicnt = 0;
2302
2303 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2304 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002305 tp->link_config.active_duplex == DUPLEX_FULL &&
2306 (tp->link_config.active_speed == SPEED_100 ||
2307 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002308 u32 eeectl;
2309
2310 if (tp->link_config.active_speed == SPEED_1000)
2311 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2312 else
2313 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2314
2315 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2316
Matt Carlson3110f5f52010-12-06 08:28:50 +00002317 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2318 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002319
Matt Carlsonb0c59432011-05-19 12:12:48 +00002320 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2321 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002322 tp->setlpicnt = 2;
2323 }
2324
2325 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002326 if (current_link_up == 1 &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002327 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002328 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002329 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002330 }
2331
Matt Carlson52b02d02010-10-14 10:37:41 +00002332 val = tr32(TG3_CPMU_EEE_MODE);
2333 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2334 }
2335}
2336
Matt Carlsonb0c59432011-05-19 12:12:48 +00002337static void tg3_phy_eee_enable(struct tg3 *tp)
2338{
2339 u32 val;
2340
2341 if (tp->link_config.active_speed == SPEED_1000 &&
Joe Perches41535772013-02-16 11:20:04 +00002342 (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2343 tg3_asic_rev(tp) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002344 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002345 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002346 val = MII_TG3_DSP_TAP26_ALNOKO |
2347 MII_TG3_DSP_TAP26_RMRXSTO;
2348 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002349 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002350 }
2351
2352 val = tr32(TG3_CPMU_EEE_MODE);
2353 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356static int tg3_wait_macro_done(struct tg3 *tp)
2357{
2358 int limit = 100;
2359
2360 while (limit--) {
2361 u32 tmp32;
2362
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002363 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if ((tmp32 & 0x1000) == 0)
2365 break;
2366 }
2367 }
Roel Kluind4675b52009-02-12 16:33:27 -08002368 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return -EBUSY;
2370
2371 return 0;
2372}
2373
2374static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2375{
2376 static const u32 test_pat[4][6] = {
2377 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2378 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2379 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2380 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2381 };
2382 int chan;
2383
2384 for (chan = 0; chan < 4; chan++) {
2385 int i;
2386
2387 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2388 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002389 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 for (i = 0; i < 6; i++)
2392 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2393 test_pat[chan][i]);
2394
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002395 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (tg3_wait_macro_done(tp)) {
2397 *resetp = 1;
2398 return -EBUSY;
2399 }
2400
2401 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2402 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002403 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (tg3_wait_macro_done(tp)) {
2405 *resetp = 1;
2406 return -EBUSY;
2407 }
2408
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002409 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (tg3_wait_macro_done(tp)) {
2411 *resetp = 1;
2412 return -EBUSY;
2413 }
2414
2415 for (i = 0; i < 6; i += 2) {
2416 u32 low, high;
2417
2418 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2419 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2420 tg3_wait_macro_done(tp)) {
2421 *resetp = 1;
2422 return -EBUSY;
2423 }
2424 low &= 0x7fff;
2425 high &= 0x000f;
2426 if (low != test_pat[chan][i] ||
2427 high != test_pat[chan][i+1]) {
2428 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2429 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2430 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2431
2432 return -EBUSY;
2433 }
2434 }
2435 }
2436
2437 return 0;
2438}
2439
2440static int tg3_phy_reset_chanpat(struct tg3 *tp)
2441{
2442 int chan;
2443
2444 for (chan = 0; chan < 4; chan++) {
2445 int i;
2446
2447 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2448 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002449 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 for (i = 0; i < 6; i++)
2451 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002452 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (tg3_wait_macro_done(tp))
2454 return -EBUSY;
2455 }
2456
2457 return 0;
2458}
2459
2460static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2461{
2462 u32 reg32, phy9_orig;
2463 int retries, do_phy_reset, err;
2464
2465 retries = 10;
2466 do_phy_reset = 1;
2467 do {
2468 if (do_phy_reset) {
2469 err = tg3_bmcr_reset(tp);
2470 if (err)
2471 return err;
2472 do_phy_reset = 0;
2473 }
2474
2475 /* Disable transmitter and interrupt. */
2476 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2477 continue;
2478
2479 reg32 |= 0x3000;
2480 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2481
2482 /* Set full-duplex, 1000 mbps. */
2483 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002484 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002487 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 continue;
2489
Matt Carlson221c5632011-06-13 13:39:01 +00002490 tg3_writephy(tp, MII_CTRL1000,
2491 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002493 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002494 if (err)
2495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002498 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2501 if (!err)
2502 break;
2503 } while (--retries);
2504
2505 err = tg3_phy_reset_chanpat(tp);
2506 if (err)
2507 return err;
2508
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002509 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002512 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002514 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Matt Carlson221c5632011-06-13 13:39:01 +00002516 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2519 reg32 &= ~0x3000;
2520 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2521 } else if (!err)
2522 err = -EBUSY;
2523
2524 return err;
2525}
2526
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002527static void tg3_carrier_off(struct tg3 *tp)
2528{
2529 netif_carrier_off(tp->dev);
2530 tp->link_up = false;
2531}
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/* This will reset the tigon3 PHY if there is no valid
2534 * link unless the FORCE argument is non-zero.
2535 */
2536static int tg3_phy_reset(struct tg3 *tp)
2537{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002538 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 int err;
2540
Joe Perches41535772013-02-16 11:20:04 +00002541 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002542 val = tr32(GRC_MISC_CFG);
2543 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2544 udelay(40);
2545 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002546 err = tg3_readphy(tp, MII_BMSR, &val);
2547 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (err != 0)
2549 return -EBUSY;
2550
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002551 if (netif_running(tp->dev) && tp->link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00002552 netif_carrier_off(tp->dev);
Michael Chanc8e1e822006-04-29 18:55:17 -07002553 tg3_link_report(tp);
2554 }
2555
Joe Perches41535772013-02-16 11:20:04 +00002556 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
2557 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2558 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 err = tg3_phy_reset_5703_4_5(tp);
2560 if (err)
2561 return err;
2562 goto out;
2563 }
2564
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002565 cpmuctrl = 0;
Joe Perches41535772013-02-16 11:20:04 +00002566 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
2567 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002568 cpmuctrl = tr32(TG3_CPMU_CTRL);
2569 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2570 tw32(TG3_CPMU_CTRL,
2571 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2572 }
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 err = tg3_bmcr_reset(tp);
2575 if (err)
2576 return err;
2577
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002578 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002579 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2580 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002581
2582 tw32(TG3_CPMU_CTRL, cpmuctrl);
2583 }
2584
Joe Perches41535772013-02-16 11:20:04 +00002585 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2586 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002587 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2588 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2589 CPMU_LSPD_1000MB_MACCLK_12_5) {
2590 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2591 udelay(40);
2592 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2593 }
2594 }
2595
Joe Perches63c3a662011-04-26 08:12:10 +00002596 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002597 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002598 return 0;
2599
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002600 tg3_phy_apply_otp(tp);
2601
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002602 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002603 tg3_phy_toggle_apd(tp, true);
2604 else
2605 tg3_phy_toggle_apd(tp, false);
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002608 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002609 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002610 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2611 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002612 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002614
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002615 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002616 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2617 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002619
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002620 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002621 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002622 tg3_phydsp_write(tp, 0x000a, 0x310b);
2623 tg3_phydsp_write(tp, 0x201f, 0x9506);
2624 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002625 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002626 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002627 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002628 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002629 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2630 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2631 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2632 tg3_writephy(tp, MII_TG3_TEST1,
2633 MII_TG3_TEST1_TRIM_EN | 0x4);
2634 } else
2635 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2636
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002637 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002638 }
Michael Chanc424cb22006-04-29 18:56:34 -07002639 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /* Set Extended packet length bit (bit 14) on all chips that */
2642 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002643 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002645 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002646 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002648 err = tg3_phy_auxctl_read(tp,
2649 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2650 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002651 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2652 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
2655 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2656 * jumbo frames transmission.
2657 */
Joe Perches63c3a662011-04-26 08:12:10 +00002658 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002659 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002660 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002661 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663
Joe Perches41535772013-02-16 11:20:04 +00002664 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002665 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002666 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002667 }
2668
Joe Perches41535772013-02-16 11:20:04 +00002669 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5762_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00002670 tg3_phydsp_write(tp, 0xffb, 0x4000);
2671
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002672 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 tg3_phy_set_wirespeed(tp);
2674 return 0;
2675}
2676
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002677#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2678#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2679#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2680 TG3_GPIO_MSG_NEED_VAUX)
2681#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2682 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2683 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2684 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2685 (TG3_GPIO_MSG_DRVR_PRES << 12))
2686
2687#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2688 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2689 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2690 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2691 (TG3_GPIO_MSG_NEED_VAUX << 12))
2692
2693static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2694{
2695 u32 status, shift;
2696
Joe Perches41535772013-02-16 11:20:04 +00002697 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2698 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002699 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2700 else
2701 status = tr32(TG3_CPMU_DRV_STATUS);
2702
2703 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2704 status &= ~(TG3_GPIO_MSG_MASK << shift);
2705 status |= (newstat << shift);
2706
Joe Perches41535772013-02-16 11:20:04 +00002707 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2708 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002709 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2710 else
2711 tw32(TG3_CPMU_DRV_STATUS, status);
2712
2713 return status >> TG3_APE_GPIO_MSG_SHIFT;
2714}
2715
Matt Carlson520b2752011-06-13 13:39:02 +00002716static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2717{
2718 if (!tg3_flag(tp, IS_NIC))
2719 return 0;
2720
Joe Perches41535772013-02-16 11:20:04 +00002721 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2722 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2723 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002724 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2725 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002726
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002727 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2728
2729 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2730 TG3_GRC_LCLCTL_PWRSW_DELAY);
2731
2732 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2733 } else {
2734 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2735 TG3_GRC_LCLCTL_PWRSW_DELAY);
2736 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002737
Matt Carlson520b2752011-06-13 13:39:02 +00002738 return 0;
2739}
2740
2741static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2742{
2743 u32 grc_local_ctrl;
2744
2745 if (!tg3_flag(tp, IS_NIC) ||
Joe Perches41535772013-02-16 11:20:04 +00002746 tg3_asic_rev(tp) == ASIC_REV_5700 ||
2747 tg3_asic_rev(tp) == ASIC_REV_5701)
Matt Carlson520b2752011-06-13 13:39:02 +00002748 return;
2749
2750 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2751
2752 tw32_wait_f(GRC_LOCAL_CTRL,
2753 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2754 TG3_GRC_LCLCTL_PWRSW_DELAY);
2755
2756 tw32_wait_f(GRC_LOCAL_CTRL,
2757 grc_local_ctrl,
2758 TG3_GRC_LCLCTL_PWRSW_DELAY);
2759
2760 tw32_wait_f(GRC_LOCAL_CTRL,
2761 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2762 TG3_GRC_LCLCTL_PWRSW_DELAY);
2763}
2764
2765static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2766{
2767 if (!tg3_flag(tp, IS_NIC))
2768 return;
2769
Joe Perches41535772013-02-16 11:20:04 +00002770 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2771 tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson520b2752011-06-13 13:39:02 +00002772 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2773 (GRC_LCLCTRL_GPIO_OE0 |
2774 GRC_LCLCTRL_GPIO_OE1 |
2775 GRC_LCLCTRL_GPIO_OE2 |
2776 GRC_LCLCTRL_GPIO_OUTPUT0 |
2777 GRC_LCLCTRL_GPIO_OUTPUT1),
2778 TG3_GRC_LCLCTL_PWRSW_DELAY);
2779 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2780 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2781 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2782 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2783 GRC_LCLCTRL_GPIO_OE1 |
2784 GRC_LCLCTRL_GPIO_OE2 |
2785 GRC_LCLCTRL_GPIO_OUTPUT0 |
2786 GRC_LCLCTRL_GPIO_OUTPUT1 |
2787 tp->grc_local_ctrl;
2788 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2789 TG3_GRC_LCLCTL_PWRSW_DELAY);
2790
2791 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2792 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2793 TG3_GRC_LCLCTL_PWRSW_DELAY);
2794
2795 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2796 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2797 TG3_GRC_LCLCTL_PWRSW_DELAY);
2798 } else {
2799 u32 no_gpio2;
2800 u32 grc_local_ctrl = 0;
2801
2802 /* Workaround to prevent overdrawing Amps. */
Joe Perches41535772013-02-16 11:20:04 +00002803 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Matt Carlson520b2752011-06-13 13:39:02 +00002804 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2805 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2806 grc_local_ctrl,
2807 TG3_GRC_LCLCTL_PWRSW_DELAY);
2808 }
2809
2810 /* On 5753 and variants, GPIO2 cannot be used. */
2811 no_gpio2 = tp->nic_sram_data_cfg &
2812 NIC_SRAM_DATA_CFG_NO_GPIO2;
2813
2814 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2815 GRC_LCLCTRL_GPIO_OE1 |
2816 GRC_LCLCTRL_GPIO_OE2 |
2817 GRC_LCLCTRL_GPIO_OUTPUT1 |
2818 GRC_LCLCTRL_GPIO_OUTPUT2;
2819 if (no_gpio2) {
2820 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2821 GRC_LCLCTRL_GPIO_OUTPUT2);
2822 }
2823 tw32_wait_f(GRC_LOCAL_CTRL,
2824 tp->grc_local_ctrl | grc_local_ctrl,
2825 TG3_GRC_LCLCTL_PWRSW_DELAY);
2826
2827 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2828
2829 tw32_wait_f(GRC_LOCAL_CTRL,
2830 tp->grc_local_ctrl | grc_local_ctrl,
2831 TG3_GRC_LCLCTL_PWRSW_DELAY);
2832
2833 if (!no_gpio2) {
2834 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2835 tw32_wait_f(GRC_LOCAL_CTRL,
2836 tp->grc_local_ctrl | grc_local_ctrl,
2837 TG3_GRC_LCLCTL_PWRSW_DELAY);
2838 }
2839 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002840}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002841
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002842static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002843{
2844 u32 msg = 0;
2845
2846 /* Serialize power state transitions */
2847 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2848 return;
2849
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002850 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002851 msg = TG3_GPIO_MSG_NEED_VAUX;
2852
2853 msg = tg3_set_function_status(tp, msg);
2854
2855 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2856 goto done;
2857
2858 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2859 tg3_pwrsrc_switch_to_vaux(tp);
2860 else
2861 tg3_pwrsrc_die_with_vmain(tp);
2862
2863done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002864 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002865}
2866
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002867static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Matt Carlson683644b2011-03-09 16:58:23 +00002869 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Matt Carlson334355a2010-01-20 16:58:10 +00002871 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002872 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return;
2874
Joe Perches41535772013-02-16 11:20:04 +00002875 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
2876 tg3_asic_rev(tp) == ASIC_REV_5719 ||
2877 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002878 tg3_frob_aux_power_5717(tp, include_wol ?
2879 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002880 return;
2881 }
2882
2883 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002884 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002886 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002887
Michael Chanbc1c7562006-03-20 17:48:03 -08002888 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002889 if (dev_peer) {
2890 struct tg3 *tp_peer = netdev_priv(dev_peer);
2891
Joe Perches63c3a662011-04-26 08:12:10 +00002892 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002893 return;
2894
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002895 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002896 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002897 need_vaux = true;
2898 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002901 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2902 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002903 need_vaux = true;
2904
Matt Carlson520b2752011-06-13 13:39:02 +00002905 if (need_vaux)
2906 tg3_pwrsrc_switch_to_vaux(tp);
2907 else
2908 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002911static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2912{
2913 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2914 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002915 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002916 if (speed != SPEED_10)
2917 return 1;
2918 } else if (speed == SPEED_10)
2919 return 1;
2920
2921 return 0;
2922}
2923
Matt Carlson0a459aa2008-11-03 16:54:15 -08002924static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002925{
Matt Carlsonce057f02007-11-12 21:08:03 -08002926 u32 val;
2927
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002928 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00002929 if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Michael Chan51297242007-02-13 12:17:57 -08002930 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2931 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2932
2933 sg_dig_ctrl |=
2934 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2935 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2936 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2937 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002938 return;
Michael Chan51297242007-02-13 12:17:57 -08002939 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002940
Joe Perches41535772013-02-16 11:20:04 +00002941 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002942 tg3_bmcr_reset(tp);
2943 val = tr32(GRC_MISC_CFG);
2944 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2945 udelay(40);
2946 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002947 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002948 u32 phytest;
2949 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2950 u32 phy;
2951
2952 tg3_writephy(tp, MII_ADVERTISE, 0);
2953 tg3_writephy(tp, MII_BMCR,
2954 BMCR_ANENABLE | BMCR_ANRESTART);
2955
2956 tg3_writephy(tp, MII_TG3_FET_TEST,
2957 phytest | MII_TG3_FET_SHADOW_EN);
2958 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2959 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2960 tg3_writephy(tp,
2961 MII_TG3_FET_SHDW_AUXMODE4,
2962 phy);
2963 }
2964 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2965 }
2966 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002967 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002968 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2969 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002970
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002971 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2972 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2973 MII_TG3_AUXCTL_PCTL_VREG_11V;
2974 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002975 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002976
Michael Chan15c3b692006-03-22 01:06:52 -08002977 /* The PHY should not be powered down on some chips because
2978 * of bugs.
2979 */
Joe Perches41535772013-02-16 11:20:04 +00002980 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
2981 tg3_asic_rev(tp) == ASIC_REV_5704 ||
2982 (tg3_asic_rev(tp) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002983 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
Joe Perches41535772013-02-16 11:20:04 +00002984 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002985 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002986 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002987
Joe Perches41535772013-02-16 11:20:04 +00002988 if (tg3_chip_rev(tp) == CHIPREV_5784_AX ||
2989 tg3_chip_rev(tp) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002990 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2991 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2992 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2993 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2994 }
2995
Michael Chan15c3b692006-03-22 01:06:52 -08002996 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2997}
2998
Matt Carlson3f007892008-11-03 16:51:36 -08002999/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003000static int tg3_nvram_lock(struct tg3 *tp)
3001{
Joe Perches63c3a662011-04-26 08:12:10 +00003002 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003003 int i;
3004
3005 if (tp->nvram_lock_cnt == 0) {
3006 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
3007 for (i = 0; i < 8000; i++) {
3008 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
3009 break;
3010 udelay(20);
3011 }
3012 if (i == 8000) {
3013 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
3014 return -ENODEV;
3015 }
3016 }
3017 tp->nvram_lock_cnt++;
3018 }
3019 return 0;
3020}
3021
3022/* tp->lock is held. */
3023static void tg3_nvram_unlock(struct tg3 *tp)
3024{
Joe Perches63c3a662011-04-26 08:12:10 +00003025 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003026 if (tp->nvram_lock_cnt > 0)
3027 tp->nvram_lock_cnt--;
3028 if (tp->nvram_lock_cnt == 0)
3029 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3030 }
3031}
3032
3033/* tp->lock is held. */
3034static void tg3_enable_nvram_access(struct tg3 *tp)
3035{
Joe Perches63c3a662011-04-26 08:12:10 +00003036 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003037 u32 nvaccess = tr32(NVRAM_ACCESS);
3038
3039 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3040 }
3041}
3042
3043/* tp->lock is held. */
3044static void tg3_disable_nvram_access(struct tg3 *tp)
3045{
Joe Perches63c3a662011-04-26 08:12:10 +00003046 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003047 u32 nvaccess = tr32(NVRAM_ACCESS);
3048
3049 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3050 }
3051}
3052
3053static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3054 u32 offset, u32 *val)
3055{
3056 u32 tmp;
3057 int i;
3058
3059 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3060 return -EINVAL;
3061
3062 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3063 EEPROM_ADDR_DEVID_MASK |
3064 EEPROM_ADDR_READ);
3065 tw32(GRC_EEPROM_ADDR,
3066 tmp |
3067 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3068 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3069 EEPROM_ADDR_ADDR_MASK) |
3070 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3071
3072 for (i = 0; i < 1000; i++) {
3073 tmp = tr32(GRC_EEPROM_ADDR);
3074
3075 if (tmp & EEPROM_ADDR_COMPLETE)
3076 break;
3077 msleep(1);
3078 }
3079 if (!(tmp & EEPROM_ADDR_COMPLETE))
3080 return -EBUSY;
3081
Matt Carlson62cedd12009-04-20 14:52:29 -07003082 tmp = tr32(GRC_EEPROM_DATA);
3083
3084 /*
3085 * The data will always be opposite the native endian
3086 * format. Perform a blind byteswap to compensate.
3087 */
3088 *val = swab32(tmp);
3089
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003090 return 0;
3091}
3092
3093#define NVRAM_CMD_TIMEOUT 10000
3094
3095static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3096{
3097 int i;
3098
3099 tw32(NVRAM_CMD, nvram_cmd);
3100 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3101 udelay(10);
3102 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3103 udelay(10);
3104 break;
3105 }
3106 }
3107
3108 if (i == NVRAM_CMD_TIMEOUT)
3109 return -EBUSY;
3110
3111 return 0;
3112}
3113
3114static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3115{
Joe Perches63c3a662011-04-26 08:12:10 +00003116 if (tg3_flag(tp, NVRAM) &&
3117 tg3_flag(tp, NVRAM_BUFFERED) &&
3118 tg3_flag(tp, FLASH) &&
3119 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003120 (tp->nvram_jedecnum == JEDEC_ATMEL))
3121
3122 addr = ((addr / tp->nvram_pagesize) <<
3123 ATMEL_AT45DB0X1B_PAGE_POS) +
3124 (addr % tp->nvram_pagesize);
3125
3126 return addr;
3127}
3128
3129static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3130{
Joe Perches63c3a662011-04-26 08:12:10 +00003131 if (tg3_flag(tp, NVRAM) &&
3132 tg3_flag(tp, NVRAM_BUFFERED) &&
3133 tg3_flag(tp, FLASH) &&
3134 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003135 (tp->nvram_jedecnum == JEDEC_ATMEL))
3136
3137 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3138 tp->nvram_pagesize) +
3139 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3140
3141 return addr;
3142}
3143
Matt Carlsone4f34112009-02-25 14:25:00 +00003144/* NOTE: Data read in from NVRAM is byteswapped according to
3145 * the byteswapping settings for all other register accesses.
3146 * tg3 devices are BE devices, so on a BE machine, the data
3147 * returned will be exactly as it is seen in NVRAM. On a LE
3148 * machine, the 32-bit value will be byteswapped.
3149 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003150static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3151{
3152 int ret;
3153
Joe Perches63c3a662011-04-26 08:12:10 +00003154 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003155 return tg3_nvram_read_using_eeprom(tp, offset, val);
3156
3157 offset = tg3_nvram_phys_addr(tp, offset);
3158
3159 if (offset > NVRAM_ADDR_MSK)
3160 return -EINVAL;
3161
3162 ret = tg3_nvram_lock(tp);
3163 if (ret)
3164 return ret;
3165
3166 tg3_enable_nvram_access(tp);
3167
3168 tw32(NVRAM_ADDR, offset);
3169 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3170 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3171
3172 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003173 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003174
3175 tg3_disable_nvram_access(tp);
3176
3177 tg3_nvram_unlock(tp);
3178
3179 return ret;
3180}
3181
Matt Carlsona9dc5292009-02-25 14:25:30 +00003182/* Ensures NVRAM data is in bytestream format. */
3183static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003184{
3185 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003186 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003187 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003188 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003189 return res;
3190}
3191
Matt Carlsondbe9b922012-02-13 10:20:09 +00003192static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3193 u32 offset, u32 len, u8 *buf)
3194{
3195 int i, j, rc = 0;
3196 u32 val;
3197
3198 for (i = 0; i < len; i += 4) {
3199 u32 addr;
3200 __be32 data;
3201
3202 addr = offset + i;
3203
3204 memcpy(&data, buf + i, 4);
3205
3206 /*
3207 * The SEEPROM interface expects the data to always be opposite
3208 * the native endian format. We accomplish this by reversing
3209 * all the operations that would have been performed on the
3210 * data from a call to tg3_nvram_read_be32().
3211 */
3212 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3213
3214 val = tr32(GRC_EEPROM_ADDR);
3215 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3216
3217 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3218 EEPROM_ADDR_READ);
3219 tw32(GRC_EEPROM_ADDR, val |
3220 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3221 (addr & EEPROM_ADDR_ADDR_MASK) |
3222 EEPROM_ADDR_START |
3223 EEPROM_ADDR_WRITE);
3224
3225 for (j = 0; j < 1000; j++) {
3226 val = tr32(GRC_EEPROM_ADDR);
3227
3228 if (val & EEPROM_ADDR_COMPLETE)
3229 break;
3230 msleep(1);
3231 }
3232 if (!(val & EEPROM_ADDR_COMPLETE)) {
3233 rc = -EBUSY;
3234 break;
3235 }
3236 }
3237
3238 return rc;
3239}
3240
3241/* offset and length are dword aligned */
3242static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3243 u8 *buf)
3244{
3245 int ret = 0;
3246 u32 pagesize = tp->nvram_pagesize;
3247 u32 pagemask = pagesize - 1;
3248 u32 nvram_cmd;
3249 u8 *tmp;
3250
3251 tmp = kmalloc(pagesize, GFP_KERNEL);
3252 if (tmp == NULL)
3253 return -ENOMEM;
3254
3255 while (len) {
3256 int j;
3257 u32 phy_addr, page_off, size;
3258
3259 phy_addr = offset & ~pagemask;
3260
3261 for (j = 0; j < pagesize; j += 4) {
3262 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3263 (__be32 *) (tmp + j));
3264 if (ret)
3265 break;
3266 }
3267 if (ret)
3268 break;
3269
3270 page_off = offset & pagemask;
3271 size = pagesize;
3272 if (len < size)
3273 size = len;
3274
3275 len -= size;
3276
3277 memcpy(tmp + page_off, buf, size);
3278
3279 offset = offset + (pagesize - page_off);
3280
3281 tg3_enable_nvram_access(tp);
3282
3283 /*
3284 * Before we can erase the flash page, we need
3285 * to issue a special "write enable" command.
3286 */
3287 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3288
3289 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3290 break;
3291
3292 /* Erase the target page */
3293 tw32(NVRAM_ADDR, phy_addr);
3294
3295 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3296 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3297
3298 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3299 break;
3300
3301 /* Issue another write enable to start the write. */
3302 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3303
3304 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3305 break;
3306
3307 for (j = 0; j < pagesize; j += 4) {
3308 __be32 data;
3309
3310 data = *((__be32 *) (tmp + j));
3311
3312 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3313
3314 tw32(NVRAM_ADDR, phy_addr + j);
3315
3316 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3317 NVRAM_CMD_WR;
3318
3319 if (j == 0)
3320 nvram_cmd |= NVRAM_CMD_FIRST;
3321 else if (j == (pagesize - 4))
3322 nvram_cmd |= NVRAM_CMD_LAST;
3323
3324 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3325 if (ret)
3326 break;
3327 }
3328 if (ret)
3329 break;
3330 }
3331
3332 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3333 tg3_nvram_exec_cmd(tp, nvram_cmd);
3334
3335 kfree(tmp);
3336
3337 return ret;
3338}
3339
3340/* offset and length are dword aligned */
3341static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3342 u8 *buf)
3343{
3344 int i, ret = 0;
3345
3346 for (i = 0; i < len; i += 4, offset += 4) {
3347 u32 page_off, phy_addr, nvram_cmd;
3348 __be32 data;
3349
3350 memcpy(&data, buf + i, 4);
3351 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3352
3353 page_off = offset % tp->nvram_pagesize;
3354
3355 phy_addr = tg3_nvram_phys_addr(tp, offset);
3356
Matt Carlsondbe9b922012-02-13 10:20:09 +00003357 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3358
3359 if (page_off == 0 || i == 0)
3360 nvram_cmd |= NVRAM_CMD_FIRST;
3361 if (page_off == (tp->nvram_pagesize - 4))
3362 nvram_cmd |= NVRAM_CMD_LAST;
3363
3364 if (i == (len - 4))
3365 nvram_cmd |= NVRAM_CMD_LAST;
3366
Matt Carlson42278222012-02-13 15:20:11 +00003367 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3368 !tg3_flag(tp, FLASH) ||
3369 !tg3_flag(tp, 57765_PLUS))
3370 tw32(NVRAM_ADDR, phy_addr);
3371
Joe Perches41535772013-02-16 11:20:04 +00003372 if (tg3_asic_rev(tp) != ASIC_REV_5752 &&
Matt Carlsondbe9b922012-02-13 10:20:09 +00003373 !tg3_flag(tp, 5755_PLUS) &&
3374 (tp->nvram_jedecnum == JEDEC_ST) &&
3375 (nvram_cmd & NVRAM_CMD_FIRST)) {
3376 u32 cmd;
3377
3378 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3379 ret = tg3_nvram_exec_cmd(tp, cmd);
3380 if (ret)
3381 break;
3382 }
3383 if (!tg3_flag(tp, FLASH)) {
3384 /* We always do complete word writes to eeprom. */
3385 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3386 }
3387
3388 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3389 if (ret)
3390 break;
3391 }
3392 return ret;
3393}
3394
3395/* offset and length are dword aligned */
3396static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3397{
3398 int ret;
3399
3400 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3401 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3402 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3403 udelay(40);
3404 }
3405
3406 if (!tg3_flag(tp, NVRAM)) {
3407 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3408 } else {
3409 u32 grc_mode;
3410
3411 ret = tg3_nvram_lock(tp);
3412 if (ret)
3413 return ret;
3414
3415 tg3_enable_nvram_access(tp);
3416 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3417 tw32(NVRAM_WRITE1, 0x406);
3418
3419 grc_mode = tr32(GRC_MODE);
3420 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3421
3422 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3423 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3424 buf);
3425 } else {
3426 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3427 buf);
3428 }
3429
3430 grc_mode = tr32(GRC_MODE);
3431 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3432
3433 tg3_disable_nvram_access(tp);
3434 tg3_nvram_unlock(tp);
3435 }
3436
3437 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3438 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3439 udelay(40);
3440 }
3441
3442 return ret;
3443}
3444
Matt Carlson997b4f12011-08-31 11:44:53 +00003445#define RX_CPU_SCRATCH_BASE 0x30000
3446#define RX_CPU_SCRATCH_SIZE 0x04000
3447#define TX_CPU_SCRATCH_BASE 0x34000
3448#define TX_CPU_SCRATCH_SIZE 0x04000
3449
3450/* tp->lock is held. */
3451static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3452{
3453 int i;
3454
3455 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3456
Joe Perches41535772013-02-16 11:20:04 +00003457 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003458 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3459
3460 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3461 return 0;
3462 }
3463 if (offset == RX_CPU_BASE) {
3464 for (i = 0; i < 10000; i++) {
3465 tw32(offset + CPU_STATE, 0xffffffff);
3466 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3467 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3468 break;
3469 }
3470
3471 tw32(offset + CPU_STATE, 0xffffffff);
3472 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3473 udelay(10);
3474 } else {
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003475 /*
3476 * There is only an Rx CPU for the 5750 derivative in the
3477 * BCM4785.
3478 */
3479 if (tg3_flag(tp, IS_SSB_CORE))
3480 return 0;
3481
Matt Carlson997b4f12011-08-31 11:44:53 +00003482 for (i = 0; i < 10000; i++) {
3483 tw32(offset + CPU_STATE, 0xffffffff);
3484 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3485 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3486 break;
3487 }
3488 }
3489
3490 if (i >= 10000) {
3491 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3492 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3493 return -ENODEV;
3494 }
3495
3496 /* Clear firmware's nvram arbitration. */
3497 if (tg3_flag(tp, NVRAM))
3498 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3499 return 0;
3500}
3501
3502struct fw_info {
3503 unsigned int fw_base;
3504 unsigned int fw_len;
3505 const __be32 *fw_data;
3506};
3507
3508/* tp->lock is held. */
3509static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3510 u32 cpu_scratch_base, int cpu_scratch_size,
3511 struct fw_info *info)
3512{
3513 int err, lock_err, i;
3514 void (*write_op)(struct tg3 *, u32, u32);
3515
3516 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3517 netdev_err(tp->dev,
3518 "%s: Trying to load TX cpu firmware which is 5705\n",
3519 __func__);
3520 return -EINVAL;
3521 }
3522
3523 if (tg3_flag(tp, 5705_PLUS))
3524 write_op = tg3_write_mem;
3525 else
3526 write_op = tg3_write_indirect_reg32;
3527
3528 /* It is possible that bootcode is still loading at this point.
3529 * Get the nvram lock first before halting the cpu.
3530 */
3531 lock_err = tg3_nvram_lock(tp);
3532 err = tg3_halt_cpu(tp, cpu_base);
3533 if (!lock_err)
3534 tg3_nvram_unlock(tp);
3535 if (err)
3536 goto out;
3537
3538 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3539 write_op(tp, cpu_scratch_base + i, 0);
3540 tw32(cpu_base + CPU_STATE, 0xffffffff);
3541 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3542 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3543 write_op(tp, (cpu_scratch_base +
3544 (info->fw_base & 0xffff) +
3545 (i * sizeof(u32))),
3546 be32_to_cpu(info->fw_data[i]));
3547
3548 err = 0;
3549
3550out:
3551 return err;
3552}
3553
3554/* tp->lock is held. */
3555static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3556{
3557 struct fw_info info;
3558 const __be32 *fw_data;
3559 int err, i;
3560
3561 fw_data = (void *)tp->fw->data;
3562
3563 /* Firmware blob starts with version numbers, followed by
3564 start address and length. We are setting complete length.
3565 length = end_address_of_bss - start_address_of_text.
3566 Remainder is the blob to be loaded contiguously
3567 from start address. */
3568
3569 info.fw_base = be32_to_cpu(fw_data[1]);
3570 info.fw_len = tp->fw->size - 12;
3571 info.fw_data = &fw_data[3];
3572
3573 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3574 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3575 &info);
3576 if (err)
3577 return err;
3578
3579 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3580 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3581 &info);
3582 if (err)
3583 return err;
3584
3585 /* Now startup only the RX cpu. */
3586 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3587 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3588
3589 for (i = 0; i < 5; i++) {
3590 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3591 break;
3592 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3593 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3594 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3595 udelay(1000);
3596 }
3597 if (i >= 5) {
3598 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3599 "should be %08x\n", __func__,
3600 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3601 return -ENODEV;
3602 }
3603 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3604 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3605
3606 return 0;
3607}
3608
3609/* tp->lock is held. */
3610static int tg3_load_tso_firmware(struct tg3 *tp)
3611{
3612 struct fw_info info;
3613 const __be32 *fw_data;
3614 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3615 int err, i;
3616
3617 if (tg3_flag(tp, HW_TSO_1) ||
3618 tg3_flag(tp, HW_TSO_2) ||
3619 tg3_flag(tp, HW_TSO_3))
3620 return 0;
3621
3622 fw_data = (void *)tp->fw->data;
3623
3624 /* Firmware blob starts with version numbers, followed by
3625 start address and length. We are setting complete length.
3626 length = end_address_of_bss - start_address_of_text.
3627 Remainder is the blob to be loaded contiguously
3628 from start address. */
3629
3630 info.fw_base = be32_to_cpu(fw_data[1]);
3631 cpu_scratch_size = tp->fw_len;
3632 info.fw_len = tp->fw->size - 12;
3633 info.fw_data = &fw_data[3];
3634
Joe Perches41535772013-02-16 11:20:04 +00003635 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlson997b4f12011-08-31 11:44:53 +00003636 cpu_base = RX_CPU_BASE;
3637 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3638 } else {
3639 cpu_base = TX_CPU_BASE;
3640 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3641 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3642 }
3643
3644 err = tg3_load_firmware_cpu(tp, cpu_base,
3645 cpu_scratch_base, cpu_scratch_size,
3646 &info);
3647 if (err)
3648 return err;
3649
3650 /* Now startup the cpu. */
3651 tw32(cpu_base + CPU_STATE, 0xffffffff);
3652 tw32_f(cpu_base + CPU_PC, info.fw_base);
3653
3654 for (i = 0; i < 5; i++) {
3655 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3656 break;
3657 tw32(cpu_base + CPU_STATE, 0xffffffff);
3658 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3659 tw32_f(cpu_base + CPU_PC, info.fw_base);
3660 udelay(1000);
3661 }
3662 if (i >= 5) {
3663 netdev_err(tp->dev,
3664 "%s fails to set CPU PC, is %08x should be %08x\n",
3665 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3666 return -ENODEV;
3667 }
3668 tw32(cpu_base + CPU_STATE, 0xffffffff);
3669 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3670 return 0;
3671}
3672
3673
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003674/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003675static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3676{
3677 u32 addr_high, addr_low;
3678 int i;
3679
3680 addr_high = ((tp->dev->dev_addr[0] << 8) |
3681 tp->dev->dev_addr[1]);
3682 addr_low = ((tp->dev->dev_addr[2] << 24) |
3683 (tp->dev->dev_addr[3] << 16) |
3684 (tp->dev->dev_addr[4] << 8) |
3685 (tp->dev->dev_addr[5] << 0));
3686 for (i = 0; i < 4; i++) {
3687 if (i == 1 && skip_mac_1)
3688 continue;
3689 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3690 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3691 }
3692
Joe Perches41535772013-02-16 11:20:04 +00003693 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
3694 tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson3f007892008-11-03 16:51:36 -08003695 for (i = 0; i < 12; i++) {
3696 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3697 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3698 }
3699 }
3700
3701 addr_high = (tp->dev->dev_addr[0] +
3702 tp->dev->dev_addr[1] +
3703 tp->dev->dev_addr[2] +
3704 tp->dev->dev_addr[3] +
3705 tp->dev->dev_addr[4] +
3706 tp->dev->dev_addr[5]) &
3707 TX_BACKOFF_SEED_MASK;
3708 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3709}
3710
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003711static void tg3_enable_register_access(struct tg3 *tp)
3712{
3713 /*
3714 * Make sure register accesses (indirect or otherwise) will function
3715 * correctly.
3716 */
3717 pci_write_config_dword(tp->pdev,
3718 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3719}
3720
3721static int tg3_power_up(struct tg3 *tp)
3722{
Matt Carlsonbed98292011-07-13 09:27:29 +00003723 int err;
3724
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003725 tg3_enable_register_access(tp);
3726
Matt Carlsonbed98292011-07-13 09:27:29 +00003727 err = pci_set_power_state(tp->pdev, PCI_D0);
3728 if (!err) {
3729 /* Switch out of Vaux if it is a NIC */
3730 tg3_pwrsrc_switch_to_vmain(tp);
3731 } else {
3732 netdev_err(tp->dev, "Transition to D0 failed\n");
3733 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003734
Matt Carlsonbed98292011-07-13 09:27:29 +00003735 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003736}
3737
Matt Carlson4b409522012-02-13 10:20:11 +00003738static int tg3_setup_phy(struct tg3 *, int);
3739
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003740static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003743 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003745 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003746
3747 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003748 if (tg3_flag(tp, CLKREQ_BUG))
3749 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3750 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3753 tw32(TG3PCI_MISC_HOST_CTRL,
3754 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3755
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003756 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003757 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003758
Joe Perches63c3a662011-04-26 08:12:10 +00003759 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003760 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003761 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003762 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003763 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003764 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003765
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003766 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003767
Matt Carlson80096062010-08-02 11:26:06 +00003768 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003769
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003770 tp->link_config.speed = phydev->speed;
3771 tp->link_config.duplex = phydev->duplex;
3772 tp->link_config.autoneg = phydev->autoneg;
3773 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003774
3775 advertising = ADVERTISED_TP |
3776 ADVERTISED_Pause |
3777 ADVERTISED_Autoneg |
3778 ADVERTISED_10baseT_Half;
3779
Joe Perches63c3a662011-04-26 08:12:10 +00003780 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3781 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003782 advertising |=
3783 ADVERTISED_100baseT_Half |
3784 ADVERTISED_100baseT_Full |
3785 ADVERTISED_10baseT_Full;
3786 else
3787 advertising |= ADVERTISED_10baseT_Full;
3788 }
3789
3790 phydev->advertising = advertising;
3791
3792 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003793
3794 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003795 if (phyid != PHY_ID_BCMAC131) {
3796 phyid &= PHY_BCM_OUI_MASK;
3797 if (phyid == PHY_BCM_OUI_1 ||
3798 phyid == PHY_BCM_OUI_2 ||
3799 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003800 do_low_power = true;
3801 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003802 }
Matt Carlsondd477002008-05-25 23:45:58 -07003803 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003804 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003805
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003806 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003807 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Matt Carlson2855b9f2012-02-13 15:20:14 +00003809 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003810 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
3812
Joe Perches41535772013-02-16 11:20:04 +00003813 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07003814 u32 val;
3815
3816 val = tr32(GRC_VCPU_EXT_CTRL);
3817 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003818 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003819 int i;
3820 u32 val;
3821
3822 for (i = 0; i < 200; i++) {
3823 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3824 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3825 break;
3826 msleep(1);
3827 }
3828 }
Joe Perches63c3a662011-04-26 08:12:10 +00003829 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003830 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3831 WOL_DRV_STATE_SHUTDOWN |
3832 WOL_DRV_WOL |
3833 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003834
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003835 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 u32 mac_mode;
3837
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003838 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003839 if (do_low_power &&
3840 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3841 tg3_phy_auxctl_write(tp,
3842 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3843 MII_TG3_AUXCTL_PCTL_WOL_EN |
3844 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3845 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003846 udelay(40);
3847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003849 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003850 mac_mode = MAC_MODE_PORT_MODE_GMII;
3851 else
3852 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003854 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
Joe Perches41535772013-02-16 11:20:04 +00003855 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003856 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003857 SPEED_100 : SPEED_10;
3858 if (tg3_5700_link_polarity(tp, speed))
3859 mac_mode |= MAC_MODE_LINK_POLARITY;
3860 else
3861 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 } else {
3864 mac_mode = MAC_MODE_PORT_MODE_TBI;
3865 }
3866
Joe Perches63c3a662011-04-26 08:12:10 +00003867 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 tw32(MAC_LED_CTRL, tp->led_ctrl);
3869
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003870 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003871 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3872 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003873 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Joe Perches63c3a662011-04-26 08:12:10 +00003875 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003876 mac_mode |= MAC_MODE_APE_TX_EN |
3877 MAC_MODE_APE_RX_EN |
3878 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 tw32_f(MAC_MODE, mac_mode);
3881 udelay(100);
3882
3883 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3884 udelay(10);
3885 }
3886
Joe Perches63c3a662011-04-26 08:12:10 +00003887 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Joe Perches41535772013-02-16 11:20:04 +00003888 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3889 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 u32 base_val;
3891
3892 base_val = tp->pci_clock_ctrl;
3893 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3894 CLOCK_CTRL_TXCLK_DISABLE);
3895
Michael Chanb401e9e2005-12-19 16:27:04 -08003896 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3897 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003898 } else if (tg3_flag(tp, 5780_CLASS) ||
3899 tg3_flag(tp, CPMU_PRESENT) ||
Joe Perches41535772013-02-16 11:20:04 +00003900 tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003901 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003902 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 u32 newbits1, newbits2;
3904
Joe Perches41535772013-02-16 11:20:04 +00003905 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3906 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3908 CLOCK_CTRL_TXCLK_DISABLE |
3909 CLOCK_CTRL_ALTCLK);
3910 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003911 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 newbits1 = CLOCK_CTRL_625_CORE;
3913 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3914 } else {
3915 newbits1 = CLOCK_CTRL_ALTCLK;
3916 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3917 }
3918
Michael Chanb401e9e2005-12-19 16:27:04 -08003919 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3920 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Michael Chanb401e9e2005-12-19 16:27:04 -08003922 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3923 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Joe Perches63c3a662011-04-26 08:12:10 +00003925 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 u32 newbits3;
3927
Joe Perches41535772013-02-16 11:20:04 +00003928 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
3929 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3931 CLOCK_CTRL_TXCLK_DISABLE |
3932 CLOCK_CTRL_44MHZ_CORE);
3933 } else {
3934 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3935 }
3936
Michael Chanb401e9e2005-12-19 16:27:04 -08003937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3938 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
3940 }
3941
Joe Perches63c3a662011-04-26 08:12:10 +00003942 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003943 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003944
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003945 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 /* Workaround for unstable PLL clock */
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00003948 if ((!tg3_flag(tp, IS_SSB_CORE)) &&
Joe Perches41535772013-02-16 11:20:04 +00003949 ((tg3_chip_rev(tp) == CHIPREV_5750_AX) ||
3950 (tg3_chip_rev(tp) == CHIPREV_5750_BX))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 u32 val = tr32(0x7d00);
3952
3953 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3954 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003955 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003956 int err;
3957
3958 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003960 if (!err)
3961 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964
Michael Chanbbadf502006-04-06 21:46:34 -07003965 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return 0;
3968}
3969
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003970static void tg3_power_down(struct tg3 *tp)
3971{
3972 tg3_power_down_prepare(tp);
3973
Joe Perches63c3a662011-04-26 08:12:10 +00003974 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003975 pci_set_power_state(tp->pdev, PCI_D3hot);
3976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3979{
3980 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3981 case MII_TG3_AUX_STAT_10HALF:
3982 *speed = SPEED_10;
3983 *duplex = DUPLEX_HALF;
3984 break;
3985
3986 case MII_TG3_AUX_STAT_10FULL:
3987 *speed = SPEED_10;
3988 *duplex = DUPLEX_FULL;
3989 break;
3990
3991 case MII_TG3_AUX_STAT_100HALF:
3992 *speed = SPEED_100;
3993 *duplex = DUPLEX_HALF;
3994 break;
3995
3996 case MII_TG3_AUX_STAT_100FULL:
3997 *speed = SPEED_100;
3998 *duplex = DUPLEX_FULL;
3999 break;
4000
4001 case MII_TG3_AUX_STAT_1000HALF:
4002 *speed = SPEED_1000;
4003 *duplex = DUPLEX_HALF;
4004 break;
4005
4006 case MII_TG3_AUX_STAT_1000FULL:
4007 *speed = SPEED_1000;
4008 *duplex = DUPLEX_FULL;
4009 break;
4010
4011 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004012 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07004013 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
4014 SPEED_10;
4015 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
4016 DUPLEX_HALF;
4017 break;
4018 }
Matt Carlsone7405222012-02-13 15:20:16 +00004019 *speed = SPEED_UNKNOWN;
4020 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Matt Carlson42b64a42011-05-19 12:12:49 +00004025static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Matt Carlson42b64a42011-05-19 12:12:49 +00004027 int err = 0;
4028 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Matt Carlson42b64a42011-05-19 12:12:49 +00004030 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00004031 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004032 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Matt Carlson42b64a42011-05-19 12:12:49 +00004034 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4035 if (err)
4036 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Matt Carlson4f272092011-12-14 11:09:57 +00004038 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4039 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004040
Joe Perches41535772013-02-16 11:20:04 +00004041 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4042 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)
Matt Carlson4f272092011-12-14 11:09:57 +00004043 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004044
Matt Carlson4f272092011-12-14 11:09:57 +00004045 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4046 if (err)
4047 goto done;
4048 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004049
Matt Carlson42b64a42011-05-19 12:12:49 +00004050 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4051 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Matt Carlson42b64a42011-05-19 12:12:49 +00004053 tw32(TG3_CPMU_EEE_MODE,
4054 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004055
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004056 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004057 if (!err) {
4058 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004059
Matt Carlsona6b68da2010-12-06 08:28:52 +00004060 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004061 /* Advertise 100-BaseTX EEE ability */
4062 if (advertise & ADVERTISED_100baseT_Full)
4063 val |= MDIO_AN_EEE_ADV_100TX;
4064 /* Advertise 1000-BaseT EEE ability */
4065 if (advertise & ADVERTISED_1000baseT_Full)
4066 val |= MDIO_AN_EEE_ADV_1000T;
4067 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004068 if (err)
4069 val = 0;
4070
Joe Perches41535772013-02-16 11:20:04 +00004071 switch (tg3_asic_rev(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00004072 case ASIC_REV_5717:
4073 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004074 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004075 case ASIC_REV_5719:
4076 /* If we advertised any eee advertisements above... */
4077 if (val)
4078 val = MII_TG3_DSP_TAP26_ALNOKO |
4079 MII_TG3_DSP_TAP26_RMRXSTO |
4080 MII_TG3_DSP_TAP26_OPCSINPT;
4081 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4082 /* Fall through */
4083 case ASIC_REV_5720:
Michael Chanc65a17f2013-01-06 12:51:07 +00004084 case ASIC_REV_5762:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004085 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4086 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4087 MII_TG3_DSP_CH34TP2_HIBW01);
4088 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004089
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004090 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004091 if (!err)
4092 err = err2;
4093 }
4094
4095done:
4096 return err;
4097}
4098
4099static void tg3_phy_copper_begin(struct tg3 *tp)
4100{
Matt Carlsond13ba512012-02-22 12:35:19 +00004101 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4102 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4103 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004104
Matt Carlsond13ba512012-02-22 12:35:19 +00004105 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4106 adv = ADVERTISED_10baseT_Half |
4107 ADVERTISED_10baseT_Full;
4108 if (tg3_flag(tp, WOL_SPEED_100MB))
4109 adv |= ADVERTISED_100baseT_Half |
4110 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004111
Matt Carlsond13ba512012-02-22 12:35:19 +00004112 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004113 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004114 adv = tp->link_config.advertising;
4115 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4116 adv &= ~(ADVERTISED_1000baseT_Half |
4117 ADVERTISED_1000baseT_Full);
4118
4119 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004120 }
4121
Matt Carlsond13ba512012-02-22 12:35:19 +00004122 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004123
Matt Carlsond13ba512012-02-22 12:35:19 +00004124 tg3_writephy(tp, MII_BMCR,
4125 BMCR_ANENABLE | BMCR_ANRESTART);
4126 } else {
4127 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 u32 bmcr, orig_bmcr;
4129
4130 tp->link_config.active_speed = tp->link_config.speed;
4131 tp->link_config.active_duplex = tp->link_config.duplex;
4132
4133 bmcr = 0;
4134 switch (tp->link_config.speed) {
4135 default:
4136 case SPEED_10:
4137 break;
4138
4139 case SPEED_100:
4140 bmcr |= BMCR_SPEED100;
4141 break;
4142
4143 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004144 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 if (tp->link_config.duplex == DUPLEX_FULL)
4149 bmcr |= BMCR_FULLDPLX;
4150
4151 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4152 (bmcr != orig_bmcr)) {
4153 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4154 for (i = 0; i < 1500; i++) {
4155 u32 tmp;
4156
4157 udelay(10);
4158 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4159 tg3_readphy(tp, MII_BMSR, &tmp))
4160 continue;
4161 if (!(tmp & BMSR_LSTATUS)) {
4162 udelay(40);
4163 break;
4164 }
4165 }
4166 tg3_writephy(tp, MII_BMCR, bmcr);
4167 udelay(40);
4168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
4170}
4171
4172static int tg3_init_5401phy_dsp(struct tg3 *tp)
4173{
4174 int err;
4175
4176 /* Turn off tap power management. */
4177 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004178 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004180 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4181 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4182 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4183 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4184 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
4186 udelay(40);
4187
4188 return err;
4189}
4190
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004191static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004193 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004194
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004195 advertising = tp->link_config.advertising;
4196 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004198 advmsk = ADVERTISE_ALL;
4199 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004200 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004201 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004204 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4205 return false;
4206
4207 if ((*lcladv & advmsk) != tgtadv)
4208 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004209
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004210 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 u32 tg3_ctrl;
4212
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004213 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004214
Matt Carlson221c5632011-06-13 13:39:01 +00004215 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004216 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Matt Carlson3198e072012-02-13 15:20:10 +00004218 if (tgtadv &&
Joe Perches41535772013-02-16 11:20:04 +00004219 (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4220 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0)) {
Matt Carlson3198e072012-02-13 15:20:10 +00004221 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4222 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4223 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4224 } else {
4225 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4226 }
4227
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004228 if (tg3_ctrl != tgtadv)
4229 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004231
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004232 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004233}
4234
Matt Carlson859edb22011-12-08 14:40:16 +00004235static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4236{
4237 u32 lpeth = 0;
4238
4239 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4240 u32 val;
4241
4242 if (tg3_readphy(tp, MII_STAT1000, &val))
4243 return false;
4244
4245 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4246 }
4247
4248 if (tg3_readphy(tp, MII_LPA, rmtadv))
4249 return false;
4250
4251 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4252 tp->link_config.rmt_adv = lpeth;
4253
4254 return true;
4255}
4256
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004257static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4258{
4259 if (curr_link_up != tp->link_up) {
4260 if (curr_link_up) {
Nithin Sujir84421b92013-03-08 08:01:24 +00004261 netif_carrier_on(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004262 } else {
Nithin Sujir84421b92013-03-08 08:01:24 +00004263 netif_carrier_off(tp->dev);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004264 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4265 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4266 }
4267
4268 tg3_link_report(tp);
4269 return true;
4270 }
4271
4272 return false;
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4276{
4277 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004278 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004279 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 u16 current_speed;
4281 u8 current_duplex;
4282 int i, err;
4283
4284 tw32(MAC_EVENT, 0);
4285
4286 tw32_f(MAC_STATUS,
4287 (MAC_STATUS_SYNC_CHANGED |
4288 MAC_STATUS_CFG_CHANGED |
4289 MAC_STATUS_MI_COMPLETION |
4290 MAC_STATUS_LNKSTATE_CHANGED));
4291 udelay(40);
4292
Matt Carlson8ef21422008-05-02 16:47:53 -07004293 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4294 tw32_f(MAC_MI_MODE,
4295 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4296 udelay(80);
4297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004299 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 /* Some third-party PHYs need to be reset on link going
4302 * down.
4303 */
Joe Perches41535772013-02-16 11:20:04 +00004304 if ((tg3_asic_rev(tp) == ASIC_REV_5703 ||
4305 tg3_asic_rev(tp) == ASIC_REV_5704 ||
4306 tg3_asic_rev(tp) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004307 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 tg3_readphy(tp, MII_BMSR, &bmsr);
4309 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4310 !(bmsr & BMSR_LSTATUS))
4311 force_reset = 1;
4312 }
4313 if (force_reset)
4314 tg3_phy_reset(tp);
4315
Matt Carlson79eb6902010-02-17 15:17:03 +00004316 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 tg3_readphy(tp, MII_BMSR, &bmsr);
4318 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004319 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 bmsr = 0;
4321
4322 if (!(bmsr & BMSR_LSTATUS)) {
4323 err = tg3_init_5401phy_dsp(tp);
4324 if (err)
4325 return err;
4326
4327 tg3_readphy(tp, MII_BMSR, &bmsr);
4328 for (i = 0; i < 1000; i++) {
4329 udelay(10);
4330 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4331 (bmsr & BMSR_LSTATUS)) {
4332 udelay(40);
4333 break;
4334 }
4335 }
4336
Matt Carlson79eb6902010-02-17 15:17:03 +00004337 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4338 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 !(bmsr & BMSR_LSTATUS) &&
4340 tp->link_config.active_speed == SPEED_1000) {
4341 err = tg3_phy_reset(tp);
4342 if (!err)
4343 err = tg3_init_5401phy_dsp(tp);
4344 if (err)
4345 return err;
4346 }
4347 }
Joe Perches41535772013-02-16 11:20:04 +00004348 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
4349 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 /* 5701 {A0,B0} CRC bug workaround */
4351 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004352 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4353 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4354 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 }
4356
4357 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004358 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4359 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004361 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004363 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4365
Joe Perches41535772013-02-16 11:20:04 +00004366 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
4367 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4369 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4370 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4371 else
4372 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4373 }
4374
4375 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004376 current_speed = SPEED_UNKNOWN;
4377 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004378 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004379 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004381 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004382 err = tg3_phy_auxctl_read(tp,
4383 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4384 &val);
4385 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004386 tg3_phy_auxctl_write(tp,
4387 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4388 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 goto relink;
4390 }
4391 }
4392
4393 bmsr = 0;
4394 for (i = 0; i < 100; i++) {
4395 tg3_readphy(tp, MII_BMSR, &bmsr);
4396 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4397 (bmsr & BMSR_LSTATUS))
4398 break;
4399 udelay(40);
4400 }
4401
4402 if (bmsr & BMSR_LSTATUS) {
4403 u32 aux_stat, bmcr;
4404
4405 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4406 for (i = 0; i < 2000; i++) {
4407 udelay(10);
4408 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4409 aux_stat)
4410 break;
4411 }
4412
4413 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4414 &current_speed,
4415 &current_duplex);
4416
4417 bmcr = 0;
4418 for (i = 0; i < 200; i++) {
4419 tg3_readphy(tp, MII_BMCR, &bmcr);
4420 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4421 continue;
4422 if (bmcr && bmcr != 0x7fff)
4423 break;
4424 udelay(10);
4425 }
4426
Matt Carlsonef167e22007-12-20 20:10:01 -08004427 lcl_adv = 0;
4428 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Matt Carlsonef167e22007-12-20 20:10:01 -08004430 tp->link_config.active_speed = current_speed;
4431 tp->link_config.active_duplex = current_duplex;
4432
4433 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4434 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004435 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004436 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004437 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 } else {
4439 if (!(bmcr & BMCR_ANENABLE) &&
4440 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004441 tp->link_config.duplex == current_duplex &&
4442 tp->link_config.flowctrl ==
4443 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 }
4446 }
4447
Matt Carlsonef167e22007-12-20 20:10:01 -08004448 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004449 tp->link_config.active_duplex == DUPLEX_FULL) {
4450 u32 reg, bit;
4451
4452 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4453 reg = MII_TG3_FET_GEN_STAT;
4454 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4455 } else {
4456 reg = MII_TG3_EXT_STAT;
4457 bit = MII_TG3_EXT_STAT_MDIX;
4458 }
4459
4460 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4461 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4462
Matt Carlsonef167e22007-12-20 20:10:01 -08004463 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467relink:
Matt Carlson80096062010-08-02 11:26:06 +00004468 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 tg3_phy_copper_begin(tp);
4470
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004471 if (tg3_flag(tp, ROBOSWITCH)) {
4472 current_link_up = 1;
4473 /* FIXME: when BCM5325 switch is used use 100 MBit/s */
4474 current_speed = SPEED_1000;
4475 current_duplex = DUPLEX_FULL;
4476 tp->link_config.active_speed = current_speed;
4477 tp->link_config.active_duplex = current_duplex;
4478 }
4479
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004480 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004481 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4482 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 current_link_up = 1;
4484 }
4485
4486 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4487 if (current_link_up == 1) {
4488 if (tp->link_config.active_speed == SPEED_100 ||
4489 tp->link_config.active_speed == SPEED_10)
4490 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4491 else
4492 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004493 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004494 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4495 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4497
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00004498 /* In order for the 5750 core in BCM4785 chip to work properly
4499 * in RGMII mode, the Led Control Register must be set up.
4500 */
4501 if (tg3_flag(tp, RGMII_MODE)) {
4502 u32 led_ctrl = tr32(MAC_LED_CTRL);
4503 led_ctrl &= ~(LED_CTRL_1000MBPS_ON | LED_CTRL_100MBPS_ON);
4504
4505 if (tp->link_config.active_speed == SPEED_10)
4506 led_ctrl |= LED_CTRL_LNKLED_OVERRIDE;
4507 else if (tp->link_config.active_speed == SPEED_100)
4508 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4509 LED_CTRL_100MBPS_ON);
4510 else if (tp->link_config.active_speed == SPEED_1000)
4511 led_ctrl |= (LED_CTRL_LNKLED_OVERRIDE |
4512 LED_CTRL_1000MBPS_ON);
4513
4514 tw32(MAC_LED_CTRL, led_ctrl);
4515 udelay(40);
4516 }
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4519 if (tp->link_config.active_duplex == DUPLEX_HALF)
4520 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4521
Joe Perches41535772013-02-16 11:20:04 +00004522 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004523 if (current_link_up == 1 &&
4524 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004526 else
4527 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529
4530 /* ??? Without this setting Netgear GA302T PHY does not
4531 * ??? send/receive packets...
4532 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004533 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Joe Perches41535772013-02-16 11:20:04 +00004534 tg3_chip_rev_id(tp) == CHIPREV_ID_5700_ALTIMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4536 tw32_f(MAC_MI_MODE, tp->mi_mode);
4537 udelay(80);
4538 }
4539
4540 tw32_f(MAC_MODE, tp->mac_mode);
4541 udelay(40);
4542
Matt Carlson52b02d02010-10-14 10:37:41 +00004543 tg3_phy_eee_adjust(tp, current_link_up);
4544
Joe Perches63c3a662011-04-26 08:12:10 +00004545 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 /* Polled via timer. */
4547 tw32_f(MAC_EVENT, 0);
4548 } else {
4549 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4550 }
4551 udelay(40);
4552
Joe Perches41535772013-02-16 11:20:04 +00004553 if (tg3_asic_rev(tp) == ASIC_REV_5700 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 current_link_up == 1 &&
4555 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004556 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 udelay(120);
4558 tw32_f(MAC_STATUS,
4559 (MAC_STATUS_SYNC_CHANGED |
4560 MAC_STATUS_CFG_CHANGED));
4561 udelay(40);
4562 tg3_write_mem(tp,
4563 NIC_SRAM_FIRMWARE_MBOX,
4564 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4565 }
4566
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004567 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004568 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004569 if (tp->link_config.active_speed == SPEED_100 ||
4570 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004571 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4572 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004573 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004574 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4575 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004576 }
4577
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004578 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 return 0;
4581}
4582
4583struct tg3_fiber_aneginfo {
4584 int state;
4585#define ANEG_STATE_UNKNOWN 0
4586#define ANEG_STATE_AN_ENABLE 1
4587#define ANEG_STATE_RESTART_INIT 2
4588#define ANEG_STATE_RESTART 3
4589#define ANEG_STATE_DISABLE_LINK_OK 4
4590#define ANEG_STATE_ABILITY_DETECT_INIT 5
4591#define ANEG_STATE_ABILITY_DETECT 6
4592#define ANEG_STATE_ACK_DETECT_INIT 7
4593#define ANEG_STATE_ACK_DETECT 8
4594#define ANEG_STATE_COMPLETE_ACK_INIT 9
4595#define ANEG_STATE_COMPLETE_ACK 10
4596#define ANEG_STATE_IDLE_DETECT_INIT 11
4597#define ANEG_STATE_IDLE_DETECT 12
4598#define ANEG_STATE_LINK_OK 13
4599#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4600#define ANEG_STATE_NEXT_PAGE_WAIT 15
4601
4602 u32 flags;
4603#define MR_AN_ENABLE 0x00000001
4604#define MR_RESTART_AN 0x00000002
4605#define MR_AN_COMPLETE 0x00000004
4606#define MR_PAGE_RX 0x00000008
4607#define MR_NP_LOADED 0x00000010
4608#define MR_TOGGLE_TX 0x00000020
4609#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4610#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4611#define MR_LP_ADV_SYM_PAUSE 0x00000100
4612#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4613#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4614#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4615#define MR_LP_ADV_NEXT_PAGE 0x00001000
4616#define MR_TOGGLE_RX 0x00002000
4617#define MR_NP_RX 0x00004000
4618
4619#define MR_LINK_OK 0x80000000
4620
4621 unsigned long link_time, cur_time;
4622
4623 u32 ability_match_cfg;
4624 int ability_match_count;
4625
4626 char ability_match, idle_match, ack_match;
4627
4628 u32 txconfig, rxconfig;
4629#define ANEG_CFG_NP 0x00000080
4630#define ANEG_CFG_ACK 0x00000040
4631#define ANEG_CFG_RF2 0x00000020
4632#define ANEG_CFG_RF1 0x00000010
4633#define ANEG_CFG_PS2 0x00000001
4634#define ANEG_CFG_PS1 0x00008000
4635#define ANEG_CFG_HD 0x00004000
4636#define ANEG_CFG_FD 0x00002000
4637#define ANEG_CFG_INVAL 0x00001f06
4638
4639};
4640#define ANEG_OK 0
4641#define ANEG_DONE 1
4642#define ANEG_TIMER_ENAB 2
4643#define ANEG_FAILED -1
4644
4645#define ANEG_STATE_SETTLE_TIME 10000
4646
4647static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4648 struct tg3_fiber_aneginfo *ap)
4649{
Matt Carlson5be73b42007-12-20 20:09:29 -08004650 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 unsigned long delta;
4652 u32 rx_cfg_reg;
4653 int ret;
4654
4655 if (ap->state == ANEG_STATE_UNKNOWN) {
4656 ap->rxconfig = 0;
4657 ap->link_time = 0;
4658 ap->cur_time = 0;
4659 ap->ability_match_cfg = 0;
4660 ap->ability_match_count = 0;
4661 ap->ability_match = 0;
4662 ap->idle_match = 0;
4663 ap->ack_match = 0;
4664 }
4665 ap->cur_time++;
4666
4667 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4668 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4669
4670 if (rx_cfg_reg != ap->ability_match_cfg) {
4671 ap->ability_match_cfg = rx_cfg_reg;
4672 ap->ability_match = 0;
4673 ap->ability_match_count = 0;
4674 } else {
4675 if (++ap->ability_match_count > 1) {
4676 ap->ability_match = 1;
4677 ap->ability_match_cfg = rx_cfg_reg;
4678 }
4679 }
4680 if (rx_cfg_reg & ANEG_CFG_ACK)
4681 ap->ack_match = 1;
4682 else
4683 ap->ack_match = 0;
4684
4685 ap->idle_match = 0;
4686 } else {
4687 ap->idle_match = 1;
4688 ap->ability_match_cfg = 0;
4689 ap->ability_match_count = 0;
4690 ap->ability_match = 0;
4691 ap->ack_match = 0;
4692
4693 rx_cfg_reg = 0;
4694 }
4695
4696 ap->rxconfig = rx_cfg_reg;
4697 ret = ANEG_OK;
4698
Matt Carlson33f401a2010-04-05 10:19:27 +00004699 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 case ANEG_STATE_UNKNOWN:
4701 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4702 ap->state = ANEG_STATE_AN_ENABLE;
4703
4704 /* fallthru */
4705 case ANEG_STATE_AN_ENABLE:
4706 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4707 if (ap->flags & MR_AN_ENABLE) {
4708 ap->link_time = 0;
4709 ap->cur_time = 0;
4710 ap->ability_match_cfg = 0;
4711 ap->ability_match_count = 0;
4712 ap->ability_match = 0;
4713 ap->idle_match = 0;
4714 ap->ack_match = 0;
4715
4716 ap->state = ANEG_STATE_RESTART_INIT;
4717 } else {
4718 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4719 }
4720 break;
4721
4722 case ANEG_STATE_RESTART_INIT:
4723 ap->link_time = ap->cur_time;
4724 ap->flags &= ~(MR_NP_LOADED);
4725 ap->txconfig = 0;
4726 tw32(MAC_TX_AUTO_NEG, 0);
4727 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4728 tw32_f(MAC_MODE, tp->mac_mode);
4729 udelay(40);
4730
4731 ret = ANEG_TIMER_ENAB;
4732 ap->state = ANEG_STATE_RESTART;
4733
4734 /* fallthru */
4735 case ANEG_STATE_RESTART:
4736 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004737 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004739 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 break;
4742
4743 case ANEG_STATE_DISABLE_LINK_OK:
4744 ret = ANEG_DONE;
4745 break;
4746
4747 case ANEG_STATE_ABILITY_DETECT_INIT:
4748 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004749 ap->txconfig = ANEG_CFG_FD;
4750 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4751 if (flowctrl & ADVERTISE_1000XPAUSE)
4752 ap->txconfig |= ANEG_CFG_PS1;
4753 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4754 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4756 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4757 tw32_f(MAC_MODE, tp->mac_mode);
4758 udelay(40);
4759
4760 ap->state = ANEG_STATE_ABILITY_DETECT;
4761 break;
4762
4763 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004764 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 break;
4767
4768 case ANEG_STATE_ACK_DETECT_INIT:
4769 ap->txconfig |= ANEG_CFG_ACK;
4770 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4771 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4772 tw32_f(MAC_MODE, tp->mac_mode);
4773 udelay(40);
4774
4775 ap->state = ANEG_STATE_ACK_DETECT;
4776
4777 /* fallthru */
4778 case ANEG_STATE_ACK_DETECT:
4779 if (ap->ack_match != 0) {
4780 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4781 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4782 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4783 } else {
4784 ap->state = ANEG_STATE_AN_ENABLE;
4785 }
4786 } else if (ap->ability_match != 0 &&
4787 ap->rxconfig == 0) {
4788 ap->state = ANEG_STATE_AN_ENABLE;
4789 }
4790 break;
4791
4792 case ANEG_STATE_COMPLETE_ACK_INIT:
4793 if (ap->rxconfig & ANEG_CFG_INVAL) {
4794 ret = ANEG_FAILED;
4795 break;
4796 }
4797 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4798 MR_LP_ADV_HALF_DUPLEX |
4799 MR_LP_ADV_SYM_PAUSE |
4800 MR_LP_ADV_ASYM_PAUSE |
4801 MR_LP_ADV_REMOTE_FAULT1 |
4802 MR_LP_ADV_REMOTE_FAULT2 |
4803 MR_LP_ADV_NEXT_PAGE |
4804 MR_TOGGLE_RX |
4805 MR_NP_RX);
4806 if (ap->rxconfig & ANEG_CFG_FD)
4807 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4808 if (ap->rxconfig & ANEG_CFG_HD)
4809 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4810 if (ap->rxconfig & ANEG_CFG_PS1)
4811 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4812 if (ap->rxconfig & ANEG_CFG_PS2)
4813 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4814 if (ap->rxconfig & ANEG_CFG_RF1)
4815 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4816 if (ap->rxconfig & ANEG_CFG_RF2)
4817 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4818 if (ap->rxconfig & ANEG_CFG_NP)
4819 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4820
4821 ap->link_time = ap->cur_time;
4822
4823 ap->flags ^= (MR_TOGGLE_TX);
4824 if (ap->rxconfig & 0x0008)
4825 ap->flags |= MR_TOGGLE_RX;
4826 if (ap->rxconfig & ANEG_CFG_NP)
4827 ap->flags |= MR_NP_RX;
4828 ap->flags |= MR_PAGE_RX;
4829
4830 ap->state = ANEG_STATE_COMPLETE_ACK;
4831 ret = ANEG_TIMER_ENAB;
4832 break;
4833
4834 case ANEG_STATE_COMPLETE_ACK:
4835 if (ap->ability_match != 0 &&
4836 ap->rxconfig == 0) {
4837 ap->state = ANEG_STATE_AN_ENABLE;
4838 break;
4839 }
4840 delta = ap->cur_time - ap->link_time;
4841 if (delta > ANEG_STATE_SETTLE_TIME) {
4842 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4843 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4844 } else {
4845 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4846 !(ap->flags & MR_NP_RX)) {
4847 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4848 } else {
4849 ret = ANEG_FAILED;
4850 }
4851 }
4852 }
4853 break;
4854
4855 case ANEG_STATE_IDLE_DETECT_INIT:
4856 ap->link_time = ap->cur_time;
4857 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4858 tw32_f(MAC_MODE, tp->mac_mode);
4859 udelay(40);
4860
4861 ap->state = ANEG_STATE_IDLE_DETECT;
4862 ret = ANEG_TIMER_ENAB;
4863 break;
4864
4865 case ANEG_STATE_IDLE_DETECT:
4866 if (ap->ability_match != 0 &&
4867 ap->rxconfig == 0) {
4868 ap->state = ANEG_STATE_AN_ENABLE;
4869 break;
4870 }
4871 delta = ap->cur_time - ap->link_time;
4872 if (delta > ANEG_STATE_SETTLE_TIME) {
4873 /* XXX another gem from the Broadcom driver :( */
4874 ap->state = ANEG_STATE_LINK_OK;
4875 }
4876 break;
4877
4878 case ANEG_STATE_LINK_OK:
4879 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4880 ret = ANEG_DONE;
4881 break;
4882
4883 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4884 /* ??? unimplemented */
4885 break;
4886
4887 case ANEG_STATE_NEXT_PAGE_WAIT:
4888 /* ??? unimplemented */
4889 break;
4890
4891 default:
4892 ret = ANEG_FAILED;
4893 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 return ret;
4897}
4898
Matt Carlson5be73b42007-12-20 20:09:29 -08004899static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
4901 int res = 0;
4902 struct tg3_fiber_aneginfo aninfo;
4903 int status = ANEG_FAILED;
4904 unsigned int tick;
4905 u32 tmp;
4906
4907 tw32_f(MAC_TX_AUTO_NEG, 0);
4908
4909 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4910 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4911 udelay(40);
4912
4913 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4914 udelay(40);
4915
4916 memset(&aninfo, 0, sizeof(aninfo));
4917 aninfo.flags |= MR_AN_ENABLE;
4918 aninfo.state = ANEG_STATE_UNKNOWN;
4919 aninfo.cur_time = 0;
4920 tick = 0;
4921 while (++tick < 195000) {
4922 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4923 if (status == ANEG_DONE || status == ANEG_FAILED)
4924 break;
4925
4926 udelay(1);
4927 }
4928
4929 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4930 tw32_f(MAC_MODE, tp->mac_mode);
4931 udelay(40);
4932
Matt Carlson5be73b42007-12-20 20:09:29 -08004933 *txflags = aninfo.txconfig;
4934 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936 if (status == ANEG_DONE &&
4937 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4938 MR_LP_ADV_FULL_DUPLEX)))
4939 res = 1;
4940
4941 return res;
4942}
4943
4944static void tg3_init_bcm8002(struct tg3 *tp)
4945{
4946 u32 mac_status = tr32(MAC_STATUS);
4947 int i;
4948
4949 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004950 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 !(mac_status & MAC_STATUS_PCS_SYNCED))
4952 return;
4953
4954 /* Set PLL lock range. */
4955 tg3_writephy(tp, 0x16, 0x8007);
4956
4957 /* SW reset */
4958 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4959
4960 /* Wait for reset to complete. */
4961 /* XXX schedule_timeout() ... */
4962 for (i = 0; i < 500; i++)
4963 udelay(10);
4964
4965 /* Config mode; select PMA/Ch 1 regs. */
4966 tg3_writephy(tp, 0x10, 0x8411);
4967
4968 /* Enable auto-lock and comdet, select txclk for tx. */
4969 tg3_writephy(tp, 0x11, 0x0a10);
4970
4971 tg3_writephy(tp, 0x18, 0x00a0);
4972 tg3_writephy(tp, 0x16, 0x41ff);
4973
4974 /* Assert and deassert POR. */
4975 tg3_writephy(tp, 0x13, 0x0400);
4976 udelay(40);
4977 tg3_writephy(tp, 0x13, 0x0000);
4978
4979 tg3_writephy(tp, 0x11, 0x0a50);
4980 udelay(40);
4981 tg3_writephy(tp, 0x11, 0x0a10);
4982
4983 /* Wait for signal to stabilize */
4984 /* XXX schedule_timeout() ... */
4985 for (i = 0; i < 15000; i++)
4986 udelay(10);
4987
4988 /* Deselect the channel register so we can read the PHYID
4989 * later.
4990 */
4991 tg3_writephy(tp, 0x10, 0x8011);
4992}
4993
4994static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4995{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004996 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 u32 sg_dig_ctrl, sg_dig_status;
4998 u32 serdes_cfg, expected_sg_dig_ctrl;
4999 int workaround, port_a;
5000 int current_link_up;
5001
5002 serdes_cfg = 0;
5003 expected_sg_dig_ctrl = 0;
5004 workaround = 0;
5005 port_a = 1;
5006 current_link_up = 0;
5007
Joe Perches41535772013-02-16 11:20:04 +00005008 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A0 &&
5009 tg3_chip_rev_id(tp) != CHIPREV_ID_5704_A1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 workaround = 1;
5011 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
5012 port_a = 0;
5013
5014 /* preserve bits 0-11,13,14 for signal pre-emphasis */
5015 /* preserve bits 20-23 for voltage regulator */
5016 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
5017 }
5018
5019 sg_dig_ctrl = tr32(SG_DIG_CTRL);
5020
5021 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005022 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 if (workaround) {
5024 u32 val = serdes_cfg;
5025
5026 if (port_a)
5027 val |= 0xc010000;
5028 else
5029 val |= 0x4010000;
5030 tw32_f(MAC_SERDES_CFG, val);
5031 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005032
5033 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 }
5035 if (mac_status & MAC_STATUS_PCS_SYNCED) {
5036 tg3_setup_flow_control(tp, 0, 0);
5037 current_link_up = 1;
5038 }
5039 goto out;
5040 }
5041
5042 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005043 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Matt Carlson82cd3d12007-12-20 20:09:00 -08005045 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
5046 if (flowctrl & ADVERTISE_1000XPAUSE)
5047 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
5048 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
5049 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
5051 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005052 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07005053 tp->serdes_counter &&
5054 ((mac_status & (MAC_STATUS_PCS_SYNCED |
5055 MAC_STATUS_RCVD_CFG)) ==
5056 MAC_STATUS_PCS_SYNCED)) {
5057 tp->serdes_counter--;
5058 current_link_up = 1;
5059 goto out;
5060 }
5061restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 if (workaround)
5063 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005064 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 udelay(5);
5066 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5067
Michael Chan3d3ebe72006-09-27 15:59:15 -07005068 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005069 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5071 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005072 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 mac_status = tr32(MAC_STATUS);
5074
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005075 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005077 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Matt Carlson82cd3d12007-12-20 20:09:00 -08005079 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5080 local_adv |= ADVERTISE_1000XPAUSE;
5081 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5082 local_adv |= ADVERTISE_1000XPSE_ASYM;
5083
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005084 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005085 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005086 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005087 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Matt Carlson859edb22011-12-08 14:40:16 +00005089 tp->link_config.rmt_adv =
5090 mii_adv_to_ethtool_adv_x(remote_adv);
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 tg3_setup_flow_control(tp, local_adv, remote_adv);
5093 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005094 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005095 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005096 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005097 if (tp->serdes_counter)
5098 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 else {
5100 if (workaround) {
5101 u32 val = serdes_cfg;
5102
5103 if (port_a)
5104 val |= 0xc010000;
5105 else
5106 val |= 0x4010000;
5107
5108 tw32_f(MAC_SERDES_CFG, val);
5109 }
5110
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005111 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 udelay(40);
5113
5114 /* Link parallel detection - link is up */
5115 /* only if we have PCS_SYNC and not */
5116 /* receiving config code words */
5117 mac_status = tr32(MAC_STATUS);
5118 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5119 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5120 tg3_setup_flow_control(tp, 0, 0);
5121 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005122 tp->phy_flags |=
5123 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005124 tp->serdes_counter =
5125 SERDES_PARALLEL_DET_TIMEOUT;
5126 } else
5127 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 }
5129 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005130 } else {
5131 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005132 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134
5135out:
5136 return current_link_up;
5137}
5138
5139static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5140{
5141 int current_link_up = 0;
5142
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005143 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
5146 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005147 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005149
Matt Carlson5be73b42007-12-20 20:09:29 -08005150 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5151 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Matt Carlson5be73b42007-12-20 20:09:29 -08005153 if (txflags & ANEG_CFG_PS1)
5154 local_adv |= ADVERTISE_1000XPAUSE;
5155 if (txflags & ANEG_CFG_PS2)
5156 local_adv |= ADVERTISE_1000XPSE_ASYM;
5157
5158 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5159 remote_adv |= LPA_1000XPAUSE;
5160 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5161 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Matt Carlson859edb22011-12-08 14:40:16 +00005163 tp->link_config.rmt_adv =
5164 mii_adv_to_ethtool_adv_x(remote_adv);
5165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 tg3_setup_flow_control(tp, local_adv, remote_adv);
5167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 current_link_up = 1;
5169 }
5170 for (i = 0; i < 30; i++) {
5171 udelay(20);
5172 tw32_f(MAC_STATUS,
5173 (MAC_STATUS_SYNC_CHANGED |
5174 MAC_STATUS_CFG_CHANGED));
5175 udelay(40);
5176 if ((tr32(MAC_STATUS) &
5177 (MAC_STATUS_SYNC_CHANGED |
5178 MAC_STATUS_CFG_CHANGED)) == 0)
5179 break;
5180 }
5181
5182 mac_status = tr32(MAC_STATUS);
5183 if (current_link_up == 0 &&
5184 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5185 !(mac_status & MAC_STATUS_RCVD_CFG))
5186 current_link_up = 1;
5187 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005188 tg3_setup_flow_control(tp, 0, 0);
5189
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 /* Forcing 1000FD link up. */
5191 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
5193 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5194 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005195
5196 tw32_f(MAC_MODE, tp->mac_mode);
5197 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199
5200out:
5201 return current_link_up;
5202}
5203
5204static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5205{
5206 u32 orig_pause_cfg;
5207 u16 orig_active_speed;
5208 u8 orig_active_duplex;
5209 u32 mac_status;
5210 int current_link_up;
5211 int i;
5212
Matt Carlson8d018622007-12-20 20:05:44 -08005213 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 orig_active_speed = tp->link_config.active_speed;
5215 orig_active_duplex = tp->link_config.active_duplex;
5216
Joe Perches63c3a662011-04-26 08:12:10 +00005217 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005218 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005219 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 mac_status = tr32(MAC_STATUS);
5221 mac_status &= (MAC_STATUS_PCS_SYNCED |
5222 MAC_STATUS_SIGNAL_DET |
5223 MAC_STATUS_CFG_CHANGED |
5224 MAC_STATUS_RCVD_CFG);
5225 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5226 MAC_STATUS_SIGNAL_DET)) {
5227 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5228 MAC_STATUS_CFG_CHANGED));
5229 return 0;
5230 }
5231 }
5232
5233 tw32_f(MAC_TX_AUTO_NEG, 0);
5234
5235 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5236 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5237 tw32_f(MAC_MODE, tp->mac_mode);
5238 udelay(40);
5239
Matt Carlson79eb6902010-02-17 15:17:03 +00005240 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 tg3_init_bcm8002(tp);
5242
5243 /* Enable link change event even when serdes polling. */
5244 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5245 udelay(40);
5246
5247 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005248 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 mac_status = tr32(MAC_STATUS);
5250
Joe Perches63c3a662011-04-26 08:12:10 +00005251 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5253 else
5254 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5255
Matt Carlson898a56f2009-08-28 14:02:40 +00005256 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005258 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
5260 for (i = 0; i < 100; i++) {
5261 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5262 MAC_STATUS_CFG_CHANGED));
5263 udelay(5);
5264 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005265 MAC_STATUS_CFG_CHANGED |
5266 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 break;
5268 }
5269
5270 mac_status = tr32(MAC_STATUS);
5271 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5272 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005273 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5274 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 tw32_f(MAC_MODE, (tp->mac_mode |
5276 MAC_MODE_SEND_CONFIGS));
5277 udelay(1);
5278 tw32_f(MAC_MODE, tp->mac_mode);
5279 }
5280 }
5281
5282 if (current_link_up == 1) {
5283 tp->link_config.active_speed = SPEED_1000;
5284 tp->link_config.active_duplex = DUPLEX_FULL;
5285 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5286 LED_CTRL_LNKLED_OVERRIDE |
5287 LED_CTRL_1000MBPS_ON));
5288 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005289 tp->link_config.active_speed = SPEED_UNKNOWN;
5290 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5292 LED_CTRL_LNKLED_OVERRIDE |
5293 LED_CTRL_TRAFFIC_OVERRIDE));
5294 }
5295
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005296 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005297 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 if (orig_pause_cfg != now_pause_cfg ||
5299 orig_active_speed != tp->link_config.active_speed ||
5300 orig_active_duplex != tp->link_config.active_duplex)
5301 tg3_link_report(tp);
5302 }
5303
5304 return 0;
5305}
5306
Michael Chan747e8f82005-07-25 12:33:22 -07005307static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5308{
5309 int current_link_up, err = 0;
5310 u32 bmsr, bmcr;
5311 u16 current_speed;
5312 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005313 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005314
5315 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5316 tw32_f(MAC_MODE, tp->mac_mode);
5317 udelay(40);
5318
5319 tw32(MAC_EVENT, 0);
5320
5321 tw32_f(MAC_STATUS,
5322 (MAC_STATUS_SYNC_CHANGED |
5323 MAC_STATUS_CFG_CHANGED |
5324 MAC_STATUS_MI_COMPLETION |
5325 MAC_STATUS_LNKSTATE_CHANGED));
5326 udelay(40);
5327
5328 if (force_reset)
5329 tg3_phy_reset(tp);
5330
5331 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005332 current_speed = SPEED_UNKNOWN;
5333 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005334 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005335
5336 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5337 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005338 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005339 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5340 bmsr |= BMSR_LSTATUS;
5341 else
5342 bmsr &= ~BMSR_LSTATUS;
5343 }
Michael Chan747e8f82005-07-25 12:33:22 -07005344
5345 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5346
5347 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005348 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005349 /* do nothing, just check for link up at the end */
5350 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005351 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005352
5353 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005354 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5355 ADVERTISE_1000XPAUSE |
5356 ADVERTISE_1000XPSE_ASYM |
5357 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005358
Matt Carlson28011cf2011-11-16 18:36:59 -05005359 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005360 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005361
Matt Carlson28011cf2011-11-16 18:36:59 -05005362 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5363 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005364 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5365 tg3_writephy(tp, MII_BMCR, bmcr);
5366
5367 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005368 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005369 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005370
5371 return err;
5372 }
5373 } else {
5374 u32 new_bmcr;
5375
5376 bmcr &= ~BMCR_SPEED1000;
5377 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5378
5379 if (tp->link_config.duplex == DUPLEX_FULL)
5380 new_bmcr |= BMCR_FULLDPLX;
5381
5382 if (new_bmcr != bmcr) {
5383 /* BMCR_SPEED1000 is a reserved bit that needs
5384 * to be set on write.
5385 */
5386 new_bmcr |= BMCR_SPEED1000;
5387
5388 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005389 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005390 u32 adv;
5391
5392 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5393 adv &= ~(ADVERTISE_1000XFULL |
5394 ADVERTISE_1000XHALF |
5395 ADVERTISE_SLCT);
5396 tg3_writephy(tp, MII_ADVERTISE, adv);
5397 tg3_writephy(tp, MII_BMCR, bmcr |
5398 BMCR_ANRESTART |
5399 BMCR_ANENABLE);
5400 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005401 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005402 }
5403 tg3_writephy(tp, MII_BMCR, new_bmcr);
5404 bmcr = new_bmcr;
5405 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5406 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Joe Perches41535772013-02-16 11:20:04 +00005407 if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08005408 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5409 bmsr |= BMSR_LSTATUS;
5410 else
5411 bmsr &= ~BMSR_LSTATUS;
5412 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005413 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005414 }
5415 }
5416
5417 if (bmsr & BMSR_LSTATUS) {
5418 current_speed = SPEED_1000;
5419 current_link_up = 1;
5420 if (bmcr & BMCR_FULLDPLX)
5421 current_duplex = DUPLEX_FULL;
5422 else
5423 current_duplex = DUPLEX_HALF;
5424
Matt Carlsonef167e22007-12-20 20:10:01 -08005425 local_adv = 0;
5426 remote_adv = 0;
5427
Michael Chan747e8f82005-07-25 12:33:22 -07005428 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005429 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005430
5431 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5432 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5433 common = local_adv & remote_adv;
5434 if (common & (ADVERTISE_1000XHALF |
5435 ADVERTISE_1000XFULL)) {
5436 if (common & ADVERTISE_1000XFULL)
5437 current_duplex = DUPLEX_FULL;
5438 else
5439 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005440
5441 tp->link_config.rmt_adv =
5442 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005443 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005444 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005445 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005446 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005447 }
Michael Chan747e8f82005-07-25 12:33:22 -07005448 }
5449 }
5450
Matt Carlsonef167e22007-12-20 20:10:01 -08005451 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5452 tg3_setup_flow_control(tp, local_adv, remote_adv);
5453
Michael Chan747e8f82005-07-25 12:33:22 -07005454 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5455 if (tp->link_config.active_duplex == DUPLEX_HALF)
5456 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5457
5458 tw32_f(MAC_MODE, tp->mac_mode);
5459 udelay(40);
5460
5461 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5462
5463 tp->link_config.active_speed = current_speed;
5464 tp->link_config.active_duplex = current_duplex;
5465
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005466 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005467 return err;
5468}
5469
5470static void tg3_serdes_parallel_detect(struct tg3 *tp)
5471{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005472 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005473 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005474 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005475 return;
5476 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005477
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005478 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005479 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5480 u32 bmcr;
5481
5482 tg3_readphy(tp, MII_BMCR, &bmcr);
5483 if (bmcr & BMCR_ANENABLE) {
5484 u32 phy1, phy2;
5485
5486 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005487 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5488 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005489
5490 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005491 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5492 MII_TG3_DSP_EXP1_INT_STAT);
5493 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5494 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005495
5496 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5497 /* We have signal detect and not receiving
5498 * config code words, link is up by parallel
5499 * detection.
5500 */
5501
5502 bmcr &= ~BMCR_ANENABLE;
5503 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5504 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005505 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005506 }
5507 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005508 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005509 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005510 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005511 u32 phy2;
5512
5513 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005514 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5515 MII_TG3_DSP_EXP1_INT_STAT);
5516 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005517 if (phy2 & 0x20) {
5518 u32 bmcr;
5519
5520 /* Config code words received, turn on autoneg. */
5521 tg3_readphy(tp, MII_BMCR, &bmcr);
5522 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5523
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005524 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005525
5526 }
5527 }
5528}
5529
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5531{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005532 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 int err;
5534
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005535 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005537 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005538 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005539 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
Joe Perches41535772013-02-16 11:20:04 +00005542 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005543 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005544
5545 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5546 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5547 scale = 65;
5548 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5549 scale = 6;
5550 else
5551 scale = 12;
5552
5553 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5554 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5555 tw32(GRC_MISC_CFG, val);
5556 }
5557
Matt Carlsonf2096f92011-04-05 14:22:48 +00005558 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5559 (6 << TX_LENGTHS_IPG_SHIFT);
Joe Perches41535772013-02-16 11:20:04 +00005560 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
5561 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005562 val |= tr32(MAC_TX_LENGTHS) &
5563 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5564 TX_LENGTHS_CNT_DWN_VAL_MSK);
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 if (tp->link_config.active_speed == SPEED_1000 &&
5567 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005568 tw32(MAC_TX_LENGTHS, val |
5569 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005571 tw32(MAC_TX_LENGTHS, val |
5572 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
Joe Perches63c3a662011-04-26 08:12:10 +00005574 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005575 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005577 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 } else {
5579 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5580 }
5581 }
5582
Joe Perches63c3a662011-04-26 08:12:10 +00005583 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005584 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005585 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005586 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5587 tp->pwrmgmt_thresh;
5588 else
5589 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5590 tw32(PCIE_PWR_MGMT_THRESH, val);
5591 }
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 return err;
5594}
5595
Matt Carlsonbe947302012-12-03 19:36:57 +00005596/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005597static u64 tg3_refclk_read(struct tg3 *tp)
5598{
5599 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5600 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5601}
5602
5603/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005604static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5605{
5606 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5607 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5608 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5609 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5610}
5611
Matt Carlson7d41e492012-12-03 19:36:58 +00005612static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5613static inline void tg3_full_unlock(struct tg3 *tp);
5614static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5615{
5616 struct tg3 *tp = netdev_priv(dev);
5617
5618 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5619 SOF_TIMESTAMPING_RX_SOFTWARE |
5620 SOF_TIMESTAMPING_SOFTWARE |
5621 SOF_TIMESTAMPING_TX_HARDWARE |
5622 SOF_TIMESTAMPING_RX_HARDWARE |
5623 SOF_TIMESTAMPING_RAW_HARDWARE;
5624
5625 if (tp->ptp_clock)
5626 info->phc_index = ptp_clock_index(tp->ptp_clock);
5627 else
5628 info->phc_index = -1;
5629
5630 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5631
5632 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5633 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5634 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5635 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5636 return 0;
5637}
5638
5639static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5640{
5641 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5642 bool neg_adj = false;
5643 u32 correction = 0;
5644
5645 if (ppb < 0) {
5646 neg_adj = true;
5647 ppb = -ppb;
5648 }
5649
5650 /* Frequency adjustment is performed using hardware with a 24 bit
5651 * accumulator and a programmable correction value. On each clk, the
5652 * correction value gets added to the accumulator and when it
5653 * overflows, the time counter is incremented/decremented.
5654 *
5655 * So conversion from ppb to correction value is
5656 * ppb * (1 << 24) / 1000000000
5657 */
5658 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5659 TG3_EAV_REF_CLK_CORRECT_MASK;
5660
5661 tg3_full_lock(tp, 0);
5662
5663 if (correction)
5664 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5665 TG3_EAV_REF_CLK_CORRECT_EN |
5666 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5667 else
5668 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5669
5670 tg3_full_unlock(tp);
5671
5672 return 0;
5673}
5674
5675static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5676{
5677 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5678
5679 tg3_full_lock(tp, 0);
5680 tp->ptp_adjust += delta;
5681 tg3_full_unlock(tp);
5682
5683 return 0;
5684}
5685
5686static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5687{
5688 u64 ns;
5689 u32 remainder;
5690 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5691
5692 tg3_full_lock(tp, 0);
5693 ns = tg3_refclk_read(tp);
5694 ns += tp->ptp_adjust;
5695 tg3_full_unlock(tp);
5696
5697 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5698 ts->tv_nsec = remainder;
5699
5700 return 0;
5701}
5702
5703static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5704 const struct timespec *ts)
5705{
5706 u64 ns;
5707 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5708
5709 ns = timespec_to_ns(ts);
5710
5711 tg3_full_lock(tp, 0);
5712 tg3_refclk_write(tp, ns);
5713 tp->ptp_adjust = 0;
5714 tg3_full_unlock(tp);
5715
5716 return 0;
5717}
5718
5719static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5720 struct ptp_clock_request *rq, int on)
5721{
5722 return -EOPNOTSUPP;
5723}
5724
5725static const struct ptp_clock_info tg3_ptp_caps = {
5726 .owner = THIS_MODULE,
5727 .name = "tg3 clock",
5728 .max_adj = 250000000,
5729 .n_alarm = 0,
5730 .n_ext_ts = 0,
5731 .n_per_out = 0,
5732 .pps = 0,
5733 .adjfreq = tg3_ptp_adjfreq,
5734 .adjtime = tg3_ptp_adjtime,
5735 .gettime = tg3_ptp_gettime,
5736 .settime = tg3_ptp_settime,
5737 .enable = tg3_ptp_enable,
5738};
5739
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005740static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5741 struct skb_shared_hwtstamps *timestamp)
5742{
5743 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5744 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5745 tp->ptp_adjust);
5746}
5747
Matt Carlsonbe947302012-12-03 19:36:57 +00005748/* tp->lock must be held */
5749static void tg3_ptp_init(struct tg3 *tp)
5750{
5751 if (!tg3_flag(tp, PTP_CAPABLE))
5752 return;
5753
5754 /* Initialize the hardware clock to the system time. */
5755 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5756 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005757 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005758}
5759
5760/* tp->lock must be held */
5761static void tg3_ptp_resume(struct tg3 *tp)
5762{
5763 if (!tg3_flag(tp, PTP_CAPABLE))
5764 return;
5765
5766 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5767 tp->ptp_adjust = 0;
5768}
5769
5770static void tg3_ptp_fini(struct tg3 *tp)
5771{
5772 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5773 return;
5774
Matt Carlson7d41e492012-12-03 19:36:58 +00005775 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005776 tp->ptp_clock = NULL;
5777 tp->ptp_adjust = 0;
5778}
5779
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005780static inline int tg3_irq_sync(struct tg3 *tp)
5781{
5782 return tp->irq_sync;
5783}
5784
Matt Carlson97bd8e42011-04-13 11:05:04 +00005785static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5786{
5787 int i;
5788
5789 dst = (u32 *)((u8 *)dst + off);
5790 for (i = 0; i < len; i += sizeof(u32))
5791 *dst++ = tr32(off + i);
5792}
5793
5794static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5795{
5796 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5797 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5798 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5799 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5800 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5801 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5802 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5803 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5804 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5805 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5806 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5807 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5808 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5809 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5810 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5811 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5812 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5813 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5814 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5815
Joe Perches63c3a662011-04-26 08:12:10 +00005816 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005817 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5818
5819 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5820 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5821 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5822 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5823 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5824 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5825 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5826 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5827
Joe Perches63c3a662011-04-26 08:12:10 +00005828 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005829 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5830 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5831 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5832 }
5833
5834 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5835 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5836 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5837 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5838 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5839
Joe Perches63c3a662011-04-26 08:12:10 +00005840 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005841 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5842}
5843
5844static void tg3_dump_state(struct tg3 *tp)
5845{
5846 int i;
5847 u32 *regs;
5848
5849 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
Joe Perchesb2adaca2013-02-03 17:43:58 +00005850 if (!regs)
Matt Carlson97bd8e42011-04-13 11:05:04 +00005851 return;
Matt Carlson97bd8e42011-04-13 11:05:04 +00005852
Joe Perches63c3a662011-04-26 08:12:10 +00005853 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005854 /* Read up to but not including private PCI registers */
5855 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5856 regs[i / sizeof(u32)] = tr32(i);
5857 } else
5858 tg3_dump_legacy_regs(tp, regs);
5859
5860 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5861 if (!regs[i + 0] && !regs[i + 1] &&
5862 !regs[i + 2] && !regs[i + 3])
5863 continue;
5864
5865 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5866 i * 4,
5867 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5868 }
5869
5870 kfree(regs);
5871
5872 for (i = 0; i < tp->irq_cnt; i++) {
5873 struct tg3_napi *tnapi = &tp->napi[i];
5874
5875 /* SW status block */
5876 netdev_err(tp->dev,
5877 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5878 i,
5879 tnapi->hw_status->status,
5880 tnapi->hw_status->status_tag,
5881 tnapi->hw_status->rx_jumbo_consumer,
5882 tnapi->hw_status->rx_consumer,
5883 tnapi->hw_status->rx_mini_consumer,
5884 tnapi->hw_status->idx[0].rx_producer,
5885 tnapi->hw_status->idx[0].tx_consumer);
5886
5887 netdev_err(tp->dev,
5888 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5889 i,
5890 tnapi->last_tag, tnapi->last_irq_tag,
5891 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5892 tnapi->rx_rcb_ptr,
5893 tnapi->prodring.rx_std_prod_idx,
5894 tnapi->prodring.rx_std_cons_idx,
5895 tnapi->prodring.rx_jmb_prod_idx,
5896 tnapi->prodring.rx_jmb_cons_idx);
5897 }
5898}
5899
Michael Chandf3e6542006-05-26 17:48:07 -07005900/* This is called whenever we suspect that the system chipset is re-
5901 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5902 * is bogus tx completions. We try to recover by setting the
5903 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5904 * in the workqueue.
5905 */
5906static void tg3_tx_recover(struct tg3 *tp)
5907{
Joe Perches63c3a662011-04-26 08:12:10 +00005908 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005909 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5910
Matt Carlson5129c3a2010-04-05 10:19:23 +00005911 netdev_warn(tp->dev,
5912 "The system may be re-ordering memory-mapped I/O "
5913 "cycles to the network device, attempting to recover. "
5914 "Please report the problem to the driver maintainer "
5915 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005916
5917 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005918 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005919 spin_unlock(&tp->lock);
5920}
5921
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005922static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005923{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005924 /* Tell compiler to fetch tx indices from memory. */
5925 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005926 return tnapi->tx_pending -
5927 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005928}
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930/* Tigon3 never reports partial packet sends. So we do not
5931 * need special logic to handle SKBs that have not had all
5932 * of their frags sent yet, like SunGEM does.
5933 */
Matt Carlson17375d22009-08-28 14:02:18 +00005934static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935{
Matt Carlson17375d22009-08-28 14:02:18 +00005936 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005937 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005938 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005939 struct netdev_queue *txq;
5940 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005941 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005942
Joe Perches63c3a662011-04-26 08:12:10 +00005943 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005944 index--;
5945
5946 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
5948 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005949 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005951 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Michael Chandf3e6542006-05-26 17:48:07 -07005953 if (unlikely(skb == NULL)) {
5954 tg3_tx_recover(tp);
5955 return;
5956 }
5957
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005958 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
5959 struct skb_shared_hwtstamps timestamp;
5960 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
5961 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
5962
5963 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
5964
5965 skb_tstamp_tx(skb, &timestamp);
5966 }
5967
Alexander Duyckf4188d82009-12-02 16:48:38 +00005968 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005969 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005970 skb_headlen(skb),
5971 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973 ri->skb = NULL;
5974
Matt Carlsone01ee142011-07-27 14:20:50 +00005975 while (ri->fragmented) {
5976 ri->fragmented = false;
5977 sw_idx = NEXT_TX(sw_idx);
5978 ri = &tnapi->tx_buffers[sw_idx];
5979 }
5980
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 sw_idx = NEXT_TX(sw_idx);
5982
5983 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005984 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005985 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5986 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005987
5988 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005989 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005990 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005991 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005992
5993 while (ri->fragmented) {
5994 ri->fragmented = false;
5995 sw_idx = NEXT_TX(sw_idx);
5996 ri = &tnapi->tx_buffers[sw_idx];
5997 }
5998
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 sw_idx = NEXT_TX(sw_idx);
6000 }
6001
Tom Herbert298376d2011-11-28 16:33:30 +00006002 pkts_compl++;
6003 bytes_compl += skb->len;
6004
David S. Millerf47c11e2005-06-24 20:18:35 -07006005 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07006006
6007 if (unlikely(tx_bug)) {
6008 tg3_tx_recover(tp);
6009 return;
6010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 }
6012
Tom Herbert5cb917b2012-03-05 19:53:50 +00006013 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00006014
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006015 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Michael Chan1b2a7202006-08-07 21:46:02 -07006017 /* Need to make the tx_cons update visible to tg3_start_xmit()
6018 * before checking for netif_queue_stopped(). Without the
6019 * memory barrier, there is a small possibility that tg3_start_xmit()
6020 * will miss it and cause the queue to be stopped forever.
6021 */
6022 smp_mb();
6023
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006024 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006025 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006026 __netif_tx_lock(txq, smp_processor_id());
6027 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006028 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00006029 netif_tx_wake_queue(txq);
6030 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006034static void tg3_frag_free(bool is_frag, void *data)
6035{
6036 if (is_frag)
6037 put_page(virt_to_head_page(data));
6038 else
6039 kfree(data);
6040}
6041
Eric Dumazet9205fd92011-11-18 06:47:01 +00006042static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006043{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006044 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
6045 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6046
Eric Dumazet9205fd92011-11-18 06:47:01 +00006047 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006048 return;
6049
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006050 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006051 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006052 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006053 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006054}
6055
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006056
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057/* Returns size of skb allocated or < 0 on error.
6058 *
6059 * We only need to fill in the address because the other members
6060 * of the RX descriptor are invariant, see tg3_init_rings.
6061 *
6062 * Note the purposeful assymetry of cpu vs. chip accesses. For
6063 * posting buffers we only dirty the first cache line of the RX
6064 * descriptor (containing the address). Whereas for the RX status
6065 * buffers the cpu only reads the last cacheline of the RX descriptor
6066 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6067 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006068static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006069 u32 opaque_key, u32 dest_idx_unmasked,
6070 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071{
6072 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006073 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006074 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006076 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 switch (opaque_key) {
6079 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006080 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006081 desc = &tpr->rx_std[dest_idx];
6082 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006083 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 break;
6085
6086 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006087 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006088 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006089 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006090 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 break;
6092
6093 default:
6094 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 /* Do not overwrite any of the map or rp information
6098 * until we are sure we can commit to a new buffer.
6099 *
6100 * Callers depend upon this behavior and assume that
6101 * we leave everything unchanged if we fail.
6102 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006103 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6104 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006105 if (skb_size <= PAGE_SIZE) {
6106 data = netdev_alloc_frag(skb_size);
6107 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006108 } else {
6109 data = kmalloc(skb_size, GFP_ATOMIC);
6110 *frag_size = 0;
6111 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006112 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 return -ENOMEM;
6114
Eric Dumazet9205fd92011-11-18 06:47:01 +00006115 mapping = pci_map_single(tp->pdev,
6116 data + TG3_RX_OFFSET(tp),
6117 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006119 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006120 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006121 return -EIO;
6122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Eric Dumazet9205fd92011-11-18 06:47:01 +00006124 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006125 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 desc->addr_hi = ((u64)mapping >> 32);
6128 desc->addr_lo = ((u64)mapping & 0xffffffff);
6129
Eric Dumazet9205fd92011-11-18 06:47:01 +00006130 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131}
6132
6133/* We only need to move over in the address because the other
6134 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006135 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 */
Matt Carlsona3896162009-11-13 13:03:44 +00006137static void tg3_recycle_rx(struct tg3_napi *tnapi,
6138 struct tg3_rx_prodring_set *dpr,
6139 u32 opaque_key, int src_idx,
6140 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141{
Matt Carlson17375d22009-08-28 14:02:18 +00006142 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6144 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006145 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006146 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
6148 switch (opaque_key) {
6149 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006150 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006151 dest_desc = &dpr->rx_std[dest_idx];
6152 dest_map = &dpr->rx_std_buffers[dest_idx];
6153 src_desc = &spr->rx_std[src_idx];
6154 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 break;
6156
6157 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006158 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006159 dest_desc = &dpr->rx_jmb[dest_idx].std;
6160 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6161 src_desc = &spr->rx_jmb[src_idx].std;
6162 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 break;
6164
6165 default:
6166 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
Eric Dumazet9205fd92011-11-18 06:47:01 +00006169 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006170 dma_unmap_addr_set(dest_map, mapping,
6171 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 dest_desc->addr_hi = src_desc->addr_hi;
6173 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006174
6175 /* Ensure that the update to the skb happens after the physical
6176 * addresses have been transferred to the new BD location.
6177 */
6178 smp_wmb();
6179
Eric Dumazet9205fd92011-11-18 06:47:01 +00006180 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181}
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183/* The RX ring scheme is composed of multiple rings which post fresh
6184 * buffers to the chip, and one special ring the chip uses to report
6185 * status back to the host.
6186 *
6187 * The special ring reports the status of received packets to the
6188 * host. The chip does not write into the original descriptor the
6189 * RX buffer was obtained from. The chip simply takes the original
6190 * descriptor as provided by the host, updates the status and length
6191 * field, then writes this into the next status ring entry.
6192 *
6193 * Each ring the host uses to post buffers to the chip is described
6194 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6195 * it is first placed into the on-chip ram. When the packet's length
6196 * is known, it walks down the TG3_BDINFO entries to select the ring.
6197 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6198 * which is within the range of the new packet's length is chosen.
6199 *
6200 * The "separate ring for rx status" scheme may sound queer, but it makes
6201 * sense from a cache coherency perspective. If only the host writes
6202 * to the buffer post rings, and only the chip writes to the rx status
6203 * rings, then cache lines never move beyond shared-modified state.
6204 * If both the host and chip were to write into the same ring, cache line
6205 * eviction could occur since both entities want it in an exclusive state.
6206 */
Matt Carlson17375d22009-08-28 14:02:18 +00006207static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
Matt Carlson17375d22009-08-28 14:02:18 +00006209 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006210 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006211 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006212 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006213 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006215 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006217 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 /*
6219 * We need to order the read of hw_idx and the read of
6220 * the opaque cookie.
6221 */
6222 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 work_mask = 0;
6224 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006225 std_prod_idx = tpr->rx_std_prod_idx;
6226 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006228 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006229 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 unsigned int len;
6231 struct sk_buff *skb;
6232 dma_addr_t dma_addr;
6233 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006234 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006235 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6238 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6239 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006240 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006241 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006242 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006243 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006244 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006246 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006247 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006248 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006249 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006250 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
6253 work_mask |= opaque_key;
6254
6255 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6256 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6257 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006258 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 desc_idx, *post_ptr);
6260 drop_it_no_recycle:
6261 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006262 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 goto next_pkt;
6264 }
6265
Eric Dumazet9205fd92011-11-18 06:47:01 +00006266 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006267 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6268 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006270 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6271 RXD_FLAG_PTPSTAT_PTPV1 ||
6272 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6273 RXD_FLAG_PTPSTAT_PTPV2) {
6274 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6275 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6276 }
6277
Matt Carlsond2757fc2010-04-12 06:58:27 +00006278 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006280 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Eric Dumazet9205fd92011-11-18 06:47:01 +00006282 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006283 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 if (skb_size < 0)
6285 goto drop_it;
6286
Matt Carlson287be122009-08-28 13:58:46 +00006287 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 PCI_DMA_FROMDEVICE);
6289
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006290 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006291 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006292 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006293 goto drop_it_no_recycle;
6294 }
6295 skb_reserve(skb, TG3_RX_OFFSET(tp));
6296 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006297 * after the usage of the old DMA mapping.
6298 */
6299 smp_wmb();
6300
Eric Dumazet9205fd92011-11-18 06:47:01 +00006301 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006302
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006304 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 desc_idx, *post_ptr);
6306
Eric Dumazet9205fd92011-11-18 06:47:01 +00006307 skb = netdev_alloc_skb(tp->dev,
6308 len + TG3_RAW_IP_ALIGN);
6309 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 goto drop_it_no_recycle;
6311
Eric Dumazet9205fd92011-11-18 06:47:01 +00006312 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006314 memcpy(skb->data,
6315 data + TG3_RX_OFFSET(tp),
6316 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 }
6319
Eric Dumazet9205fd92011-11-18 06:47:01 +00006320 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006321 if (tstamp)
6322 tg3_hwclock_to_timestamp(tp, tstamp,
6323 skb_hwtstamps(skb));
6324
Michał Mirosławdc668912011-04-07 03:35:07 +00006325 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6327 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6328 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6329 skb->ip_summed = CHECKSUM_UNNECESSARY;
6330 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006331 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
6333 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006334
6335 if (len > (tp->dev->mtu + ETH_HLEN) &&
6336 skb->protocol != htons(ETH_P_8021Q)) {
6337 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006338 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006339 }
6340
Matt Carlson9dc7a112010-04-12 06:58:28 +00006341 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006342 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6343 __vlan_hwaccel_put_tag(skb,
6344 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006345
Matt Carlsonbf933c82011-01-25 15:58:49 +00006346 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 received++;
6349 budget--;
6350
6351next_pkt:
6352 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006353
6354 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006355 tpr->rx_std_prod_idx = std_prod_idx &
6356 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006357 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6358 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006359 work_mask &= ~RXD_OPAQUE_RING_STD;
6360 rx_std_posted = 0;
6361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006363 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006364 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006365
6366 /* Refresh hw_idx to see if there is new work */
6367 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006368 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006369 rmb();
6370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 }
6372
6373 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006374 tnapi->rx_rcb_ptr = sw_idx;
6375 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
6377 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006378 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006379 /* Sync BD data before updating mailbox */
6380 wmb();
6381
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006382 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006383 tpr->rx_std_prod_idx = std_prod_idx &
6384 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006385 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6386 tpr->rx_std_prod_idx);
6387 }
6388 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006389 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6390 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006391 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6392 tpr->rx_jmb_prod_idx);
6393 }
6394 mmiowb();
6395 } else if (work_mask) {
6396 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6397 * updated before the producer indices can be updated.
6398 */
6399 smp_wmb();
6400
Matt Carlson2c49a442010-09-30 10:34:35 +00006401 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6402 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006403
Michael Chan7ae52892012-03-21 15:38:33 +00006404 if (tnapi != &tp->napi[1]) {
6405 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006406 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
6410 return received;
6411}
6412
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006413static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006416 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006417 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6418
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 if (sblk->status & SD_STATUS_LINK_CHG) {
6420 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006421 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006422 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006423 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006424 tw32_f(MAC_STATUS,
6425 (MAC_STATUS_SYNC_CHANGED |
6426 MAC_STATUS_CFG_CHANGED |
6427 MAC_STATUS_MI_COMPLETION |
6428 MAC_STATUS_LNKSTATE_CHANGED));
6429 udelay(40);
6430 } else
6431 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006432 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 }
6434 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006435}
6436
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006437static int tg3_rx_prodring_xfer(struct tg3 *tp,
6438 struct tg3_rx_prodring_set *dpr,
6439 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006440{
6441 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006442 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006443
6444 while (1) {
6445 src_prod_idx = spr->rx_std_prod_idx;
6446
6447 /* Make sure updates to the rx_std_buffers[] entries and the
6448 * standard producer index are seen in the correct order.
6449 */
6450 smp_rmb();
6451
6452 if (spr->rx_std_cons_idx == src_prod_idx)
6453 break;
6454
6455 if (spr->rx_std_cons_idx < src_prod_idx)
6456 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6457 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006458 cpycnt = tp->rx_std_ring_mask + 1 -
6459 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006460
Matt Carlson2c49a442010-09-30 10:34:35 +00006461 cpycnt = min(cpycnt,
6462 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006463
6464 si = spr->rx_std_cons_idx;
6465 di = dpr->rx_std_prod_idx;
6466
Matt Carlsone92967b2010-02-12 14:47:06 +00006467 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006468 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006469 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006470 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006471 break;
6472 }
6473 }
6474
6475 if (!cpycnt)
6476 break;
6477
6478 /* Ensure that updates to the rx_std_buffers ring and the
6479 * shadowed hardware producer ring from tg3_recycle_skb() are
6480 * ordered correctly WRT the skb check above.
6481 */
6482 smp_rmb();
6483
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006484 memcpy(&dpr->rx_std_buffers[di],
6485 &spr->rx_std_buffers[si],
6486 cpycnt * sizeof(struct ring_info));
6487
6488 for (i = 0; i < cpycnt; i++, di++, si++) {
6489 struct tg3_rx_buffer_desc *sbd, *dbd;
6490 sbd = &spr->rx_std[si];
6491 dbd = &dpr->rx_std[di];
6492 dbd->addr_hi = sbd->addr_hi;
6493 dbd->addr_lo = sbd->addr_lo;
6494 }
6495
Matt Carlson2c49a442010-09-30 10:34:35 +00006496 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6497 tp->rx_std_ring_mask;
6498 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6499 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006500 }
6501
6502 while (1) {
6503 src_prod_idx = spr->rx_jmb_prod_idx;
6504
6505 /* Make sure updates to the rx_jmb_buffers[] entries and
6506 * the jumbo producer index are seen in the correct order.
6507 */
6508 smp_rmb();
6509
6510 if (spr->rx_jmb_cons_idx == src_prod_idx)
6511 break;
6512
6513 if (spr->rx_jmb_cons_idx < src_prod_idx)
6514 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6515 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006516 cpycnt = tp->rx_jmb_ring_mask + 1 -
6517 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006518
6519 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006520 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006521
6522 si = spr->rx_jmb_cons_idx;
6523 di = dpr->rx_jmb_prod_idx;
6524
Matt Carlsone92967b2010-02-12 14:47:06 +00006525 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006526 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006527 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006528 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006529 break;
6530 }
6531 }
6532
6533 if (!cpycnt)
6534 break;
6535
6536 /* Ensure that updates to the rx_jmb_buffers ring and the
6537 * shadowed hardware producer ring from tg3_recycle_skb() are
6538 * ordered correctly WRT the skb check above.
6539 */
6540 smp_rmb();
6541
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006542 memcpy(&dpr->rx_jmb_buffers[di],
6543 &spr->rx_jmb_buffers[si],
6544 cpycnt * sizeof(struct ring_info));
6545
6546 for (i = 0; i < cpycnt; i++, di++, si++) {
6547 struct tg3_rx_buffer_desc *sbd, *dbd;
6548 sbd = &spr->rx_jmb[si].std;
6549 dbd = &dpr->rx_jmb[di].std;
6550 dbd->addr_hi = sbd->addr_hi;
6551 dbd->addr_lo = sbd->addr_lo;
6552 }
6553
Matt Carlson2c49a442010-09-30 10:34:35 +00006554 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6555 tp->rx_jmb_ring_mask;
6556 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6557 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006558 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006559
6560 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006561}
6562
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006563static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6564{
6565 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566
6567 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006568 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006569 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006570 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006571 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 }
6573
Matt Carlsonf891ea12012-04-24 13:37:01 +00006574 if (!tnapi->rx_rcb_prod_idx)
6575 return work_done;
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 /* run RX thread, within the bounds set by NAPI.
6578 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006579 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006581 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006582 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Joe Perches63c3a662011-04-26 08:12:10 +00006584 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006585 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006586 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006587 u32 std_prod_idx = dpr->rx_std_prod_idx;
6588 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006589
Michael Chan7ae52892012-03-21 15:38:33 +00006590 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006591 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006592 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006593 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006594
6595 wmb();
6596
Matt Carlsone4af1af2010-02-12 14:47:05 +00006597 if (std_prod_idx != dpr->rx_std_prod_idx)
6598 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6599 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006600
Matt Carlsone4af1af2010-02-12 14:47:05 +00006601 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6602 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6603 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006604
6605 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006606
6607 if (err)
6608 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006609 }
6610
David S. Miller6f535762007-10-11 18:08:29 -07006611 return work_done;
6612}
David S. Millerf7383c22005-05-18 22:50:53 -07006613
Matt Carlsondb219972011-11-04 09:15:03 +00006614static inline void tg3_reset_task_schedule(struct tg3 *tp)
6615{
6616 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6617 schedule_work(&tp->reset_task);
6618}
6619
6620static inline void tg3_reset_task_cancel(struct tg3 *tp)
6621{
6622 cancel_work_sync(&tp->reset_task);
6623 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006624 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006625}
6626
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006627static int tg3_poll_msix(struct napi_struct *napi, int budget)
6628{
6629 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6630 struct tg3 *tp = tnapi->tp;
6631 int work_done = 0;
6632 struct tg3_hw_status *sblk = tnapi->hw_status;
6633
6634 while (1) {
6635 work_done = tg3_poll_work(tnapi, work_done, budget);
6636
Joe Perches63c3a662011-04-26 08:12:10 +00006637 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006638 goto tx_recovery;
6639
6640 if (unlikely(work_done >= budget))
6641 break;
6642
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006643 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006644 * to tell the hw how much work has been processed,
6645 * so we must read it before checking for more work.
6646 */
6647 tnapi->last_tag = sblk->status_tag;
6648 tnapi->last_irq_tag = tnapi->last_tag;
6649 rmb();
6650
6651 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006652 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6653 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006654
6655 /* This test here is not race free, but will reduce
6656 * the number of interrupts by looping again.
6657 */
6658 if (tnapi == &tp->napi[1] && tp->rx_refill)
6659 continue;
6660
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006661 napi_complete(napi);
6662 /* Reenable interrupts. */
6663 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006664
6665 /* This test here is synchronized by napi_schedule()
6666 * and napi_complete() to close the race condition.
6667 */
6668 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6669 tw32(HOSTCC_MODE, tp->coalesce_mode |
6670 HOSTCC_MODE_ENABLE |
6671 tnapi->coal_now);
6672 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006673 mmiowb();
6674 break;
6675 }
6676 }
6677
6678 return work_done;
6679
6680tx_recovery:
6681 /* work_done is guaranteed to be less than budget. */
6682 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006683 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006684 return work_done;
6685}
6686
Matt Carlsone64de4e2011-04-13 11:05:05 +00006687static void tg3_process_error(struct tg3 *tp)
6688{
6689 u32 val;
6690 bool real_error = false;
6691
Joe Perches63c3a662011-04-26 08:12:10 +00006692 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006693 return;
6694
6695 /* Check Flow Attention register */
6696 val = tr32(HOSTCC_FLOW_ATTN);
6697 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6698 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6699 real_error = true;
6700 }
6701
6702 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6703 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6704 real_error = true;
6705 }
6706
6707 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6708 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6709 real_error = true;
6710 }
6711
6712 if (!real_error)
6713 return;
6714
6715 tg3_dump_state(tp);
6716
Joe Perches63c3a662011-04-26 08:12:10 +00006717 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006718 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006719}
6720
David S. Miller6f535762007-10-11 18:08:29 -07006721static int tg3_poll(struct napi_struct *napi, int budget)
6722{
Matt Carlson8ef04422009-08-28 14:01:37 +00006723 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6724 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006725 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006726 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006727
6728 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006729 if (sblk->status & SD_STATUS_ERROR)
6730 tg3_process_error(tp);
6731
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006732 tg3_poll_link(tp);
6733
Matt Carlson17375d22009-08-28 14:02:18 +00006734 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006735
Joe Perches63c3a662011-04-26 08:12:10 +00006736 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006737 goto tx_recovery;
6738
6739 if (unlikely(work_done >= budget))
6740 break;
6741
Joe Perches63c3a662011-04-26 08:12:10 +00006742 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006743 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006744 * to tell the hw how much work has been processed,
6745 * so we must read it before checking for more work.
6746 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006747 tnapi->last_tag = sblk->status_tag;
6748 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006749 rmb();
6750 } else
6751 sblk->status &= ~SD_STATUS_UPDATED;
6752
Matt Carlson17375d22009-08-28 14:02:18 +00006753 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006754 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006755 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006756 break;
6757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 }
6759
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006760 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006761
6762tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006763 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006764 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006765 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006766 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767}
6768
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006769static void tg3_napi_disable(struct tg3 *tp)
6770{
6771 int i;
6772
6773 for (i = tp->irq_cnt - 1; i >= 0; i--)
6774 napi_disable(&tp->napi[i].napi);
6775}
6776
6777static void tg3_napi_enable(struct tg3 *tp)
6778{
6779 int i;
6780
6781 for (i = 0; i < tp->irq_cnt; i++)
6782 napi_enable(&tp->napi[i].napi);
6783}
6784
6785static void tg3_napi_init(struct tg3 *tp)
6786{
6787 int i;
6788
6789 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6790 for (i = 1; i < tp->irq_cnt; i++)
6791 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6792}
6793
6794static void tg3_napi_fini(struct tg3 *tp)
6795{
6796 int i;
6797
6798 for (i = 0; i < tp->irq_cnt; i++)
6799 netif_napi_del(&tp->napi[i].napi);
6800}
6801
6802static inline void tg3_netif_stop(struct tg3 *tp)
6803{
6804 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6805 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006806 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006807 netif_tx_disable(tp->dev);
6808}
6809
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006810/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006811static inline void tg3_netif_start(struct tg3 *tp)
6812{
Matt Carlsonbe947302012-12-03 19:36:57 +00006813 tg3_ptp_resume(tp);
6814
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006815 /* NOTE: unconditional netif_tx_wake_all_queues is only
6816 * appropriate so long as all callers are assured to
6817 * have free tx slots (such as after tg3_init_hw)
6818 */
6819 netif_tx_wake_all_queues(tp->dev);
6820
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006821 if (tp->link_up)
6822 netif_carrier_on(tp->dev);
6823
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006824 tg3_napi_enable(tp);
6825 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6826 tg3_enable_ints(tp);
6827}
6828
David S. Millerf47c11e2005-06-24 20:18:35 -07006829static void tg3_irq_quiesce(struct tg3 *tp)
6830{
Matt Carlson4f125f42009-09-01 12:55:02 +00006831 int i;
6832
David S. Millerf47c11e2005-06-24 20:18:35 -07006833 BUG_ON(tp->irq_sync);
6834
6835 tp->irq_sync = 1;
6836 smp_mb();
6837
Matt Carlson4f125f42009-09-01 12:55:02 +00006838 for (i = 0; i < tp->irq_cnt; i++)
6839 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006840}
6841
David S. Millerf47c11e2005-06-24 20:18:35 -07006842/* Fully shutdown all tg3 driver activity elsewhere in the system.
6843 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6844 * with as well. Most of the time, this is not necessary except when
6845 * shutting down the device.
6846 */
6847static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6848{
Michael Chan46966542007-07-11 19:47:19 -07006849 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006850 if (irq_sync)
6851 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006852}
6853
6854static inline void tg3_full_unlock(struct tg3 *tp)
6855{
David S. Millerf47c11e2005-06-24 20:18:35 -07006856 spin_unlock_bh(&tp->lock);
6857}
6858
Michael Chanfcfa0a32006-03-20 22:28:41 -08006859/* One-shot MSI handler - Chip automatically disables interrupt
6860 * after sending MSI so driver doesn't have to do it.
6861 */
David Howells7d12e782006-10-05 14:55:46 +01006862static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006863{
Matt Carlson09943a12009-08-28 14:01:57 +00006864 struct tg3_napi *tnapi = dev_id;
6865 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006866
Matt Carlson898a56f2009-08-28 14:02:40 +00006867 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006868 if (tnapi->rx_rcb)
6869 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006870
6871 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006872 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006873
6874 return IRQ_HANDLED;
6875}
6876
Michael Chan88b06bc22005-04-21 17:13:25 -07006877/* MSI ISR - No need to check for interrupt sharing and no need to
6878 * flush status block and interrupt mailbox. PCI ordering rules
6879 * guarantee that MSI will arrive after the status block.
6880 */
David Howells7d12e782006-10-05 14:55:46 +01006881static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006882{
Matt Carlson09943a12009-08-28 14:01:57 +00006883 struct tg3_napi *tnapi = dev_id;
6884 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006885
Matt Carlson898a56f2009-08-28 14:02:40 +00006886 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006887 if (tnapi->rx_rcb)
6888 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006889 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006890 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006891 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006892 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006893 * NIC to stop sending us irqs, engaging "in-intr-handler"
6894 * event coalescing.
6895 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006896 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006897 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006898 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006899
Michael Chan88b06bc22005-04-21 17:13:25 -07006900 return IRQ_RETVAL(1);
6901}
6902
David Howells7d12e782006-10-05 14:55:46 +01006903static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904{
Matt Carlson09943a12009-08-28 14:01:57 +00006905 struct tg3_napi *tnapi = dev_id;
6906 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006907 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 unsigned int handled = 1;
6909
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 /* In INTx mode, it is possible for the interrupt to arrive at
6911 * the CPU before the status block posted prior to the interrupt.
6912 * Reading the PCI State register will confirm whether the
6913 * interrupt is ours and will flush the status block.
6914 */
Michael Chand18edcb2007-03-24 20:57:11 -07006915 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006916 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006917 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6918 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006919 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006920 }
Michael Chand18edcb2007-03-24 20:57:11 -07006921 }
6922
6923 /*
6924 * Writing any value to intr-mbox-0 clears PCI INTA# and
6925 * chip-internal interrupt pending events.
6926 * Writing non-zero to intr-mbox-0 additional tells the
6927 * NIC to stop sending us irqs, engaging "in-intr-handler"
6928 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006929 *
6930 * Flush the mailbox to de-assert the IRQ immediately to prevent
6931 * spurious interrupts. The flush impacts performance but
6932 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006933 */
Michael Chanc04cb342007-05-07 00:26:15 -07006934 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006935 if (tg3_irq_sync(tp))
6936 goto out;
6937 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006938 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006939 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006940 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006941 } else {
6942 /* No work, shared interrupt perhaps? re-enable
6943 * interrupts, and flush that PCI write
6944 */
6945 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6946 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006947 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006948out:
David S. Millerfac9b832005-05-18 22:46:34 -07006949 return IRQ_RETVAL(handled);
6950}
6951
David Howells7d12e782006-10-05 14:55:46 +01006952static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006953{
Matt Carlson09943a12009-08-28 14:01:57 +00006954 struct tg3_napi *tnapi = dev_id;
6955 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006956 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006957 unsigned int handled = 1;
6958
David S. Millerfac9b832005-05-18 22:46:34 -07006959 /* In INTx mode, it is possible for the interrupt to arrive at
6960 * the CPU before the status block posted prior to the interrupt.
6961 * Reading the PCI State register will confirm whether the
6962 * interrupt is ours and will flush the status block.
6963 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006964 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006965 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006966 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6967 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006968 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 }
Michael Chand18edcb2007-03-24 20:57:11 -07006970 }
6971
6972 /*
6973 * writing any value to intr-mbox-0 clears PCI INTA# and
6974 * chip-internal interrupt pending events.
6975 * writing non-zero to intr-mbox-0 additional tells the
6976 * NIC to stop sending us irqs, engaging "in-intr-handler"
6977 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006978 *
6979 * Flush the mailbox to de-assert the IRQ immediately to prevent
6980 * spurious interrupts. The flush impacts performance but
6981 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006982 */
Michael Chanc04cb342007-05-07 00:26:15 -07006983 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006984
6985 /*
6986 * In a shared interrupt configuration, sometimes other devices'
6987 * interrupts will scream. We record the current status tag here
6988 * so that the above check can report that the screaming interrupts
6989 * are unhandled. Eventually they will be silenced.
6990 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006991 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006992
Michael Chand18edcb2007-03-24 20:57:11 -07006993 if (tg3_irq_sync(tp))
6994 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006995
Matt Carlson72334482009-08-28 14:03:01 +00006996 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006997
Matt Carlson09943a12009-08-28 14:01:57 +00006998 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006999
David S. Millerf47c11e2005-06-24 20:18:35 -07007000out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 return IRQ_RETVAL(handled);
7002}
7003
Michael Chan79381092005-04-21 17:13:59 -07007004/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01007005static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07007006{
Matt Carlson09943a12009-08-28 14:01:57 +00007007 struct tg3_napi *tnapi = dev_id;
7008 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00007009 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07007010
Michael Chanf9804dd2005-09-27 12:13:10 -07007011 if ((sblk->status & SD_STATUS_UPDATED) ||
7012 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07007013 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07007014 return IRQ_RETVAL(1);
7015 }
7016 return IRQ_RETVAL(0);
7017}
7018
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019#ifdef CONFIG_NET_POLL_CONTROLLER
7020static void tg3_poll_controller(struct net_device *dev)
7021{
Matt Carlson4f125f42009-09-01 12:55:02 +00007022 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07007023 struct tg3 *tp = netdev_priv(dev);
7024
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00007025 if (tg3_irq_sync(tp))
7026 return;
7027
Matt Carlson4f125f42009-09-01 12:55:02 +00007028 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00007029 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030}
7031#endif
7032
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033static void tg3_tx_timeout(struct net_device *dev)
7034{
7035 struct tg3 *tp = netdev_priv(dev);
7036
Michael Chanb0408752007-02-13 12:18:30 -08007037 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00007038 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00007039 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08007040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
Matt Carlsondb219972011-11-04 09:15:03 +00007042 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043}
7044
Michael Chanc58ec932005-09-17 00:46:27 -07007045/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
7046static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
7047{
7048 u32 base = (u32) mapping & 0xffffffff;
7049
Eric Dumazet807540b2010-09-23 05:40:09 +00007050 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07007051}
7052
Michael Chan72f2afb2006-03-06 19:28:35 -08007053/* Test for DMA addresses > 40-bit */
7054static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
7055 int len)
7056{
7057#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00007058 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00007059 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007060 return 0;
7061#else
7062 return 0;
7063#endif
7064}
7065
Matt Carlsond1a3b732011-07-27 14:20:51 +00007066static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007067 dma_addr_t mapping, u32 len, u32 flags,
7068 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007069{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007070 txbd->addr_hi = ((u64) mapping >> 32);
7071 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7072 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7073 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
Matt Carlson84b67b22011-07-27 14:20:52 +00007076static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007077 dma_addr_t map, u32 len, u32 flags,
7078 u32 mss, u32 vlan)
7079{
7080 struct tg3 *tp = tnapi->tp;
7081 bool hwbug = false;
7082
7083 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007084 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007085
7086 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007087 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007088
7089 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007090 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007091
Matt Carlsona4cb4282011-12-14 11:09:58 +00007092 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007093 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007094 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007095 while (len > tp->dma_limit && *budget) {
7096 u32 frag_len = tp->dma_limit;
7097 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007098
Matt Carlsonb9e45482011-11-04 09:14:59 +00007099 /* Avoid the 8byte DMA problem */
7100 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007101 len += tp->dma_limit / 2;
7102 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007103 }
7104
Matt Carlsonb9e45482011-11-04 09:14:59 +00007105 tnapi->tx_buffers[*entry].fragmented = true;
7106
7107 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7108 frag_len, tmp_flag, mss, vlan);
7109 *budget -= 1;
7110 prvidx = *entry;
7111 *entry = NEXT_TX(*entry);
7112
Matt Carlsone31aa982011-07-27 14:20:53 +00007113 map += frag_len;
7114 }
7115
7116 if (len) {
7117 if (*budget) {
7118 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7119 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007120 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007121 *entry = NEXT_TX(*entry);
7122 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007123 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007124 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007125 }
7126 }
7127 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007128 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7129 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007130 *entry = NEXT_TX(*entry);
7131 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007132
7133 return hwbug;
7134}
7135
Matt Carlson0d681b22011-07-27 14:20:49 +00007136static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007137{
7138 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007139 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007140 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007141
Matt Carlson0d681b22011-07-27 14:20:49 +00007142 skb = txb->skb;
7143 txb->skb = NULL;
7144
Matt Carlson432aa7e2011-05-19 12:12:45 +00007145 pci_unmap_single(tnapi->tp->pdev,
7146 dma_unmap_addr(txb, mapping),
7147 skb_headlen(skb),
7148 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007149
7150 while (txb->fragmented) {
7151 txb->fragmented = false;
7152 entry = NEXT_TX(entry);
7153 txb = &tnapi->tx_buffers[entry];
7154 }
7155
Matt Carlsonba1142e2011-11-04 09:15:00 +00007156 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007157 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007158
7159 entry = NEXT_TX(entry);
7160 txb = &tnapi->tx_buffers[entry];
7161
7162 pci_unmap_page(tnapi->tp->pdev,
7163 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007164 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007165
7166 while (txb->fragmented) {
7167 txb->fragmented = false;
7168 entry = NEXT_TX(entry);
7169 txb = &tnapi->tx_buffers[entry];
7170 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007171 }
7172}
7173
Michael Chan72f2afb2006-03-06 19:28:35 -08007174/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007175static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007176 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007177 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007178 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007180 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007181 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007182 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007183 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
Joe Perches41535772013-02-16 11:20:04 +00007185 if (tg3_asic_rev(tp) != ASIC_REV_5701)
Matt Carlson41588ba2008-04-19 18:12:33 -07007186 new_skb = skb_copy(skb, GFP_ATOMIC);
7187 else {
7188 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7189
7190 new_skb = skb_copy_expand(skb,
7191 skb_headroom(skb) + more_headroom,
7192 skb_tailroom(skb), GFP_ATOMIC);
7193 }
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007196 ret = -1;
7197 } else {
7198 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007199 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7200 PCI_DMA_TODEVICE);
7201 /* Make sure the mapping succeeded */
7202 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007203 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007204 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007205 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007206 u32 save_entry = *entry;
7207
Matt Carlson92cd3a12011-07-27 14:20:47 +00007208 base_flags |= TXD_FLAG_END;
7209
Matt Carlson84b67b22011-07-27 14:20:52 +00007210 tnapi->tx_buffers[*entry].skb = new_skb;
7211 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007212 mapping, new_addr);
7213
Matt Carlson84b67b22011-07-27 14:20:52 +00007214 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007215 new_skb->len, base_flags,
7216 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007217 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007218 dev_kfree_skb(new_skb);
7219 ret = -1;
7220 }
Michael Chanc58ec932005-09-17 00:46:27 -07007221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 }
7223
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007225 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227}
7228
Matt Carlson2ffcc982011-05-19 12:12:44 +00007229static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007230
7231/* Use GSO to workaround a rare TSO bug that may be triggered when the
7232 * TSO header is greater than 80 bytes.
7233 */
7234static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7235{
7236 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007237 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007238
7239 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007240 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007241 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007242
7243 /* netif_tx_stop_queue() must be done before checking
7244 * checking tx index in tg3_tx_avail() below, because in
7245 * tg3_tx(), we update tx index before checking for
7246 * netif_tx_queue_stopped().
7247 */
7248 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007249 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007250 return NETDEV_TX_BUSY;
7251
7252 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007253 }
7254
7255 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007256 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007257 goto tg3_tso_bug_end;
7258
7259 do {
7260 nskb = segs;
7261 segs = segs->next;
7262 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007263 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007264 } while (segs);
7265
7266tg3_tso_bug_end:
7267 dev_kfree_skb(skb);
7268
7269 return NETDEV_TX_OK;
7270}
Michael Chan52c0fd82006-06-29 20:15:54 -07007271
Michael Chan5a6f3072006-03-20 22:28:05 -08007272/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007273 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007274 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007275static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007276{
7277 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007278 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007279 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007280 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007281 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007282 struct tg3_napi *tnapi;
7283 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007284 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007285
Matt Carlson24f4efd2009-11-13 13:03:35 +00007286 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7287 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007288 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007289 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Matt Carlson84b67b22011-07-27 14:20:52 +00007291 budget = tg3_tx_avail(tnapi);
7292
Michael Chan00b70502006-06-17 21:58:45 -07007293 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007294 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007295 * interrupt. Furthermore, IRQ processing runs lockless so we have
7296 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007298 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007299 if (!netif_tx_queue_stopped(txq)) {
7300 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007301
7302 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007303 netdev_err(dev,
7304 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 return NETDEV_TX_BUSY;
7307 }
7308
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007309 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007311 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007313
Matt Carlsonbe98da62010-07-11 09:31:46 +00007314 mss = skb_shinfo(skb)->gso_size;
7315 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007316 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007317 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
7319 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007320 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7321 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322
Matt Carlson34195c32010-07-11 09:31:42 +00007323 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007324 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
Eric Dumazeta5a11952012-01-23 01:22:09 +00007326 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007327
Eric Dumazeta5a11952012-01-23 01:22:09 +00007328 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007329 iph->check = 0;
7330 iph->tot_len = htons(mss + hdr_len);
7331 }
7332
Michael Chan52c0fd82006-06-29 20:15:54 -07007333 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007334 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007335 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007336
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7338 TXD_FLAG_CPU_POST_DMA);
7339
Joe Perches63c3a662011-04-26 08:12:10 +00007340 if (tg3_flag(tp, HW_TSO_1) ||
7341 tg3_flag(tp, HW_TSO_2) ||
7342 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007343 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007345 } else
7346 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7347 iph->daddr, 0,
7348 IPPROTO_TCP,
7349 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350
Joe Perches63c3a662011-04-26 08:12:10 +00007351 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007352 mss |= (hdr_len & 0xc) << 12;
7353 if (hdr_len & 0x10)
7354 base_flags |= 0x00000010;
7355 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007356 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007357 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007358 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +00007359 tg3_asic_rev(tp) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007360 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 int tsflags;
7362
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007363 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 mss |= (tsflags << 11);
7365 }
7366 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007367 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 int tsflags;
7369
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007370 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 base_flags |= tsflags << 12;
7372 }
7373 }
7374 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007375
Matt Carlson93a700a2011-08-31 11:44:54 +00007376 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7377 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7378 base_flags |= TXD_FLAG_JMB_PKT;
7379
Matt Carlson92cd3a12011-07-27 14:20:47 +00007380 if (vlan_tx_tag_present(skb)) {
7381 base_flags |= TXD_FLAG_VLAN;
7382 vlan = vlan_tx_tag_get(skb);
7383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007385 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7386 tg3_flag(tp, TX_TSTAMP_EN)) {
7387 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7388 base_flags |= TXD_FLAG_HWTSTAMP;
7389 }
7390
Alexander Duyckf4188d82009-12-02 16:48:38 +00007391 len = skb_headlen(skb);
7392
7393 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007394 if (pci_dma_mapping_error(tp->pdev, mapping))
7395 goto drop;
7396
David S. Miller90079ce2008-09-11 04:52:51 -07007397
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007398 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007399 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400
7401 would_hit_hwbug = 0;
7402
Joe Perches63c3a662011-04-26 08:12:10 +00007403 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007404 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405
Matt Carlson84b67b22011-07-27 14:20:52 +00007406 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007407 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007408 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007409 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007410 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007411 u32 tmp_mss = mss;
7412
7413 if (!tg3_flag(tp, HW_TSO_1) &&
7414 !tg3_flag(tp, HW_TSO_2) &&
7415 !tg3_flag(tp, HW_TSO_3))
7416 tmp_mss = 0;
7417
Matt Carlsonc5665a52012-02-13 10:20:12 +00007418 /* Now loop through additional data
7419 * fragments, and queue them.
7420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 last = skb_shinfo(skb)->nr_frags - 1;
7422 for (i = 0; i <= last; i++) {
7423 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7424
Eric Dumazet9e903e02011-10-18 21:00:24 +00007425 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007426 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007427 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007429 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007430 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007431 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007432 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007433 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
Matt Carlsonb9e45482011-11-04 09:14:59 +00007435 if (!budget ||
7436 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007437 len, base_flags |
7438 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007439 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007440 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007441 break;
7442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 }
7444 }
7445
7446 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007447 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448
7449 /* If the workaround fails due to memory/mapping
7450 * failure, silently drop this packet.
7451 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007452 entry = tnapi->tx_prod;
7453 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007454 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007455 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007456 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 }
7458
Richard Cochrand515b452011-06-19 03:31:41 +00007459 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007460 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007461
Michael Chan6541b802012-03-04 14:48:14 +00007462 /* Sync BD data before updating mailbox */
7463 wmb();
7464
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007466 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007468 tnapi->tx_prod = entry;
7469 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007470 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007471
7472 /* netif_tx_stop_queue() must be done before checking
7473 * checking tx index in tg3_tx_avail() below, because in
7474 * tg3_tx(), we update tx index before checking for
7475 * netif_tx_queue_stopped().
7476 */
7477 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007478 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007479 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007482 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007484
7485dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007486 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007487 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007488drop:
7489 dev_kfree_skb(skb);
7490drop_nofree:
7491 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007492 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493}
7494
Matt Carlson6e01b202011-08-19 13:58:20 +00007495static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7496{
7497 if (enable) {
7498 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7499 MAC_MODE_PORT_MODE_MASK);
7500
7501 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7502
7503 if (!tg3_flag(tp, 5705_PLUS))
7504 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7505
7506 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7507 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7508 else
7509 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7510 } else {
7511 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7512
7513 if (tg3_flag(tp, 5705_PLUS) ||
7514 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
Joe Perches41535772013-02-16 11:20:04 +00007515 tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlson6e01b202011-08-19 13:58:20 +00007516 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7517 }
7518
7519 tw32(MAC_MODE, tp->mac_mode);
7520 udelay(40);
7521}
7522
Matt Carlson941ec902011-08-19 13:58:23 +00007523static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007524{
Matt Carlson941ec902011-08-19 13:58:23 +00007525 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007526
7527 tg3_phy_toggle_apd(tp, false);
7528 tg3_phy_toggle_automdix(tp, 0);
7529
Matt Carlson941ec902011-08-19 13:58:23 +00007530 if (extlpbk && tg3_phy_set_extloopbk(tp))
7531 return -EIO;
7532
7533 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007534 switch (speed) {
7535 case SPEED_10:
7536 break;
7537 case SPEED_100:
7538 bmcr |= BMCR_SPEED100;
7539 break;
7540 case SPEED_1000:
7541 default:
7542 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7543 speed = SPEED_100;
7544 bmcr |= BMCR_SPEED100;
7545 } else {
7546 speed = SPEED_1000;
7547 bmcr |= BMCR_SPEED1000;
7548 }
7549 }
7550
Matt Carlson941ec902011-08-19 13:58:23 +00007551 if (extlpbk) {
7552 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7553 tg3_readphy(tp, MII_CTRL1000, &val);
7554 val |= CTL1000_AS_MASTER |
7555 CTL1000_ENABLE_MASTER;
7556 tg3_writephy(tp, MII_CTRL1000, val);
7557 } else {
7558 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7559 MII_TG3_FET_PTEST_TRIM_2;
7560 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7561 }
7562 } else
7563 bmcr |= BMCR_LOOPBACK;
7564
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007565 tg3_writephy(tp, MII_BMCR, bmcr);
7566
7567 /* The write needs to be flushed for the FETs */
7568 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7569 tg3_readphy(tp, MII_BMCR, &bmcr);
7570
7571 udelay(40);
7572
7573 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +00007574 tg3_asic_rev(tp) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007575 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007576 MII_TG3_FET_PTEST_FRC_TX_LINK |
7577 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7578
7579 /* The write needs to be flushed for the AC131 */
7580 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7581 }
7582
7583 /* Reset to prevent losing 1st rx packet intermittently */
7584 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7585 tg3_flag(tp, 5780_CLASS)) {
7586 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7587 udelay(10);
7588 tw32_f(MAC_RX_MODE, tp->rx_mode);
7589 }
7590
7591 mac_mode = tp->mac_mode &
7592 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7593 if (speed == SPEED_1000)
7594 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7595 else
7596 mac_mode |= MAC_MODE_PORT_MODE_MII;
7597
Joe Perches41535772013-02-16 11:20:04 +00007598 if (tg3_asic_rev(tp) == ASIC_REV_5700) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007599 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7600
7601 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7602 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7603 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7604 mac_mode |= MAC_MODE_LINK_POLARITY;
7605
7606 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7607 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7608 }
7609
7610 tw32(MAC_MODE, mac_mode);
7611 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007612
7613 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007614}
7615
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007616static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007617{
7618 struct tg3 *tp = netdev_priv(dev);
7619
7620 if (features & NETIF_F_LOOPBACK) {
7621 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7622 return;
7623
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007624 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007625 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007626 netif_carrier_on(tp->dev);
7627 spin_unlock_bh(&tp->lock);
7628 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7629 } else {
7630 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7631 return;
7632
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007633 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007634 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007635 /* Force link status check */
7636 tg3_setup_phy(tp, 1);
7637 spin_unlock_bh(&tp->lock);
7638 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7639 }
7640}
7641
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007642static netdev_features_t tg3_fix_features(struct net_device *dev,
7643 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007644{
7645 struct tg3 *tp = netdev_priv(dev);
7646
Joe Perches63c3a662011-04-26 08:12:10 +00007647 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007648 features &= ~NETIF_F_ALL_TSO;
7649
7650 return features;
7651}
7652
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007653static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007654{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007655 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007656
7657 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7658 tg3_set_loopback(dev, features);
7659
7660 return 0;
7661}
7662
Matt Carlson21f581a2009-08-28 14:00:25 +00007663static void tg3_rx_prodring_free(struct tg3 *tp,
7664 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 int i;
7667
Matt Carlson8fea32b2010-09-15 08:59:58 +00007668 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007669 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007670 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007671 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007672 tp->rx_pkt_map_sz);
7673
Joe Perches63c3a662011-04-26 08:12:10 +00007674 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007675 for (i = tpr->rx_jmb_cons_idx;
7676 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007677 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007678 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007679 TG3_RX_JMB_MAP_SZ);
7680 }
7681 }
7682
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007683 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685
Matt Carlson2c49a442010-09-30 10:34:35 +00007686 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007687 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007688 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689
Joe Perches63c3a662011-04-26 08:12:10 +00007690 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007691 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007692 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007693 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 }
7695}
7696
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007697/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 *
7699 * The chip has been shut down and the driver detached from
7700 * the networking, so no interrupts or new tx packets will
7701 * end up in the driver. tp->{tx,}lock are held and thus
7702 * we may not sleep.
7703 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007704static int tg3_rx_prodring_alloc(struct tg3 *tp,
7705 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706{
Matt Carlson287be122009-08-28 13:58:46 +00007707 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007709 tpr->rx_std_cons_idx = 0;
7710 tpr->rx_std_prod_idx = 0;
7711 tpr->rx_jmb_cons_idx = 0;
7712 tpr->rx_jmb_prod_idx = 0;
7713
Matt Carlson8fea32b2010-09-15 08:59:58 +00007714 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007715 memset(&tpr->rx_std_buffers[0], 0,
7716 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007717 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007718 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007719 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007720 goto done;
7721 }
7722
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007724 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
Matt Carlson287be122009-08-28 13:58:46 +00007726 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007727 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007728 tp->dev->mtu > ETH_DATA_LEN)
7729 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7730 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007731
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 /* Initialize invariants of the rings, we only set this
7733 * stuff once. This works because the card does not
7734 * write into the rx buffer posting rings.
7735 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007736 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007737 struct tg3_rx_buffer_desc *rxd;
7738
Matt Carlson21f581a2009-08-28 14:00:25 +00007739 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007740 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7742 rxd->opaque = (RXD_OPAQUE_RING_STD |
7743 (i << RXD_OPAQUE_INDEX_SHIFT));
7744 }
7745
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007746 /* Now allocate fresh SKBs for each rx ring. */
7747 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007748 unsigned int frag_size;
7749
7750 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7751 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007752 netdev_warn(tp->dev,
7753 "Using a smaller RX standard ring. Only "
7754 "%d out of %d buffers were allocated "
7755 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007756 if (i == 0)
7757 goto initfail;
7758 tp->rx_pending = i;
7759 break;
7760 }
7761 }
7762
Joe Perches63c3a662011-04-26 08:12:10 +00007763 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007764 goto done;
7765
Matt Carlson2c49a442010-09-30 10:34:35 +00007766 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007767
Joe Perches63c3a662011-04-26 08:12:10 +00007768 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007769 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
Matt Carlson2c49a442010-09-30 10:34:35 +00007771 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007772 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773
Matt Carlson0d86df82010-02-17 15:17:00 +00007774 rxd = &tpr->rx_jmb[i].std;
7775 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7776 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7777 RXD_FLAG_JUMBO;
7778 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7779 (i << RXD_OPAQUE_INDEX_SHIFT));
7780 }
7781
7782 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007783 unsigned int frag_size;
7784
7785 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7786 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007787 netdev_warn(tp->dev,
7788 "Using a smaller RX jumbo ring. Only %d "
7789 "out of %d buffers were allocated "
7790 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007791 if (i == 0)
7792 goto initfail;
7793 tp->rx_jumbo_pending = i;
7794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 }
7796 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007797
7798done:
Michael Chan32d8c572006-07-25 16:38:29 -07007799 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007800
7801initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007802 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007803 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804}
7805
Matt Carlson21f581a2009-08-28 14:00:25 +00007806static void tg3_rx_prodring_fini(struct tg3 *tp,
7807 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808{
Matt Carlson21f581a2009-08-28 14:00:25 +00007809 kfree(tpr->rx_std_buffers);
7810 tpr->rx_std_buffers = NULL;
7811 kfree(tpr->rx_jmb_buffers);
7812 tpr->rx_jmb_buffers = NULL;
7813 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007814 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7815 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007816 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007818 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007819 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7820 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007821 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007823}
7824
Matt Carlson21f581a2009-08-28 14:00:25 +00007825static int tg3_rx_prodring_init(struct tg3 *tp,
7826 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007827{
Matt Carlson2c49a442010-09-30 10:34:35 +00007828 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7829 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007830 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007831 return -ENOMEM;
7832
Matt Carlson4bae65c2010-11-24 08:31:52 +00007833 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7834 TG3_RX_STD_RING_BYTES(tp),
7835 &tpr->rx_std_mapping,
7836 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007837 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007838 goto err_out;
7839
Joe Perches63c3a662011-04-26 08:12:10 +00007840 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007841 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007842 GFP_KERNEL);
7843 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007844 goto err_out;
7845
Matt Carlson4bae65c2010-11-24 08:31:52 +00007846 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7847 TG3_RX_JMB_RING_BYTES(tp),
7848 &tpr->rx_jmb_mapping,
7849 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007850 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007851 goto err_out;
7852 }
7853
7854 return 0;
7855
7856err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007857 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007858 return -ENOMEM;
7859}
7860
7861/* Free up pending packets in all rx/tx rings.
7862 *
7863 * The chip has been shut down and the driver detached from
7864 * the networking, so no interrupts or new tx packets will
7865 * end up in the driver. tp->{tx,}lock is not held and we are not
7866 * in an interrupt context and thus may sleep.
7867 */
7868static void tg3_free_rings(struct tg3 *tp)
7869{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007870 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007871
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007872 for (j = 0; j < tp->irq_cnt; j++) {
7873 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007874
Matt Carlson8fea32b2010-09-15 08:59:58 +00007875 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007876
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007877 if (!tnapi->tx_buffers)
7878 continue;
7879
Matt Carlson0d681b22011-07-27 14:20:49 +00007880 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7881 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007882
Matt Carlson0d681b22011-07-27 14:20:49 +00007883 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007884 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007885
Matt Carlsonba1142e2011-11-04 09:15:00 +00007886 tg3_tx_skb_unmap(tnapi, i,
7887 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007888
7889 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007890 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007891 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007892 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007893}
7894
7895/* Initialize tx/rx rings for packet processing.
7896 *
7897 * The chip has been shut down and the driver detached from
7898 * the networking, so no interrupts or new tx packets will
7899 * end up in the driver. tp->{tx,}lock are held and thus
7900 * we may not sleep.
7901 */
7902static int tg3_init_rings(struct tg3 *tp)
7903{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007904 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007905
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007906 /* Free up all the SKBs. */
7907 tg3_free_rings(tp);
7908
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007909 for (i = 0; i < tp->irq_cnt; i++) {
7910 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007911
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007912 tnapi->last_tag = 0;
7913 tnapi->last_irq_tag = 0;
7914 tnapi->hw_status->status = 0;
7915 tnapi->hw_status->status_tag = 0;
7916 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7917
7918 tnapi->tx_prod = 0;
7919 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007920 if (tnapi->tx_ring)
7921 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007922
7923 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007924 if (tnapi->rx_rcb)
7925 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007926
Matt Carlson8fea32b2010-09-15 08:59:58 +00007927 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007928 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007929 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007930 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007931 }
Matt Carlson72334482009-08-28 14:03:01 +00007932
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007933 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007934}
7935
Michael Chan49a359e2012-09-28 07:12:37 +00007936static void tg3_mem_tx_release(struct tg3 *tp)
7937{
7938 int i;
7939
7940 for (i = 0; i < tp->irq_max; i++) {
7941 struct tg3_napi *tnapi = &tp->napi[i];
7942
7943 if (tnapi->tx_ring) {
7944 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7945 tnapi->tx_ring, tnapi->tx_desc_mapping);
7946 tnapi->tx_ring = NULL;
7947 }
7948
7949 kfree(tnapi->tx_buffers);
7950 tnapi->tx_buffers = NULL;
7951 }
7952}
7953
7954static int tg3_mem_tx_acquire(struct tg3 *tp)
7955{
7956 int i;
7957 struct tg3_napi *tnapi = &tp->napi[0];
7958
7959 /* If multivector TSS is enabled, vector 0 does not handle
7960 * tx interrupts. Don't allocate any resources for it.
7961 */
7962 if (tg3_flag(tp, ENABLE_TSS))
7963 tnapi++;
7964
7965 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7966 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7967 TG3_TX_RING_SIZE, GFP_KERNEL);
7968 if (!tnapi->tx_buffers)
7969 goto err_out;
7970
7971 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7972 TG3_TX_RING_BYTES,
7973 &tnapi->tx_desc_mapping,
7974 GFP_KERNEL);
7975 if (!tnapi->tx_ring)
7976 goto err_out;
7977 }
7978
7979 return 0;
7980
7981err_out:
7982 tg3_mem_tx_release(tp);
7983 return -ENOMEM;
7984}
7985
7986static void tg3_mem_rx_release(struct tg3 *tp)
7987{
7988 int i;
7989
7990 for (i = 0; i < tp->irq_max; i++) {
7991 struct tg3_napi *tnapi = &tp->napi[i];
7992
7993 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7994
7995 if (!tnapi->rx_rcb)
7996 continue;
7997
7998 dma_free_coherent(&tp->pdev->dev,
7999 TG3_RX_RCB_RING_BYTES(tp),
8000 tnapi->rx_rcb,
8001 tnapi->rx_rcb_mapping);
8002 tnapi->rx_rcb = NULL;
8003 }
8004}
8005
8006static int tg3_mem_rx_acquire(struct tg3 *tp)
8007{
8008 unsigned int i, limit;
8009
8010 limit = tp->rxq_cnt;
8011
8012 /* If RSS is enabled, we need a (dummy) producer ring
8013 * set on vector zero. This is the true hw prodring.
8014 */
8015 if (tg3_flag(tp, ENABLE_RSS))
8016 limit++;
8017
8018 for (i = 0; i < limit; i++) {
8019 struct tg3_napi *tnapi = &tp->napi[i];
8020
8021 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
8022 goto err_out;
8023
8024 /* If multivector RSS is enabled, vector 0
8025 * does not handle rx or tx interrupts.
8026 * Don't allocate any resources for it.
8027 */
8028 if (!i && tg3_flag(tp, ENABLE_RSS))
8029 continue;
8030
8031 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
8032 TG3_RX_RCB_RING_BYTES(tp),
8033 &tnapi->rx_rcb_mapping,
8034 GFP_KERNEL);
8035 if (!tnapi->rx_rcb)
8036 goto err_out;
8037
8038 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
8039 }
8040
8041 return 0;
8042
8043err_out:
8044 tg3_mem_rx_release(tp);
8045 return -ENOMEM;
8046}
8047
Matt Carlsoncf7a7292009-08-28 13:59:57 +00008048/*
8049 * Must not be invoked with interrupt sources disabled and
8050 * the hardware shutdown down.
8051 */
8052static void tg3_free_consistent(struct tg3 *tp)
8053{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008054 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008055
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008056 for (i = 0; i < tp->irq_cnt; i++) {
8057 struct tg3_napi *tnapi = &tp->napi[i];
8058
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008059 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008060 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8061 tnapi->hw_status,
8062 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008063 tnapi->hw_status = NULL;
8064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008066
Michael Chan49a359e2012-09-28 07:12:37 +00008067 tg3_mem_rx_release(tp);
8068 tg3_mem_tx_release(tp);
8069
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008071 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8072 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073 tp->hw_stats = NULL;
8074 }
8075}
8076
8077/*
8078 * Must not be invoked with interrupt sources disabled and
8079 * the hardware shutdown down. Can sleep.
8080 */
8081static int tg3_alloc_consistent(struct tg3 *tp)
8082{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008083 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008084
Matt Carlson4bae65c2010-11-24 08:31:52 +00008085 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8086 sizeof(struct tg3_hw_stats),
8087 &tp->stats_mapping,
8088 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 if (!tp->hw_stats)
8090 goto err_out;
8091
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8093
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008094 for (i = 0; i < tp->irq_cnt; i++) {
8095 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008096 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008097
Matt Carlson4bae65c2010-11-24 08:31:52 +00008098 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8099 TG3_HW_STATUS_SIZE,
8100 &tnapi->status_mapping,
8101 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008102 if (!tnapi->hw_status)
8103 goto err_out;
8104
8105 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008106 sblk = tnapi->hw_status;
8107
Michael Chan49a359e2012-09-28 07:12:37 +00008108 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008109 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008110
Michael Chan49a359e2012-09-28 07:12:37 +00008111 /*
8112 * When RSS is enabled, the status block format changes
8113 * slightly. The "rx_jumbo_consumer", "reserved",
8114 * and "rx_mini_consumer" members get mapped to the
8115 * other three rx return ring producer indexes.
8116 */
8117 switch (i) {
8118 case 1:
8119 prodptr = &sblk->idx[0].rx_producer;
8120 break;
8121 case 2:
8122 prodptr = &sblk->rx_jumbo_consumer;
8123 break;
8124 case 3:
8125 prodptr = &sblk->reserved;
8126 break;
8127 case 4:
8128 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008129 break;
8130 }
Michael Chan49a359e2012-09-28 07:12:37 +00008131 tnapi->rx_rcb_prod_idx = prodptr;
8132 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008133 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008134 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008135 }
8136
Michael Chan49a359e2012-09-28 07:12:37 +00008137 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8138 goto err_out;
8139
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 return 0;
8141
8142err_out:
8143 tg3_free_consistent(tp);
8144 return -ENOMEM;
8145}
8146
8147#define MAX_WAIT_CNT 1000
8148
8149/* To stop a block, clear the enable bit and poll till it
8150 * clears. tp->lock is held.
8151 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008152static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153{
8154 unsigned int i;
8155 u32 val;
8156
Joe Perches63c3a662011-04-26 08:12:10 +00008157 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 switch (ofs) {
8159 case RCVLSC_MODE:
8160 case DMAC_MODE:
8161 case MBFREE_MODE:
8162 case BUFMGR_MODE:
8163 case MEMARB_MODE:
8164 /* We can't enable/disable these bits of the
8165 * 5705/5750, just say success.
8166 */
8167 return 0;
8168
8169 default:
8170 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 }
8173
8174 val = tr32(ofs);
8175 val &= ~enable_bit;
8176 tw32_f(ofs, val);
8177
8178 for (i = 0; i < MAX_WAIT_CNT; i++) {
8179 udelay(100);
8180 val = tr32(ofs);
8181 if ((val & enable_bit) == 0)
8182 break;
8183 }
8184
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008185 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008186 dev_err(&tp->pdev->dev,
8187 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8188 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 return -ENODEV;
8190 }
8191
8192 return 0;
8193}
8194
8195/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008196static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197{
8198 int i, err;
8199
8200 tg3_disable_ints(tp);
8201
8202 tp->rx_mode &= ~RX_MODE_ENABLE;
8203 tw32_f(MAC_RX_MODE, tp->rx_mode);
8204 udelay(10);
8205
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008206 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8207 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8208 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8209 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8210 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8211 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008213 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8214 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8215 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8216 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8217 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8218 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8219 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220
8221 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8222 tw32_f(MAC_MODE, tp->mac_mode);
8223 udelay(40);
8224
8225 tp->tx_mode &= ~TX_MODE_ENABLE;
8226 tw32_f(MAC_TX_MODE, tp->tx_mode);
8227
8228 for (i = 0; i < MAX_WAIT_CNT; i++) {
8229 udelay(100);
8230 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8231 break;
8232 }
8233 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008234 dev_err(&tp->pdev->dev,
8235 "%s timed out, TX_MODE_ENABLE will not clear "
8236 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008237 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 }
8239
Michael Chane6de8ad2005-05-05 14:42:41 -07008240 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008241 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8242 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
8244 tw32(FTQ_RESET, 0xffffffff);
8245 tw32(FTQ_RESET, 0x00000000);
8246
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008247 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8248 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008250 for (i = 0; i < tp->irq_cnt; i++) {
8251 struct tg3_napi *tnapi = &tp->napi[i];
8252 if (tnapi->hw_status)
8253 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256 return err;
8257}
8258
Michael Chanee6a99b2007-07-18 21:49:10 -07008259/* Save PCI command register before chip reset */
8260static void tg3_save_pci_state(struct tg3 *tp)
8261{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008262 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008263}
8264
8265/* Restore PCI state after chip reset */
8266static void tg3_restore_pci_state(struct tg3 *tp)
8267{
8268 u32 val;
8269
8270 /* Re-enable indirect register accesses. */
8271 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8272 tp->misc_host_ctrl);
8273
8274 /* Set MAX PCI retry to zero. */
8275 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
Joe Perches41535772013-02-16 11:20:04 +00008276 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008277 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008278 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008279 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008280 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008281 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008282 PCISTATE_ALLOW_APE_SHMEM_WR |
8283 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008284 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8285
Matt Carlson8a6eac92007-10-21 16:17:55 -07008286 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008287
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008288 if (!tg3_flag(tp, PCI_EXPRESS)) {
8289 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8290 tp->pci_cacheline_sz);
8291 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8292 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008293 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008294
Michael Chanee6a99b2007-07-18 21:49:10 -07008295 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008296 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008297 u16 pcix_cmd;
8298
8299 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8300 &pcix_cmd);
8301 pcix_cmd &= ~PCI_X_CMD_ERO;
8302 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8303 pcix_cmd);
8304 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008305
Joe Perches63c3a662011-04-26 08:12:10 +00008306 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008307
8308 /* Chip reset on 5780 will reset MSI enable bit,
8309 * so need to restore it.
8310 */
Joe Perches63c3a662011-04-26 08:12:10 +00008311 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008312 u16 ctrl;
8313
8314 pci_read_config_word(tp->pdev,
8315 tp->msi_cap + PCI_MSI_FLAGS,
8316 &ctrl);
8317 pci_write_config_word(tp->pdev,
8318 tp->msi_cap + PCI_MSI_FLAGS,
8319 ctrl | PCI_MSI_FLAGS_ENABLE);
8320 val = tr32(MSGINT_MODE);
8321 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8322 }
8323 }
8324}
8325
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326/* tp->lock is held. */
8327static int tg3_chip_reset(struct tg3 *tp)
8328{
8329 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008330 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008331 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008332
David S. Millerf49639e2006-06-09 11:58:36 -07008333 tg3_nvram_lock(tp);
8334
Matt Carlson77b483f2008-08-15 14:07:24 -07008335 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8336
David S. Millerf49639e2006-06-09 11:58:36 -07008337 /* No matching tg3_nvram_unlock() after this because
8338 * chip reset below will undo the nvram lock.
8339 */
8340 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341
Michael Chanee6a99b2007-07-18 21:49:10 -07008342 /* GRC_MISC_CFG core clock reset will clear the memory
8343 * enable bit in PCI register 4 and the MSI enable bit
8344 * on some chips, so we save relevant registers here.
8345 */
8346 tg3_save_pci_state(tp);
8347
Joe Perches41535772013-02-16 11:20:04 +00008348 if (tg3_asic_rev(tp) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008349 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008350 tw32(GRC_FASTBOOT_PC, 0);
8351
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 /*
8353 * We must avoid the readl() that normally takes place.
8354 * It locks machines, causes machine checks, and other
8355 * fun things. So, temporarily disable the 5701
8356 * hardware workaround, while we do the reset.
8357 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008358 write_op = tp->write32;
8359 if (write_op == tg3_write_flush_reg32)
8360 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361
Michael Chand18edcb2007-03-24 20:57:11 -07008362 /* Prevent the irq handler from reading or writing PCI registers
8363 * during chip reset when the memory enable bit in the PCI command
8364 * register may be cleared. The chip does not generate interrupt
8365 * at this time, but the irq handler may still be called due to irq
8366 * sharing or irqpoll.
8367 */
Joe Perches63c3a662011-04-26 08:12:10 +00008368 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008369 for (i = 0; i < tp->irq_cnt; i++) {
8370 struct tg3_napi *tnapi = &tp->napi[i];
8371 if (tnapi->hw_status) {
8372 tnapi->hw_status->status = 0;
8373 tnapi->hw_status->status_tag = 0;
8374 }
8375 tnapi->last_tag = 0;
8376 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008377 }
Michael Chand18edcb2007-03-24 20:57:11 -07008378 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008379
8380 for (i = 0; i < tp->irq_cnt; i++)
8381 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008382
Joe Perches41535772013-02-16 11:20:04 +00008383 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson255ca312009-08-25 10:07:27 +00008384 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8385 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8386 }
8387
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 /* do the reset */
8389 val = GRC_MISC_CFG_CORECLK_RESET;
8390
Joe Perches63c3a662011-04-26 08:12:10 +00008391 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008392 /* Force PCIe 1.0a mode */
Joe Perches41535772013-02-16 11:20:04 +00008393 if (tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008394 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008395 tr32(TG3_PCIE_PHY_TSTCTL) ==
8396 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8397 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8398
Joe Perches41535772013-02-16 11:20:04 +00008399 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 tw32(GRC_MISC_CFG, (1 << 29));
8401 val |= (1 << 29);
8402 }
8403 }
8404
Joe Perches41535772013-02-16 11:20:04 +00008405 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -07008406 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8407 tw32(GRC_VCPU_EXT_CTRL,
8408 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8409 }
8410
Matt Carlsonf37500d2010-08-02 11:25:59 +00008411 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008412 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008414
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 tw32(GRC_MISC_CFG, val);
8416
Michael Chan1ee582d2005-08-09 20:16:46 -07008417 /* restore 5701 hardware bug workaround write method */
8418 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419
8420 /* Unfortunately, we have to delay before the PCI read back.
8421 * Some 575X chips even will not respond to a PCI cfg access
8422 * when the reset command is given to the chip.
8423 *
8424 * How do these hardware designers expect things to work
8425 * properly if the PCI write is posted for a long period
8426 * of time? It is always necessary to have some method by
8427 * which a register read back can occur to push the write
8428 * out which does the reset.
8429 *
8430 * For most tg3 variants the trick below was working.
8431 * Ho hum...
8432 */
8433 udelay(120);
8434
8435 /* Flush PCI posted writes. The normal MMIO registers
8436 * are inaccessible at this time so this is the only
8437 * way to make this reliably (actually, this is no longer
8438 * the case, see above). I tried to use indirect
8439 * register read/write but this upset some 5701 variants.
8440 */
8441 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8442
8443 udelay(120);
8444
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008445 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008446 u16 val16;
8447
Joe Perches41535772013-02-16 11:20:04 +00008448 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008449 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450 u32 cfg_val;
8451
8452 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008453 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 udelay(100);
8455
8456 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8457 pci_write_config_dword(tp->pdev, 0xc4,
8458 cfg_val | (1 << 15));
8459 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008460
Matt Carlsone7126992009-08-25 10:08:16 +00008461 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008462 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008463 /*
8464 * Older PCIe devices only support the 128 byte
8465 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008466 */
Joe Perches63c3a662011-04-26 08:12:10 +00008467 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008468 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8469 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008470
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008471 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008472 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008473 PCI_EXP_DEVSTA_CED |
8474 PCI_EXP_DEVSTA_NFED |
8475 PCI_EXP_DEVSTA_FED |
8476 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 }
8478
Michael Chanee6a99b2007-07-18 21:49:10 -07008479 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
Joe Perches63c3a662011-04-26 08:12:10 +00008481 tg3_flag_clear(tp, CHIP_RESETTING);
8482 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008483
Michael Chanee6a99b2007-07-18 21:49:10 -07008484 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008485 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008486 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008487 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488
Joe Perches41535772013-02-16 11:20:04 +00008489 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490 tg3_stop_fw(tp);
8491 tw32(0x5000, 0x400);
8492 }
8493
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +00008494 if (tg3_flag(tp, IS_SSB_CORE)) {
8495 /*
8496 * BCM4785: In order to avoid repercussions from using
8497 * potentially defective internal ROM, stop the Rx RISC CPU,
8498 * which is not required.
8499 */
8500 tg3_stop_fw(tp);
8501 tg3_halt_cpu(tp, RX_CPU_BASE);
8502 }
8503
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 tw32(GRC_MODE, tp->grc_mode);
8505
Joe Perches41535772013-02-16 11:20:04 +00008506 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008507 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508
8509 tw32(0xc4, val | (1 << 15));
8510 }
8511
8512 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
Joe Perches41535772013-02-16 11:20:04 +00008513 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
Joe Perches41535772013-02-16 11:20:04 +00008515 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8517 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8518 }
8519
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008520 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008521 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008522 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008523 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008524 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008525 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008527 val = 0;
8528
8529 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 udelay(40);
8531
Matt Carlson77b483f2008-08-15 14:07:24 -07008532 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8533
Michael Chan7a6f4362006-09-27 16:03:31 -07008534 err = tg3_poll_fw(tp);
8535 if (err)
8536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537
Matt Carlson0a9140c2009-08-28 12:27:50 +00008538 tg3_mdio_start(tp);
8539
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +00008541 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
8542 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008543 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008544 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545
8546 tw32(0x7c00, val | (1 << 25));
8547 }
8548
Joe Perches41535772013-02-16 11:20:04 +00008549 if (tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlsond78b59f2011-04-05 14:22:46 +00008550 val = tr32(TG3_CPMU_CLCK_ORIDE);
8551 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8552 }
8553
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008555 tg3_flag_clear(tp, ENABLE_ASF);
8556 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8558 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8559 u32 nic_cfg;
8560
8561 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8562 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008563 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008564 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008565 if (tg3_flag(tp, 5750_PLUS))
8566 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 }
8568 }
8569
8570 return 0;
8571}
8572
Matt Carlson65ec6982012-02-28 23:33:37 +00008573static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8574static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008575
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008577static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578{
8579 int err;
8580
8581 tg3_stop_fw(tp);
8582
Michael Chan944d9802005-05-29 14:57:48 -07008583 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008585 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 err = tg3_chip_reset(tp);
8587
Matt Carlsondaba2a62009-04-20 06:58:52 +00008588 __tg3_set_mac_addr(tp, 0);
8589
Michael Chan944d9802005-05-29 14:57:48 -07008590 tg3_write_sig_legacy(tp, kind);
8591 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Matt Carlson92feeab2011-12-08 14:40:14 +00008593 if (tp->hw_stats) {
8594 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008595 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008596 tg3_get_estats(tp, &tp->estats_prev);
8597
8598 /* And make sure the next sample is new data */
8599 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8600 }
8601
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602 if (err)
8603 return err;
8604
8605 return 0;
8606}
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608static int tg3_set_mac_addr(struct net_device *dev, void *p)
8609{
8610 struct tg3 *tp = netdev_priv(dev);
8611 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008612 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
Michael Chanf9804dd2005-09-27 12:13:10 -07008614 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008615 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008616
Linus Torvalds1da177e2005-04-16 15:20:36 -07008617 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8618
Michael Chane75f7c92006-03-20 21:33:26 -08008619 if (!netif_running(dev))
8620 return 0;
8621
Joe Perches63c3a662011-04-26 08:12:10 +00008622 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008623 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008624
Michael Chan986e0ae2007-05-05 12:10:20 -07008625 addr0_high = tr32(MAC_ADDR_0_HIGH);
8626 addr0_low = tr32(MAC_ADDR_0_LOW);
8627 addr1_high = tr32(MAC_ADDR_1_HIGH);
8628 addr1_low = tr32(MAC_ADDR_1_LOW);
8629
8630 /* Skip MAC addr 1 if ASF is using it. */
8631 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8632 !(addr1_high == 0 && addr1_low == 0))
8633 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008634 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008635 spin_lock_bh(&tp->lock);
8636 __tg3_set_mac_addr(tp, skip_mac_1);
8637 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638
Michael Chanb9ec6c12006-07-25 16:37:27 -07008639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640}
8641
8642/* tp->lock is held. */
8643static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8644 dma_addr_t mapping, u32 maxlen_flags,
8645 u32 nic_addr)
8646{
8647 tg3_write_mem(tp,
8648 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8649 ((u64) mapping >> 32));
8650 tg3_write_mem(tp,
8651 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8652 ((u64) mapping & 0xffffffff));
8653 tg3_write_mem(tp,
8654 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8655 maxlen_flags);
8656
Joe Perches63c3a662011-04-26 08:12:10 +00008657 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 tg3_write_mem(tp,
8659 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8660 nic_addr);
8661}
8662
Michael Chana489b6d2012-09-28 07:12:39 +00008663
8664static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008665{
Michael Chana489b6d2012-09-28 07:12:39 +00008666 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008667
Joe Perches63c3a662011-04-26 08:12:10 +00008668 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008669 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8670 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8671 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008672 } else {
8673 tw32(HOSTCC_TXCOL_TICKS, 0);
8674 tw32(HOSTCC_TXMAX_FRAMES, 0);
8675 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008676
8677 for (; i < tp->txq_cnt; i++) {
8678 u32 reg;
8679
8680 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8681 tw32(reg, ec->tx_coalesce_usecs);
8682 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8683 tw32(reg, ec->tx_max_coalesced_frames);
8684 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8685 tw32(reg, ec->tx_max_coalesced_frames_irq);
8686 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008687 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008688
Michael Chana489b6d2012-09-28 07:12:39 +00008689 for (; i < tp->irq_max - 1; i++) {
8690 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8691 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8692 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8693 }
8694}
8695
8696static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8697{
8698 int i = 0;
8699 u32 limit = tp->rxq_cnt;
8700
Joe Perches63c3a662011-04-26 08:12:10 +00008701 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008702 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8703 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8704 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008705 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008706 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008707 tw32(HOSTCC_RXCOL_TICKS, 0);
8708 tw32(HOSTCC_RXMAX_FRAMES, 0);
8709 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008710 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008711
Michael Chana489b6d2012-09-28 07:12:39 +00008712 for (; i < limit; i++) {
8713 u32 reg;
8714
8715 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8716 tw32(reg, ec->rx_coalesce_usecs);
8717 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8718 tw32(reg, ec->rx_max_coalesced_frames);
8719 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8720 tw32(reg, ec->rx_max_coalesced_frames_irq);
8721 }
8722
8723 for (; i < tp->irq_max - 1; i++) {
8724 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8725 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8726 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8727 }
8728}
8729
8730static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8731{
8732 tg3_coal_tx_init(tp, ec);
8733 tg3_coal_rx_init(tp, ec);
8734
Joe Perches63c3a662011-04-26 08:12:10 +00008735 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008736 u32 val = ec->stats_block_coalesce_usecs;
8737
Matt Carlsonb6080e12009-09-01 13:12:00 +00008738 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8739 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8740
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008741 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008742 val = 0;
8743
8744 tw32(HOSTCC_STAT_COAL_TICKS, val);
8745 }
8746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747
8748/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008749static void tg3_rings_reset(struct tg3 *tp)
8750{
8751 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008752 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008753 struct tg3_napi *tnapi = &tp->napi[0];
8754
8755 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008756 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008757 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008758 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008759 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Michael Chanc65a17f2013-01-06 12:51:07 +00008760 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008761 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonb703df62009-12-03 08:36:21 +00008762 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008763 else
8764 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8765
8766 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8767 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8768 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8769 BDINFO_FLAGS_DISABLED);
8770
8771
8772 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008773 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008774 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008775 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008776 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches41535772013-02-16 11:20:04 +00008777 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8778 tg3_asic_rev(tp) == ASIC_REV_5762 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008779 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008780 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8781 else
8782 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8783
8784 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8785 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8786 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8787 BDINFO_FLAGS_DISABLED);
8788
8789 /* Disable interrupts */
8790 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008791 tp->napi[0].chk_msi_cnt = 0;
8792 tp->napi[0].last_rx_cons = 0;
8793 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008794
8795 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008796 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008797 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008798 tp->napi[i].tx_prod = 0;
8799 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008800 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008801 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008802 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8803 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008804 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008805 tp->napi[i].last_rx_cons = 0;
8806 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008807 }
Joe Perches63c3a662011-04-26 08:12:10 +00008808 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008809 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008810 } else {
8811 tp->napi[0].tx_prod = 0;
8812 tp->napi[0].tx_cons = 0;
8813 tw32_mailbox(tp->napi[0].prodmbox, 0);
8814 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8815 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008816
8817 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008818 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008819 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8820 for (i = 0; i < 16; i++)
8821 tw32_tx_mbox(mbox + i * 8, 0);
8822 }
8823
8824 txrcb = NIC_SRAM_SEND_RCB;
8825 rxrcb = NIC_SRAM_RCV_RET_RCB;
8826
8827 /* Clear status block in ram. */
8828 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8829
8830 /* Set status block DMA address */
8831 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8832 ((u64) tnapi->status_mapping >> 32));
8833 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8834 ((u64) tnapi->status_mapping & 0xffffffff));
8835
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008836 if (tnapi->tx_ring) {
8837 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8838 (TG3_TX_RING_SIZE <<
8839 BDINFO_FLAGS_MAXLEN_SHIFT),
8840 NIC_SRAM_TX_BUFFER_DESC);
8841 txrcb += TG3_BDINFO_SIZE;
8842 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008843
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008844 if (tnapi->rx_rcb) {
8845 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008846 (tp->rx_ret_ring_mask + 1) <<
8847 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008848 rxrcb += TG3_BDINFO_SIZE;
8849 }
8850
8851 stblk = HOSTCC_STATBLCK_RING1;
8852
8853 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8854 u64 mapping = (u64)tnapi->status_mapping;
8855 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8856 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8857
8858 /* Clear status block in ram. */
8859 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8860
Matt Carlson19cfaec2009-12-03 08:36:20 +00008861 if (tnapi->tx_ring) {
8862 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8863 (TG3_TX_RING_SIZE <<
8864 BDINFO_FLAGS_MAXLEN_SHIFT),
8865 NIC_SRAM_TX_BUFFER_DESC);
8866 txrcb += TG3_BDINFO_SIZE;
8867 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008868
8869 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008870 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008871 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8872
8873 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008874 rxrcb += TG3_BDINFO_SIZE;
8875 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008876}
8877
Matt Carlsoneb07a942011-04-20 07:57:36 +00008878static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8879{
8880 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8881
Joe Perches63c3a662011-04-26 08:12:10 +00008882 if (!tg3_flag(tp, 5750_PLUS) ||
8883 tg3_flag(tp, 5780_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00008884 tg3_asic_rev(tp) == ASIC_REV_5750 ||
8885 tg3_asic_rev(tp) == ASIC_REV_5752 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008886 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008887 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
Joe Perches41535772013-02-16 11:20:04 +00008888 else if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
8889 tg3_asic_rev(tp) == ASIC_REV_5787)
Matt Carlsoneb07a942011-04-20 07:57:36 +00008890 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8891 else
8892 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8893
8894 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8895 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8896
8897 val = min(nic_rep_thresh, host_rep_thresh);
8898 tw32(RCVBDI_STD_THRESH, val);
8899
Joe Perches63c3a662011-04-26 08:12:10 +00008900 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008901 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8902
Joe Perches63c3a662011-04-26 08:12:10 +00008903 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008904 return;
8905
Matt Carlson513aa6e2011-11-21 15:01:18 +00008906 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008907
8908 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8909
8910 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8911 tw32(RCVBDI_JUMBO_THRESH, val);
8912
Joe Perches63c3a662011-04-26 08:12:10 +00008913 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008914 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8915}
8916
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008917static inline u32 calc_crc(unsigned char *buf, int len)
8918{
8919 u32 reg;
8920 u32 tmp;
8921 int j, k;
8922
8923 reg = 0xffffffff;
8924
8925 for (j = 0; j < len; j++) {
8926 reg ^= buf[j];
8927
8928 for (k = 0; k < 8; k++) {
8929 tmp = reg & 0x01;
8930
8931 reg >>= 1;
8932
8933 if (tmp)
8934 reg ^= 0xedb88320;
8935 }
8936 }
8937
8938 return ~reg;
8939}
8940
8941static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8942{
8943 /* accept or reject all multicast frames */
8944 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8945 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8946 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8947 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8948}
8949
8950static void __tg3_set_rx_mode(struct net_device *dev)
8951{
8952 struct tg3 *tp = netdev_priv(dev);
8953 u32 rx_mode;
8954
8955 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8956 RX_MODE_KEEP_VLAN_TAG);
8957
8958#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8959 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8960 * flag clear.
8961 */
8962 if (!tg3_flag(tp, ENABLE_ASF))
8963 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8964#endif
8965
8966 if (dev->flags & IFF_PROMISC) {
8967 /* Promiscuous mode. */
8968 rx_mode |= RX_MODE_PROMISC;
8969 } else if (dev->flags & IFF_ALLMULTI) {
8970 /* Accept all multicast. */
8971 tg3_set_multi(tp, 1);
8972 } else if (netdev_mc_empty(dev)) {
8973 /* Reject all multicast. */
8974 tg3_set_multi(tp, 0);
8975 } else {
8976 /* Accept one or more multicast(s). */
8977 struct netdev_hw_addr *ha;
8978 u32 mc_filter[4] = { 0, };
8979 u32 regidx;
8980 u32 bit;
8981 u32 crc;
8982
8983 netdev_for_each_mc_addr(ha, dev) {
8984 crc = calc_crc(ha->addr, ETH_ALEN);
8985 bit = ~crc & 0x7f;
8986 regidx = (bit & 0x60) >> 5;
8987 bit &= 0x1f;
8988 mc_filter[regidx] |= (1 << bit);
8989 }
8990
8991 tw32(MAC_HASH_REG_0, mc_filter[0]);
8992 tw32(MAC_HASH_REG_1, mc_filter[1]);
8993 tw32(MAC_HASH_REG_2, mc_filter[2]);
8994 tw32(MAC_HASH_REG_3, mc_filter[3]);
8995 }
8996
8997 if (rx_mode != tp->rx_mode) {
8998 tp->rx_mode = rx_mode;
8999 tw32_f(MAC_RX_MODE, rx_mode);
9000 udelay(10);
9001 }
9002}
9003
Michael Chan91024262012-09-28 07:12:38 +00009004static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00009005{
9006 int i;
9007
9008 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00009009 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00009010}
9011
9012static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009013{
9014 int i;
9015
9016 if (!tg3_flag(tp, SUPPORT_MSIX))
9017 return;
9018
Michael Chan0b3ba052012-11-14 14:44:29 +00009019 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009020 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00009021 return;
9022 }
9023
9024 /* Validate table against current IRQ count */
9025 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00009026 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00009027 break;
9028 }
9029
9030 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00009031 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009032}
9033
Matt Carlson90415472011-12-16 13:33:23 +00009034static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009035{
9036 int i = 0;
9037 u32 reg = MAC_RSS_INDIR_TBL_0;
9038
9039 while (i < TG3_RSS_INDIR_TBL_SIZE) {
9040 u32 val = tp->rss_ind_tbl[i];
9041 i++;
9042 for (; i % 8; i++) {
9043 val <<= 4;
9044 val |= tp->rss_ind_tbl[i];
9045 }
9046 tw32(reg, val);
9047 reg += 4;
9048 }
9049}
9050
Matt Carlson2d31eca2009-09-01 12:53:31 +00009051/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009052static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053{
9054 u32 val, rdmac_mode;
9055 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00009056 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009057
9058 tg3_disable_ints(tp);
9059
9060 tg3_stop_fw(tp);
9061
9062 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
9063
Joe Perches63c3a662011-04-26 08:12:10 +00009064 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07009065 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066
Matt Carlson699c0192010-12-06 08:28:51 +00009067 /* Enable MAC control of LPI */
9068 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009069 val = TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
9070 TG3_CPMU_EEE_LNKIDL_UART_IDL;
Joe Perches41535772013-02-16 11:20:04 +00009071 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Michael Chanc65a17f2013-01-06 12:51:07 +00009072 val |= TG3_CPMU_EEE_LNKIDL_APE_TX_MT;
9073
9074 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL, val);
Matt Carlson699c0192010-12-06 08:28:51 +00009075
9076 tw32_f(TG3_CPMU_EEE_CTRL,
9077 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9078
Matt Carlsona386b902010-12-06 08:28:53 +00009079 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9080 TG3_CPMU_EEEMD_LPI_IN_TX |
9081 TG3_CPMU_EEEMD_LPI_IN_RX |
9082 TG3_CPMU_EEEMD_EEE_ENABLE;
9083
Joe Perches41535772013-02-16 11:20:04 +00009084 if (tg3_asic_rev(tp) != ASIC_REV_5717)
Matt Carlsona386b902010-12-06 08:28:53 +00009085 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9086
Joe Perches63c3a662011-04-26 08:12:10 +00009087 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009088 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9089
9090 tw32_f(TG3_CPMU_EEE_MODE, val);
9091
9092 tw32_f(TG3_CPMU_EEE_DBTMR1,
9093 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9094 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9095
9096 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009097 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009098 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009099 }
9100
Matt Carlson603f1172010-02-12 14:47:10 +00009101 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009102 tg3_phy_reset(tp);
9103
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 err = tg3_chip_reset(tp);
9105 if (err)
9106 return err;
9107
9108 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9109
Joe Perches41535772013-02-16 11:20:04 +00009110 if (tg3_chip_rev(tp) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009111 val = tr32(TG3_CPMU_CTRL);
9112 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9113 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009114
9115 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9116 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9117 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9118 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9119
9120 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9121 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9122 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9123 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9124
9125 val = tr32(TG3_CPMU_HST_ACC);
9126 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9127 val |= CPMU_HST_ACC_MACCLK_6_25;
9128 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009129 }
9130
Joe Perches41535772013-02-16 11:20:04 +00009131 if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson33466d92009-04-20 06:57:41 +00009132 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9133 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9134 PCIE_PWR_MGMT_L1_THRESH_4MS;
9135 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009136
9137 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9138 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9139
9140 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00009141
Matt Carlsonf40386c2009-11-02 14:24:02 +00009142 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9143 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009144 }
9145
Joe Perches63c3a662011-04-26 08:12:10 +00009146 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009147 u32 grc_mode = tr32(GRC_MODE);
9148
9149 /* Access the lower 1K of PL PCIE block registers. */
9150 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9151 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9152
9153 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9154 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9155 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9156
9157 tw32(GRC_MODE, grc_mode);
9158 }
9159
Matt Carlson55086ad2011-12-14 11:09:59 +00009160 if (tg3_flag(tp, 57765_CLASS)) {
Joe Perches41535772013-02-16 11:20:04 +00009161 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009162 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009163
Matt Carlson5093eed2010-11-24 08:31:45 +00009164 /* Access the lower 1K of PL PCIE block registers. */
9165 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9166 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009167
Matt Carlson5093eed2010-11-24 08:31:45 +00009168 val = tr32(TG3_PCIE_TLDLPL_PORT +
9169 TG3_PCIE_PL_LO_PHYCTL5);
9170 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9171 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009172
Matt Carlson5093eed2010-11-24 08:31:45 +00009173 tw32(GRC_MODE, grc_mode);
9174 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009175
Joe Perches41535772013-02-16 11:20:04 +00009176 if (tg3_chip_rev(tp) != CHIPREV_57765_AX) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009177 u32 grc_mode;
9178
9179 /* Fix transmit hangs */
9180 val = tr32(TG3_CPMU_PADRNG_CTL);
9181 val |= TG3_CPMU_PADRNG_CTL_RDIV2;
9182 tw32(TG3_CPMU_PADRNG_CTL, val);
9183
9184 grc_mode = tr32(GRC_MODE);
Matt Carlson1ff30a52011-05-19 12:12:46 +00009185
9186 /* Access the lower 1K of DL PCIE block registers. */
9187 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9188 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9189
9190 val = tr32(TG3_PCIE_TLDLPL_PORT +
9191 TG3_PCIE_DL_LO_FTSMAX);
9192 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9193 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9194 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9195
9196 tw32(GRC_MODE, grc_mode);
9197 }
9198
Matt Carlsona977dbe2010-04-12 06:58:26 +00009199 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9200 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9201 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9202 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009203 }
9204
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 /* This works around an issue with Athlon chipsets on
9206 * B3 tigon3 silicon. This bit has no effect on any
9207 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009208 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 */
Joe Perches63c3a662011-04-26 08:12:10 +00009210 if (!tg3_flag(tp, CPMU_PRESENT)) {
9211 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009212 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9213 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
Joe Perches41535772013-02-16 11:20:04 +00009216 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009217 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 val = tr32(TG3PCI_PCISTATE);
9219 val |= PCISTATE_RETRY_SAME_DMA;
9220 tw32(TG3PCI_PCISTATE, val);
9221 }
9222
Joe Perches63c3a662011-04-26 08:12:10 +00009223 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009224 /* Allow reads and writes to the
9225 * APE register and memory space.
9226 */
9227 val = tr32(TG3PCI_PCISTATE);
9228 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009229 PCISTATE_ALLOW_APE_SHMEM_WR |
9230 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009231 tw32(TG3PCI_PCISTATE, val);
9232 }
9233
Joe Perches41535772013-02-16 11:20:04 +00009234 if (tg3_chip_rev(tp) == CHIPREV_5704_BX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 /* Enable some hw fixes. */
9236 val = tr32(TG3PCI_MSI_DATA);
9237 val |= (1 << 26) | (1 << 28) | (1 << 29);
9238 tw32(TG3PCI_MSI_DATA, val);
9239 }
9240
9241 /* Descriptor ring init may make accesses to the
9242 * NIC SRAM area to setup the TX descriptors, so we
9243 * can only do this after the hardware has been
9244 * successfully reset.
9245 */
Michael Chan32d8c572006-07-25 16:38:29 -07009246 err = tg3_init_rings(tp);
9247 if (err)
9248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249
Joe Perches63c3a662011-04-26 08:12:10 +00009250 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009251 val = tr32(TG3PCI_DMA_RW_CTRL) &
9252 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Joe Perches41535772013-02-16 11:20:04 +00009253 if (tg3_chip_rev_id(tp) == CHIPREV_ID_57765_A0)
Matt Carlson1a319022010-04-12 06:58:25 +00009254 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009255 if (!tg3_flag(tp, 57765_CLASS) &&
Joe Perches41535772013-02-16 11:20:04 +00009256 tg3_asic_rev(tp) != ASIC_REV_5717 &&
9257 tg3_asic_rev(tp) != ASIC_REV_5762)
Matt Carlson0aebff42011-04-25 12:42:45 +00009258 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009259 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
Joe Perches41535772013-02-16 11:20:04 +00009260 } else if (tg3_asic_rev(tp) != ASIC_REV_5784 &&
9261 tg3_asic_rev(tp) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009262 /* This value is determined during the probe time DMA
9263 * engine test, tg3_test_dma.
9264 */
9265 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
9268 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9269 GRC_MODE_4X_NIC_SEND_RINGS |
9270 GRC_MODE_NO_TX_PHDR_CSUM |
9271 GRC_MODE_NO_RX_PHDR_CSUM);
9272 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009273
9274 /* Pseudo-header checksum is done by hardware logic and not
9275 * the offload processers, so make the chip do the pseudo-
9276 * header checksums on receive. For transmit it is more
9277 * convenient to do the pseudo-header checksum in software
9278 * as Linux does that on transmit for us in all cases.
9279 */
9280 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009282 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9283 if (tp->rxptpctl)
9284 tw32(TG3_RX_PTP_CTL,
9285 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9286
9287 if (tg3_flag(tp, PTP_CAPABLE))
9288 val |= GRC_MODE_TIME_SYNC_ENABLE;
9289
9290 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291
9292 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9293 val = tr32(GRC_MISC_CFG);
9294 val &= ~0xff;
9295 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9296 tw32(GRC_MISC_CFG, val);
9297
9298 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009299 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 /* Do nothing. */
Joe Perches41535772013-02-16 11:20:04 +00009301 } else if (tg3_asic_rev(tp) != ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
Joe Perches41535772013-02-16 11:20:04 +00009303 if (tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9305 else
9306 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9307 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9308 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009309 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310 int fw_len;
9311
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009312 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9314 tw32(BUFMGR_MB_POOL_ADDR,
9315 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9316 tw32(BUFMGR_MB_POOL_SIZE,
9317 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319
Michael Chan0f893dc2005-07-25 12:30:38 -07009320 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9322 tp->bufmgr_config.mbuf_read_dma_low_water);
9323 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9324 tp->bufmgr_config.mbuf_mac_rx_low_water);
9325 tw32(BUFMGR_MB_HIGH_WATER,
9326 tp->bufmgr_config.mbuf_high_water);
9327 } else {
9328 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9329 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9330 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9331 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9332 tw32(BUFMGR_MB_HIGH_WATER,
9333 tp->bufmgr_config.mbuf_high_water_jumbo);
9334 }
9335 tw32(BUFMGR_DMA_LOW_WATER,
9336 tp->bufmgr_config.dma_low_water);
9337 tw32(BUFMGR_DMA_HIGH_WATER,
9338 tp->bufmgr_config.dma_high_water);
9339
Matt Carlsond309a462010-09-30 10:34:31 +00009340 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
Joe Perches41535772013-02-16 11:20:04 +00009341 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsond309a462010-09-30 10:34:31 +00009342 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Joe Perches41535772013-02-16 11:20:04 +00009343 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
9344 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9345 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0)
Matt Carlson4d958472011-04-20 07:57:35 +00009346 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009347 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348 for (i = 0; i < 2000; i++) {
9349 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9350 break;
9351 udelay(10);
9352 }
9353 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009354 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009355 return -ENODEV;
9356 }
9357
Joe Perches41535772013-02-16 11:20:04 +00009358 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5906_A1)
Matt Carlsoneb07a942011-04-20 07:57:36 +00009359 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009360
Matt Carlsoneb07a942011-04-20 07:57:36 +00009361 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362
9363 /* Initialize TG3_BDINFO's at:
9364 * RCVDBDI_STD_BD: standard eth size rx ring
9365 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9366 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9367 *
9368 * like so:
9369 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9370 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9371 * ring attribute flags
9372 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9373 *
9374 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9375 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9376 *
9377 * The size of each ring is fixed in the firmware, but the location is
9378 * configurable.
9379 */
9380 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009381 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009383 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009384 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009385 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9386 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009388 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009389 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9391 BDINFO_FLAGS_DISABLED);
9392
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009393 /* Program the jumbo buffer descriptor ring control
9394 * blocks on those devices that have them.
9395 */
Joe Perches41535772013-02-16 11:20:04 +00009396 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009397 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398
Joe Perches63c3a662011-04-26 08:12:10 +00009399 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009401 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009403 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009404 val = TG3_RX_JMB_RING_SIZE(tp) <<
9405 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009407 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009408 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Michael Chanc65a17f2013-01-06 12:51:07 +00009409 tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +00009410 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson87668d32009-11-13 13:03:34 +00009411 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9412 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 } else {
9414 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9415 BDINFO_FLAGS_DISABLED);
9416 }
9417
Joe Perches63c3a662011-04-26 08:12:10 +00009418 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009419 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009420 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9421 val |= (TG3_RX_STD_DMA_SZ << 2);
9422 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009423 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009424 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009425 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009426
9427 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
Matt Carlson411da642009-11-13 13:03:46 +00009429 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009430 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431
Joe Perches63c3a662011-04-26 08:12:10 +00009432 tpr->rx_jmb_prod_idx =
9433 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009434 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435
Matt Carlson2d31eca2009-09-01 12:53:31 +00009436 tg3_rings_reset(tp);
9437
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009439 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440
9441 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009442 tw32(MAC_RX_MTU_SIZE,
9443 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444
9445 /* The slot time is changed by tg3_setup_phy if we
9446 * run at gigabit with half duplex.
9447 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009448 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9449 (6 << TX_LENGTHS_IPG_SHIFT) |
9450 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9451
Joe Perches41535772013-02-16 11:20:04 +00009452 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9453 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009454 val |= tr32(MAC_TX_LENGTHS) &
9455 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9456 TX_LENGTHS_CNT_DWN_VAL_MSK);
9457
9458 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459
9460 /* Receive rules. */
9461 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9462 tw32(RCVLPC_CONFIG, 0x0181);
9463
9464 /* Calculate RDMAC_MODE setting early, we need it to determine
9465 * the RCVLPC_STATE_ENABLE mask.
9466 */
9467 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9468 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9469 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9470 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9471 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009472
Joe Perches41535772013-02-16 11:20:04 +00009473 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009474 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9475
Joe Perches41535772013-02-16 11:20:04 +00009476 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
9477 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9478 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009479 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9480 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9481 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9482
Joe Perches41535772013-02-16 11:20:04 +00009483 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9484 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009485 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009486 tg3_asic_rev(tp) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9488 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009489 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9491 }
9492 }
9493
Joe Perches63c3a662011-04-26 08:12:10 +00009494 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009495 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9496
Joe Perches41535772013-02-16 11:20:04 +00009497 if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlsond3f677a2013-02-14 14:27:51 +00009498 tp->dma_limit = 0;
9499 if (tp->dev->mtu <= ETH_DATA_LEN) {
9500 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
9501 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
9502 }
9503 }
9504
Joe Perches63c3a662011-04-26 08:12:10 +00009505 if (tg3_flag(tp, HW_TSO_1) ||
9506 tg3_flag(tp, HW_TSO_2) ||
9507 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009508 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9509
Matt Carlson108a6c12011-05-19 12:12:47 +00009510 if (tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009511 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9512 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson027455a2008-12-21 20:19:30 -08009513 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514
Joe Perches41535772013-02-16 11:20:04 +00009515 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9516 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +00009517 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9518
Joe Perches41535772013-02-16 11:20:04 +00009519 if (tg3_asic_rev(tp) == ASIC_REV_5761 ||
9520 tg3_asic_rev(tp) == ASIC_REV_5784 ||
9521 tg3_asic_rev(tp) == ASIC_REV_5785 ||
9522 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009523 tg3_flag(tp, 57765_PLUS)) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009524 u32 tgtreg;
9525
Joe Perches41535772013-02-16 11:20:04 +00009526 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009527 tgtreg = TG3_RDMA_RSRVCTRL_REG2;
9528 else
9529 tgtreg = TG3_RDMA_RSRVCTRL_REG;
9530
9531 val = tr32(tgtreg);
Joe Perches41535772013-02-16 11:20:04 +00009532 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
9533 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009534 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9535 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9536 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9537 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9538 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9539 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009540 }
Michael Chanc65a17f2013-01-06 12:51:07 +00009541 tw32(tgtreg, val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009542 }
9543
Joe Perches41535772013-02-16 11:20:04 +00009544 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
9545 tg3_asic_rev(tp) == ASIC_REV_5720 ||
9546 tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc65a17f2013-01-06 12:51:07 +00009547 u32 tgtreg;
9548
Joe Perches41535772013-02-16 11:20:04 +00009549 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc65a17f2013-01-06 12:51:07 +00009550 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL2;
9551 else
9552 tgtreg = TG3_LSO_RD_DMA_CRPTEN_CTRL;
9553
9554 val = tr32(tgtreg);
9555 tw32(tgtreg, val |
Matt Carlsond309a462010-09-30 10:34:31 +00009556 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9557 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9558 }
9559
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009561 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009562 val = tr32(RCVLPC_STATS_ENABLE);
9563 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9564 tw32(RCVLPC_STATS_ENABLE, val);
9565 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009566 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 val = tr32(RCVLPC_STATS_ENABLE);
9568 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9569 tw32(RCVLPC_STATS_ENABLE, val);
9570 } else {
9571 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9572 }
9573 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9574 tw32(SNDDATAI_STATSENAB, 0xffffff);
9575 tw32(SNDDATAI_STATSCTRL,
9576 (SNDDATAI_SCTRL_ENABLE |
9577 SNDDATAI_SCTRL_FASTUPD));
9578
9579 /* Setup host coalescing engine. */
9580 tw32(HOSTCC_MODE, 0);
9581 for (i = 0; i < 2000; i++) {
9582 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9583 break;
9584 udelay(10);
9585 }
9586
Michael Chand244c892005-07-05 14:42:33 -07009587 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588
Joe Perches63c3a662011-04-26 08:12:10 +00009589 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590 /* Status/statistics block address. See tg3_timer,
9591 * the tg3_periodic_fetch_stats call there, and
9592 * tg3_get_stats to see how this works for 5705/5750 chips.
9593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009594 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9595 ((u64) tp->stats_mapping >> 32));
9596 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9597 ((u64) tp->stats_mapping & 0xffffffff));
9598 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009599
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009601
9602 /* Clear statistics and status block memory areas */
9603 for (i = NIC_SRAM_STATS_BLK;
9604 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9605 i += sizeof(u32)) {
9606 tg3_write_mem(tp, i, 0);
9607 udelay(40);
9608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 }
9610
9611 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9612
9613 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9614 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009615 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009618 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9619 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009620 /* reset to prevent losing 1st rx packet intermittently */
9621 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9622 udelay(10);
9623 }
9624
Matt Carlson3bda1252008-08-15 14:08:22 -07009625 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009626 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9627 MAC_MODE_FHDE_ENABLE;
9628 if (tg3_flag(tp, ENABLE_APE))
9629 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009630 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009631 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009632 tg3_asic_rev(tp) != ASIC_REV_5700)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009633 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9635 udelay(40);
9636
Michael Chan314fba32005-04-21 17:07:04 -07009637 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009638 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009639 * register to preserve the GPIO settings for LOMs. The GPIOs,
9640 * whether used as inputs or outputs, are set by boot code after
9641 * reset.
9642 */
Joe Perches63c3a662011-04-26 08:12:10 +00009643 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009644 u32 gpio_mask;
9645
Michael Chan9d26e212006-12-07 00:21:14 -08009646 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9647 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9648 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009649
Joe Perches41535772013-02-16 11:20:04 +00009650 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -07009651 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9652 GRC_LCLCTRL_GPIO_OUTPUT3;
9653
Joe Perches41535772013-02-16 11:20:04 +00009654 if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanaf36e6b2006-03-23 01:28:06 -08009655 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9656
Gary Zambranoaaf84462007-05-05 11:51:45 -07009657 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009658 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9659
9660 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009661 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009662 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9663 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9666 udelay(100);
9667
Matt Carlsonc3b50032012-01-17 15:27:23 +00009668 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009669 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009670 val |= MSGINT_MODE_ENABLE;
9671 if (tp->irq_cnt > 1)
9672 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009673 if (!tg3_flag(tp, 1SHOT_MSI))
9674 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009675 tw32(MSGINT_MODE, val);
9676 }
9677
Joe Perches63c3a662011-04-26 08:12:10 +00009678 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9680 udelay(40);
9681 }
9682
9683 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9684 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9685 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9686 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9687 WDMAC_MODE_LNGREAD_ENAB);
9688
Joe Perches41535772013-02-16 11:20:04 +00009689 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
9690 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009691 if (tg3_flag(tp, TSO_CAPABLE) &&
Joe Perches41535772013-02-16 11:20:04 +00009692 (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 ||
9693 tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 /* nothing */
9695 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009696 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009697 val |= WDMAC_MODE_RX_ACCEL;
9698 }
9699 }
9700
Michael Chand9ab5ad12006-03-20 22:27:35 -08009701 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009702 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009703 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009704
Joe Perches41535772013-02-16 11:20:04 +00009705 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlson788a0352009-11-02 14:26:03 +00009706 val |= WDMAC_MODE_BURST_ALL_DATA;
9707
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708 tw32_f(WDMAC_MODE, val);
9709 udelay(40);
9710
Joe Perches63c3a662011-04-26 08:12:10 +00009711 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009712 u16 pcix_cmd;
9713
9714 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9715 &pcix_cmd);
Joe Perches41535772013-02-16 11:20:04 +00009716 if (tg3_asic_rev(tp) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009717 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9718 pcix_cmd |= PCI_X_CMD_READ_2K;
Joe Perches41535772013-02-16 11:20:04 +00009719 } else if (tg3_asic_rev(tp) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009720 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9721 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722 }
Matt Carlson9974a352007-10-07 23:27:28 -07009723 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9724 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725 }
9726
9727 tw32_f(RDMAC_MODE, rdmac_mode);
9728 udelay(40);
9729
Joe Perches41535772013-02-16 11:20:04 +00009730 if (tg3_asic_rev(tp) == ASIC_REV_5719) {
Michael Chan091f0ea2012-07-29 19:15:43 +00009731 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9732 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9733 break;
9734 }
9735 if (i < TG3_NUM_RDMA_CHANNELS) {
9736 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9737 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9738 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9739 tg3_flag_set(tp, 5719_RDMA_BUG);
9740 }
9741 }
9742
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009744 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009746
Joe Perches41535772013-02-16 11:20:04 +00009747 if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson9936bcf2007-10-10 18:03:07 -07009748 tw32(SNDDATAC_MODE,
9749 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9750 else
9751 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9752
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9754 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009755 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009756 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009757 val |= RCVDBDI_MODE_LRG_RING_SZ;
9758 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009760 if (tg3_flag(tp, HW_TSO_1) ||
9761 tg3_flag(tp, HW_TSO_2) ||
9762 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009764 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009765 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009766 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9767 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9769
Joe Perches41535772013-02-16 11:20:04 +00009770 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 err = tg3_load_5701_a0_firmware_fix(tp);
9772 if (err)
9773 return err;
9774 }
9775
Joe Perches63c3a662011-04-26 08:12:10 +00009776 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 err = tg3_load_tso_firmware(tp);
9778 if (err)
9779 return err;
9780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781
9782 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009783
Joe Perches63c3a662011-04-26 08:12:10 +00009784 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +00009785 tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonb1d05212010-06-05 17:24:31 +00009786 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009787
Joe Perches41535772013-02-16 11:20:04 +00009788 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
9789 tg3_asic_rev(tp) == ASIC_REV_5762) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00009790 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9791 tp->tx_mode &= ~val;
9792 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9793 }
9794
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 tw32_f(MAC_TX_MODE, tp->tx_mode);
9796 udelay(100);
9797
Joe Perches63c3a662011-04-26 08:12:10 +00009798 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009799 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009800
9801 /* Setup the "secret" hash key. */
9802 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9803 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9804 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9805 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9806 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9807 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9808 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9809 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9810 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9811 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9812 }
9813
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009815 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009816 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9817
Joe Perches63c3a662011-04-26 08:12:10 +00009818 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009819 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9820 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9821 RX_MODE_RSS_IPV6_HASH_EN |
9822 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9823 RX_MODE_RSS_IPV4_HASH_EN |
9824 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9825
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 tw32_f(MAC_RX_MODE, tp->rx_mode);
9827 udelay(10);
9828
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829 tw32(MAC_LED_CTRL, tp->led_ctrl);
9830
9831 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009832 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9834 udelay(10);
9835 }
9836 tw32_f(MAC_RX_MODE, tp->rx_mode);
9837 udelay(10);
9838
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009839 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Joe Perches41535772013-02-16 11:20:04 +00009840 if ((tg3_asic_rev(tp) == ASIC_REV_5704) &&
9841 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 /* Set drive transmission level to 1.2V */
9843 /* only if the signal pre-emphasis bit is not set */
9844 val = tr32(MAC_SERDES_CFG);
9845 val &= 0xfffff000;
9846 val |= 0x880;
9847 tw32(MAC_SERDES_CFG, val);
9848 }
Joe Perches41535772013-02-16 11:20:04 +00009849 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 tw32(MAC_SERDES_CFG, 0x616000);
9851 }
9852
9853 /* Prevent chip from dropping frames when flow control
9854 * is enabled.
9855 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009856 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009857 val = 1;
9858 else
9859 val = 2;
9860 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861
Joe Perches41535772013-02-16 11:20:04 +00009862 if (tg3_asic_rev(tp) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009863 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009865 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866 }
9867
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009868 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +00009869 tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009870 u32 tmp;
9871
9872 tmp = tr32(SERDES_RX_CTRL);
9873 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9874 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9875 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9876 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9877 }
9878
Joe Perches63c3a662011-04-26 08:12:10 +00009879 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009880 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009881 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882
Matt Carlsondd477002008-05-25 23:45:58 -07009883 err = tg3_setup_phy(tp, 0);
9884 if (err)
9885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009887 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9888 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009889 u32 tmp;
9890
9891 /* Clear CRC stats. */
9892 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9893 tg3_writephy(tp, MII_TG3_TEST1,
9894 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009895 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 }
9898 }
9899
9900 __tg3_set_rx_mode(tp->dev);
9901
9902 /* Initialize receive rules. */
9903 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9904 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9905 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9906 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9907
Joe Perches63c3a662011-04-26 08:12:10 +00009908 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909 limit = 8;
9910 else
9911 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009912 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009913 limit -= 4;
9914 switch (limit) {
9915 case 16:
9916 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9917 case 15:
9918 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9919 case 14:
9920 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9921 case 13:
9922 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9923 case 12:
9924 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9925 case 11:
9926 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9927 case 10:
9928 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9929 case 9:
9930 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9931 case 8:
9932 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9933 case 7:
9934 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9935 case 6:
9936 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9937 case 5:
9938 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9939 case 4:
9940 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9941 case 3:
9942 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9943 case 2:
9944 case 1:
9945
9946 default:
9947 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949
Joe Perches63c3a662011-04-26 08:12:10 +00009950 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009951 /* Write our heartbeat update interval to APE. */
9952 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9953 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009954
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9956
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957 return 0;
9958}
9959
9960/* Called at device open time to get the chip ready for
9961 * packet processing. Invoked with tp->lock held.
9962 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009963static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965 tg3_switch_clocks(tp);
9966
9967 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9968
Matt Carlson2f751b62008-08-04 23:17:34 -07009969 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009970}
9971
Michael Chanaed93e02012-07-16 16:24:02 +00009972static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9973{
9974 int i;
9975
9976 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9977 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9978
9979 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9980 off += len;
9981
9982 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9983 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9984 memset(ocir, 0, TG3_OCIR_LEN);
9985 }
9986}
9987
9988/* sysfs attributes for hwmon */
9989static ssize_t tg3_show_temp(struct device *dev,
9990 struct device_attribute *devattr, char *buf)
9991{
9992 struct pci_dev *pdev = to_pci_dev(dev);
9993 struct net_device *netdev = pci_get_drvdata(pdev);
9994 struct tg3 *tp = netdev_priv(netdev);
9995 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9996 u32 temperature;
9997
9998 spin_lock_bh(&tp->lock);
9999 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
10000 sizeof(temperature));
10001 spin_unlock_bh(&tp->lock);
10002 return sprintf(buf, "%u\n", temperature);
10003}
10004
10005
10006static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
10007 TG3_TEMP_SENSOR_OFFSET);
10008static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
10009 TG3_TEMP_CAUTION_OFFSET);
10010static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
10011 TG3_TEMP_MAX_OFFSET);
10012
10013static struct attribute *tg3_attributes[] = {
10014 &sensor_dev_attr_temp1_input.dev_attr.attr,
10015 &sensor_dev_attr_temp1_crit.dev_attr.attr,
10016 &sensor_dev_attr_temp1_max.dev_attr.attr,
10017 NULL
10018};
10019
10020static const struct attribute_group tg3_group = {
10021 .attrs = tg3_attributes,
10022};
10023
Michael Chanaed93e02012-07-16 16:24:02 +000010024static void tg3_hwmon_close(struct tg3 *tp)
10025{
Michael Chanaed93e02012-07-16 16:24:02 +000010026 if (tp->hwmon_dev) {
10027 hwmon_device_unregister(tp->hwmon_dev);
10028 tp->hwmon_dev = NULL;
10029 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
10030 }
Michael Chanaed93e02012-07-16 16:24:02 +000010031}
10032
10033static void tg3_hwmon_open(struct tg3 *tp)
10034{
Michael Chanaed93e02012-07-16 16:24:02 +000010035 int i, err;
10036 u32 size = 0;
10037 struct pci_dev *pdev = tp->pdev;
10038 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
10039
10040 tg3_sd_scan_scratchpad(tp, ocirs);
10041
10042 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
10043 if (!ocirs[i].src_data_length)
10044 continue;
10045
10046 size += ocirs[i].src_hdr_length;
10047 size += ocirs[i].src_data_length;
10048 }
10049
10050 if (!size)
10051 return;
10052
10053 /* Register hwmon sysfs hooks */
10054 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
10055 if (err) {
10056 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
10057 return;
10058 }
10059
10060 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
10061 if (IS_ERR(tp->hwmon_dev)) {
10062 tp->hwmon_dev = NULL;
10063 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
10064 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
10065 }
Michael Chanaed93e02012-07-16 16:24:02 +000010066}
10067
10068
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069#define TG3_STAT_ADD32(PSTAT, REG) \
10070do { u32 __val = tr32(REG); \
10071 (PSTAT)->low += __val; \
10072 if ((PSTAT)->low < __val) \
10073 (PSTAT)->high += 1; \
10074} while (0)
10075
10076static void tg3_periodic_fetch_stats(struct tg3 *tp)
10077{
10078 struct tg3_hw_stats *sp = tp->hw_stats;
10079
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010080 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081 return;
10082
10083 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
10084 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
10085 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
10086 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
10087 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
10088 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
10089 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
10090 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
10091 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
10092 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
10093 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
10094 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
10095 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +000010096 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
10097 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
10098 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
10099 u32 val;
10100
10101 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
10102 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
10103 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
10104 tg3_flag_clear(tp, 5719_RDMA_BUG);
10105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
10107 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
10108 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
10109 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10110 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10111 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10112 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10113 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10114 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10115 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10116 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10117 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10118 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10119 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10120 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010121
10122 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Joe Perches41535772013-02-16 11:20:04 +000010123 if (tg3_asic_rev(tp) != ASIC_REV_5717 &&
10124 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0 &&
10125 tg3_chip_rev_id(tp) != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010126 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10127 } else {
10128 u32 val = tr32(HOSTCC_FLOW_ATTN);
10129 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10130 if (val) {
10131 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10132 sp->rx_discards.low += val;
10133 if (sp->rx_discards.low < val)
10134 sp->rx_discards.high += 1;
10135 }
10136 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10137 }
Michael Chan463d3052006-05-22 16:36:27 -070010138 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139}
10140
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010141static void tg3_chk_missed_msi(struct tg3 *tp)
10142{
10143 u32 i;
10144
10145 for (i = 0; i < tp->irq_cnt; i++) {
10146 struct tg3_napi *tnapi = &tp->napi[i];
10147
10148 if (tg3_has_work(tnapi)) {
10149 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10150 tnapi->last_tx_cons == tnapi->tx_cons) {
10151 if (tnapi->chk_msi_cnt < 1) {
10152 tnapi->chk_msi_cnt++;
10153 return;
10154 }
Matt Carlson7f230732011-08-31 11:44:48 +000010155 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010156 }
10157 }
10158 tnapi->chk_msi_cnt = 0;
10159 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10160 tnapi->last_tx_cons = tnapi->tx_cons;
10161 }
10162}
10163
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164static void tg3_timer(unsigned long __opaque)
10165{
10166 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167
Matt Carlson5b190622011-11-04 09:15:04 +000010168 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010169 goto restart_timer;
10170
David S. Millerf47c11e2005-06-24 20:18:35 -070010171 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172
Joe Perches41535772013-02-16 11:20:04 +000010173 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010174 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010175 tg3_chk_missed_msi(tp);
10176
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000010177 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
10178 /* BCM4785: Flush posted writes from GbE to host memory. */
10179 tr32(HOSTCC_MODE);
10180 }
10181
Joe Perches63c3a662011-04-26 08:12:10 +000010182 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010183 /* All of this garbage is because when using non-tagged
10184 * IRQ status the mailbox/status_block protocol the chip
10185 * uses with the cpu is race prone.
10186 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010187 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010188 tw32(GRC_LOCAL_CTRL,
10189 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10190 } else {
10191 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010192 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010194
David S. Millerfac9b832005-05-18 22:46:34 -070010195 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010196 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010197 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010198 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200 }
10201
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202 /* This part only runs once per second. */
10203 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010204 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010205 tg3_periodic_fetch_stats(tp);
10206
Matt Carlsonb0c59432011-05-19 12:12:48 +000010207 if (tp->setlpicnt && !--tp->setlpicnt)
10208 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010209
Joe Perches63c3a662011-04-26 08:12:10 +000010210 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211 u32 mac_stat;
10212 int phy_event;
10213
10214 mac_stat = tr32(MAC_STATUS);
10215
10216 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010217 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10219 phy_event = 1;
10220 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10221 phy_event = 1;
10222
10223 if (phy_event)
10224 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010225 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 u32 mac_stat = tr32(MAC_STATUS);
10227 int need_setup = 0;
10228
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010229 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10231 need_setup = 1;
10232 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010233 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10235 MAC_STATUS_SIGNAL_DET))) {
10236 need_setup = 1;
10237 }
10238 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010239 if (!tp->serdes_counter) {
10240 tw32_f(MAC_MODE,
10241 (tp->mac_mode &
10242 ~MAC_MODE_PORT_MODE_MASK));
10243 udelay(40);
10244 tw32_f(MAC_MODE, tp->mac_mode);
10245 udelay(40);
10246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 tg3_setup_phy(tp, 0);
10248 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010249 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010250 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010251 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253
10254 tp->timer_counter = tp->timer_multiplier;
10255 }
10256
Michael Chan130b8e42006-09-27 16:00:40 -070010257 /* Heartbeat is only sent once every 2 seconds.
10258 *
10259 * The heartbeat is to tell the ASF firmware that the host
10260 * driver is still alive. In the event that the OS crashes,
10261 * ASF needs to reset the hardware to free up the FIFO space
10262 * that may be filled with rx packets destined for the host.
10263 * If the FIFO is full, ASF will no longer function properly.
10264 *
10265 * Unintended resets have been reported on real time kernels
10266 * where the timer doesn't run on time. Netpoll will also have
10267 * same problem.
10268 *
10269 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10270 * to check the ring condition when the heartbeat is expiring
10271 * before doing the reset. This will prevent most unintended
10272 * resets.
10273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010274 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010275 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010276 tg3_wait_for_event_ack(tp);
10277
Michael Chanbbadf502006-04-06 21:46:34 -070010278 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010279 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010280 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010281 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10282 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010283
10284 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285 }
10286 tp->asf_counter = tp->asf_multiplier;
10287 }
10288
David S. Millerf47c11e2005-06-24 20:18:35 -070010289 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290
Michael Chanf475f162006-03-27 23:20:14 -080010291restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010292 tp->timer.expires = jiffies + tp->timer_offset;
10293 add_timer(&tp->timer);
10294}
10295
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010296static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010297{
10298 if (tg3_flag(tp, TAGGED_STATUS) &&
Joe Perches41535772013-02-16 11:20:04 +000010299 tg3_asic_rev(tp) != ASIC_REV_5717 &&
Matt Carlson21f76382012-02-22 12:35:21 +000010300 !tg3_flag(tp, 57765_CLASS))
10301 tp->timer_offset = HZ;
10302 else
10303 tp->timer_offset = HZ / 10;
10304
10305 BUG_ON(tp->timer_offset > HZ);
10306
10307 tp->timer_multiplier = (HZ / tp->timer_offset);
10308 tp->asf_multiplier = (HZ / tp->timer_offset) *
10309 TG3_FW_UPDATE_FREQ_SEC;
10310
10311 init_timer(&tp->timer);
10312 tp->timer.data = (unsigned long) tp;
10313 tp->timer.function = tg3_timer;
10314}
10315
10316static void tg3_timer_start(struct tg3 *tp)
10317{
10318 tp->asf_counter = tp->asf_multiplier;
10319 tp->timer_counter = tp->timer_multiplier;
10320
10321 tp->timer.expires = jiffies + tp->timer_offset;
10322 add_timer(&tp->timer);
10323}
10324
10325static void tg3_timer_stop(struct tg3 *tp)
10326{
10327 del_timer_sync(&tp->timer);
10328}
10329
10330/* Restart hardware after configuration changes, self-test, etc.
10331 * Invoked with tp->lock held.
10332 */
10333static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10334 __releases(tp->lock)
10335 __acquires(tp->lock)
10336{
10337 int err;
10338
10339 err = tg3_init_hw(tp, reset_phy);
10340 if (err) {
10341 netdev_err(tp->dev,
10342 "Failed to re-initialize device, aborting\n");
10343 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10344 tg3_full_unlock(tp);
10345 tg3_timer_stop(tp);
10346 tp->irq_sync = 0;
10347 tg3_napi_enable(tp);
10348 dev_close(tp->dev);
10349 tg3_full_lock(tp, 0);
10350 }
10351 return err;
10352}
10353
10354static void tg3_reset_task(struct work_struct *work)
10355{
10356 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10357 int err;
10358
10359 tg3_full_lock(tp, 0);
10360
10361 if (!netif_running(tp->dev)) {
10362 tg3_flag_clear(tp, RESET_TASK_PENDING);
10363 tg3_full_unlock(tp);
10364 return;
10365 }
10366
10367 tg3_full_unlock(tp);
10368
10369 tg3_phy_stop(tp);
10370
10371 tg3_netif_stop(tp);
10372
10373 tg3_full_lock(tp, 1);
10374
10375 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10376 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10377 tp->write32_rx_mbox = tg3_write_flush_reg32;
10378 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10379 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10380 }
10381
10382 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10383 err = tg3_init_hw(tp, 1);
10384 if (err)
10385 goto out;
10386
10387 tg3_netif_start(tp);
10388
10389out:
10390 tg3_full_unlock(tp);
10391
10392 if (!err)
10393 tg3_phy_start(tp);
10394
10395 tg3_flag_clear(tp, RESET_TASK_PENDING);
10396}
10397
Matt Carlson4f125f42009-09-01 12:55:02 +000010398static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010399{
David Howells7d12e782006-10-05 14:55:46 +010010400 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010401 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010402 char *name;
10403 struct tg3_napi *tnapi = &tp->napi[irq_num];
10404
10405 if (tp->irq_cnt == 1)
10406 name = tp->dev->name;
10407 else {
10408 name = &tnapi->irq_lbl[0];
10409 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10410 name[IFNAMSIZ-1] = 0;
10411 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010414 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010415 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010416 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010417 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010418 } else {
10419 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010420 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010421 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010422 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010423 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010424
10425 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010426}
10427
Michael Chan79381092005-04-21 17:13:59 -070010428static int tg3_test_interrupt(struct tg3 *tp)
10429{
Matt Carlson09943a12009-08-28 14:01:57 +000010430 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010431 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010432 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010433 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010434
Michael Chand4bc3922005-05-29 14:59:20 -070010435 if (!netif_running(dev))
10436 return -ENODEV;
10437
Michael Chan79381092005-04-21 17:13:59 -070010438 tg3_disable_ints(tp);
10439
Matt Carlson4f125f42009-09-01 12:55:02 +000010440 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010441
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010442 /*
10443 * Turn off MSI one shot mode. Otherwise this test has no
10444 * observable way to know whether the interrupt was delivered.
10445 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010446 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010447 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10448 tw32(MSGINT_MODE, val);
10449 }
10450
Matt Carlson4f125f42009-09-01 12:55:02 +000010451 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010452 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010453 if (err)
10454 return err;
10455
Matt Carlson898a56f2009-08-28 14:02:40 +000010456 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010457 tg3_enable_ints(tp);
10458
10459 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010460 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010461
10462 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010463 u32 int_mbox, misc_host_ctrl;
10464
Matt Carlson898a56f2009-08-28 14:02:40 +000010465 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010466 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10467
10468 if ((int_mbox != 0) ||
10469 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10470 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010471 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010472 }
10473
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010474 if (tg3_flag(tp, 57765_PLUS) &&
10475 tnapi->hw_status->status_tag != tnapi->last_tag)
10476 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10477
Michael Chan79381092005-04-21 17:13:59 -070010478 msleep(10);
10479 }
10480
10481 tg3_disable_ints(tp);
10482
Matt Carlson4f125f42009-09-01 12:55:02 +000010483 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010484
Matt Carlson4f125f42009-09-01 12:55:02 +000010485 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010486
10487 if (err)
10488 return err;
10489
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010490 if (intr_ok) {
10491 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010492 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010493 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10494 tw32(MSGINT_MODE, val);
10495 }
Michael Chan79381092005-04-21 17:13:59 -070010496 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010497 }
Michael Chan79381092005-04-21 17:13:59 -070010498
10499 return -EIO;
10500}
10501
10502/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10503 * successfully restored
10504 */
10505static int tg3_test_msi(struct tg3 *tp)
10506{
Michael Chan79381092005-04-21 17:13:59 -070010507 int err;
10508 u16 pci_cmd;
10509
Joe Perches63c3a662011-04-26 08:12:10 +000010510 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010511 return 0;
10512
10513 /* Turn off SERR reporting in case MSI terminates with Master
10514 * Abort.
10515 */
10516 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10517 pci_write_config_word(tp->pdev, PCI_COMMAND,
10518 pci_cmd & ~PCI_COMMAND_SERR);
10519
10520 err = tg3_test_interrupt(tp);
10521
10522 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10523
10524 if (!err)
10525 return 0;
10526
10527 /* other failures */
10528 if (err != -EIO)
10529 return err;
10530
10531 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010532 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10533 "to INTx mode. Please report this failure to the PCI "
10534 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010535
Matt Carlson4f125f42009-09-01 12:55:02 +000010536 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010537
Michael Chan79381092005-04-21 17:13:59 -070010538 pci_disable_msi(tp->pdev);
10539
Joe Perches63c3a662011-04-26 08:12:10 +000010540 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010541 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010542
Matt Carlson4f125f42009-09-01 12:55:02 +000010543 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010544 if (err)
10545 return err;
10546
10547 /* Need to reset the chip because the MSI cycle may have terminated
10548 * with Master Abort.
10549 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010550 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010551
Michael Chan944d9802005-05-29 14:57:48 -070010552 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010553 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010554
David S. Millerf47c11e2005-06-24 20:18:35 -070010555 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010556
10557 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010558 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010559
10560 return err;
10561}
10562
Matt Carlson9e9fd122009-01-19 16:57:45 -080010563static int tg3_request_firmware(struct tg3 *tp)
10564{
10565 const __be32 *fw_data;
10566
10567 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010568 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10569 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010570 return -ENOENT;
10571 }
10572
10573 fw_data = (void *)tp->fw->data;
10574
10575 /* Firmware blob starts with version numbers, followed by
10576 * start address and _full_ length including BSS sections
10577 * (which must be longer than the actual data, of course
10578 */
10579
10580 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10581 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010582 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10583 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010584 release_firmware(tp->fw);
10585 tp->fw = NULL;
10586 return -EINVAL;
10587 }
10588
10589 /* We no longer need firmware; we have it. */
10590 tp->fw_needed = NULL;
10591 return 0;
10592}
10593
Michael Chan91024262012-09-28 07:12:38 +000010594static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010595{
Michael Chan91024262012-09-28 07:12:38 +000010596 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010597
Michael Chan91024262012-09-28 07:12:38 +000010598 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010599 /* We want as many rx rings enabled as there are cpus.
10600 * In multiqueue MSI-X mode, the first MSI-X vector
10601 * only deals with link interrupts, etc, so we add
10602 * one to the number of vectors we are requesting.
10603 */
Michael Chan91024262012-09-28 07:12:38 +000010604 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010605 }
Matt Carlson679563f2009-09-01 12:55:46 +000010606
Michael Chan91024262012-09-28 07:12:38 +000010607 return irq_cnt;
10608}
10609
10610static bool tg3_enable_msix(struct tg3 *tp)
10611{
10612 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010613 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010614
Michael Chan09681692012-09-28 07:12:42 +000010615 tp->txq_cnt = tp->txq_req;
10616 tp->rxq_cnt = tp->rxq_req;
10617 if (!tp->rxq_cnt)
10618 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010619 if (tp->rxq_cnt > tp->rxq_max)
10620 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010621
10622 /* Disable multiple TX rings by default. Simple round-robin hardware
10623 * scheduling of the TX rings can cause starvation of rings with
10624 * small packets when other rings have TSO or jumbo packets.
10625 */
10626 if (!tp->txq_req)
10627 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010628
10629 tp->irq_cnt = tg3_irq_count(tp);
10630
Matt Carlson679563f2009-09-01 12:55:46 +000010631 for (i = 0; i < tp->irq_max; i++) {
10632 msix_ent[i].entry = i;
10633 msix_ent[i].vector = 0;
10634 }
10635
10636 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010637 if (rc < 0) {
10638 return false;
10639 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010640 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10641 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010642 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10643 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010644 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010645 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010646 if (tp->txq_cnt)
10647 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010648 }
10649
10650 for (i = 0; i < tp->irq_max; i++)
10651 tp->napi[i].irq_vec = msix_ent[i].vector;
10652
Michael Chan49a359e2012-09-28 07:12:37 +000010653 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010654 pci_disable_msix(tp->pdev);
10655 return false;
10656 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010657
Michael Chan91024262012-09-28 07:12:38 +000010658 if (tp->irq_cnt == 1)
10659 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010660
Michael Chan91024262012-09-28 07:12:38 +000010661 tg3_flag_set(tp, ENABLE_RSS);
10662
10663 if (tp->txq_cnt > 1)
10664 tg3_flag_set(tp, ENABLE_TSS);
10665
10666 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010667
Matt Carlson679563f2009-09-01 12:55:46 +000010668 return true;
10669}
10670
Matt Carlson07b01732009-08-28 14:01:15 +000010671static void tg3_ints_init(struct tg3 *tp)
10672{
Joe Perches63c3a662011-04-26 08:12:10 +000010673 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10674 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010675 /* All MSI supporting chips should support tagged
10676 * status. Assert that this is the case.
10677 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010678 netdev_warn(tp->dev,
10679 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010680 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010681 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010682
Joe Perches63c3a662011-04-26 08:12:10 +000010683 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10684 tg3_flag_set(tp, USING_MSIX);
10685 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10686 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010687
Joe Perches63c3a662011-04-26 08:12:10 +000010688 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010689 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010690 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010691 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010692 if (!tg3_flag(tp, 1SHOT_MSI))
10693 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010694 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10695 }
10696defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010697 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010698 tp->irq_cnt = 1;
10699 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010700 }
10701
10702 if (tp->irq_cnt == 1) {
10703 tp->txq_cnt = 1;
10704 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010705 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010706 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010707 }
Matt Carlson07b01732009-08-28 14:01:15 +000010708}
10709
10710static void tg3_ints_fini(struct tg3 *tp)
10711{
Joe Perches63c3a662011-04-26 08:12:10 +000010712 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010713 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010714 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010715 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010716 tg3_flag_clear(tp, USING_MSI);
10717 tg3_flag_clear(tp, USING_MSIX);
10718 tg3_flag_clear(tp, ENABLE_RSS);
10719 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010720}
10721
Matt Carlsonbe947302012-12-03 19:36:57 +000010722static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10723 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724{
Michael Chand8f4cd32012-09-28 07:12:40 +000010725 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010726 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727
Matt Carlson679563f2009-09-01 12:55:46 +000010728 /*
10729 * Setup interrupts first so we know how
10730 * many NAPI resources to allocate
10731 */
10732 tg3_ints_init(tp);
10733
Matt Carlson90415472011-12-16 13:33:23 +000010734 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010735
Linus Torvalds1da177e2005-04-16 15:20:36 -070010736 /* The placement of this call is tied
10737 * to the setup and use of Host TX descriptors.
10738 */
10739 err = tg3_alloc_consistent(tp);
10740 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010741 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010742
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010743 tg3_napi_init(tp);
10744
Matt Carlsonfed97812009-09-01 13:10:19 +000010745 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010746
Matt Carlson4f125f42009-09-01 12:55:02 +000010747 for (i = 0; i < tp->irq_cnt; i++) {
10748 struct tg3_napi *tnapi = &tp->napi[i];
10749 err = tg3_request_irq(tp, i);
10750 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010751 for (i--; i >= 0; i--) {
10752 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010753 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010754 }
10755 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010756 }
10757 }
Matt Carlson07b01732009-08-28 14:01:15 +000010758
David S. Millerf47c11e2005-06-24 20:18:35 -070010759 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760
Michael Chand8f4cd32012-09-28 07:12:40 +000010761 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010763 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010764 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 }
10766
David S. Millerf47c11e2005-06-24 20:18:35 -070010767 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010768
Matt Carlson07b01732009-08-28 14:01:15 +000010769 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010770 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010771
Michael Chand8f4cd32012-09-28 07:12:40 +000010772 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010773 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010774
Michael Chan79381092005-04-21 17:13:59 -070010775 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010776 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010777 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010778 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010779 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010780
Matt Carlson679563f2009-09-01 12:55:46 +000010781 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010782 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010783
Joe Perches63c3a662011-04-26 08:12:10 +000010784 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010785 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010786
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010787 tw32(PCIE_TRANSACTION_CFG,
10788 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010789 }
Michael Chan79381092005-04-21 17:13:59 -070010790 }
10791
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010792 tg3_phy_start(tp);
10793
Michael Chanaed93e02012-07-16 16:24:02 +000010794 tg3_hwmon_open(tp);
10795
David S. Millerf47c11e2005-06-24 20:18:35 -070010796 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010797
Matt Carlson21f76382012-02-22 12:35:21 +000010798 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010799 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010800 tg3_enable_ints(tp);
10801
Matt Carlsonbe947302012-12-03 19:36:57 +000010802 if (init)
10803 tg3_ptp_init(tp);
10804 else
10805 tg3_ptp_resume(tp);
10806
10807
David S. Millerf47c11e2005-06-24 20:18:35 -070010808 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010810 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010811
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010812 /*
10813 * Reset loopback feature if it was turned on while the device was down
10814 * make sure that it's installed properly now.
10815 */
10816 if (dev->features & NETIF_F_LOOPBACK)
10817 tg3_set_loopback(dev, dev->features);
10818
Linus Torvalds1da177e2005-04-16 15:20:36 -070010819 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010820
Matt Carlson679563f2009-09-01 12:55:46 +000010821err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010822 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10823 struct tg3_napi *tnapi = &tp->napi[i];
10824 free_irq(tnapi->irq_vec, tnapi);
10825 }
Matt Carlson07b01732009-08-28 14:01:15 +000010826
Matt Carlson679563f2009-09-01 12:55:46 +000010827err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010828 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010829 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010830 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010831
10832err_out1:
10833 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010834
Matt Carlson07b01732009-08-28 14:01:15 +000010835 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010836}
10837
Michael Chan65138592012-09-28 07:12:41 +000010838static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010839{
Matt Carlson4f125f42009-09-01 12:55:02 +000010840 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010841
Matt Carlsondb219972011-11-04 09:15:03 +000010842 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010843 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010844
Matt Carlson21f76382012-02-22 12:35:21 +000010845 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010846
Michael Chanaed93e02012-07-16 16:24:02 +000010847 tg3_hwmon_close(tp);
10848
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010849 tg3_phy_stop(tp);
10850
David S. Millerf47c11e2005-06-24 20:18:35 -070010851 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010852
10853 tg3_disable_ints(tp);
10854
Michael Chan944d9802005-05-29 14:57:48 -070010855 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010856 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010857 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010858
David S. Millerf47c11e2005-06-24 20:18:35 -070010859 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010860
Matt Carlson4f125f42009-09-01 12:55:02 +000010861 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10862 struct tg3_napi *tnapi = &tp->napi[i];
10863 free_irq(tnapi->irq_vec, tnapi);
10864 }
Matt Carlson07b01732009-08-28 14:01:15 +000010865
10866 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010867
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010868 tg3_napi_fini(tp);
10869
Linus Torvalds1da177e2005-04-16 15:20:36 -070010870 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010871}
10872
Michael Chand8f4cd32012-09-28 07:12:40 +000010873static int tg3_open(struct net_device *dev)
10874{
10875 struct tg3 *tp = netdev_priv(dev);
10876 int err;
10877
10878 if (tp->fw_needed) {
10879 err = tg3_request_firmware(tp);
Joe Perches41535772013-02-16 11:20:04 +000010880 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0) {
Michael Chand8f4cd32012-09-28 07:12:40 +000010881 if (err)
10882 return err;
10883 } else if (err) {
10884 netdev_warn(tp->dev, "TSO capability disabled\n");
10885 tg3_flag_clear(tp, TSO_CAPABLE);
10886 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10887 netdev_notice(tp->dev, "TSO capability restored\n");
10888 tg3_flag_set(tp, TSO_CAPABLE);
10889 }
10890 }
10891
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010892 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010893
10894 err = tg3_power_up(tp);
10895 if (err)
10896 return err;
10897
10898 tg3_full_lock(tp, 0);
10899
10900 tg3_disable_ints(tp);
10901 tg3_flag_clear(tp, INIT_COMPLETE);
10902
10903 tg3_full_unlock(tp);
10904
Matt Carlsonbe947302012-12-03 19:36:57 +000010905 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010906 if (err) {
10907 tg3_frob_aux_power(tp, false);
10908 pci_set_power_state(tp->pdev, PCI_D3hot);
10909 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010910
Matt Carlson7d41e492012-12-03 19:36:58 +000010911 if (tg3_flag(tp, PTP_CAPABLE)) {
10912 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10913 &tp->pdev->dev);
10914 if (IS_ERR(tp->ptp_clock))
10915 tp->ptp_clock = NULL;
10916 }
10917
Linus Torvalds1da177e2005-04-16 15:20:36 -070010918 return err;
10919}
10920
10921static int tg3_close(struct net_device *dev)
10922{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010923 struct tg3 *tp = netdev_priv(dev);
10924
Matt Carlsonbe947302012-12-03 19:36:57 +000010925 tg3_ptp_fini(tp);
10926
Michael Chan65138592012-09-28 07:12:41 +000010927 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010928
10929 /* Clear stats across close / open calls */
10930 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10931 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010932
10933 tg3_power_down(tp);
10934
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010935 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010936
10937 return 0;
10938}
10939
10940static inline u64 get_stat64(tg3_stat64_t *val)
10941{
10942 return ((u64)val->high << 32) | ((u64)val->low);
10943}
10944
10945static u64 tg3_calc_crc_errors(struct tg3 *tp)
10946{
10947 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10948
10949 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000010950 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
10951 tg3_asic_rev(tp) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952 u32 val;
10953
10954 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10955 tg3_writephy(tp, MII_TG3_TEST1,
10956 val | MII_TG3_TEST1_CRC_EN);
10957 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10958 } else
10959 val = 0;
10960
10961 tp->phy_crc_errors += val;
10962
10963 return tp->phy_crc_errors;
10964 }
10965
10966 return get_stat64(&hw_stats->rx_fcs_errors);
10967}
10968
10969#define ESTAT_ADD(member) \
10970 estats->member = old_estats->member + \
10971 get_stat64(&hw_stats->member)
10972
10973static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10974{
10975 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10976 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10977
10978 ESTAT_ADD(rx_octets);
10979 ESTAT_ADD(rx_fragments);
10980 ESTAT_ADD(rx_ucast_packets);
10981 ESTAT_ADD(rx_mcast_packets);
10982 ESTAT_ADD(rx_bcast_packets);
10983 ESTAT_ADD(rx_fcs_errors);
10984 ESTAT_ADD(rx_align_errors);
10985 ESTAT_ADD(rx_xon_pause_rcvd);
10986 ESTAT_ADD(rx_xoff_pause_rcvd);
10987 ESTAT_ADD(rx_mac_ctrl_rcvd);
10988 ESTAT_ADD(rx_xoff_entered);
10989 ESTAT_ADD(rx_frame_too_long_errors);
10990 ESTAT_ADD(rx_jabbers);
10991 ESTAT_ADD(rx_undersize_packets);
10992 ESTAT_ADD(rx_in_length_errors);
10993 ESTAT_ADD(rx_out_length_errors);
10994 ESTAT_ADD(rx_64_or_less_octet_packets);
10995 ESTAT_ADD(rx_65_to_127_octet_packets);
10996 ESTAT_ADD(rx_128_to_255_octet_packets);
10997 ESTAT_ADD(rx_256_to_511_octet_packets);
10998 ESTAT_ADD(rx_512_to_1023_octet_packets);
10999 ESTAT_ADD(rx_1024_to_1522_octet_packets);
11000 ESTAT_ADD(rx_1523_to_2047_octet_packets);
11001 ESTAT_ADD(rx_2048_to_4095_octet_packets);
11002 ESTAT_ADD(rx_4096_to_8191_octet_packets);
11003 ESTAT_ADD(rx_8192_to_9022_octet_packets);
11004
11005 ESTAT_ADD(tx_octets);
11006 ESTAT_ADD(tx_collisions);
11007 ESTAT_ADD(tx_xon_sent);
11008 ESTAT_ADD(tx_xoff_sent);
11009 ESTAT_ADD(tx_flow_control);
11010 ESTAT_ADD(tx_mac_errors);
11011 ESTAT_ADD(tx_single_collisions);
11012 ESTAT_ADD(tx_mult_collisions);
11013 ESTAT_ADD(tx_deferred);
11014 ESTAT_ADD(tx_excessive_collisions);
11015 ESTAT_ADD(tx_late_collisions);
11016 ESTAT_ADD(tx_collide_2times);
11017 ESTAT_ADD(tx_collide_3times);
11018 ESTAT_ADD(tx_collide_4times);
11019 ESTAT_ADD(tx_collide_5times);
11020 ESTAT_ADD(tx_collide_6times);
11021 ESTAT_ADD(tx_collide_7times);
11022 ESTAT_ADD(tx_collide_8times);
11023 ESTAT_ADD(tx_collide_9times);
11024 ESTAT_ADD(tx_collide_10times);
11025 ESTAT_ADD(tx_collide_11times);
11026 ESTAT_ADD(tx_collide_12times);
11027 ESTAT_ADD(tx_collide_13times);
11028 ESTAT_ADD(tx_collide_14times);
11029 ESTAT_ADD(tx_collide_15times);
11030 ESTAT_ADD(tx_ucast_packets);
11031 ESTAT_ADD(tx_mcast_packets);
11032 ESTAT_ADD(tx_bcast_packets);
11033 ESTAT_ADD(tx_carrier_sense_errors);
11034 ESTAT_ADD(tx_discards);
11035 ESTAT_ADD(tx_errors);
11036
11037 ESTAT_ADD(dma_writeq_full);
11038 ESTAT_ADD(dma_write_prioq_full);
11039 ESTAT_ADD(rxbds_empty);
11040 ESTAT_ADD(rx_discards);
11041 ESTAT_ADD(rx_errors);
11042 ESTAT_ADD(rx_threshold_hit);
11043
11044 ESTAT_ADD(dma_readq_full);
11045 ESTAT_ADD(dma_read_prioq_full);
11046 ESTAT_ADD(tx_comp_queue_full);
11047
11048 ESTAT_ADD(ring_set_send_prod_index);
11049 ESTAT_ADD(ring_status_update);
11050 ESTAT_ADD(nic_irqs);
11051 ESTAT_ADD(nic_avoided_irqs);
11052 ESTAT_ADD(nic_tx_threshold_hit);
11053
Matt Carlson4452d092011-05-19 12:12:51 +000011054 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011055}
11056
Matt Carlson65ec6982012-02-28 23:33:37 +000011057static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058{
Eric Dumazet511d2222010-07-07 20:44:24 +000011059 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011060 struct tg3_hw_stats *hw_stats = tp->hw_stats;
11061
Linus Torvalds1da177e2005-04-16 15:20:36 -070011062 stats->rx_packets = old_stats->rx_packets +
11063 get_stat64(&hw_stats->rx_ucast_packets) +
11064 get_stat64(&hw_stats->rx_mcast_packets) +
11065 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011066
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067 stats->tx_packets = old_stats->tx_packets +
11068 get_stat64(&hw_stats->tx_ucast_packets) +
11069 get_stat64(&hw_stats->tx_mcast_packets) +
11070 get_stat64(&hw_stats->tx_bcast_packets);
11071
11072 stats->rx_bytes = old_stats->rx_bytes +
11073 get_stat64(&hw_stats->rx_octets);
11074 stats->tx_bytes = old_stats->tx_bytes +
11075 get_stat64(&hw_stats->tx_octets);
11076
11077 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070011078 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011079 stats->tx_errors = old_stats->tx_errors +
11080 get_stat64(&hw_stats->tx_errors) +
11081 get_stat64(&hw_stats->tx_mac_errors) +
11082 get_stat64(&hw_stats->tx_carrier_sense_errors) +
11083 get_stat64(&hw_stats->tx_discards);
11084
11085 stats->multicast = old_stats->multicast +
11086 get_stat64(&hw_stats->rx_mcast_packets);
11087 stats->collisions = old_stats->collisions +
11088 get_stat64(&hw_stats->tx_collisions);
11089
11090 stats->rx_length_errors = old_stats->rx_length_errors +
11091 get_stat64(&hw_stats->rx_frame_too_long_errors) +
11092 get_stat64(&hw_stats->rx_undersize_packets);
11093
11094 stats->rx_over_errors = old_stats->rx_over_errors +
11095 get_stat64(&hw_stats->rxbds_empty);
11096 stats->rx_frame_errors = old_stats->rx_frame_errors +
11097 get_stat64(&hw_stats->rx_align_errors);
11098 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
11099 get_stat64(&hw_stats->tx_discards);
11100 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
11101 get_stat64(&hw_stats->tx_carrier_sense_errors);
11102
11103 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000011104 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105
John W. Linville4f63b872005-09-12 14:43:18 -070011106 stats->rx_missed_errors = old_stats->rx_missed_errors +
11107 get_stat64(&hw_stats->rx_discards);
11108
Eric Dumazetb0057c52010-10-10 19:55:52 +000011109 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000011110 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011111}
11112
Linus Torvalds1da177e2005-04-16 15:20:36 -070011113static int tg3_get_regs_len(struct net_device *dev)
11114{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011115 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011116}
11117
11118static void tg3_get_regs(struct net_device *dev,
11119 struct ethtool_regs *regs, void *_p)
11120{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011121 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011122
11123 regs->version = 0;
11124
Matt Carlson97bd8e42011-04-13 11:05:04 +000011125 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011126
Matt Carlson80096062010-08-02 11:26:06 +000011127 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011128 return;
11129
David S. Millerf47c11e2005-06-24 20:18:35 -070011130 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011131
Matt Carlson97bd8e42011-04-13 11:05:04 +000011132 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011133
David S. Millerf47c11e2005-06-24 20:18:35 -070011134 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135}
11136
11137static int tg3_get_eeprom_len(struct net_device *dev)
11138{
11139 struct tg3 *tp = netdev_priv(dev);
11140
11141 return tp->nvram_size;
11142}
11143
Linus Torvalds1da177e2005-04-16 15:20:36 -070011144static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11145{
11146 struct tg3 *tp = netdev_priv(dev);
11147 int ret;
11148 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011149 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011150 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151
Joe Perches63c3a662011-04-26 08:12:10 +000011152 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011153 return -EINVAL;
11154
Matt Carlson80096062010-08-02 11:26:06 +000011155 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011156 return -EAGAIN;
11157
Linus Torvalds1da177e2005-04-16 15:20:36 -070011158 offset = eeprom->offset;
11159 len = eeprom->len;
11160 eeprom->len = 0;
11161
11162 eeprom->magic = TG3_EEPROM_MAGIC;
11163
11164 if (offset & 3) {
11165 /* adjustments to start on required 4 byte boundary */
11166 b_offset = offset & 3;
11167 b_count = 4 - b_offset;
11168 if (b_count > len) {
11169 /* i.e. offset=1 len=2 */
11170 b_count = len;
11171 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011172 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011173 if (ret)
11174 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011175 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011176 len -= b_count;
11177 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011178 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011179 }
11180
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011181 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011182 pd = &data[eeprom->len];
11183 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011184 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011185 if (ret) {
11186 eeprom->len += i;
11187 return ret;
11188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011189 memcpy(pd + i, &val, 4);
11190 }
11191 eeprom->len += i;
11192
11193 if (len & 3) {
11194 /* read last bytes not ending on 4 byte boundary */
11195 pd = &data[eeprom->len];
11196 b_count = len & 3;
11197 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011198 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011199 if (ret)
11200 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011201 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011202 eeprom->len += b_count;
11203 }
11204 return 0;
11205}
11206
Linus Torvalds1da177e2005-04-16 15:20:36 -070011207static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11208{
11209 struct tg3 *tp = netdev_priv(dev);
11210 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011211 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011212 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011213 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214
Matt Carlson80096062010-08-02 11:26:06 +000011215 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011216 return -EAGAIN;
11217
Joe Perches63c3a662011-04-26 08:12:10 +000011218 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011219 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011220 return -EINVAL;
11221
11222 offset = eeprom->offset;
11223 len = eeprom->len;
11224
11225 if ((b_offset = (offset & 3))) {
11226 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011227 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011228 if (ret)
11229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011230 len += b_offset;
11231 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070011232 if (len < 4)
11233 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011234 }
11235
11236 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070011237 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011238 /* adjustments to end on required 4 byte boundary */
11239 odd_len = 1;
11240 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011241 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011242 if (ret)
11243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011244 }
11245
11246 buf = data;
11247 if (b_offset || odd_len) {
11248 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011249 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011250 return -ENOMEM;
11251 if (b_offset)
11252 memcpy(buf, &start, 4);
11253 if (odd_len)
11254 memcpy(buf+len-4, &end, 4);
11255 memcpy(buf + b_offset, data, eeprom->len);
11256 }
11257
11258 ret = tg3_nvram_write_block(tp, offset, len, buf);
11259
11260 if (buf != data)
11261 kfree(buf);
11262
11263 return ret;
11264}
11265
11266static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11267{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011268 struct tg3 *tp = netdev_priv(dev);
11269
Joe Perches63c3a662011-04-26 08:12:10 +000011270 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011271 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011272 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011273 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011274 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11275 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011276 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011277
Linus Torvalds1da177e2005-04-16 15:20:36 -070011278 cmd->supported = (SUPPORTED_Autoneg);
11279
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011280 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011281 cmd->supported |= (SUPPORTED_1000baseT_Half |
11282 SUPPORTED_1000baseT_Full);
11283
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011284 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011285 cmd->supported |= (SUPPORTED_100baseT_Half |
11286 SUPPORTED_100baseT_Full |
11287 SUPPORTED_10baseT_Half |
11288 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011289 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011290 cmd->port = PORT_TP;
11291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011292 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011293 cmd->port = PORT_FIBRE;
11294 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011295
Linus Torvalds1da177e2005-04-16 15:20:36 -070011296 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011297 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11298 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11299 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11300 cmd->advertising |= ADVERTISED_Pause;
11301 } else {
11302 cmd->advertising |= ADVERTISED_Pause |
11303 ADVERTISED_Asym_Pause;
11304 }
11305 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11306 cmd->advertising |= ADVERTISED_Asym_Pause;
11307 }
11308 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011309 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011310 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011311 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011312 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011313 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11314 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11315 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11316 else
11317 cmd->eth_tp_mdix = ETH_TP_MDI;
11318 }
Matt Carlson64c22182010-10-14 10:37:44 +000011319 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011320 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11321 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011322 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011323 }
Matt Carlson882e9792009-09-01 13:21:36 +000011324 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011325 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011326 cmd->autoneg = tp->link_config.autoneg;
11327 cmd->maxtxpkt = 0;
11328 cmd->maxrxpkt = 0;
11329 return 0;
11330}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011331
Linus Torvalds1da177e2005-04-16 15:20:36 -070011332static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11333{
11334 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011335 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011336
Joe Perches63c3a662011-04-26 08:12:10 +000011337 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011338 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011339 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011340 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011341 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11342 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011343 }
11344
Matt Carlson7e5856b2009-02-25 14:23:01 +000011345 if (cmd->autoneg != AUTONEG_ENABLE &&
11346 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011347 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011348
11349 if (cmd->autoneg == AUTONEG_DISABLE &&
11350 cmd->duplex != DUPLEX_FULL &&
11351 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011353
Matt Carlson7e5856b2009-02-25 14:23:01 +000011354 if (cmd->autoneg == AUTONEG_ENABLE) {
11355 u32 mask = ADVERTISED_Autoneg |
11356 ADVERTISED_Pause |
11357 ADVERTISED_Asym_Pause;
11358
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011359 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011360 mask |= ADVERTISED_1000baseT_Half |
11361 ADVERTISED_1000baseT_Full;
11362
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011363 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011364 mask |= ADVERTISED_100baseT_Half |
11365 ADVERTISED_100baseT_Full |
11366 ADVERTISED_10baseT_Half |
11367 ADVERTISED_10baseT_Full |
11368 ADVERTISED_TP;
11369 else
11370 mask |= ADVERTISED_FIBRE;
11371
11372 if (cmd->advertising & ~mask)
11373 return -EINVAL;
11374
11375 mask &= (ADVERTISED_1000baseT_Half |
11376 ADVERTISED_1000baseT_Full |
11377 ADVERTISED_100baseT_Half |
11378 ADVERTISED_100baseT_Full |
11379 ADVERTISED_10baseT_Half |
11380 ADVERTISED_10baseT_Full);
11381
11382 cmd->advertising &= mask;
11383 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011384 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011385 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011386 return -EINVAL;
11387
11388 if (cmd->duplex != DUPLEX_FULL)
11389 return -EINVAL;
11390 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011391 if (speed != SPEED_100 &&
11392 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011393 return -EINVAL;
11394 }
11395 }
11396
David S. Millerf47c11e2005-06-24 20:18:35 -070011397 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011398
11399 tp->link_config.autoneg = cmd->autoneg;
11400 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011401 tp->link_config.advertising = (cmd->advertising |
11402 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011403 tp->link_config.speed = SPEED_UNKNOWN;
11404 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011405 } else {
11406 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011407 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011409 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011410
Linus Torvalds1da177e2005-04-16 15:20:36 -070011411 if (netif_running(dev))
11412 tg3_setup_phy(tp, 1);
11413
David S. Millerf47c11e2005-06-24 20:18:35 -070011414 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011415
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416 return 0;
11417}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011418
Linus Torvalds1da177e2005-04-16 15:20:36 -070011419static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11420{
11421 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011422
Rick Jones68aad782011-11-07 13:29:27 +000011423 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11424 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11425 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11426 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011427}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011428
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11430{
11431 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011432
Joe Perches63c3a662011-04-26 08:12:10 +000011433 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011434 wol->supported = WAKE_MAGIC;
11435 else
11436 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011437 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011438 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439 wol->wolopts = WAKE_MAGIC;
11440 memset(&wol->sopass, 0, sizeof(wol->sopass));
11441}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011442
Linus Torvalds1da177e2005-04-16 15:20:36 -070011443static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11444{
11445 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011446 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011447
Linus Torvalds1da177e2005-04-16 15:20:36 -070011448 if (wol->wolopts & ~WAKE_MAGIC)
11449 return -EINVAL;
11450 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011451 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011452 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011453
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011454 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11455
David S. Millerf47c11e2005-06-24 20:18:35 -070011456 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011457 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011458 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011459 else
Joe Perches63c3a662011-04-26 08:12:10 +000011460 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011461 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011462
Linus Torvalds1da177e2005-04-16 15:20:36 -070011463 return 0;
11464}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011465
Linus Torvalds1da177e2005-04-16 15:20:36 -070011466static u32 tg3_get_msglevel(struct net_device *dev)
11467{
11468 struct tg3 *tp = netdev_priv(dev);
11469 return tp->msg_enable;
11470}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011471
Linus Torvalds1da177e2005-04-16 15:20:36 -070011472static void tg3_set_msglevel(struct net_device *dev, u32 value)
11473{
11474 struct tg3 *tp = netdev_priv(dev);
11475 tp->msg_enable = value;
11476}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011477
Linus Torvalds1da177e2005-04-16 15:20:36 -070011478static int tg3_nway_reset(struct net_device *dev)
11479{
11480 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011481 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011482
Linus Torvalds1da177e2005-04-16 15:20:36 -070011483 if (!netif_running(dev))
11484 return -EAGAIN;
11485
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011486 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011487 return -EINVAL;
11488
Joe Perches63c3a662011-04-26 08:12:10 +000011489 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011490 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011491 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011492 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011493 } else {
11494 u32 bmcr;
11495
11496 spin_lock_bh(&tp->lock);
11497 r = -EINVAL;
11498 tg3_readphy(tp, MII_BMCR, &bmcr);
11499 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11500 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011501 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011502 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11503 BMCR_ANENABLE);
11504 r = 0;
11505 }
11506 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011507 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011508
Linus Torvalds1da177e2005-04-16 15:20:36 -070011509 return r;
11510}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011511
Linus Torvalds1da177e2005-04-16 15:20:36 -070011512static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11513{
11514 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011515
Matt Carlson2c49a442010-09-30 10:34:35 +000011516 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011517 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011518 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011519 else
11520 ering->rx_jumbo_max_pending = 0;
11521
11522 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011523
11524 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011525 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011526 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11527 else
11528 ering->rx_jumbo_pending = 0;
11529
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011530 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011531}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011532
Linus Torvalds1da177e2005-04-16 15:20:36 -070011533static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11534{
11535 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011536 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011537
Matt Carlson2c49a442010-09-30 10:34:35 +000011538 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11539 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011540 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11541 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011542 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011543 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011544 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011545
Michael Chanbbe832c2005-06-24 20:20:04 -070011546 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011547 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011549 irq_sync = 1;
11550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011551
Michael Chanbbe832c2005-06-24 20:20:04 -070011552 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011553
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554 tp->rx_pending = ering->rx_pending;
11555
Joe Perches63c3a662011-04-26 08:12:10 +000011556 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011557 tp->rx_pending > 63)
11558 tp->rx_pending = 63;
11559 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011560
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011561 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011562 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563
11564 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011565 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011566 err = tg3_restart_hw(tp, 1);
11567 if (!err)
11568 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569 }
11570
David S. Millerf47c11e2005-06-24 20:18:35 -070011571 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011572
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011573 if (irq_sync && !err)
11574 tg3_phy_start(tp);
11575
Michael Chanb9ec6c12006-07-25 16:37:27 -070011576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011577}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011578
Linus Torvalds1da177e2005-04-16 15:20:36 -070011579static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11580{
11581 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011582
Joe Perches63c3a662011-04-26 08:12:10 +000011583 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011584
Matt Carlson4a2db502011-12-08 14:40:17 +000011585 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011586 epause->rx_pause = 1;
11587 else
11588 epause->rx_pause = 0;
11589
Matt Carlson4a2db502011-12-08 14:40:17 +000011590 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011591 epause->tx_pause = 1;
11592 else
11593 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011594}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011595
Linus Torvalds1da177e2005-04-16 15:20:36 -070011596static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11597{
11598 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011599 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011600
Joe Perches63c3a662011-04-26 08:12:10 +000011601 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011602 u32 newadv;
11603 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011604
Matt Carlson27121682010-02-17 15:16:57 +000011605 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011606
Matt Carlson27121682010-02-17 15:16:57 +000011607 if (!(phydev->supported & SUPPORTED_Pause) ||
11608 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011609 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011610 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011611
Matt Carlson27121682010-02-17 15:16:57 +000011612 tp->link_config.flowctrl = 0;
11613 if (epause->rx_pause) {
11614 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011615
Matt Carlson27121682010-02-17 15:16:57 +000011616 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011617 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011618 newadv = ADVERTISED_Pause;
11619 } else
11620 newadv = ADVERTISED_Pause |
11621 ADVERTISED_Asym_Pause;
11622 } else if (epause->tx_pause) {
11623 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11624 newadv = ADVERTISED_Asym_Pause;
11625 } else
11626 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011627
Matt Carlson27121682010-02-17 15:16:57 +000011628 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011629 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011630 else
Joe Perches63c3a662011-04-26 08:12:10 +000011631 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011632
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011633 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011634 u32 oldadv = phydev->advertising &
11635 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11636 if (oldadv != newadv) {
11637 phydev->advertising &=
11638 ~(ADVERTISED_Pause |
11639 ADVERTISED_Asym_Pause);
11640 phydev->advertising |= newadv;
11641 if (phydev->autoneg) {
11642 /*
11643 * Always renegotiate the link to
11644 * inform our link partner of our
11645 * flow control settings, even if the
11646 * flow control is forced. Let
11647 * tg3_adjust_link() do the final
11648 * flow control setup.
11649 */
11650 return phy_start_aneg(phydev);
11651 }
11652 }
11653
11654 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011655 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011656 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011657 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011658 ~(ADVERTISED_Pause |
11659 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011660 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011661 }
11662 } else {
11663 int irq_sync = 0;
11664
11665 if (netif_running(dev)) {
11666 tg3_netif_stop(tp);
11667 irq_sync = 1;
11668 }
11669
11670 tg3_full_lock(tp, irq_sync);
11671
11672 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011673 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011674 else
Joe Perches63c3a662011-04-26 08:12:10 +000011675 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011676 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011677 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011678 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011679 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011680 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011681 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011682 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011683 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011684
11685 if (netif_running(dev)) {
11686 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11687 err = tg3_restart_hw(tp, 1);
11688 if (!err)
11689 tg3_netif_start(tp);
11690 }
11691
11692 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694
Michael Chanb9ec6c12006-07-25 16:37:27 -070011695 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011696}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011697
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011698static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011699{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011700 switch (sset) {
11701 case ETH_SS_TEST:
11702 return TG3_NUM_TEST;
11703 case ETH_SS_STATS:
11704 return TG3_NUM_STATS;
11705 default:
11706 return -EOPNOTSUPP;
11707 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011708}
11709
Matt Carlson90415472011-12-16 13:33:23 +000011710static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11711 u32 *rules __always_unused)
11712{
11713 struct tg3 *tp = netdev_priv(dev);
11714
11715 if (!tg3_flag(tp, SUPPORT_MSIX))
11716 return -EOPNOTSUPP;
11717
11718 switch (info->cmd) {
11719 case ETHTOOL_GRXRINGS:
11720 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011721 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011722 else {
11723 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011724 if (info->data > TG3_RSS_MAX_NUM_QS)
11725 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011726 }
11727
11728 /* The first interrupt vector only
11729 * handles link interrupts.
11730 */
11731 info->data -= 1;
11732 return 0;
11733
11734 default:
11735 return -EOPNOTSUPP;
11736 }
11737}
11738
11739static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11740{
11741 u32 size = 0;
11742 struct tg3 *tp = netdev_priv(dev);
11743
11744 if (tg3_flag(tp, SUPPORT_MSIX))
11745 size = TG3_RSS_INDIR_TBL_SIZE;
11746
11747 return size;
11748}
11749
11750static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11751{
11752 struct tg3 *tp = netdev_priv(dev);
11753 int i;
11754
11755 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11756 indir[i] = tp->rss_ind_tbl[i];
11757
11758 return 0;
11759}
11760
11761static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11762{
11763 struct tg3 *tp = netdev_priv(dev);
11764 size_t i;
11765
11766 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11767 tp->rss_ind_tbl[i] = indir[i];
11768
11769 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11770 return 0;
11771
11772 /* It is legal to write the indirection
11773 * table while the device is running.
11774 */
11775 tg3_full_lock(tp, 0);
11776 tg3_rss_write_indir_tbl(tp);
11777 tg3_full_unlock(tp);
11778
11779 return 0;
11780}
11781
Michael Chan09681692012-09-28 07:12:42 +000011782static void tg3_get_channels(struct net_device *dev,
11783 struct ethtool_channels *channel)
11784{
11785 struct tg3 *tp = netdev_priv(dev);
11786 u32 deflt_qs = netif_get_num_default_rss_queues();
11787
11788 channel->max_rx = tp->rxq_max;
11789 channel->max_tx = tp->txq_max;
11790
11791 if (netif_running(dev)) {
11792 channel->rx_count = tp->rxq_cnt;
11793 channel->tx_count = tp->txq_cnt;
11794 } else {
11795 if (tp->rxq_req)
11796 channel->rx_count = tp->rxq_req;
11797 else
11798 channel->rx_count = min(deflt_qs, tp->rxq_max);
11799
11800 if (tp->txq_req)
11801 channel->tx_count = tp->txq_req;
11802 else
11803 channel->tx_count = min(deflt_qs, tp->txq_max);
11804 }
11805}
11806
11807static int tg3_set_channels(struct net_device *dev,
11808 struct ethtool_channels *channel)
11809{
11810 struct tg3 *tp = netdev_priv(dev);
11811
11812 if (!tg3_flag(tp, SUPPORT_MSIX))
11813 return -EOPNOTSUPP;
11814
11815 if (channel->rx_count > tp->rxq_max ||
11816 channel->tx_count > tp->txq_max)
11817 return -EINVAL;
11818
11819 tp->rxq_req = channel->rx_count;
11820 tp->txq_req = channel->tx_count;
11821
11822 if (!netif_running(dev))
11823 return 0;
11824
11825 tg3_stop(tp);
11826
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011827 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011828
Matt Carlsonbe947302012-12-03 19:36:57 +000011829 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011830
11831 return 0;
11832}
11833
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011834static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835{
11836 switch (stringset) {
11837 case ETH_SS_STATS:
11838 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11839 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011840 case ETH_SS_TEST:
11841 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011843 default:
11844 WARN_ON(1); /* we need a WARN() */
11845 break;
11846 }
11847}
11848
stephen hemminger81b87092011-04-04 08:43:50 +000011849static int tg3_set_phys_id(struct net_device *dev,
11850 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011851{
11852 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011853
11854 if (!netif_running(tp->dev))
11855 return -EAGAIN;
11856
stephen hemminger81b87092011-04-04 08:43:50 +000011857 switch (state) {
11858 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011859 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011860
stephen hemminger81b87092011-04-04 08:43:50 +000011861 case ETHTOOL_ID_ON:
11862 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11863 LED_CTRL_1000MBPS_ON |
11864 LED_CTRL_100MBPS_ON |
11865 LED_CTRL_10MBPS_ON |
11866 LED_CTRL_TRAFFIC_OVERRIDE |
11867 LED_CTRL_TRAFFIC_BLINK |
11868 LED_CTRL_TRAFFIC_LED);
11869 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011870
stephen hemminger81b87092011-04-04 08:43:50 +000011871 case ETHTOOL_ID_OFF:
11872 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11873 LED_CTRL_TRAFFIC_OVERRIDE);
11874 break;
Michael Chan4009a932005-09-05 17:52:54 -070011875
stephen hemminger81b87092011-04-04 08:43:50 +000011876 case ETHTOOL_ID_INACTIVE:
11877 tw32(MAC_LED_CTRL, tp->led_ctrl);
11878 break;
Michael Chan4009a932005-09-05 17:52:54 -070011879 }
stephen hemminger81b87092011-04-04 08:43:50 +000011880
Michael Chan4009a932005-09-05 17:52:54 -070011881 return 0;
11882}
11883
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011884static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011885 struct ethtool_stats *estats, u64 *tmp_stats)
11886{
11887 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011888
Matt Carlsonb546e462012-02-13 15:20:09 +000011889 if (tp->hw_stats)
11890 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11891 else
11892 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011893}
11894
Matt Carlson535a4902011-07-20 10:20:56 +000011895static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011896{
11897 int i;
11898 __be32 *buf;
11899 u32 offset = 0, len = 0;
11900 u32 magic, val;
11901
Joe Perches63c3a662011-04-26 08:12:10 +000011902 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011903 return NULL;
11904
11905 if (magic == TG3_EEPROM_MAGIC) {
11906 for (offset = TG3_NVM_DIR_START;
11907 offset < TG3_NVM_DIR_END;
11908 offset += TG3_NVM_DIRENT_SIZE) {
11909 if (tg3_nvram_read(tp, offset, &val))
11910 return NULL;
11911
11912 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11913 TG3_NVM_DIRTYPE_EXTVPD)
11914 break;
11915 }
11916
11917 if (offset != TG3_NVM_DIR_END) {
11918 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11919 if (tg3_nvram_read(tp, offset + 4, &offset))
11920 return NULL;
11921
11922 offset = tg3_nvram_logical_addr(tp, offset);
11923 }
11924 }
11925
11926 if (!offset || !len) {
11927 offset = TG3_NVM_VPD_OFF;
11928 len = TG3_NVM_VPD_LEN;
11929 }
11930
11931 buf = kmalloc(len, GFP_KERNEL);
11932 if (buf == NULL)
11933 return NULL;
11934
11935 if (magic == TG3_EEPROM_MAGIC) {
11936 for (i = 0; i < len; i += 4) {
11937 /* The data is in little-endian format in NVRAM.
11938 * Use the big-endian read routines to preserve
11939 * the byte order as it exists in NVRAM.
11940 */
11941 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11942 goto error;
11943 }
11944 } else {
11945 u8 *ptr;
11946 ssize_t cnt;
11947 unsigned int pos = 0;
11948
11949 ptr = (u8 *)&buf[0];
11950 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11951 cnt = pci_read_vpd(tp->pdev, pos,
11952 len - pos, ptr);
11953 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11954 cnt = 0;
11955 else if (cnt < 0)
11956 goto error;
11957 }
11958 if (pos != len)
11959 goto error;
11960 }
11961
Matt Carlson535a4902011-07-20 10:20:56 +000011962 *vpdlen = len;
11963
Matt Carlsonc3e94502011-04-13 11:05:08 +000011964 return buf;
11965
11966error:
11967 kfree(buf);
11968 return NULL;
11969}
11970
Michael Chan566f86a2005-05-29 14:56:58 -070011971#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011972#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11973#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11974#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011975#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11976#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011977#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011978#define NVRAM_SELFBOOT_HW_SIZE 0x20
11979#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011980
11981static int tg3_test_nvram(struct tg3 *tp)
11982{
Matt Carlson535a4902011-07-20 10:20:56 +000011983 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011984 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011985 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011986
Joe Perches63c3a662011-04-26 08:12:10 +000011987 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011988 return 0;
11989
Matt Carlsone4f34112009-02-25 14:25:00 +000011990 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011991 return -EIO;
11992
Michael Chan1b277772006-03-20 22:27:48 -080011993 if (magic == TG3_EEPROM_MAGIC)
11994 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011995 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011996 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11997 TG3_EEPROM_SB_FORMAT_1) {
11998 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11999 case TG3_EEPROM_SB_REVISION_0:
12000 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
12001 break;
12002 case TG3_EEPROM_SB_REVISION_2:
12003 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
12004 break;
12005 case TG3_EEPROM_SB_REVISION_3:
12006 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
12007 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000012008 case TG3_EEPROM_SB_REVISION_4:
12009 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
12010 break;
12011 case TG3_EEPROM_SB_REVISION_5:
12012 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
12013 break;
12014 case TG3_EEPROM_SB_REVISION_6:
12015 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
12016 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080012017 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000012018 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080012019 }
12020 } else
Michael Chan1b277772006-03-20 22:27:48 -080012021 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070012022 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
12023 size = NVRAM_SELFBOOT_HW_SIZE;
12024 else
Michael Chan1b277772006-03-20 22:27:48 -080012025 return -EIO;
12026
12027 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070012028 if (buf == NULL)
12029 return -ENOMEM;
12030
Michael Chan1b277772006-03-20 22:27:48 -080012031 err = -EIO;
12032 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012033 err = tg3_nvram_read_be32(tp, i, &buf[j]);
12034 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070012035 break;
Michael Chan566f86a2005-05-29 14:56:58 -070012036 }
Michael Chan1b277772006-03-20 22:27:48 -080012037 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070012038 goto out;
12039
Michael Chan1b277772006-03-20 22:27:48 -080012040 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000012041 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080012042 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012043 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080012044 u8 *buf8 = (u8 *) buf, csum8 = 0;
12045
Al Virob9fc7dc2007-12-17 22:59:57 -080012046 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080012047 TG3_EEPROM_SB_REVISION_2) {
12048 /* For rev 2, the csum doesn't include the MBA. */
12049 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
12050 csum8 += buf8[i];
12051 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
12052 csum8 += buf8[i];
12053 } else {
12054 for (i = 0; i < size; i++)
12055 csum8 += buf8[i];
12056 }
Michael Chan1b277772006-03-20 22:27:48 -080012057
Adrian Bunkad96b482006-04-05 22:21:04 -070012058 if (csum8 == 0) {
12059 err = 0;
12060 goto out;
12061 }
12062
12063 err = -EIO;
12064 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080012065 }
Michael Chan566f86a2005-05-29 14:56:58 -070012066
Al Virob9fc7dc2007-12-17 22:59:57 -080012067 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070012068 TG3_EEPROM_MAGIC_HW) {
12069 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000012070 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070012071 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070012072
12073 /* Separate the parity bits and the data bytes. */
12074 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
12075 if ((i == 0) || (i == 8)) {
12076 int l;
12077 u8 msk;
12078
12079 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
12080 parity[k++] = buf8[i] & msk;
12081 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000012082 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070012083 int l;
12084 u8 msk;
12085
12086 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
12087 parity[k++] = buf8[i] & msk;
12088 i++;
12089
12090 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
12091 parity[k++] = buf8[i] & msk;
12092 i++;
12093 }
12094 data[j++] = buf8[i];
12095 }
12096
12097 err = -EIO;
12098 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
12099 u8 hw8 = hweight8(data[i]);
12100
12101 if ((hw8 & 0x1) && parity[i])
12102 goto out;
12103 else if (!(hw8 & 0x1) && !parity[i])
12104 goto out;
12105 }
12106 err = 0;
12107 goto out;
12108 }
12109
Matt Carlson01c3a392011-03-09 16:58:20 +000012110 err = -EIO;
12111
Michael Chan566f86a2005-05-29 14:56:58 -070012112 /* Bootstrap checksum at offset 0x10 */
12113 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000012114 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012115 goto out;
12116
12117 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12118 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012119 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012120 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012121
Matt Carlsonc3e94502011-04-13 11:05:08 +000012122 kfree(buf);
12123
Matt Carlson535a4902011-07-20 10:20:56 +000012124 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012125 if (!buf)
12126 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012127
Matt Carlson535a4902011-07-20 10:20:56 +000012128 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012129 if (i > 0) {
12130 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12131 if (j < 0)
12132 goto out;
12133
Matt Carlson535a4902011-07-20 10:20:56 +000012134 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012135 goto out;
12136
12137 i += PCI_VPD_LRDT_TAG_SIZE;
12138 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12139 PCI_VPD_RO_KEYWORD_CHKSUM);
12140 if (j > 0) {
12141 u8 csum8 = 0;
12142
12143 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12144
12145 for (i = 0; i <= j; i++)
12146 csum8 += ((u8 *)buf)[i];
12147
12148 if (csum8)
12149 goto out;
12150 }
12151 }
12152
Michael Chan566f86a2005-05-29 14:56:58 -070012153 err = 0;
12154
12155out:
12156 kfree(buf);
12157 return err;
12158}
12159
Michael Chanca430072005-05-29 14:57:23 -070012160#define TG3_SERDES_TIMEOUT_SEC 2
12161#define TG3_COPPER_TIMEOUT_SEC 6
12162
12163static int tg3_test_link(struct tg3 *tp)
12164{
12165 int i, max;
12166
12167 if (!netif_running(tp->dev))
12168 return -ENODEV;
12169
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012170 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012171 max = TG3_SERDES_TIMEOUT_SEC;
12172 else
12173 max = TG3_COPPER_TIMEOUT_SEC;
12174
12175 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012176 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012177 return 0;
12178
12179 if (msleep_interruptible(1000))
12180 break;
12181 }
12182
12183 return -EIO;
12184}
12185
Michael Chana71116d2005-05-29 14:58:11 -070012186/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012187static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012188{
Michael Chanb16250e2006-09-27 16:10:14 -070012189 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012190 u32 offset, read_mask, write_mask, val, save_val, read_val;
12191 static struct {
12192 u16 offset;
12193 u16 flags;
12194#define TG3_FL_5705 0x1
12195#define TG3_FL_NOT_5705 0x2
12196#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012197#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012198 u32 read_mask;
12199 u32 write_mask;
12200 } reg_tbl[] = {
12201 /* MAC Control Registers */
12202 { MAC_MODE, TG3_FL_NOT_5705,
12203 0x00000000, 0x00ef6f8c },
12204 { MAC_MODE, TG3_FL_5705,
12205 0x00000000, 0x01ef6b8c },
12206 { MAC_STATUS, TG3_FL_NOT_5705,
12207 0x03800107, 0x00000000 },
12208 { MAC_STATUS, TG3_FL_5705,
12209 0x03800100, 0x00000000 },
12210 { MAC_ADDR_0_HIGH, 0x0000,
12211 0x00000000, 0x0000ffff },
12212 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012213 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012214 { MAC_RX_MTU_SIZE, 0x0000,
12215 0x00000000, 0x0000ffff },
12216 { MAC_TX_MODE, 0x0000,
12217 0x00000000, 0x00000070 },
12218 { MAC_TX_LENGTHS, 0x0000,
12219 0x00000000, 0x00003fff },
12220 { MAC_RX_MODE, TG3_FL_NOT_5705,
12221 0x00000000, 0x000007fc },
12222 { MAC_RX_MODE, TG3_FL_5705,
12223 0x00000000, 0x000007dc },
12224 { MAC_HASH_REG_0, 0x0000,
12225 0x00000000, 0xffffffff },
12226 { MAC_HASH_REG_1, 0x0000,
12227 0x00000000, 0xffffffff },
12228 { MAC_HASH_REG_2, 0x0000,
12229 0x00000000, 0xffffffff },
12230 { MAC_HASH_REG_3, 0x0000,
12231 0x00000000, 0xffffffff },
12232
12233 /* Receive Data and Receive BD Initiator Control Registers. */
12234 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12235 0x00000000, 0xffffffff },
12236 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12237 0x00000000, 0xffffffff },
12238 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12239 0x00000000, 0x00000003 },
12240 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12241 0x00000000, 0xffffffff },
12242 { RCVDBDI_STD_BD+0, 0x0000,
12243 0x00000000, 0xffffffff },
12244 { RCVDBDI_STD_BD+4, 0x0000,
12245 0x00000000, 0xffffffff },
12246 { RCVDBDI_STD_BD+8, 0x0000,
12247 0x00000000, 0xffff0002 },
12248 { RCVDBDI_STD_BD+0xc, 0x0000,
12249 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012250
Michael Chana71116d2005-05-29 14:58:11 -070012251 /* Receive BD Initiator Control Registers. */
12252 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12253 0x00000000, 0xffffffff },
12254 { RCVBDI_STD_THRESH, TG3_FL_5705,
12255 0x00000000, 0x000003ff },
12256 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12257 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012258
Michael Chana71116d2005-05-29 14:58:11 -070012259 /* Host Coalescing Control Registers. */
12260 { HOSTCC_MODE, TG3_FL_NOT_5705,
12261 0x00000000, 0x00000004 },
12262 { HOSTCC_MODE, TG3_FL_5705,
12263 0x00000000, 0x000000f6 },
12264 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12265 0x00000000, 0xffffffff },
12266 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12267 0x00000000, 0x000003ff },
12268 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12269 0x00000000, 0xffffffff },
12270 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12271 0x00000000, 0x000003ff },
12272 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12273 0x00000000, 0xffffffff },
12274 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12275 0x00000000, 0x000000ff },
12276 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12277 0x00000000, 0xffffffff },
12278 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12279 0x00000000, 0x000000ff },
12280 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12281 0x00000000, 0xffffffff },
12282 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12283 0x00000000, 0xffffffff },
12284 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12285 0x00000000, 0xffffffff },
12286 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12287 0x00000000, 0x000000ff },
12288 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12289 0x00000000, 0xffffffff },
12290 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12291 0x00000000, 0x000000ff },
12292 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12293 0x00000000, 0xffffffff },
12294 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12295 0x00000000, 0xffffffff },
12296 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12297 0x00000000, 0xffffffff },
12298 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12299 0x00000000, 0xffffffff },
12300 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12301 0x00000000, 0xffffffff },
12302 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12303 0xffffffff, 0x00000000 },
12304 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12305 0xffffffff, 0x00000000 },
12306
12307 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012308 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012309 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012310 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012311 0x00000000, 0x007fffff },
12312 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12313 0x00000000, 0x0000003f },
12314 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12315 0x00000000, 0x000001ff },
12316 { BUFMGR_MB_HIGH_WATER, 0x0000,
12317 0x00000000, 0x000001ff },
12318 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12319 0xffffffff, 0x00000000 },
12320 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12321 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012322
Michael Chana71116d2005-05-29 14:58:11 -070012323 /* Mailbox Registers */
12324 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12325 0x00000000, 0x000001ff },
12326 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12327 0x00000000, 0x000001ff },
12328 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12329 0x00000000, 0x000007ff },
12330 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12331 0x00000000, 0x000001ff },
12332
12333 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12334 };
12335
Michael Chanb16250e2006-09-27 16:10:14 -070012336 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012337 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012338 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012339 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012340 is_5750 = 1;
12341 }
Michael Chana71116d2005-05-29 14:58:11 -070012342
12343 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12344 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12345 continue;
12346
12347 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12348 continue;
12349
Joe Perches63c3a662011-04-26 08:12:10 +000012350 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012351 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12352 continue;
12353
Michael Chanb16250e2006-09-27 16:10:14 -070012354 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12355 continue;
12356
Michael Chana71116d2005-05-29 14:58:11 -070012357 offset = (u32) reg_tbl[i].offset;
12358 read_mask = reg_tbl[i].read_mask;
12359 write_mask = reg_tbl[i].write_mask;
12360
12361 /* Save the original register content */
12362 save_val = tr32(offset);
12363
12364 /* Determine the read-only value. */
12365 read_val = save_val & read_mask;
12366
12367 /* Write zero to the register, then make sure the read-only bits
12368 * are not changed and the read/write bits are all zeros.
12369 */
12370 tw32(offset, 0);
12371
12372 val = tr32(offset);
12373
12374 /* Test the read-only and read/write bits. */
12375 if (((val & read_mask) != read_val) || (val & write_mask))
12376 goto out;
12377
12378 /* Write ones to all the bits defined by RdMask and WrMask, then
12379 * make sure the read-only bits are not changed and the
12380 * read/write bits are all ones.
12381 */
12382 tw32(offset, read_mask | write_mask);
12383
12384 val = tr32(offset);
12385
12386 /* Test the read-only bits. */
12387 if ((val & read_mask) != read_val)
12388 goto out;
12389
12390 /* Test the read/write bits. */
12391 if ((val & write_mask) != write_mask)
12392 goto out;
12393
12394 tw32(offset, save_val);
12395 }
12396
12397 return 0;
12398
12399out:
Michael Chan9f88f292006-12-07 00:22:54 -080012400 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012401 netdev_err(tp->dev,
12402 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012403 tw32(offset, save_val);
12404 return -EIO;
12405}
12406
Michael Chan7942e1d2005-05-29 14:58:36 -070012407static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12408{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012409 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012410 int i;
12411 u32 j;
12412
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012413 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012414 for (j = 0; j < len; j += 4) {
12415 u32 val;
12416
12417 tg3_write_mem(tp, offset + j, test_pattern[i]);
12418 tg3_read_mem(tp, offset + j, &val);
12419 if (val != test_pattern[i])
12420 return -EIO;
12421 }
12422 }
12423 return 0;
12424}
12425
12426static int tg3_test_memory(struct tg3 *tp)
12427{
12428 static struct mem_entry {
12429 u32 offset;
12430 u32 len;
12431 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012432 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012433 { 0x00002000, 0x1c000},
12434 { 0xffffffff, 0x00000}
12435 }, mem_tbl_5705[] = {
12436 { 0x00000100, 0x0000c},
12437 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012438 { 0x00004000, 0x00800},
12439 { 0x00006000, 0x01000},
12440 { 0x00008000, 0x02000},
12441 { 0x00010000, 0x0e000},
12442 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012443 }, mem_tbl_5755[] = {
12444 { 0x00000200, 0x00008},
12445 { 0x00004000, 0x00800},
12446 { 0x00006000, 0x00800},
12447 { 0x00008000, 0x02000},
12448 { 0x00010000, 0x0c000},
12449 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012450 }, mem_tbl_5906[] = {
12451 { 0x00000200, 0x00008},
12452 { 0x00004000, 0x00400},
12453 { 0x00006000, 0x00400},
12454 { 0x00008000, 0x01000},
12455 { 0x00010000, 0x01000},
12456 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012457 }, mem_tbl_5717[] = {
12458 { 0x00000200, 0x00008},
12459 { 0x00010000, 0x0a000},
12460 { 0x00020000, 0x13c00},
12461 { 0xffffffff, 0x00000}
12462 }, mem_tbl_57765[] = {
12463 { 0x00000200, 0x00008},
12464 { 0x00004000, 0x00800},
12465 { 0x00006000, 0x09800},
12466 { 0x00010000, 0x0a000},
12467 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012468 };
12469 struct mem_entry *mem_tbl;
12470 int err = 0;
12471 int i;
12472
Joe Perches63c3a662011-04-26 08:12:10 +000012473 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012474 mem_tbl = mem_tbl_5717;
Michael Chanc65a17f2013-01-06 12:51:07 +000012475 else if (tg3_flag(tp, 57765_CLASS) ||
Joe Perches41535772013-02-16 11:20:04 +000012476 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012477 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012478 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012479 mem_tbl = mem_tbl_5755;
Joe Perches41535772013-02-16 11:20:04 +000012480 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlson321d32a2008-11-21 17:22:19 -080012481 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012482 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012483 mem_tbl = mem_tbl_5705;
12484 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012485 mem_tbl = mem_tbl_570x;
12486
12487 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012488 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12489 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012490 break;
12491 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012492
Michael Chan7942e1d2005-05-29 14:58:36 -070012493 return err;
12494}
12495
Matt Carlsonbb158d62011-04-25 12:42:47 +000012496#define TG3_TSO_MSS 500
12497
12498#define TG3_TSO_IP_HDR_LEN 20
12499#define TG3_TSO_TCP_HDR_LEN 20
12500#define TG3_TSO_TCP_OPT_LEN 12
12501
12502static const u8 tg3_tso_header[] = {
125030x08, 0x00,
125040x45, 0x00, 0x00, 0x00,
125050x00, 0x00, 0x40, 0x00,
125060x40, 0x06, 0x00, 0x00,
125070x0a, 0x00, 0x00, 0x01,
125080x0a, 0x00, 0x00, 0x02,
125090x0d, 0x00, 0xe0, 0x00,
125100x00, 0x00, 0x01, 0x00,
125110x00, 0x00, 0x02, 0x00,
125120x80, 0x10, 0x10, 0x00,
125130x14, 0x09, 0x00, 0x00,
125140x01, 0x01, 0x08, 0x0a,
125150x11, 0x11, 0x11, 0x11,
125160x11, 0x11, 0x11, 0x11,
12517};
Michael Chan9f40dea2005-09-05 17:53:06 -070012518
Matt Carlson28a45952011-08-19 13:58:22 +000012519static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012520{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012521 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012522 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012523 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012524 struct sk_buff *skb;
12525 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012526 dma_addr_t map;
12527 int num_pkts, tx_len, rx_len, i, err;
12528 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012529 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012530 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012531
Matt Carlsonc8873402010-02-12 14:47:11 +000012532 tnapi = &tp->napi[0];
12533 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012534 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012535 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012536 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012537 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012538 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012539 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012540 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012541
Michael Chanc76949a2005-05-29 14:58:59 -070012542 err = -EIO;
12543
Matt Carlson4852a862011-04-13 11:05:07 +000012544 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012545 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012546 if (!skb)
12547 return -ENOMEM;
12548
Michael Chanc76949a2005-05-29 14:58:59 -070012549 tx_data = skb_put(skb, tx_len);
12550 memcpy(tx_data, tp->dev->dev_addr, 6);
12551 memset(tx_data + 6, 0x0, 8);
12552
Matt Carlson4852a862011-04-13 11:05:07 +000012553 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012554
Matt Carlson28a45952011-08-19 13:58:22 +000012555 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012556 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12557
12558 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12559 TG3_TSO_TCP_OPT_LEN;
12560
12561 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12562 sizeof(tg3_tso_header));
12563 mss = TG3_TSO_MSS;
12564
12565 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12566 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12567
12568 /* Set the total length field in the IP header */
12569 iph->tot_len = htons((u16)(mss + hdr_len));
12570
12571 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12572 TXD_FLAG_CPU_POST_DMA);
12573
Joe Perches63c3a662011-04-26 08:12:10 +000012574 if (tg3_flag(tp, HW_TSO_1) ||
12575 tg3_flag(tp, HW_TSO_2) ||
12576 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012577 struct tcphdr *th;
12578 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12579 th = (struct tcphdr *)&tx_data[val];
12580 th->check = 0;
12581 } else
12582 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12583
Joe Perches63c3a662011-04-26 08:12:10 +000012584 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012585 mss |= (hdr_len & 0xc) << 12;
12586 if (hdr_len & 0x10)
12587 base_flags |= 0x00000010;
12588 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012589 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012590 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012591 else if (tg3_flag(tp, HW_TSO_1) ||
Joe Perches41535772013-02-16 11:20:04 +000012592 tg3_asic_rev(tp) == ASIC_REV_5705) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012593 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12594 } else {
12595 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12596 }
12597
12598 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12599 } else {
12600 num_pkts = 1;
12601 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012602
12603 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12604 tx_len > VLAN_ETH_FRAME_LEN)
12605 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012606 }
12607
12608 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012609 tx_data[i] = (u8) (i & 0xff);
12610
Alexander Duyckf4188d82009-12-02 16:48:38 +000012611 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12612 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012613 dev_kfree_skb(skb);
12614 return -EIO;
12615 }
Michael Chanc76949a2005-05-29 14:58:59 -070012616
Matt Carlson0d681b22011-07-27 14:20:49 +000012617 val = tnapi->tx_prod;
12618 tnapi->tx_buffers[val].skb = skb;
12619 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12620
Michael Chanc76949a2005-05-29 14:58:59 -070012621 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012622 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012623
12624 udelay(10);
12625
Matt Carlson898a56f2009-08-28 14:02:40 +000012626 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012627
Matt Carlson84b67b22011-07-27 14:20:52 +000012628 budget = tg3_tx_avail(tnapi);
12629 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012630 base_flags | TXD_FLAG_END, mss, 0)) {
12631 tnapi->tx_buffers[val].skb = NULL;
12632 dev_kfree_skb(skb);
12633 return -EIO;
12634 }
Michael Chanc76949a2005-05-29 14:58:59 -070012635
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012636 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012637
Michael Chan6541b802012-03-04 14:48:14 +000012638 /* Sync BD data before updating mailbox */
12639 wmb();
12640
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012641 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12642 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012643
12644 udelay(10);
12645
Matt Carlson303fc922009-11-02 14:27:34 +000012646 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12647 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012648 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012649 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012650
12651 udelay(10);
12652
Matt Carlson898a56f2009-08-28 14:02:40 +000012653 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12654 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012655 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012656 (rx_idx == (rx_start_idx + num_pkts)))
12657 break;
12658 }
12659
Matt Carlsonba1142e2011-11-04 09:15:00 +000012660 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012661 dev_kfree_skb(skb);
12662
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012663 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012664 goto out;
12665
12666 if (rx_idx != rx_start_idx + num_pkts)
12667 goto out;
12668
Matt Carlsonbb158d62011-04-25 12:42:47 +000012669 val = data_off;
12670 while (rx_idx != rx_start_idx) {
12671 desc = &rnapi->rx_rcb[rx_start_idx++];
12672 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12673 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012674
Matt Carlsonbb158d62011-04-25 12:42:47 +000012675 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12676 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012677 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012678
Matt Carlsonbb158d62011-04-25 12:42:47 +000012679 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12680 - ETH_FCS_LEN;
12681
Matt Carlson28a45952011-08-19 13:58:22 +000012682 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012683 if (rx_len != tx_len)
12684 goto out;
12685
12686 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12687 if (opaque_key != RXD_OPAQUE_RING_STD)
12688 goto out;
12689 } else {
12690 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12691 goto out;
12692 }
12693 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12694 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012695 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012696 goto out;
12697 }
12698
12699 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012700 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012701 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12702 mapping);
12703 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012704 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012705 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12706 mapping);
12707 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012708 goto out;
12709
Matt Carlsonbb158d62011-04-25 12:42:47 +000012710 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12711 PCI_DMA_FROMDEVICE);
12712
Eric Dumazet9205fd92011-11-18 06:47:01 +000012713 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012714 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012715 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012716 goto out;
12717 }
Matt Carlson4852a862011-04-13 11:05:07 +000012718 }
12719
Michael Chanc76949a2005-05-29 14:58:59 -070012720 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012721
Eric Dumazet9205fd92011-11-18 06:47:01 +000012722 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012723out:
12724 return err;
12725}
12726
Matt Carlson00c266b2011-04-25 12:42:46 +000012727#define TG3_STD_LOOPBACK_FAILED 1
12728#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012729#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012730#define TG3_LOOPBACK_FAILED \
12731 (TG3_STD_LOOPBACK_FAILED | \
12732 TG3_JMB_LOOPBACK_FAILED | \
12733 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012734
Matt Carlson941ec902011-08-19 13:58:23 +000012735static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012736{
Matt Carlson28a45952011-08-19 13:58:22 +000012737 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012738 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012739 u32 jmb_pkt_sz = 9000;
12740
12741 if (tp->dma_limit)
12742 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012743
Matt Carlsonab789042011-01-25 15:58:54 +000012744 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12745 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12746
Matt Carlson28a45952011-08-19 13:58:22 +000012747 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012748 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12749 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012750 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012751 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012752 goto done;
12753 }
12754
Michael Chanb9ec6c12006-07-25 16:37:27 -070012755 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012756 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012757 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12758 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012759 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012760 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012761 goto done;
12762 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012763
Joe Perches63c3a662011-04-26 08:12:10 +000012764 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012765 int i;
12766
12767 /* Reroute all rx packets to the 1st queue */
12768 for (i = MAC_RSS_INDIR_TBL_0;
12769 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12770 tw32(i, 0x0);
12771 }
12772
Matt Carlson6e01b202011-08-19 13:58:20 +000012773 /* HW errata - mac loopback fails in some cases on 5780.
12774 * Normal traffic and PHY loopback are not affected by
12775 * errata. Also, the MAC loopback test is deprecated for
12776 * all newer ASIC revisions.
12777 */
Joe Perches41535772013-02-16 11:20:04 +000012778 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Matt Carlson6e01b202011-08-19 13:58:20 +000012779 !tg3_flag(tp, CPMU_PRESENT)) {
12780 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012781
Matt Carlson28a45952011-08-19 13:58:22 +000012782 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012783 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012784
12785 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012786 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012787 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012788
12789 tg3_mac_loopback(tp, false);
12790 }
Matt Carlson4852a862011-04-13 11:05:07 +000012791
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012792 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012793 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012794 int i;
12795
Matt Carlson941ec902011-08-19 13:58:23 +000012796 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012797
12798 /* Wait for link */
12799 for (i = 0; i < 100; i++) {
12800 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12801 break;
12802 mdelay(1);
12803 }
12804
Matt Carlson28a45952011-08-19 13:58:22 +000012805 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012806 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012807 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012808 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012809 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012810 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012811 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012812 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012813
Matt Carlson941ec902011-08-19 13:58:23 +000012814 if (do_extlpbk) {
12815 tg3_phy_lpbk_set(tp, 0, true);
12816
12817 /* All link indications report up, but the hardware
12818 * isn't really ready for about 20 msec. Double it
12819 * to be sure.
12820 */
12821 mdelay(40);
12822
12823 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012824 data[TG3_EXT_LOOPB_TEST] |=
12825 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012826 if (tg3_flag(tp, TSO_CAPABLE) &&
12827 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012828 data[TG3_EXT_LOOPB_TEST] |=
12829 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012830 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012831 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012832 data[TG3_EXT_LOOPB_TEST] |=
12833 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012834 }
12835
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012836 /* Re-enable gphy autopowerdown. */
12837 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12838 tg3_phy_toggle_apd(tp, true);
12839 }
Matt Carlson6833c042008-11-21 17:18:59 -080012840
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012841 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12842 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012843
Matt Carlsonab789042011-01-25 15:58:54 +000012844done:
12845 tp->phy_flags |= eee_cap;
12846
Michael Chan9f40dea2005-09-05 17:53:06 -070012847 return err;
12848}
12849
Michael Chan4cafd3f2005-05-29 14:56:34 -070012850static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12851 u64 *data)
12852{
Michael Chan566f86a2005-05-29 14:56:58 -070012853 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012854 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012855
Matt Carlsonbed98292011-07-13 09:27:29 +000012856 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12857 tg3_power_up(tp)) {
12858 etest->flags |= ETH_TEST_FL_FAILED;
12859 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12860 return;
12861 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012862
Michael Chan566f86a2005-05-29 14:56:58 -070012863 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12864
12865 if (tg3_test_nvram(tp) != 0) {
12866 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012867 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012868 }
Matt Carlson941ec902011-08-19 13:58:23 +000012869 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012870 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012871 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012872 }
Michael Chana71116d2005-05-29 14:58:11 -070012873 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012874 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012875
Michael Chanbbe832c2005-06-24 20:20:04 -070012876 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012877 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012878 tg3_netif_stop(tp);
12879 irq_sync = 1;
12880 }
12881
12882 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012883 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012884 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012885 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012886 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012887 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012888 if (!err)
12889 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012890
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012891 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012892 tg3_phy_reset(tp);
12893
Michael Chana71116d2005-05-29 14:58:11 -070012894 if (tg3_test_registers(tp) != 0) {
12895 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012896 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012897 }
Matt Carlson28a45952011-08-19 13:58:22 +000012898
Michael Chan7942e1d2005-05-29 14:58:36 -070012899 if (tg3_test_memory(tp) != 0) {
12900 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012901 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012902 }
Matt Carlson28a45952011-08-19 13:58:22 +000012903
Matt Carlson941ec902011-08-19 13:58:23 +000012904 if (doextlpbk)
12905 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12906
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012907 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012908 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012909
David S. Millerf47c11e2005-06-24 20:18:35 -070012910 tg3_full_unlock(tp);
12911
Michael Chand4bc3922005-05-29 14:59:20 -070012912 if (tg3_test_interrupt(tp) != 0) {
12913 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012914 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012915 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012916
12917 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012918
Michael Chana71116d2005-05-29 14:58:11 -070012919 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12920 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012921 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012922 err2 = tg3_restart_hw(tp, 1);
12923 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012924 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012925 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012926
12927 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012928
12929 if (irq_sync && !err2)
12930 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012931 }
Matt Carlson80096062010-08-02 11:26:06 +000012932 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012933 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012934
Michael Chan4cafd3f2005-05-29 14:56:34 -070012935}
12936
Matt Carlson0a633ac2012-12-03 19:36:59 +000012937static int tg3_hwtstamp_ioctl(struct net_device *dev,
12938 struct ifreq *ifr, int cmd)
12939{
12940 struct tg3 *tp = netdev_priv(dev);
12941 struct hwtstamp_config stmpconf;
12942
12943 if (!tg3_flag(tp, PTP_CAPABLE))
12944 return -EINVAL;
12945
12946 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
12947 return -EFAULT;
12948
12949 if (stmpconf.flags)
12950 return -EINVAL;
12951
12952 switch (stmpconf.tx_type) {
12953 case HWTSTAMP_TX_ON:
12954 tg3_flag_set(tp, TX_TSTAMP_EN);
12955 break;
12956 case HWTSTAMP_TX_OFF:
12957 tg3_flag_clear(tp, TX_TSTAMP_EN);
12958 break;
12959 default:
12960 return -ERANGE;
12961 }
12962
12963 switch (stmpconf.rx_filter) {
12964 case HWTSTAMP_FILTER_NONE:
12965 tp->rxptpctl = 0;
12966 break;
12967 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
12968 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12969 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
12970 break;
12971 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
12972 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12973 TG3_RX_PTP_CTL_SYNC_EVNT;
12974 break;
12975 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12976 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12977 TG3_RX_PTP_CTL_DELAY_REQ;
12978 break;
12979 case HWTSTAMP_FILTER_PTP_V2_EVENT:
12980 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12981 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12982 break;
12983 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12984 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12985 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12986 break;
12987 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12988 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12989 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12990 break;
12991 case HWTSTAMP_FILTER_PTP_V2_SYNC:
12992 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12993 TG3_RX_PTP_CTL_SYNC_EVNT;
12994 break;
12995 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
12996 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12997 TG3_RX_PTP_CTL_SYNC_EVNT;
12998 break;
12999 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13000 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13001 TG3_RX_PTP_CTL_SYNC_EVNT;
13002 break;
13003 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13004 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
13005 TG3_RX_PTP_CTL_DELAY_REQ;
13006 break;
13007 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13008 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
13009 TG3_RX_PTP_CTL_DELAY_REQ;
13010 break;
13011 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13012 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
13013 TG3_RX_PTP_CTL_DELAY_REQ;
13014 break;
13015 default:
13016 return -ERANGE;
13017 }
13018
13019 if (netif_running(dev) && tp->rxptpctl)
13020 tw32(TG3_RX_PTP_CTL,
13021 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
13022
13023 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
13024 -EFAULT : 0;
13025}
13026
Linus Torvalds1da177e2005-04-16 15:20:36 -070013027static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13028{
13029 struct mii_ioctl_data *data = if_mii(ifr);
13030 struct tg3 *tp = netdev_priv(dev);
13031 int err;
13032
Joe Perches63c3a662011-04-26 08:12:10 +000013033 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013034 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013035 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013036 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000013037 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000013038 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013039 }
13040
Matt Carlson33f401a2010-04-05 10:19:27 +000013041 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000013043 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044
13045 /* fallthru */
13046 case SIOCGMIIREG: {
13047 u32 mii_regval;
13048
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013049 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013050 break; /* We have no PHY */
13051
Matt Carlson34eea5a2011-04-20 07:57:38 +000013052 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013053 return -EAGAIN;
13054
David S. Millerf47c11e2005-06-24 20:18:35 -070013055 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013056 err = __tg3_readphy(tp, data->phy_id & 0x1f,
13057 data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070013058 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059
13060 data->val_out = mii_regval;
13061
13062 return err;
13063 }
13064
13065 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013066 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013067 break; /* We have no PHY */
13068
Matt Carlson34eea5a2011-04-20 07:57:38 +000013069 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080013070 return -EAGAIN;
13071
David S. Millerf47c11e2005-06-24 20:18:35 -070013072 spin_lock_bh(&tp->lock);
Hauke Mehrtens5c358042013-02-07 05:37:38 +000013073 err = __tg3_writephy(tp, data->phy_id & 0x1f,
13074 data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070013075 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013076
13077 return err;
13078
Matt Carlson0a633ac2012-12-03 19:36:59 +000013079 case SIOCSHWTSTAMP:
13080 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
13081
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082 default:
13083 /* do nothing */
13084 break;
13085 }
13086 return -EOPNOTSUPP;
13087}
13088
David S. Miller15f98502005-05-18 22:49:26 -070013089static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13090{
13091 struct tg3 *tp = netdev_priv(dev);
13092
13093 memcpy(ec, &tp->coal, sizeof(*ec));
13094 return 0;
13095}
13096
Michael Chand244c892005-07-05 14:42:33 -070013097static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
13098{
13099 struct tg3 *tp = netdev_priv(dev);
13100 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
13101 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
13102
Joe Perches63c3a662011-04-26 08:12:10 +000013103 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070013104 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
13105 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
13106 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
13107 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
13108 }
13109
13110 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
13111 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
13112 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
13113 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
13114 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
13115 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
13116 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
13117 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13118 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13119 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13120 return -EINVAL;
13121
13122 /* No rx interrupts will be generated if both are zero */
13123 if ((ec->rx_coalesce_usecs == 0) &&
13124 (ec->rx_max_coalesced_frames == 0))
13125 return -EINVAL;
13126
13127 /* No tx interrupts will be generated if both are zero */
13128 if ((ec->tx_coalesce_usecs == 0) &&
13129 (ec->tx_max_coalesced_frames == 0))
13130 return -EINVAL;
13131
13132 /* Only copy relevant parameters, ignore all others. */
13133 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13134 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13135 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13136 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13137 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13138 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13139 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13140 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13141 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13142
13143 if (netif_running(dev)) {
13144 tg3_full_lock(tp, 0);
13145 __tg3_set_coalesce(tp, &tp->coal);
13146 tg3_full_unlock(tp);
13147 }
13148 return 0;
13149}
13150
Jeff Garzik7282d492006-09-13 14:30:00 -040013151static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013152 .get_settings = tg3_get_settings,
13153 .set_settings = tg3_set_settings,
13154 .get_drvinfo = tg3_get_drvinfo,
13155 .get_regs_len = tg3_get_regs_len,
13156 .get_regs = tg3_get_regs,
13157 .get_wol = tg3_get_wol,
13158 .set_wol = tg3_set_wol,
13159 .get_msglevel = tg3_get_msglevel,
13160 .set_msglevel = tg3_set_msglevel,
13161 .nway_reset = tg3_nway_reset,
13162 .get_link = ethtool_op_get_link,
13163 .get_eeprom_len = tg3_get_eeprom_len,
13164 .get_eeprom = tg3_get_eeprom,
13165 .set_eeprom = tg3_set_eeprom,
13166 .get_ringparam = tg3_get_ringparam,
13167 .set_ringparam = tg3_set_ringparam,
13168 .get_pauseparam = tg3_get_pauseparam,
13169 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013170 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013171 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013172 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013174 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013175 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013176 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013177 .get_rxnfc = tg3_get_rxnfc,
13178 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13179 .get_rxfh_indir = tg3_get_rxfh_indir,
13180 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013181 .get_channels = tg3_get_channels,
13182 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013183 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184};
13185
David S. Millerb4017c52012-03-01 17:57:40 -050013186static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13187 struct rtnl_link_stats64 *stats)
13188{
13189 struct tg3 *tp = netdev_priv(dev);
13190
David S. Millerb4017c52012-03-01 17:57:40 -050013191 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013192 if (!tp->hw_stats) {
13193 spin_unlock_bh(&tp->lock);
13194 return &tp->net_stats_prev;
13195 }
13196
David S. Millerb4017c52012-03-01 17:57:40 -050013197 tg3_get_nstats(tp, stats);
13198 spin_unlock_bh(&tp->lock);
13199
13200 return stats;
13201}
13202
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013203static void tg3_set_rx_mode(struct net_device *dev)
13204{
13205 struct tg3 *tp = netdev_priv(dev);
13206
13207 if (!netif_running(dev))
13208 return;
13209
13210 tg3_full_lock(tp, 0);
13211 __tg3_set_rx_mode(dev);
13212 tg3_full_unlock(tp);
13213}
13214
Matt Carlsonfaf16272012-02-13 10:20:07 +000013215static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13216 int new_mtu)
13217{
13218 dev->mtu = new_mtu;
13219
13220 if (new_mtu > ETH_DATA_LEN) {
13221 if (tg3_flag(tp, 5780_CLASS)) {
13222 netdev_update_features(dev);
13223 tg3_flag_clear(tp, TSO_CAPABLE);
13224 } else {
13225 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13226 }
13227 } else {
13228 if (tg3_flag(tp, 5780_CLASS)) {
13229 tg3_flag_set(tp, TSO_CAPABLE);
13230 netdev_update_features(dev);
13231 }
13232 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13233 }
13234}
13235
13236static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13237{
13238 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013239 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013240
13241 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13242 return -EINVAL;
13243
13244 if (!netif_running(dev)) {
13245 /* We'll just catch it later when the
13246 * device is up'd.
13247 */
13248 tg3_set_mtu(dev, tp, new_mtu);
13249 return 0;
13250 }
13251
13252 tg3_phy_stop(tp);
13253
13254 tg3_netif_stop(tp);
13255
13256 tg3_full_lock(tp, 1);
13257
13258 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13259
13260 tg3_set_mtu(dev, tp, new_mtu);
13261
Michael Chan2fae5e32012-03-04 14:48:15 +000013262 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13263 * breaks all requests to 256 bytes.
13264 */
Joe Perches41535772013-02-16 11:20:04 +000013265 if (tg3_asic_rev(tp) == ASIC_REV_57766)
Michael Chan2fae5e32012-03-04 14:48:15 +000013266 reset_phy = 1;
13267
13268 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013269
13270 if (!err)
13271 tg3_netif_start(tp);
13272
13273 tg3_full_unlock(tp);
13274
13275 if (!err)
13276 tg3_phy_start(tp);
13277
13278 return err;
13279}
13280
13281static const struct net_device_ops tg3_netdev_ops = {
13282 .ndo_open = tg3_open,
13283 .ndo_stop = tg3_close,
13284 .ndo_start_xmit = tg3_start_xmit,
13285 .ndo_get_stats64 = tg3_get_stats64,
13286 .ndo_validate_addr = eth_validate_addr,
13287 .ndo_set_rx_mode = tg3_set_rx_mode,
13288 .ndo_set_mac_address = tg3_set_mac_addr,
13289 .ndo_do_ioctl = tg3_ioctl,
13290 .ndo_tx_timeout = tg3_tx_timeout,
13291 .ndo_change_mtu = tg3_change_mtu,
13292 .ndo_fix_features = tg3_fix_features,
13293 .ndo_set_features = tg3_set_features,
13294#ifdef CONFIG_NET_POLL_CONTROLLER
13295 .ndo_poll_controller = tg3_poll_controller,
13296#endif
13297};
13298
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013299static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013300{
Michael Chan1b277772006-03-20 22:27:48 -080013301 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013302
13303 tp->nvram_size = EEPROM_CHIP_SIZE;
13304
Matt Carlsone4f34112009-02-25 14:25:00 +000013305 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013306 return;
13307
Michael Chanb16250e2006-09-27 16:10:14 -070013308 if ((magic != TG3_EEPROM_MAGIC) &&
13309 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13310 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013311 return;
13312
13313 /*
13314 * Size the chip by reading offsets at increasing powers of two.
13315 * When we encounter our validation signature, we know the addressing
13316 * has wrapped around, and thus have our chip size.
13317 */
Michael Chan1b277772006-03-20 22:27:48 -080013318 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319
13320 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013321 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013322 return;
13323
Michael Chan18201802006-03-20 22:29:15 -080013324 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013325 break;
13326
13327 cursize <<= 1;
13328 }
13329
13330 tp->nvram_size = cursize;
13331}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013332
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013333static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013334{
13335 u32 val;
13336
Joe Perches63c3a662011-04-26 08:12:10 +000013337 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013338 return;
13339
13340 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013341 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013342 tg3_get_eeprom_size(tp);
13343 return;
13344 }
13345
Matt Carlson6d348f22009-02-25 14:25:52 +000013346 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013348 /* This is confusing. We want to operate on the
13349 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13350 * call will read from NVRAM and byteswap the data
13351 * according to the byteswapping settings for all
13352 * other register accesses. This ensures the data we
13353 * want will always reside in the lower 16-bits.
13354 * However, the data in NVRAM is in LE format, which
13355 * means the data from the NVRAM read will always be
13356 * opposite the endianness of the CPU. The 16-bit
13357 * byteswap then brings the data to CPU endianness.
13358 */
13359 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013360 return;
13361 }
13362 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013363 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013364}
13365
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013366static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013367{
13368 u32 nvcfg1;
13369
13370 nvcfg1 = tr32(NVRAM_CFG1);
13371 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013372 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013373 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013374 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13375 tw32(NVRAM_CFG1, nvcfg1);
13376 }
13377
Joe Perches41535772013-02-16 11:20:04 +000013378 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013379 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013380 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013381 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13382 tp->nvram_jedecnum = JEDEC_ATMEL;
13383 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013384 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013385 break;
13386 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13387 tp->nvram_jedecnum = JEDEC_ATMEL;
13388 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13389 break;
13390 case FLASH_VENDOR_ATMEL_EEPROM:
13391 tp->nvram_jedecnum = JEDEC_ATMEL;
13392 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013393 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013394 break;
13395 case FLASH_VENDOR_ST:
13396 tp->nvram_jedecnum = JEDEC_ST;
13397 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013398 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013399 break;
13400 case FLASH_VENDOR_SAIFUN:
13401 tp->nvram_jedecnum = JEDEC_SAIFUN;
13402 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13403 break;
13404 case FLASH_VENDOR_SST_SMALL:
13405 case FLASH_VENDOR_SST_LARGE:
13406 tp->nvram_jedecnum = JEDEC_SST;
13407 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013409 }
Matt Carlson8590a602009-08-28 12:29:16 +000013410 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013411 tp->nvram_jedecnum = JEDEC_ATMEL;
13412 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013413 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013414 }
13415}
13416
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013417static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013418{
13419 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13420 case FLASH_5752PAGE_SIZE_256:
13421 tp->nvram_pagesize = 256;
13422 break;
13423 case FLASH_5752PAGE_SIZE_512:
13424 tp->nvram_pagesize = 512;
13425 break;
13426 case FLASH_5752PAGE_SIZE_1K:
13427 tp->nvram_pagesize = 1024;
13428 break;
13429 case FLASH_5752PAGE_SIZE_2K:
13430 tp->nvram_pagesize = 2048;
13431 break;
13432 case FLASH_5752PAGE_SIZE_4K:
13433 tp->nvram_pagesize = 4096;
13434 break;
13435 case FLASH_5752PAGE_SIZE_264:
13436 tp->nvram_pagesize = 264;
13437 break;
13438 case FLASH_5752PAGE_SIZE_528:
13439 tp->nvram_pagesize = 528;
13440 break;
13441 }
13442}
13443
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013444static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013445{
13446 u32 nvcfg1;
13447
13448 nvcfg1 = tr32(NVRAM_CFG1);
13449
Michael Chane6af3012005-04-21 17:12:05 -070013450 /* NVRAM protection for TPM */
13451 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013452 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013453
Michael Chan361b4ac2005-04-21 17:11:21 -070013454 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013455 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13456 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13457 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013458 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013459 break;
13460 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13461 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013462 tg3_flag_set(tp, NVRAM_BUFFERED);
13463 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013464 break;
13465 case FLASH_5752VENDOR_ST_M45PE10:
13466 case FLASH_5752VENDOR_ST_M45PE20:
13467 case FLASH_5752VENDOR_ST_M45PE40:
13468 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013469 tg3_flag_set(tp, NVRAM_BUFFERED);
13470 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013471 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013472 }
13473
Joe Perches63c3a662011-04-26 08:12:10 +000013474 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013475 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013476 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013477 /* For eeprom, set pagesize to maximum eeprom size */
13478 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13479
13480 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13481 tw32(NVRAM_CFG1, nvcfg1);
13482 }
13483}
13484
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013485static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013486{
Matt Carlson989a9d22007-05-05 11:51:05 -070013487 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013488
13489 nvcfg1 = tr32(NVRAM_CFG1);
13490
13491 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013492 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013493 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013494 protect = 1;
13495 }
Michael Chand3c7b882006-03-23 01:28:25 -080013496
Matt Carlson989a9d22007-05-05 11:51:05 -070013497 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13498 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013499 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13500 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13501 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13502 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13503 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013504 tg3_flag_set(tp, NVRAM_BUFFERED);
13505 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013506 tp->nvram_pagesize = 264;
13507 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13508 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13509 tp->nvram_size = (protect ? 0x3e200 :
13510 TG3_NVRAM_SIZE_512KB);
13511 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13512 tp->nvram_size = (protect ? 0x1f200 :
13513 TG3_NVRAM_SIZE_256KB);
13514 else
13515 tp->nvram_size = (protect ? 0x1f200 :
13516 TG3_NVRAM_SIZE_128KB);
13517 break;
13518 case FLASH_5752VENDOR_ST_M45PE10:
13519 case FLASH_5752VENDOR_ST_M45PE20:
13520 case FLASH_5752VENDOR_ST_M45PE40:
13521 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013522 tg3_flag_set(tp, NVRAM_BUFFERED);
13523 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013524 tp->nvram_pagesize = 256;
13525 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13526 tp->nvram_size = (protect ?
13527 TG3_NVRAM_SIZE_64KB :
13528 TG3_NVRAM_SIZE_128KB);
13529 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13530 tp->nvram_size = (protect ?
13531 TG3_NVRAM_SIZE_64KB :
13532 TG3_NVRAM_SIZE_256KB);
13533 else
13534 tp->nvram_size = (protect ?
13535 TG3_NVRAM_SIZE_128KB :
13536 TG3_NVRAM_SIZE_512KB);
13537 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013538 }
13539}
13540
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013541static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013542{
13543 u32 nvcfg1;
13544
13545 nvcfg1 = tr32(NVRAM_CFG1);
13546
13547 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013548 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13549 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13550 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13551 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13552 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013553 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013554 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013555
Matt Carlson8590a602009-08-28 12:29:16 +000013556 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13557 tw32(NVRAM_CFG1, nvcfg1);
13558 break;
13559 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13560 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13561 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13562 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13563 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013564 tg3_flag_set(tp, NVRAM_BUFFERED);
13565 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013566 tp->nvram_pagesize = 264;
13567 break;
13568 case FLASH_5752VENDOR_ST_M45PE10:
13569 case FLASH_5752VENDOR_ST_M45PE20:
13570 case FLASH_5752VENDOR_ST_M45PE40:
13571 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013572 tg3_flag_set(tp, NVRAM_BUFFERED);
13573 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013574 tp->nvram_pagesize = 256;
13575 break;
Michael Chan1b277772006-03-20 22:27:48 -080013576 }
13577}
13578
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013579static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013580{
13581 u32 nvcfg1, protect = 0;
13582
13583 nvcfg1 = tr32(NVRAM_CFG1);
13584
13585 /* NVRAM protection for TPM */
13586 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013587 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013588 protect = 1;
13589 }
13590
13591 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13592 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013593 case FLASH_5761VENDOR_ATMEL_ADB021D:
13594 case FLASH_5761VENDOR_ATMEL_ADB041D:
13595 case FLASH_5761VENDOR_ATMEL_ADB081D:
13596 case FLASH_5761VENDOR_ATMEL_ADB161D:
13597 case FLASH_5761VENDOR_ATMEL_MDB021D:
13598 case FLASH_5761VENDOR_ATMEL_MDB041D:
13599 case FLASH_5761VENDOR_ATMEL_MDB081D:
13600 case FLASH_5761VENDOR_ATMEL_MDB161D:
13601 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013602 tg3_flag_set(tp, NVRAM_BUFFERED);
13603 tg3_flag_set(tp, FLASH);
13604 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013605 tp->nvram_pagesize = 256;
13606 break;
13607 case FLASH_5761VENDOR_ST_A_M45PE20:
13608 case FLASH_5761VENDOR_ST_A_M45PE40:
13609 case FLASH_5761VENDOR_ST_A_M45PE80:
13610 case FLASH_5761VENDOR_ST_A_M45PE16:
13611 case FLASH_5761VENDOR_ST_M_M45PE20:
13612 case FLASH_5761VENDOR_ST_M_M45PE40:
13613 case FLASH_5761VENDOR_ST_M_M45PE80:
13614 case FLASH_5761VENDOR_ST_M_M45PE16:
13615 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013616 tg3_flag_set(tp, NVRAM_BUFFERED);
13617 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013618 tp->nvram_pagesize = 256;
13619 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013620 }
13621
13622 if (protect) {
13623 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13624 } else {
13625 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013626 case FLASH_5761VENDOR_ATMEL_ADB161D:
13627 case FLASH_5761VENDOR_ATMEL_MDB161D:
13628 case FLASH_5761VENDOR_ST_A_M45PE16:
13629 case FLASH_5761VENDOR_ST_M_M45PE16:
13630 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13631 break;
13632 case FLASH_5761VENDOR_ATMEL_ADB081D:
13633 case FLASH_5761VENDOR_ATMEL_MDB081D:
13634 case FLASH_5761VENDOR_ST_A_M45PE80:
13635 case FLASH_5761VENDOR_ST_M_M45PE80:
13636 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13637 break;
13638 case FLASH_5761VENDOR_ATMEL_ADB041D:
13639 case FLASH_5761VENDOR_ATMEL_MDB041D:
13640 case FLASH_5761VENDOR_ST_A_M45PE40:
13641 case FLASH_5761VENDOR_ST_M_M45PE40:
13642 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13643 break;
13644 case FLASH_5761VENDOR_ATMEL_ADB021D:
13645 case FLASH_5761VENDOR_ATMEL_MDB021D:
13646 case FLASH_5761VENDOR_ST_A_M45PE20:
13647 case FLASH_5761VENDOR_ST_M_M45PE20:
13648 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13649 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013650 }
13651 }
13652}
13653
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013654static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013655{
13656 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013657 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013658 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13659}
13660
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013661static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013662{
13663 u32 nvcfg1;
13664
13665 nvcfg1 = tr32(NVRAM_CFG1);
13666
13667 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13668 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13669 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13670 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013671 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013672 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13673
13674 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13675 tw32(NVRAM_CFG1, nvcfg1);
13676 return;
13677 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13678 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13679 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13680 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13681 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13682 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13683 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13684 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013685 tg3_flag_set(tp, NVRAM_BUFFERED);
13686 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013687
13688 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13689 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13690 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13691 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13692 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13693 break;
13694 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13695 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13696 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13697 break;
13698 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13699 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13700 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13701 break;
13702 }
13703 break;
13704 case FLASH_5752VENDOR_ST_M45PE10:
13705 case FLASH_5752VENDOR_ST_M45PE20:
13706 case FLASH_5752VENDOR_ST_M45PE40:
13707 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013708 tg3_flag_set(tp, NVRAM_BUFFERED);
13709 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013710
13711 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13712 case FLASH_5752VENDOR_ST_M45PE10:
13713 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13714 break;
13715 case FLASH_5752VENDOR_ST_M45PE20:
13716 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13717 break;
13718 case FLASH_5752VENDOR_ST_M45PE40:
13719 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13720 break;
13721 }
13722 break;
13723 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013724 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013725 return;
13726 }
13727
Matt Carlsona1b950d2009-09-01 13:20:17 +000013728 tg3_nvram_get_pagesize(tp, nvcfg1);
13729 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013730 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013731}
13732
13733
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013734static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013735{
13736 u32 nvcfg1;
13737
13738 nvcfg1 = tr32(NVRAM_CFG1);
13739
13740 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13741 case FLASH_5717VENDOR_ATMEL_EEPROM:
13742 case FLASH_5717VENDOR_MICRO_EEPROM:
13743 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013744 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013745 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13746
13747 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13748 tw32(NVRAM_CFG1, nvcfg1);
13749 return;
13750 case FLASH_5717VENDOR_ATMEL_MDB011D:
13751 case FLASH_5717VENDOR_ATMEL_ADB011B:
13752 case FLASH_5717VENDOR_ATMEL_ADB011D:
13753 case FLASH_5717VENDOR_ATMEL_MDB021D:
13754 case FLASH_5717VENDOR_ATMEL_ADB021B:
13755 case FLASH_5717VENDOR_ATMEL_ADB021D:
13756 case FLASH_5717VENDOR_ATMEL_45USPT:
13757 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013758 tg3_flag_set(tp, NVRAM_BUFFERED);
13759 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013760
13761 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13762 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013763 /* Detect size with tg3_nvram_get_size() */
13764 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013765 case FLASH_5717VENDOR_ATMEL_ADB021B:
13766 case FLASH_5717VENDOR_ATMEL_ADB021D:
13767 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13768 break;
13769 default:
13770 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13771 break;
13772 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013773 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013774 case FLASH_5717VENDOR_ST_M_M25PE10:
13775 case FLASH_5717VENDOR_ST_A_M25PE10:
13776 case FLASH_5717VENDOR_ST_M_M45PE10:
13777 case FLASH_5717VENDOR_ST_A_M45PE10:
13778 case FLASH_5717VENDOR_ST_M_M25PE20:
13779 case FLASH_5717VENDOR_ST_A_M25PE20:
13780 case FLASH_5717VENDOR_ST_M_M45PE20:
13781 case FLASH_5717VENDOR_ST_A_M45PE20:
13782 case FLASH_5717VENDOR_ST_25USPT:
13783 case FLASH_5717VENDOR_ST_45USPT:
13784 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013785 tg3_flag_set(tp, NVRAM_BUFFERED);
13786 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013787
13788 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13789 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013790 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013791 /* Detect size with tg3_nvram_get_size() */
13792 break;
13793 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013794 case FLASH_5717VENDOR_ST_A_M45PE20:
13795 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13796 break;
13797 default:
13798 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13799 break;
13800 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013801 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013802 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013803 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013804 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013805 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013806
13807 tg3_nvram_get_pagesize(tp, nvcfg1);
13808 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013809 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013810}
13811
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013812static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013813{
13814 u32 nvcfg1, nvmpinstrp;
13815
13816 nvcfg1 = tr32(NVRAM_CFG1);
13817 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13818
Joe Perches41535772013-02-16 11:20:04 +000013819 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013820 if (!(nvcfg1 & NVRAM_CFG1_5762VENDOR_MASK)) {
13821 tg3_flag_set(tp, NO_NVRAM);
13822 return;
13823 }
13824
13825 switch (nvmpinstrp) {
13826 case FLASH_5762_EEPROM_HD:
13827 nvmpinstrp = FLASH_5720_EEPROM_HD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013828 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013829 case FLASH_5762_EEPROM_LD:
13830 nvmpinstrp = FLASH_5720_EEPROM_LD;
Dan Carpenter17e1a422013-01-11 09:57:33 +030013831 break;
Michael Chanc86a8562013-01-06 12:51:08 +000013832 }
13833 }
13834
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013835 switch (nvmpinstrp) {
13836 case FLASH_5720_EEPROM_HD:
13837 case FLASH_5720_EEPROM_LD:
13838 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013839 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013840
13841 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13842 tw32(NVRAM_CFG1, nvcfg1);
13843 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13844 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13845 else
13846 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13847 return;
13848 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13849 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13850 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13851 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13852 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13853 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13854 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13855 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13856 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13857 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13858 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13859 case FLASH_5720VENDOR_ATMEL_45USPT:
13860 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013861 tg3_flag_set(tp, NVRAM_BUFFERED);
13862 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013863
13864 switch (nvmpinstrp) {
13865 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13866 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13867 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13868 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13869 break;
13870 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13871 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13872 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13873 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13874 break;
13875 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13876 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13877 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13878 break;
13879 default:
Joe Perches41535772013-02-16 11:20:04 +000013880 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013881 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013882 break;
13883 }
13884 break;
13885 case FLASH_5720VENDOR_M_ST_M25PE10:
13886 case FLASH_5720VENDOR_M_ST_M45PE10:
13887 case FLASH_5720VENDOR_A_ST_M25PE10:
13888 case FLASH_5720VENDOR_A_ST_M45PE10:
13889 case FLASH_5720VENDOR_M_ST_M25PE20:
13890 case FLASH_5720VENDOR_M_ST_M45PE20:
13891 case FLASH_5720VENDOR_A_ST_M25PE20:
13892 case FLASH_5720VENDOR_A_ST_M45PE20:
13893 case FLASH_5720VENDOR_M_ST_M25PE40:
13894 case FLASH_5720VENDOR_M_ST_M45PE40:
13895 case FLASH_5720VENDOR_A_ST_M25PE40:
13896 case FLASH_5720VENDOR_A_ST_M45PE40:
13897 case FLASH_5720VENDOR_M_ST_M25PE80:
13898 case FLASH_5720VENDOR_M_ST_M45PE80:
13899 case FLASH_5720VENDOR_A_ST_M25PE80:
13900 case FLASH_5720VENDOR_A_ST_M45PE80:
13901 case FLASH_5720VENDOR_ST_25USPT:
13902 case FLASH_5720VENDOR_ST_45USPT:
13903 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013904 tg3_flag_set(tp, NVRAM_BUFFERED);
13905 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013906
13907 switch (nvmpinstrp) {
13908 case FLASH_5720VENDOR_M_ST_M25PE20:
13909 case FLASH_5720VENDOR_M_ST_M45PE20:
13910 case FLASH_5720VENDOR_A_ST_M25PE20:
13911 case FLASH_5720VENDOR_A_ST_M45PE20:
13912 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13913 break;
13914 case FLASH_5720VENDOR_M_ST_M25PE40:
13915 case FLASH_5720VENDOR_M_ST_M45PE40:
13916 case FLASH_5720VENDOR_A_ST_M25PE40:
13917 case FLASH_5720VENDOR_A_ST_M45PE40:
13918 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13919 break;
13920 case FLASH_5720VENDOR_M_ST_M25PE80:
13921 case FLASH_5720VENDOR_M_ST_M45PE80:
13922 case FLASH_5720VENDOR_A_ST_M25PE80:
13923 case FLASH_5720VENDOR_A_ST_M45PE80:
13924 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13925 break;
13926 default:
Joe Perches41535772013-02-16 11:20:04 +000013927 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc5d0b722013-02-14 12:13:40 +000013928 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013929 break;
13930 }
13931 break;
13932 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013933 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013934 return;
13935 }
13936
13937 tg3_nvram_get_pagesize(tp, nvcfg1);
13938 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013939 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Michael Chanc86a8562013-01-06 12:51:08 +000013940
Joe Perches41535772013-02-16 11:20:04 +000013941 if (tg3_asic_rev(tp) == ASIC_REV_5762) {
Michael Chanc86a8562013-01-06 12:51:08 +000013942 u32 val;
13943
13944 if (tg3_nvram_read(tp, 0, &val))
13945 return;
13946
13947 if (val != TG3_EEPROM_MAGIC &&
13948 (val & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW)
13949 tg3_flag_set(tp, NO_NVRAM);
13950 }
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013951}
13952
Linus Torvalds1da177e2005-04-16 15:20:36 -070013953/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013954static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013955{
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000013956 if (tg3_flag(tp, IS_SSB_CORE)) {
13957 /* No NVRAM and EEPROM on the SSB Broadcom GigE core. */
13958 tg3_flag_clear(tp, NVRAM);
13959 tg3_flag_clear(tp, NVRAM_BUFFERED);
13960 tg3_flag_set(tp, NO_NVRAM);
13961 return;
13962 }
13963
Linus Torvalds1da177e2005-04-16 15:20:36 -070013964 tw32_f(GRC_EEPROM_ADDR,
13965 (EEPROM_ADDR_FSM_RESET |
13966 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13967 EEPROM_ADDR_CLKPERD_SHIFT)));
13968
Michael Chan9d57f012006-12-07 00:23:25 -080013969 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970
13971 /* Enable seeprom accesses. */
13972 tw32_f(GRC_LOCAL_CTRL,
13973 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13974 udelay(100);
13975
Joe Perches41535772013-02-16 11:20:04 +000013976 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
13977 tg3_asic_rev(tp) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013978 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979
Michael Chanec41c7d2006-01-17 02:40:55 -080013980 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013981 netdev_warn(tp->dev,
13982 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013983 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013984 return;
13985 }
Michael Chane6af3012005-04-21 17:12:05 -070013986 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013987
Matt Carlson989a9d22007-05-05 11:51:05 -070013988 tp->nvram_size = 0;
13989
Joe Perches41535772013-02-16 11:20:04 +000013990 if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan361b4ac2005-04-21 17:11:21 -070013991 tg3_get_5752_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000013992 else if (tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chand3c7b882006-03-23 01:28:25 -080013993 tg3_get_5755_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000013994 else if (tg3_asic_rev(tp) == ASIC_REV_5787 ||
13995 tg3_asic_rev(tp) == ASIC_REV_5784 ||
13996 tg3_asic_rev(tp) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013997 tg3_get_5787_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000013998 else if (tg3_asic_rev(tp) == ASIC_REV_5761)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013999 tg3_get_5761_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014000 else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014001 tg3_get_5906_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014002 else if (tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014003 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080014004 tg3_get_57780_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014005 else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
14006 tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014007 tg3_get_5717_nvram_info(tp);
Joe Perches41535772013-02-16 11:20:04 +000014008 else if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
14009 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000014010 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070014011 else
14012 tg3_get_nvram_info(tp);
14013
Matt Carlson989a9d22007-05-05 11:51:05 -070014014 if (tp->nvram_size == 0)
14015 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016
Michael Chane6af3012005-04-21 17:12:05 -070014017 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080014018 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014019
14020 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014021 tg3_flag_clear(tp, NVRAM);
14022 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014023
14024 tg3_get_eeprom_size(tp);
14025 }
14026}
14027
Linus Torvalds1da177e2005-04-16 15:20:36 -070014028struct subsys_tbl_ent {
14029 u16 subsys_vendor, subsys_devid;
14030 u32 phy_id;
14031};
14032
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014033static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014034 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014035 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014036 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014037 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014038 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014039 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014040 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014041 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14042 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
14043 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014044 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014045 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014046 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014047 { TG3PCI_SUBVENDOR_ID_BROADCOM,
14048 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
14049 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014050 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014051 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014052 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014053 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014054 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014055 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014056 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057
14058 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014059 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014060 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014061 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014062 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014063 { TG3PCI_SUBVENDOR_ID_3COM,
14064 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
14065 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014066 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014067 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000014068 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014069
14070 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014071 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014072 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014073 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014074 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014075 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014076 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014077 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000014078 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014079
14080 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014081 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014082 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014083 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014084 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014085 { TG3PCI_SUBVENDOR_ID_COMPAQ,
14086 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
14087 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014088 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000014089 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000014090 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091
14092 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014093 { TG3PCI_SUBVENDOR_ID_IBM,
14094 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095};
14096
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014097static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014098{
14099 int i;
14100
14101 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
14102 if ((subsys_id_to_phy_id[i].subsys_vendor ==
14103 tp->pdev->subsystem_vendor) &&
14104 (subsys_id_to_phy_id[i].subsys_devid ==
14105 tp->pdev->subsystem_device))
14106 return &subsys_id_to_phy_id[i];
14107 }
14108 return NULL;
14109}
14110
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014111static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070014114
Matt Carlson79eb6902010-02-17 15:17:03 +000014115 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014116 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14117
Gary Zambranoa85feb82007-05-05 11:52:19 -070014118 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000014119 tg3_flag_set(tp, EEPROM_WRITE_PROT);
14120 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080014121
Joe Perches41535772013-02-16 11:20:04 +000014122 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080014123 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014124 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14125 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014126 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014127 val = tr32(VCPU_CFGSHDW);
14128 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000014129 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070014130 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014131 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014132 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014133 device_set_wakeup_enable(&tp->pdev->dev, true);
14134 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014135 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070014136 }
14137
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
14139 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
14140 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070014141 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070014142 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143
14144 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
14145 tp->nic_sram_data_cfg = nic_cfg;
14146
14147 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
14148 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000014149 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
14150 tg3_asic_rev(tp) != ASIC_REV_5701 &&
14151 tg3_asic_rev(tp) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152 (ver > 0) && (ver < 0x100))
14153 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
14154
Joe Perches41535772013-02-16 11:20:04 +000014155 if (tg3_asic_rev(tp) == ASIC_REV_5785)
Matt Carlsona9daf362008-05-25 23:49:44 -070014156 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14157
Linus Torvalds1da177e2005-04-16 15:20:36 -070014158 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14159 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14160 eeprom_phy_serdes = 1;
14161
14162 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14163 if (nic_phy_id != 0) {
14164 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14165 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14166
14167 eeprom_phy_id = (id1 >> 16) << 10;
14168 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14169 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14170 } else
14171 eeprom_phy_id = 0;
14172
Michael Chan7d0c41e2005-04-21 17:06:20 -070014173 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014174 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014175 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014176 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014177 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014178 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014179 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014180
Joe Perches63c3a662011-04-26 08:12:10 +000014181 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014182 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14183 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014184 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014185 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14186
14187 switch (led_cfg) {
14188 default:
14189 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14190 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14191 break;
14192
14193 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14194 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14195 break;
14196
14197 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14198 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014199
14200 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14201 * read on some older 5700/5701 bootcode.
14202 */
Joe Perches41535772013-02-16 11:20:04 +000014203 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
14204 tg3_asic_rev(tp) == ASIC_REV_5701)
Michael Chan9ba27792005-06-06 15:16:20 -070014205 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14206
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 break;
14208
14209 case SHASTA_EXT_LED_SHARED:
14210 tp->led_ctrl = LED_CTRL_MODE_SHARED;
Joe Perches41535772013-02-16 11:20:04 +000014211 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0 &&
14212 tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014213 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14214 LED_CTRL_MODE_PHY_2);
14215 break;
14216
14217 case SHASTA_EXT_LED_MAC:
14218 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14219 break;
14220
14221 case SHASTA_EXT_LED_COMBO:
14222 tp->led_ctrl = LED_CTRL_MODE_COMBO;
Joe Perches41535772013-02-16 11:20:04 +000014223 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5750_A0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14225 LED_CTRL_MODE_PHY_2);
14226 break;
14227
Stephen Hemminger855e1112008-04-16 16:37:28 -070014228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014229
Joe Perches41535772013-02-16 11:20:04 +000014230 if ((tg3_asic_rev(tp) == ASIC_REV_5700 ||
14231 tg3_asic_rev(tp) == ASIC_REV_5701) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014232 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14233 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14234
Joe Perches41535772013-02-16 11:20:04 +000014235 if (tg3_chip_rev(tp) == CHIPREV_5784_AX)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014236 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014237
Michael Chan9d26e212006-12-07 00:21:14 -080014238 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014239 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014240 if ((tp->pdev->subsystem_vendor ==
14241 PCI_VENDOR_ID_ARIMA) &&
14242 (tp->pdev->subsystem_device == 0x205a ||
14243 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014244 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014245 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014246 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14247 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249
14250 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014251 tg3_flag_set(tp, ENABLE_ASF);
14252 if (tg3_flag(tp, 5750_PLUS))
14253 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014254 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014255
14256 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014257 tg3_flag(tp, 5750_PLUS))
14258 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014259
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014260 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014261 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014262 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263
Joe Perches63c3a662011-04-26 08:12:10 +000014264 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014265 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014266 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014267 device_set_wakeup_enable(&tp->pdev->dev, true);
14268 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014269
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014271 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014272
14273 /* serdes signal pre-emphasis in register 0x590 set by */
14274 /* bootcode if bit 18 is set */
14275 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014276 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014277
Joe Perches63c3a662011-04-26 08:12:10 +000014278 if ((tg3_flag(tp, 57765_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000014279 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
14280 tg3_chip_rev(tp) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014281 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014282 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014283
Joe Perches63c3a662011-04-26 08:12:10 +000014284 if (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000014285 tg3_asic_rev(tp) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014286 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014287 u32 cfg3;
14288
14289 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14290 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014291 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014292 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014293
Matt Carlson14417062010-02-17 15:16:59 +000014294 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014295 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014296 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014297 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014298 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014299 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014300 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014301done:
Joe Perches63c3a662011-04-26 08:12:10 +000014302 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014303 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014304 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014305 else
14306 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014307}
14308
Michael Chanc86a8562013-01-06 12:51:08 +000014309static int tg3_ape_otp_read(struct tg3 *tp, u32 offset, u32 *val)
14310{
14311 int i, err;
14312 u32 val2, off = offset * 8;
14313
14314 err = tg3_nvram_lock(tp);
14315 if (err)
14316 return err;
14317
14318 tg3_ape_write32(tp, TG3_APE_OTP_ADDR, off | APE_OTP_ADDR_CPU_ENABLE);
14319 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, APE_OTP_CTRL_PROG_EN |
14320 APE_OTP_CTRL_CMD_RD | APE_OTP_CTRL_START);
14321 tg3_ape_read32(tp, TG3_APE_OTP_CTRL);
14322 udelay(10);
14323
14324 for (i = 0; i < 100; i++) {
14325 val2 = tg3_ape_read32(tp, TG3_APE_OTP_STATUS);
14326 if (val2 & APE_OTP_STATUS_CMD_DONE) {
14327 *val = tg3_ape_read32(tp, TG3_APE_OTP_RD_DATA);
14328 break;
14329 }
14330 udelay(10);
14331 }
14332
14333 tg3_ape_write32(tp, TG3_APE_OTP_CTRL, 0);
14334
14335 tg3_nvram_unlock(tp);
14336 if (val2 & APE_OTP_STATUS_CMD_DONE)
14337 return 0;
14338
14339 return -EBUSY;
14340}
14341
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014342static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014343{
14344 int i;
14345 u32 val;
14346
14347 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14348 tw32(OTP_CTRL, cmd);
14349
14350 /* Wait for up to 1 ms for command to execute. */
14351 for (i = 0; i < 100; i++) {
14352 val = tr32(OTP_STATUS);
14353 if (val & OTP_STATUS_CMD_DONE)
14354 break;
14355 udelay(10);
14356 }
14357
14358 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14359}
14360
14361/* Read the gphy configuration from the OTP region of the chip. The gphy
14362 * configuration is a 32-bit value that straddles the alignment boundary.
14363 * We do two 32-bit reads and then shift and merge the results.
14364 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014365static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014366{
14367 u32 bhalf_otp, thalf_otp;
14368
14369 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14370
14371 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14372 return 0;
14373
14374 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14375
14376 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14377 return 0;
14378
14379 thalf_otp = tr32(OTP_READ_DATA);
14380
14381 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14382
14383 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14384 return 0;
14385
14386 bhalf_otp = tr32(OTP_READ_DATA);
14387
14388 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14389}
14390
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014391static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014392{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014393 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014394
14395 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14396 adv |= ADVERTISED_1000baseT_Half |
14397 ADVERTISED_1000baseT_Full;
14398
14399 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14400 adv |= ADVERTISED_100baseT_Half |
14401 ADVERTISED_100baseT_Full |
14402 ADVERTISED_10baseT_Half |
14403 ADVERTISED_10baseT_Full |
14404 ADVERTISED_TP;
14405 else
14406 adv |= ADVERTISED_FIBRE;
14407
14408 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014409 tp->link_config.speed = SPEED_UNKNOWN;
14410 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014411 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014412 tp->link_config.active_speed = SPEED_UNKNOWN;
14413 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014414
14415 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014416}
14417
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014418static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014419{
14420 u32 hw_phy_id_1, hw_phy_id_2;
14421 u32 hw_phy_id, hw_phy_id_masked;
14422 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014423
Matt Carlsone256f8a2011-03-09 16:58:24 +000014424 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014425 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014426 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14427
Michael Chan8151ad52012-07-29 19:15:41 +000014428 if (tg3_flag(tp, ENABLE_APE)) {
14429 switch (tp->pci_fn) {
14430 case 0:
14431 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14432 break;
14433 case 1:
14434 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14435 break;
14436 case 2:
14437 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14438 break;
14439 case 3:
14440 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14441 break;
14442 }
14443 }
14444
Joe Perches63c3a662011-04-26 08:12:10 +000014445 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014446 return tg3_phy_init(tp);
14447
Linus Torvalds1da177e2005-04-16 15:20:36 -070014448 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014449 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014450 */
14451 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014452 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014453 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014454 } else {
14455 /* Now read the physical PHY_ID from the chip and verify
14456 * that it is sane. If it doesn't look good, we fall back
14457 * to either the hard-coded table based PHY_ID and failing
14458 * that the value found in the eeprom area.
14459 */
14460 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14461 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14462
14463 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14464 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14465 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14466
Matt Carlson79eb6902010-02-17 15:17:03 +000014467 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014468 }
14469
Matt Carlson79eb6902010-02-17 15:17:03 +000014470 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014471 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014472 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014473 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014474 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014475 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014476 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014477 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014478 /* Do nothing, phy ID already set up in
14479 * tg3_get_eeprom_hw_cfg().
14480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014481 } else {
14482 struct subsys_tbl_ent *p;
14483
14484 /* No eeprom signature? Try the hardcoded
14485 * subsys device table.
14486 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014487 p = tg3_lookup_by_subsys(tp);
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014488 if (p) {
14489 tp->phy_id = p->phy_id;
14490 } else if (!tg3_flag(tp, IS_SSB_CORE)) {
14491 /* For now we saw the IDs 0xbc050cd0,
14492 * 0xbc050f80 and 0xbc050c30 on devices
14493 * connected to an BCM4785 and there are
14494 * probably more. Just assume that the phy is
14495 * supported when it is connected to a SSB core
14496 * for now.
14497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014498 return -ENODEV;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000014499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014502 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014503 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504 }
14505 }
14506
Matt Carlsona6b68da2010-12-06 08:28:52 +000014507 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches41535772013-02-16 11:20:04 +000014508 (tg3_asic_rev(tp) == ASIC_REV_5719 ||
14509 tg3_asic_rev(tp) == ASIC_REV_5720 ||
14510 tg3_asic_rev(tp) == ASIC_REV_5762 ||
14511 (tg3_asic_rev(tp) == ASIC_REV_5717 &&
14512 tg3_chip_rev_id(tp) != CHIPREV_ID_5717_A0) ||
14513 (tg3_asic_rev(tp) == ASIC_REV_57765 &&
14514 tg3_chip_rev_id(tp) != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014515 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14516
Matt Carlsone256f8a2011-03-09 16:58:24 +000014517 tg3_phy_init_link_config(tp);
14518
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014519 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014520 !tg3_flag(tp, ENABLE_APE) &&
14521 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014522 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523
14524 tg3_readphy(tp, MII_BMSR, &bmsr);
14525 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14526 (bmsr & BMSR_LSTATUS))
14527 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014528
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 err = tg3_phy_reset(tp);
14530 if (err)
14531 return err;
14532
Matt Carlson42b64a42011-05-19 12:12:49 +000014533 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014535 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014536 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14537 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014538
14539 tg3_writephy(tp, MII_BMCR,
14540 BMCR_ANENABLE | BMCR_ANRESTART);
14541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014542 }
14543
14544skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014545 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014546 err = tg3_init_5401phy_dsp(tp);
14547 if (err)
14548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014549
Linus Torvalds1da177e2005-04-16 15:20:36 -070014550 err = tg3_init_5401phy_dsp(tp);
14551 }
14552
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 return err;
14554}
14555
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014556static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014558 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014559 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014560 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014561 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014562
Matt Carlson535a4902011-07-20 10:20:56 +000014563 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014564 if (!vpd_data)
14565 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014566
Matt Carlson535a4902011-07-20 10:20:56 +000014567 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014568 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014569 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014570
14571 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14572 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14573 i += PCI_VPD_LRDT_TAG_SIZE;
14574
Matt Carlson535a4902011-07-20 10:20:56 +000014575 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014576 goto out_not_found;
14577
Matt Carlson184b8902010-04-05 10:19:25 +000014578 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14579 PCI_VPD_RO_KEYWORD_MFR_ID);
14580 if (j > 0) {
14581 len = pci_vpd_info_field_size(&vpd_data[j]);
14582
14583 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14584 if (j + len > block_end || len != 4 ||
14585 memcmp(&vpd_data[j], "1028", 4))
14586 goto partno;
14587
14588 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14589 PCI_VPD_RO_KEYWORD_VENDOR0);
14590 if (j < 0)
14591 goto partno;
14592
14593 len = pci_vpd_info_field_size(&vpd_data[j]);
14594
14595 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14596 if (j + len > block_end)
14597 goto partno;
14598
14599 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014600 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014601 }
14602
14603partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014604 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14605 PCI_VPD_RO_KEYWORD_PARTNO);
14606 if (i < 0)
14607 goto out_not_found;
14608
14609 len = pci_vpd_info_field_size(&vpd_data[i]);
14610
14611 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14612 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014613 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014614 goto out_not_found;
14615
14616 memcpy(tp->board_part_number, &vpd_data[i], len);
14617
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014619 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014620 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014621 return;
14622
14623out_no_vpd:
Joe Perches41535772013-02-16 11:20:04 +000014624 if (tg3_asic_rev(tp) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014625 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14626 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014627 strcpy(tp->board_part_number, "BCM5717");
14628 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14629 strcpy(tp->board_part_number, "BCM5718");
14630 else
14631 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014632 } else if (tg3_asic_rev(tp) == ASIC_REV_57780) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014633 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14634 strcpy(tp->board_part_number, "BCM57780");
14635 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14636 strcpy(tp->board_part_number, "BCM57760");
14637 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14638 strcpy(tp->board_part_number, "BCM57790");
14639 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14640 strcpy(tp->board_part_number, "BCM57788");
14641 else
14642 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014643 } else if (tg3_asic_rev(tp) == ASIC_REV_57765) {
Matt Carlson37a949c2010-09-30 10:34:33 +000014644 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14645 strcpy(tp->board_part_number, "BCM57761");
14646 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14647 strcpy(tp->board_part_number, "BCM57765");
14648 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14649 strcpy(tp->board_part_number, "BCM57781");
14650 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14651 strcpy(tp->board_part_number, "BCM57785");
14652 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14653 strcpy(tp->board_part_number, "BCM57791");
14654 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14655 strcpy(tp->board_part_number, "BCM57795");
14656 else
14657 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014658 } else if (tg3_asic_rev(tp) == ASIC_REV_57766) {
Matt Carlson55086ad2011-12-14 11:09:59 +000014659 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14660 strcpy(tp->board_part_number, "BCM57762");
14661 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14662 strcpy(tp->board_part_number, "BCM57766");
14663 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14664 strcpy(tp->board_part_number, "BCM57782");
14665 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14666 strcpy(tp->board_part_number, "BCM57786");
14667 else
14668 goto nomatch;
Joe Perches41535772013-02-16 11:20:04 +000014669 } else if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014670 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014671 } else {
14672nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014673 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675}
14676
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014677static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014678{
14679 u32 val;
14680
Matt Carlsone4f34112009-02-25 14:25:00 +000014681 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014682 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014683 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014684 val != 0)
14685 return 0;
14686
14687 return 1;
14688}
14689
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014690static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014691{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014692 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014693 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014694 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014695
14696 if (tg3_nvram_read(tp, 0xc, &offset) ||
14697 tg3_nvram_read(tp, 0x4, &start))
14698 return;
14699
14700 offset = tg3_nvram_logical_addr(tp, offset);
14701
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014702 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014703 return;
14704
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014705 if ((val & 0xfc000000) == 0x0c000000) {
14706 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014707 return;
14708
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014709 if (val == 0)
14710 newver = true;
14711 }
14712
Matt Carlson75f99362010-04-05 10:19:24 +000014713 dst_off = strlen(tp->fw_ver);
14714
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014715 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014716 if (TG3_VER_SIZE - dst_off < 16 ||
14717 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014718 return;
14719
14720 offset = offset + ver_offset - start;
14721 for (i = 0; i < 16; i += 4) {
14722 __be32 v;
14723 if (tg3_nvram_read_be32(tp, offset + i, &v))
14724 return;
14725
Matt Carlson75f99362010-04-05 10:19:24 +000014726 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014727 }
14728 } else {
14729 u32 major, minor;
14730
14731 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14732 return;
14733
14734 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14735 TG3_NVM_BCVER_MAJSFT;
14736 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014737 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14738 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014739 }
14740}
14741
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014742static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014743{
14744 u32 val, major, minor;
14745
14746 /* Use native endian representation */
14747 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14748 return;
14749
14750 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14751 TG3_NVM_HWSB_CFG1_MAJSFT;
14752 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14753 TG3_NVM_HWSB_CFG1_MINSFT;
14754
14755 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14756}
14757
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014758static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014759{
14760 u32 offset, major, minor, build;
14761
Matt Carlson75f99362010-04-05 10:19:24 +000014762 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014763
14764 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14765 return;
14766
14767 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14768 case TG3_EEPROM_SB_REVISION_0:
14769 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14770 break;
14771 case TG3_EEPROM_SB_REVISION_2:
14772 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14773 break;
14774 case TG3_EEPROM_SB_REVISION_3:
14775 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14776 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014777 case TG3_EEPROM_SB_REVISION_4:
14778 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14779 break;
14780 case TG3_EEPROM_SB_REVISION_5:
14781 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14782 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014783 case TG3_EEPROM_SB_REVISION_6:
14784 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14785 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014786 default:
14787 return;
14788 }
14789
Matt Carlsone4f34112009-02-25 14:25:00 +000014790 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014791 return;
14792
14793 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14794 TG3_EEPROM_SB_EDH_BLD_SHFT;
14795 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14796 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14797 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14798
14799 if (minor > 99 || build > 26)
14800 return;
14801
Matt Carlson75f99362010-04-05 10:19:24 +000014802 offset = strlen(tp->fw_ver);
14803 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14804 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014805
14806 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014807 offset = strlen(tp->fw_ver);
14808 if (offset < TG3_VER_SIZE - 1)
14809 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014810 }
14811}
14812
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014813static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014814{
14815 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014816 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014817
14818 for (offset = TG3_NVM_DIR_START;
14819 offset < TG3_NVM_DIR_END;
14820 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014821 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014822 return;
14823
14824 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14825 break;
14826 }
14827
14828 if (offset == TG3_NVM_DIR_END)
14829 return;
14830
Joe Perches63c3a662011-04-26 08:12:10 +000014831 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014832 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014833 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014834 return;
14835
Matt Carlsone4f34112009-02-25 14:25:00 +000014836 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014837 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014838 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014839 return;
14840
14841 offset += val - start;
14842
Matt Carlsonacd9c112009-02-25 14:26:33 +000014843 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014844
Matt Carlsonacd9c112009-02-25 14:26:33 +000014845 tp->fw_ver[vlen++] = ',';
14846 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014847
14848 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014849 __be32 v;
14850 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014851 return;
14852
Al Virob9fc7dc2007-12-17 22:59:57 -080014853 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014854
Matt Carlsonacd9c112009-02-25 14:26:33 +000014855 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14856 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014857 break;
14858 }
14859
Matt Carlsonacd9c112009-02-25 14:26:33 +000014860 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14861 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014862 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014863}
14864
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014865static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014866{
Matt Carlson7fd76442009-02-25 14:27:20 +000014867 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014868
14869 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14870 if (apedata != APE_SEG_SIG_MAGIC)
14871 return;
14872
14873 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14874 if (!(apedata & APE_FW_STATUS_READY))
14875 return;
14876
Michael Chan165f4d12012-07-16 16:23:59 +000014877 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14878 tg3_flag_set(tp, APE_HAS_NCSI);
14879}
14880
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014881static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014882{
14883 int vlen;
14884 u32 apedata;
14885 char *fwtype;
14886
Matt Carlson7fd76442009-02-25 14:27:20 +000014887 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14888
Michael Chan165f4d12012-07-16 16:23:59 +000014889 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014890 fwtype = "NCSI";
Michael Chanc86a8562013-01-06 12:51:08 +000014891 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725)
14892 fwtype = "SMASH";
Michael Chan165f4d12012-07-16 16:23:59 +000014893 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014894 fwtype = "DASH";
14895
Matt Carlson7fd76442009-02-25 14:27:20 +000014896 vlen = strlen(tp->fw_ver);
14897
Matt Carlsonecc79642010-08-02 11:26:01 +000014898 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14899 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014900 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14901 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14902 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14903 (apedata & APE_FW_VERSION_BLDMSK));
14904}
14905
Michael Chanc86a8562013-01-06 12:51:08 +000014906static void tg3_read_otp_ver(struct tg3 *tp)
14907{
14908 u32 val, val2;
14909
Joe Perches41535772013-02-16 11:20:04 +000014910 if (tg3_asic_rev(tp) != ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000014911 return;
14912
14913 if (!tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0, &val) &&
14914 !tg3_ape_otp_read(tp, OTP_ADDRESS_MAGIC0 + 4, &val2) &&
14915 TG3_OTP_MAGIC0_VALID(val)) {
14916 u64 val64 = (u64) val << 32 | val2;
14917 u32 ver = 0;
14918 int i, vlen;
14919
14920 for (i = 0; i < 7; i++) {
14921 if ((val64 & 0xff) == 0)
14922 break;
14923 ver = val64 & 0xff;
14924 val64 >>= 8;
14925 }
14926 vlen = strlen(tp->fw_ver);
14927 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " .%02d", ver);
14928 }
14929}
14930
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014931static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014932{
14933 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014934 bool vpd_vers = false;
14935
14936 if (tp->fw_ver[0] != 0)
14937 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014938
Joe Perches63c3a662011-04-26 08:12:10 +000014939 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014940 strcat(tp->fw_ver, "sb");
Michael Chanc86a8562013-01-06 12:51:08 +000014941 tg3_read_otp_ver(tp);
Matt Carlsondf259d82009-04-20 06:57:14 +000014942 return;
14943 }
14944
Matt Carlsonacd9c112009-02-25 14:26:33 +000014945 if (tg3_nvram_read(tp, 0, &val))
14946 return;
14947
14948 if (val == TG3_EEPROM_MAGIC)
14949 tg3_read_bc_ver(tp);
14950 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14951 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014952 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14953 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014954
Michael Chan165f4d12012-07-16 16:23:59 +000014955 if (tg3_flag(tp, ENABLE_ASF)) {
14956 if (tg3_flag(tp, ENABLE_APE)) {
14957 tg3_probe_ncsi(tp);
14958 if (!vpd_vers)
14959 tg3_read_dash_ver(tp);
14960 } else if (!vpd_vers) {
14961 tg3_read_mgmtfw_ver(tp);
14962 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014963 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014964
14965 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014966}
14967
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014968static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14969{
Joe Perches63c3a662011-04-26 08:12:10 +000014970 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014971 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014972 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014973 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014974 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014975 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014976}
14977
Matt Carlson41434702011-03-09 16:58:22 +000014978static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014979 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14980 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14981 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14982 { },
14983};
14984
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014985static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014986{
14987 struct pci_dev *peer;
14988 unsigned int func, devnr = tp->pdev->devfn & ~7;
14989
14990 for (func = 0; func < 8; func++) {
14991 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14992 if (peer && peer != tp->pdev)
14993 break;
14994 pci_dev_put(peer);
14995 }
14996 /* 5704 can be configured in single-port mode, set peer to
14997 * tp->pdev in that case.
14998 */
14999 if (!peer) {
15000 peer = tp->pdev;
15001 return peer;
15002 }
15003
15004 /*
15005 * We don't need to keep the refcount elevated; there's no way
15006 * to remove one half of this device without removing the other
15007 */
15008 pci_dev_put(peer);
15009
15010 return peer;
15011}
15012
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015013static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000015014{
15015 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
Joe Perches41535772013-02-16 11:20:04 +000015016 if (tg3_asic_rev(tp) == ASIC_REV_USE_PROD_ID_REG) {
Matt Carlson42b123b2012-02-13 15:20:13 +000015017 u32 reg;
15018
15019 /* All devices that use the alternate
15020 * ASIC REV location have a CPMU.
15021 */
15022 tg3_flag_set(tp, CPMU_PRESENT);
15023
15024 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000015025 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015026 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15027 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000015028 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
15029 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
15030 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
15031 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727)
Matt Carlson42b123b2012-02-13 15:20:13 +000015032 reg = TG3PCI_GEN2_PRODID_ASICREV;
15033 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
15034 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
15035 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
15036 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
15037 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
15038 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
15039 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
15040 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
15041 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
15042 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
15043 reg = TG3PCI_GEN15_PRODID_ASICREV;
15044 else
15045 reg = TG3PCI_PRODID_ASICREV;
15046
15047 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
15048 }
15049
15050 /* Wrong chip ID in 5752 A0. This code can be removed later
15051 * as A0 is not in production.
15052 */
Joe Perches41535772013-02-16 11:20:04 +000015053 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5752_A0_HW)
Matt Carlson42b123b2012-02-13 15:20:13 +000015054 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
15055
Joe Perches41535772013-02-16 11:20:04 +000015056 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_C0)
Michael Chan79d49692012-11-05 14:26:29 +000015057 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
15058
Joe Perches41535772013-02-16 11:20:04 +000015059 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15060 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15061 tg3_asic_rev(tp) == ASIC_REV_5720)
Matt Carlson42b123b2012-02-13 15:20:13 +000015062 tg3_flag_set(tp, 5717_PLUS);
15063
Joe Perches41535772013-02-16 11:20:04 +000015064 if (tg3_asic_rev(tp) == ASIC_REV_57765 ||
15065 tg3_asic_rev(tp) == ASIC_REV_57766)
Matt Carlson42b123b2012-02-13 15:20:13 +000015066 tg3_flag_set(tp, 57765_CLASS);
15067
Michael Chanc65a17f2013-01-06 12:51:07 +000015068 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015069 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlson42b123b2012-02-13 15:20:13 +000015070 tg3_flag_set(tp, 57765_PLUS);
15071
15072 /* Intentionally exclude ASIC_REV_5906 */
Joe Perches41535772013-02-16 11:20:04 +000015073 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15074 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15075 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15076 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15077 tg3_asic_rev(tp) == ASIC_REV_5785 ||
15078 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015079 tg3_flag(tp, 57765_PLUS))
15080 tg3_flag_set(tp, 5755_PLUS);
15081
Joe Perches41535772013-02-16 11:20:04 +000015082 if (tg3_asic_rev(tp) == ASIC_REV_5780 ||
15083 tg3_asic_rev(tp) == ASIC_REV_5714)
Matt Carlson42b123b2012-02-13 15:20:13 +000015084 tg3_flag_set(tp, 5780_CLASS);
15085
Joe Perches41535772013-02-16 11:20:04 +000015086 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15087 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15088 tg3_asic_rev(tp) == ASIC_REV_5906 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015089 tg3_flag(tp, 5755_PLUS) ||
15090 tg3_flag(tp, 5780_CLASS))
15091 tg3_flag_set(tp, 5750_PLUS);
15092
Joe Perches41535772013-02-16 11:20:04 +000015093 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
Matt Carlson42b123b2012-02-13 15:20:13 +000015094 tg3_flag(tp, 5750_PLUS))
15095 tg3_flag_set(tp, 5705_PLUS);
15096}
15097
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015098static bool tg3_10_100_only_device(struct tg3 *tp,
15099 const struct pci_device_id *ent)
15100{
15101 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
15102
Joe Perches41535772013-02-16 11:20:04 +000015103 if ((tg3_asic_rev(tp) == ASIC_REV_5703 &&
15104 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015105 (tp->phy_flags & TG3_PHYFLG_IS_FET))
15106 return true;
15107
15108 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
Joe Perches41535772013-02-16 11:20:04 +000015109 if (tg3_asic_rev(tp) == ASIC_REV_5705) {
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015110 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
15111 return true;
15112 } else {
15113 return true;
15114 }
15115 }
15116
15117 return false;
15118}
15119
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000015120static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015121{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015123 u32 pci_state_reg, grc_misc_cfg;
15124 u32 val;
15125 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127
Linus Torvalds1da177e2005-04-16 15:20:36 -070015128 /* Force memory write invalidate off. If we leave it on,
15129 * then on 5700_BX chips we have to enable a workaround.
15130 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
15131 * to match the cacheline size. The Broadcom driver have this
15132 * workaround but turns MWI off all the times so never uses
15133 * it. This seems to suggest that the workaround is insufficient.
15134 */
15135 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15136 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
15137 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15138
Matt Carlson16821282011-07-13 09:27:28 +000015139 /* Important! -- Make sure register accesses are byteswapped
15140 * correctly. Also, for those chips that require it, make
15141 * sure that indirect register accesses are enabled before
15142 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015143 */
15144 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15145 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000015146 tp->misc_host_ctrl |= (misc_ctrl_reg &
15147 MISC_HOST_CTRL_CHIPREV);
15148 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15149 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150
Matt Carlson42b123b2012-02-13 15:20:13 +000015151 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070015152
Michael Chan68929142005-08-09 20:17:14 -070015153 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
15154 * we need to disable memory and use config. cycles
15155 * only to access all registers. The 5702/03 chips
15156 * can mistakenly decode the special cycles from the
15157 * ICH chipsets as memory write cycles, causing corruption
15158 * of register and memory space. Only certain ICH bridges
15159 * will drive special cycles with non-zero data during the
15160 * address phase which can fall within the 5703's address
15161 * range. This is not an ICH bug as the PCI spec allows
15162 * non-zero address during special cycles. However, only
15163 * these ICH bridges are known to drive non-zero addresses
15164 * during special cycles.
15165 *
15166 * Since special cycles do not cross PCI bridges, we only
15167 * enable this workaround if the 5703 is on the secondary
15168 * bus of these ICH bridges.
15169 */
Joe Perches41535772013-02-16 11:20:04 +000015170 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A1) ||
15171 (tg3_chip_rev_id(tp) == CHIPREV_ID_5703_A2)) {
Michael Chan68929142005-08-09 20:17:14 -070015172 static struct tg3_dev_id {
15173 u32 vendor;
15174 u32 device;
15175 u32 rev;
15176 } ich_chipsets[] = {
15177 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
15178 PCI_ANY_ID },
15179 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
15180 PCI_ANY_ID },
15181 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
15182 0xa },
15183 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
15184 PCI_ANY_ID },
15185 { },
15186 };
15187 struct tg3_dev_id *pci_id = &ich_chipsets[0];
15188 struct pci_dev *bridge = NULL;
15189
15190 while (pci_id->vendor != 0) {
15191 bridge = pci_get_device(pci_id->vendor, pci_id->device,
15192 bridge);
15193 if (!bridge) {
15194 pci_id++;
15195 continue;
15196 }
15197 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070015198 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070015199 continue;
15200 }
15201 if (bridge->subordinate &&
15202 (bridge->subordinate->number ==
15203 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015204 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070015205 pci_dev_put(bridge);
15206 break;
15207 }
15208 }
15209 }
15210
Joe Perches41535772013-02-16 11:20:04 +000015211 if (tg3_asic_rev(tp) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070015212 static struct tg3_dev_id {
15213 u32 vendor;
15214 u32 device;
15215 } bridge_chipsets[] = {
15216 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
15217 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
15218 { },
15219 };
15220 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
15221 struct pci_dev *bridge = NULL;
15222
15223 while (pci_id->vendor != 0) {
15224 bridge = pci_get_device(pci_id->vendor,
15225 pci_id->device,
15226 bridge);
15227 if (!bridge) {
15228 pci_id++;
15229 continue;
15230 }
15231 if (bridge->subordinate &&
15232 (bridge->subordinate->number <=
15233 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015234 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070015235 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015236 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070015237 pci_dev_put(bridge);
15238 break;
15239 }
15240 }
15241 }
15242
Michael Chan4a29cc22006-03-19 13:21:12 -080015243 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15244 * DMA addresses > 40-bit. This bridge may have other additional
15245 * 57xx devices behind it in some 4-port NIC designs for example.
15246 * Any tg3 device found behind the bridge will also need the 40-bit
15247 * DMA workaround.
15248 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015249 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015250 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015251 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015252 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015253 struct pci_dev *bridge = NULL;
15254
15255 do {
15256 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15257 PCI_DEVICE_ID_SERVERWORKS_EPB,
15258 bridge);
15259 if (bridge && bridge->subordinate &&
15260 (bridge->subordinate->number <=
15261 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015262 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015263 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015264 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015265 pci_dev_put(bridge);
15266 break;
15267 }
15268 } while (bridge);
15269 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015270
Joe Perches41535772013-02-16 11:20:04 +000015271 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
15272 tg3_asic_rev(tp) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015273 tp->pdev_peer = tg3_find_peer(tp);
15274
Matt Carlson507399f2009-11-13 13:03:37 +000015275 /* Determine TSO capabilities */
Joe Perches41535772013-02-16 11:20:04 +000015276 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015277 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015278 else if (tg3_flag(tp, 57765_PLUS))
15279 tg3_flag_set(tp, HW_TSO_3);
15280 else if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015281 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015282 tg3_flag_set(tp, HW_TSO_2);
15283 else if (tg3_flag(tp, 5750_PLUS)) {
15284 tg3_flag_set(tp, HW_TSO_1);
15285 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015286 if (tg3_asic_rev(tp) == ASIC_REV_5750 &&
15287 tg3_chip_rev_id(tp) >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015288 tg3_flag_clear(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015289 } else if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
15290 tg3_asic_rev(tp) != ASIC_REV_5701 &&
15291 tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015292 tg3_flag_set(tp, TSO_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015293 if (tg3_asic_rev(tp) == ASIC_REV_5705)
Matt Carlson507399f2009-11-13 13:03:37 +000015294 tp->fw_needed = FIRMWARE_TG3TSO5;
15295 else
15296 tp->fw_needed = FIRMWARE_TG3TSO;
15297 }
15298
Matt Carlsondabc5c62011-05-19 12:12:52 +000015299 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015300 if (tg3_flag(tp, HW_TSO_1) ||
15301 tg3_flag(tp, HW_TSO_2) ||
15302 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015303 tp->fw_needed) {
15304 /* For firmware TSO, assume ASF is disabled.
15305 * We'll disable TSO later if we discover ASF
15306 * is enabled in tg3_get_eeprom_hw_cfg().
15307 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015308 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015309 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015310 tg3_flag_clear(tp, TSO_CAPABLE);
15311 tg3_flag_clear(tp, TSO_BUG);
15312 tp->fw_needed = NULL;
15313 }
15314
Joe Perches41535772013-02-16 11:20:04 +000015315 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0)
Matt Carlsondabc5c62011-05-19 12:12:52 +000015316 tp->fw_needed = FIRMWARE_TG3;
15317
Matt Carlson507399f2009-11-13 13:03:37 +000015318 tp->irq_max = 1;
15319
Joe Perches63c3a662011-04-26 08:12:10 +000015320 if (tg3_flag(tp, 5750_PLUS)) {
15321 tg3_flag_set(tp, SUPPORT_MSI);
Joe Perches41535772013-02-16 11:20:04 +000015322 if (tg3_chip_rev(tp) == CHIPREV_5750_AX ||
15323 tg3_chip_rev(tp) == CHIPREV_5750_BX ||
15324 (tg3_asic_rev(tp) == ASIC_REV_5714 &&
15325 tg3_chip_rev_id(tp) <= CHIPREV_ID_5714_A2 &&
Michael Chan7544b092007-05-05 13:08:32 -070015326 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015328
Joe Perches63c3a662011-04-26 08:12:10 +000015329 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015330 tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015331 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333
Joe Perches63c3a662011-04-26 08:12:10 +000015334 if (tg3_flag(tp, 57765_PLUS)) {
15335 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015336 tp->irq_max = TG3_IRQ_MAX_VECS;
15337 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015338 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015339
Michael Chan91024262012-09-28 07:12:38 +000015340 tp->txq_max = 1;
15341 tp->rxq_max = 1;
15342 if (tp->irq_max > 1) {
15343 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15344 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15345
Joe Perches41535772013-02-16 11:20:04 +000015346 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
15347 tg3_asic_rev(tp) == ASIC_REV_5720)
Michael Chan91024262012-09-28 07:12:38 +000015348 tp->txq_max = tp->irq_max - 1;
15349 }
15350
Matt Carlsonb7abee62012-06-07 12:56:54 +000015351 if (tg3_flag(tp, 5755_PLUS) ||
Joe Perches41535772013-02-16 11:20:04 +000015352 tg3_asic_rev(tp) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015353 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015354
Joe Perches41535772013-02-16 11:20:04 +000015355 if (tg3_asic_rev(tp) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015356 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015357
Joe Perches41535772013-02-16 11:20:04 +000015358 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15359 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15360 tg3_asic_rev(tp) == ASIC_REV_5720 ||
15361 tg3_asic_rev(tp) == ASIC_REV_5762)
Joe Perches63c3a662011-04-26 08:12:10 +000015362 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015363
Joe Perches63c3a662011-04-26 08:12:10 +000015364 if (tg3_flag(tp, 57765_PLUS) &&
Joe Perches41535772013-02-16 11:20:04 +000015365 tg3_chip_rev_id(tp) != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015366 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015367
Joe Perches63c3a662011-04-26 08:12:10 +000015368 if (!tg3_flag(tp, 5705_PLUS) ||
15369 tg3_flag(tp, 5780_CLASS) ||
15370 tg3_flag(tp, USE_JUMBO_BDFLAG))
15371 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015372
Matt Carlson52f44902008-11-21 17:17:04 -080015373 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15374 &pci_state_reg);
15375
Jon Mason708ebb3a2011-06-27 12:56:50 +000015376 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015377 u16 lnkctl;
15378
Joe Perches63c3a662011-04-26 08:12:10 +000015379 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015380
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015381 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015382 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Joe Perches41535772013-02-16 11:20:04 +000015383 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015384 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015385 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015386 }
Joe Perches41535772013-02-16 11:20:04 +000015387 if (tg3_asic_rev(tp) == ASIC_REV_5784 ||
15388 tg3_asic_rev(tp) == ASIC_REV_5761 ||
15389 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A0 ||
15390 tg3_chip_rev_id(tp) == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015391 tg3_flag_set(tp, CLKREQ_BUG);
Joe Perches41535772013-02-16 11:20:04 +000015392 } else if (tg3_chip_rev_id(tp) == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015393 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015394 }
Joe Perches41535772013-02-16 11:20:04 +000015395 } else if (tg3_asic_rev(tp) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015396 /* BCM5785 devices are effectively PCIe devices, and should
15397 * follow PCIe codepaths, but do not have a PCIe capabilities
15398 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015399 */
Joe Perches63c3a662011-04-26 08:12:10 +000015400 tg3_flag_set(tp, PCI_EXPRESS);
15401 } else if (!tg3_flag(tp, 5705_PLUS) ||
15402 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015403 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15404 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015405 dev_err(&tp->pdev->dev,
15406 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015407 return -EIO;
15408 }
15409
15410 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015411 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015413
Michael Chan399de502005-10-03 14:02:39 -070015414 /* If we have an AMD 762 or VIA K8T800 chipset, write
15415 * reordering to the mailbox registers done by the host
15416 * controller can cause major troubles. We read back from
15417 * every mailbox register write to force the writes to be
15418 * posted to the chip in order.
15419 */
Matt Carlson41434702011-03-09 16:58:22 +000015420 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015421 !tg3_flag(tp, PCI_EXPRESS))
15422 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015423
Matt Carlson69fc4052008-12-21 20:19:57 -080015424 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15425 &tp->pci_cacheline_sz);
15426 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15427 &tp->pci_lat_timer);
Joe Perches41535772013-02-16 11:20:04 +000015428 if (tg3_asic_rev(tp) == ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015429 tp->pci_lat_timer < 64) {
15430 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015431 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15432 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433 }
15434
Matt Carlson16821282011-07-13 09:27:28 +000015435 /* Important! -- It is critical that the PCI-X hw workaround
15436 * situation is decided before the first MMIO register access.
15437 */
Joe Perches41535772013-02-16 11:20:04 +000015438 if (tg3_chip_rev(tp) == CHIPREV_5700_BX) {
Matt Carlson52f44902008-11-21 17:17:04 -080015439 /* 5700 BX chips need to have their TX producer index
15440 * mailboxes written twice to workaround a bug.
15441 */
Joe Perches63c3a662011-04-26 08:12:10 +000015442 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015443
Matt Carlson52f44902008-11-21 17:17:04 -080015444 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445 *
15446 * The workaround is to use indirect register accesses
15447 * for all chip writes not to mailbox registers.
15448 */
Joe Perches63c3a662011-04-26 08:12:10 +000015449 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015450 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451
Joe Perches63c3a662011-04-26 08:12:10 +000015452 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453
15454 /* The chip can have it's power management PCI config
15455 * space registers clobbered due to this bug.
15456 * So explicitly force the chip into D0 here.
15457 */
Matt Carlson9974a352007-10-07 23:27:28 -070015458 pci_read_config_dword(tp->pdev,
15459 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015460 &pm_reg);
15461 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15462 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015463 pci_write_config_dword(tp->pdev,
15464 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 pm_reg);
15466
15467 /* Also, force SERR#/PERR# in PCI command. */
15468 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15469 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15470 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15471 }
15472 }
15473
Linus Torvalds1da177e2005-04-16 15:20:36 -070015474 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015475 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015477 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478
15479 /* Chip-specific fixup from Broadcom driver */
Joe Perches41535772013-02-16 11:20:04 +000015480 if ((tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015481 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15482 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15483 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15484 }
15485
Michael Chan1ee582d2005-08-09 20:16:46 -070015486 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015487 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015488 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015489 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015490 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015491 tp->write32_tx_mbox = tg3_write32;
15492 tp->write32_rx_mbox = tg3_write32;
15493
15494 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015495 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015496 tp->write32 = tg3_write_indirect_reg32;
Joe Perches41535772013-02-16 11:20:04 +000015497 else if (tg3_asic_rev(tp) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015498 (tg3_flag(tp, PCI_EXPRESS) &&
Joe Perches41535772013-02-16 11:20:04 +000015499 tg3_chip_rev_id(tp) == CHIPREV_ID_5750_A0)) {
Matt Carlson98efd8a2007-05-05 12:47:25 -070015500 /*
15501 * Back to back register writes can cause problems on these
15502 * chips, the workaround is to read back all reg writes
15503 * except those to mailbox regs.
15504 *
15505 * See tg3_write_indirect_reg32().
15506 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015507 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015508 }
15509
Joe Perches63c3a662011-04-26 08:12:10 +000015510 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015511 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015512 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015513 tp->write32_rx_mbox = tg3_write_flush_reg32;
15514 }
Michael Chan20094932005-08-09 20:16:32 -070015515
Joe Perches63c3a662011-04-26 08:12:10 +000015516 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015517 tp->read32 = tg3_read_indirect_reg32;
15518 tp->write32 = tg3_write_indirect_reg32;
15519 tp->read32_mbox = tg3_read_indirect_mbox;
15520 tp->write32_mbox = tg3_write_indirect_mbox;
15521 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15522 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15523
15524 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015525 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015526
15527 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15528 pci_cmd &= ~PCI_COMMAND_MEMORY;
15529 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15530 }
Joe Perches41535772013-02-16 11:20:04 +000015531 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070015532 tp->read32_mbox = tg3_read32_mbox_5906;
15533 tp->write32_mbox = tg3_write32_mbox_5906;
15534 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15535 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15536 }
Michael Chan68929142005-08-09 20:17:14 -070015537
Michael Chanbbadf502006-04-06 21:46:34 -070015538 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015539 (tg3_flag(tp, PCIX_MODE) &&
Joe Perches41535772013-02-16 11:20:04 +000015540 (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15541 tg3_asic_rev(tp) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015542 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015543
Matt Carlson16821282011-07-13 09:27:28 +000015544 /* The memory arbiter has to be enabled in order for SRAM accesses
15545 * to succeed. Normally on powerup the tg3 chip firmware will make
15546 * sure it is enabled, but other entities such as system netboot
15547 * code might disable it.
15548 */
15549 val = tr32(MEMARB_MODE);
15550 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15551
Matt Carlson9dc5e342011-11-04 09:15:02 +000015552 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
Joe Perches41535772013-02-16 11:20:04 +000015553 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Matt Carlson9dc5e342011-11-04 09:15:02 +000015554 tg3_flag(tp, 5780_CLASS)) {
15555 if (tg3_flag(tp, PCIX_MODE)) {
15556 pci_read_config_dword(tp->pdev,
15557 tp->pcix_cap + PCI_X_STATUS,
15558 &val);
15559 tp->pci_fn = val & 0x7;
15560 }
Joe Perches41535772013-02-16 11:20:04 +000015561 } else if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15562 tg3_asic_rev(tp) == ASIC_REV_5719 ||
15563 tg3_asic_rev(tp) == ASIC_REV_5720) {
Matt Carlson9dc5e342011-11-04 09:15:02 +000015564 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
Michael Chan857001f2013-01-06 12:51:09 +000015565 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) != NIC_SRAM_CPMUSTAT_SIG)
15566 val = tr32(TG3_CPMU_STATUS);
15567
Joe Perches41535772013-02-16 11:20:04 +000015568 if (tg3_asic_rev(tp) == ASIC_REV_5717)
Michael Chan857001f2013-01-06 12:51:09 +000015569 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5717) ? 1 : 0;
15570 else
Matt Carlson9dc5e342011-11-04 09:15:02 +000015571 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15572 TG3_CPMU_STATUS_FSHFT_5719;
Matt Carlson69f11c92011-07-13 09:27:30 +000015573 }
15574
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015575 if (tg3_flag(tp, FLUSH_POSTED_WRITES)) {
15576 tp->write32_tx_mbox = tg3_write_flush_reg32;
15577 tp->write32_rx_mbox = tg3_write_flush_reg32;
15578 }
15579
Michael Chan7d0c41e2005-04-21 17:06:20 -070015580 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015581 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015582 * determined before calling tg3_set_power_state() so that
15583 * we know whether or not to switch out of Vaux power.
15584 * When the flag is set, it means that GPIO1 is used for eeprom
15585 * write protect and also implies that it is a LOM where GPIOs
15586 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015587 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015588 tg3_get_eeprom_hw_cfg(tp);
15589
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015590 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15591 tg3_flag_clear(tp, TSO_CAPABLE);
15592 tg3_flag_clear(tp, TSO_BUG);
15593 tp->fw_needed = NULL;
15594 }
15595
Joe Perches63c3a662011-04-26 08:12:10 +000015596 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015597 /* Allow reads and writes to the
15598 * APE register and memory space.
15599 */
15600 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015601 PCISTATE_ALLOW_APE_SHMEM_WR |
15602 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015603 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15604 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015605
15606 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015607 }
15608
Matt Carlson16821282011-07-13 09:27:28 +000015609 /* Set up tp->grc_local_ctrl before calling
15610 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15611 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015612 * It is also used as eeprom write protect on LOMs.
15613 */
15614 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Joe Perches41535772013-02-16 11:20:04 +000015615 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015616 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015617 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15618 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015619 /* Unused GPIO3 must be driven as output on 5752 because there
15620 * are no pull-up resistors on unused GPIO pins.
15621 */
Joe Perches41535772013-02-16 11:20:04 +000015622 else if (tg3_asic_rev(tp) == ASIC_REV_5752)
Michael Chan3e7d83b2005-04-21 17:10:36 -070015623 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015624
Joe Perches41535772013-02-16 11:20:04 +000015625 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15626 tg3_asic_rev(tp) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015627 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015628 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15629
Matt Carlson8d519ab2009-04-20 06:58:01 +000015630 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15631 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015632 /* Turn off the debug UART. */
15633 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015634 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015635 /* Keep VMain power. */
15636 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15637 GRC_LCLCTRL_GPIO_OUTPUT0;
15638 }
15639
Joe Perches41535772013-02-16 11:20:04 +000015640 if (tg3_asic_rev(tp) == ASIC_REV_5762)
Michael Chanc86a8562013-01-06 12:51:08 +000015641 tp->grc_local_ctrl |=
15642 tr32(GRC_LOCAL_CTRL) & GRC_LCLCTRL_GPIO_UART_SEL;
15643
Matt Carlson16821282011-07-13 09:27:28 +000015644 /* Switch out of Vaux if it is a NIC */
15645 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646
Linus Torvalds1da177e2005-04-16 15:20:36 -070015647 /* Derive initial jumbo mode from MTU assigned in
15648 * ether_setup() via the alloc_etherdev() call
15649 */
Joe Perches63c3a662011-04-26 08:12:10 +000015650 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15651 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015652
15653 /* Determine WakeOnLan speed to use. */
Joe Perches41535772013-02-16 11:20:04 +000015654 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15655 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15656 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15657 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015658 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015659 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015660 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015661 }
15662
Joe Perches41535772013-02-16 11:20:04 +000015663 if (tg3_asic_rev(tp) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015664 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015665
Linus Torvalds1da177e2005-04-16 15:20:36 -070015666 /* A few boards don't want Ethernet@WireSpeed phy feature */
Joe Perches41535772013-02-16 11:20:04 +000015667 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
15668 (tg3_asic_rev(tp) == ASIC_REV_5705 &&
15669 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A0) &&
15670 (tg3_chip_rev_id(tp) != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015671 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15672 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15673 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015674
Joe Perches41535772013-02-16 11:20:04 +000015675 if (tg3_chip_rev(tp) == CHIPREV_5703_AX ||
15676 tg3_chip_rev(tp) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015677 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Joe Perches41535772013-02-16 11:20:04 +000015678 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015679 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015680
Joe Perches63c3a662011-04-26 08:12:10 +000015681 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015682 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Joe Perches41535772013-02-16 11:20:04 +000015683 tg3_asic_rev(tp) != ASIC_REV_5785 &&
15684 tg3_asic_rev(tp) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015685 !tg3_flag(tp, 57765_PLUS)) {
Joe Perches41535772013-02-16 11:20:04 +000015686 if (tg3_asic_rev(tp) == ASIC_REV_5755 ||
15687 tg3_asic_rev(tp) == ASIC_REV_5787 ||
15688 tg3_asic_rev(tp) == ASIC_REV_5784 ||
15689 tg3_asic_rev(tp) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015690 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15691 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015692 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015693 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015694 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015695 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015696 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015698
Joe Perches41535772013-02-16 11:20:04 +000015699 if (tg3_asic_rev(tp) == ASIC_REV_5784 &&
15700 tg3_chip_rev(tp) != CHIPREV_5784_AX) {
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015701 tp->phy_otp = tg3_read_otp_phycfg(tp);
15702 if (tp->phy_otp == 0)
15703 tp->phy_otp = TG3_OTP_DEFAULT;
15704 }
15705
Joe Perches63c3a662011-04-26 08:12:10 +000015706 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015707 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15708 else
15709 tp->mi_mode = MAC_MI_MODE_BASE;
15710
Linus Torvalds1da177e2005-04-16 15:20:36 -070015711 tp->coalesce_mode = 0;
Joe Perches41535772013-02-16 11:20:04 +000015712 if (tg3_chip_rev(tp) != CHIPREV_5700_AX &&
15713 tg3_chip_rev(tp) != CHIPREV_5700_BX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15715
Matt Carlson4d958472011-04-20 07:57:35 +000015716 /* Set these bits to enable statistics workaround. */
Joe Perches41535772013-02-16 11:20:04 +000015717 if (tg3_asic_rev(tp) == ASIC_REV_5717 ||
15718 tg3_chip_rev_id(tp) == CHIPREV_ID_5719_A0 ||
15719 tg3_chip_rev_id(tp) == CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000015720 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15721 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15722 }
15723
Joe Perches41535772013-02-16 11:20:04 +000015724 if (tg3_asic_rev(tp) == ASIC_REV_5785 ||
15725 tg3_asic_rev(tp) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015726 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015727
Matt Carlson158d7ab2008-05-29 01:37:54 -070015728 err = tg3_mdio_init(tp);
15729 if (err)
15730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015731
15732 /* Initialize data/descriptor byte/word swapping. */
15733 val = tr32(GRC_MODE);
Joe Perches41535772013-02-16 11:20:04 +000015734 if (tg3_asic_rev(tp) == ASIC_REV_5720 ||
15735 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonf2096f92011-04-05 14:22:48 +000015736 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15737 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15738 GRC_MODE_B2HRX_ENABLE |
15739 GRC_MODE_HTX2B_ENABLE |
15740 GRC_MODE_HOST_STACKUP);
15741 else
15742 val &= GRC_MODE_HOST_STACKUP;
15743
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 tw32(GRC_MODE, val | tp->grc_mode);
15745
15746 tg3_switch_clocks(tp);
15747
15748 /* Clear this out for sanity. */
15749 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15750
15751 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15752 &pci_state_reg);
15753 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015754 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Joe Perches41535772013-02-16 11:20:04 +000015755 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5701_A0 ||
15756 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B0 ||
15757 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B2 ||
15758 tg3_chip_rev_id(tp) == CHIPREV_ID_5701_B5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015759 void __iomem *sram_base;
15760
15761 /* Write some dummy words into the SRAM status block
15762 * area, see if it reads back correctly. If the return
15763 * value is bad, force enable the PCIX workaround.
15764 */
15765 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15766
15767 writel(0x00000000, sram_base);
15768 writel(0x00000000, sram_base + 4);
15769 writel(0xffffffff, sram_base + 4);
15770 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015771 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772 }
15773 }
15774
15775 udelay(50);
15776 tg3_nvram_init(tp);
15777
15778 grc_misc_cfg = tr32(GRC_MISC_CFG);
15779 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15780
Joe Perches41535772013-02-16 11:20:04 +000015781 if (tg3_asic_rev(tp) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015782 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15783 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015784 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785
Joe Perches63c3a662011-04-26 08:12:10 +000015786 if (!tg3_flag(tp, IS_5788) &&
Joe Perches41535772013-02-16 11:20:04 +000015787 tg3_asic_rev(tp) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015788 tg3_flag_set(tp, TAGGED_STATUS);
15789 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015790 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15791 HOSTCC_MODE_CLRTICK_TXBD);
15792
15793 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15794 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15795 tp->misc_host_ctrl);
15796 }
15797
Matt Carlson3bda1252008-08-15 14:08:22 -070015798 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015799 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015800 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015801 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015802 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015803
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015804 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015805 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015806
15807 err = tg3_phy_probe(tp);
15808 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015809 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015810 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015811 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015812 }
15813
Matt Carlson184b8902010-04-05 10:19:25 +000015814 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015815 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015816
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015817 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15818 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015819 } else {
Joe Perches41535772013-02-16 11:20:04 +000015820 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015821 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015822 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015823 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015824 }
15825
15826 /* 5700 {AX,BX} chips have a broken status block link
15827 * change bit implementation, so we must use the
15828 * status register in those cases.
15829 */
Joe Perches41535772013-02-16 11:20:04 +000015830 if (tg3_asic_rev(tp) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015831 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015832 else
Joe Perches63c3a662011-04-26 08:12:10 +000015833 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015834
15835 /* The led_ctrl is set during tg3_phy_probe, here we might
15836 * have to force the link status polling mechanism based
15837 * upon subsystem IDs.
15838 */
15839 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Joe Perches41535772013-02-16 11:20:04 +000015840 tg3_asic_rev(tp) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015841 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15842 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015843 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015844 }
15845
15846 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015847 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015848 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015849 else
Joe Perches63c3a662011-04-26 08:12:10 +000015850 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015851
Eric Dumazet9205fd92011-11-18 06:47:01 +000015852 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015853 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Joe Perches41535772013-02-16 11:20:04 +000015854 if (tg3_asic_rev(tp) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015855 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015856 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015857#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015858 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015859#endif
15860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015861
Matt Carlson2c49a442010-09-30 10:34:35 +000015862 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15863 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015864 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15865
Matt Carlson2c49a442010-09-30 10:34:35 +000015866 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015867
15868 /* Increment the rx prod index on the rx std ring by at most
15869 * 8 for these chips to workaround hw errata.
15870 */
Joe Perches41535772013-02-16 11:20:04 +000015871 if (tg3_asic_rev(tp) == ASIC_REV_5750 ||
15872 tg3_asic_rev(tp) == ASIC_REV_5752 ||
15873 tg3_asic_rev(tp) == ASIC_REV_5755)
Michael Chanf92905d2006-06-29 20:14:29 -070015874 tp->rx_std_max_post = 8;
15875
Joe Perches63c3a662011-04-26 08:12:10 +000015876 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015877 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15878 PCIE_PWR_MGMT_L1_THRESH_MSK;
15879
Linus Torvalds1da177e2005-04-16 15:20:36 -070015880 return err;
15881}
15882
David S. Miller49b6e95f2007-03-29 01:38:42 -070015883#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015884static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015885{
15886 struct net_device *dev = tp->dev;
15887 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015888 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015889 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015890 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891
David S. Miller49b6e95f2007-03-29 01:38:42 -070015892 addr = of_get_property(dp, "local-mac-address", &len);
15893 if (addr && len == 6) {
15894 memcpy(dev->dev_addr, addr, 6);
David S. Miller49b6e95f2007-03-29 01:38:42 -070015895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896 }
15897 return -ENODEV;
15898}
15899
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015900static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015901{
15902 struct net_device *dev = tp->dev;
15903
15904 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
15905 return 0;
15906}
15907#endif
15908
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015909static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910{
15911 struct net_device *dev = tp->dev;
15912 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015913 int addr_ok = 0;
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015914 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015915
David S. Miller49b6e95f2007-03-29 01:38:42 -070015916#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015917 if (!tg3_get_macaddr_sparc(tp))
15918 return 0;
15919#endif
15920
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000015921 if (tg3_flag(tp, IS_SSB_CORE)) {
15922 err = ssb_gige_get_macaddr(tp->pdev, &dev->dev_addr[0]);
15923 if (!err && is_valid_ether_addr(&dev->dev_addr[0]))
15924 return 0;
15925 }
15926
Linus Torvalds1da177e2005-04-16 15:20:36 -070015927 mac_offset = 0x7c;
Joe Perches41535772013-02-16 11:20:04 +000015928 if (tg3_asic_rev(tp) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015929 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015930 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15931 mac_offset = 0xcc;
15932 if (tg3_nvram_lock(tp))
15933 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15934 else
15935 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015936 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015937 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015938 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015939 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015940 mac_offset += 0x18c;
Joe Perches41535772013-02-16 11:20:04 +000015941 } else if (tg3_asic_rev(tp) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015942 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015943
15944 /* First try to get it from MAC address mailbox. */
15945 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15946 if ((hi >> 16) == 0x484b) {
15947 dev->dev_addr[0] = (hi >> 8) & 0xff;
15948 dev->dev_addr[1] = (hi >> 0) & 0xff;
15949
15950 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15951 dev->dev_addr[2] = (lo >> 24) & 0xff;
15952 dev->dev_addr[3] = (lo >> 16) & 0xff;
15953 dev->dev_addr[4] = (lo >> 8) & 0xff;
15954 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015955
Michael Chan008652b2006-03-27 23:14:53 -080015956 /* Some old bootcode may report a 0 MAC address in SRAM */
15957 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15958 }
15959 if (!addr_ok) {
15960 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015961 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015962 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015963 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015964 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15965 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015966 }
15967 /* Finally just fetch it out of the MAC control regs. */
15968 else {
15969 hi = tr32(MAC_ADDR_0_HIGH);
15970 lo = tr32(MAC_ADDR_0_LOW);
15971
15972 dev->dev_addr[5] = lo & 0xff;
15973 dev->dev_addr[4] = (lo >> 8) & 0xff;
15974 dev->dev_addr[3] = (lo >> 16) & 0xff;
15975 dev->dev_addr[2] = (lo >> 24) & 0xff;
15976 dev->dev_addr[1] = hi & 0xff;
15977 dev->dev_addr[0] = (hi >> 8) & 0xff;
15978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015979 }
15980
15981 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015982#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015983 if (!tg3_get_default_macaddr_sparc(tp))
15984 return 0;
15985#endif
15986 return -EINVAL;
15987 }
15988 return 0;
15989}
15990
David S. Miller59e6b432005-05-18 22:50:10 -070015991#define BOUNDARY_SINGLE_CACHELINE 1
15992#define BOUNDARY_MULTI_CACHELINE 2
15993
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015994static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015995{
15996 int cacheline_size;
15997 u8 byte;
15998 int goal;
15999
16000 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
16001 if (byte == 0)
16002 cacheline_size = 1024;
16003 else
16004 cacheline_size = (int) byte * 4;
16005
16006 /* On 5703 and later chips, the boundary bits have no
16007 * effect.
16008 */
Joe Perches41535772013-02-16 11:20:04 +000016009 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16010 tg3_asic_rev(tp) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016011 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070016012 goto out;
16013
16014#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
16015 goal = BOUNDARY_MULTI_CACHELINE;
16016#else
16017#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
16018 goal = BOUNDARY_SINGLE_CACHELINE;
16019#else
16020 goal = 0;
16021#endif
16022#endif
16023
Joe Perches63c3a662011-04-26 08:12:10 +000016024 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016025 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
16026 goto out;
16027 }
16028
David S. Miller59e6b432005-05-18 22:50:10 -070016029 if (!goal)
16030 goto out;
16031
16032 /* PCI controllers on most RISC systems tend to disconnect
16033 * when a device tries to burst across a cache-line boundary.
16034 * Therefore, letting tg3 do so just wastes PCI bandwidth.
16035 *
16036 * Unfortunately, for PCI-E there are only limited
16037 * write-side controls for this, and thus for reads
16038 * we will still get the disconnects. We'll also waste
16039 * these PCI cycles for both read and write for chips
16040 * other than 5700 and 5701 which do not implement the
16041 * boundary bits.
16042 */
Joe Perches63c3a662011-04-26 08:12:10 +000016043 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016044 switch (cacheline_size) {
16045 case 16:
16046 case 32:
16047 case 64:
16048 case 128:
16049 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16050 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
16051 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
16052 } else {
16053 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16054 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16055 }
16056 break;
16057
16058 case 256:
16059 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
16060 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
16061 break;
16062
16063 default:
16064 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
16065 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
16066 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016067 }
Joe Perches63c3a662011-04-26 08:12:10 +000016068 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070016069 switch (cacheline_size) {
16070 case 16:
16071 case 32:
16072 case 64:
16073 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16074 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16075 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
16076 break;
16077 }
16078 /* fallthrough */
16079 case 128:
16080 default:
16081 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
16082 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
16083 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016084 }
David S. Miller59e6b432005-05-18 22:50:10 -070016085 } else {
16086 switch (cacheline_size) {
16087 case 16:
16088 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16089 val |= (DMA_RWCTRL_READ_BNDRY_16 |
16090 DMA_RWCTRL_WRITE_BNDRY_16);
16091 break;
16092 }
16093 /* fallthrough */
16094 case 32:
16095 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16096 val |= (DMA_RWCTRL_READ_BNDRY_32 |
16097 DMA_RWCTRL_WRITE_BNDRY_32);
16098 break;
16099 }
16100 /* fallthrough */
16101 case 64:
16102 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16103 val |= (DMA_RWCTRL_READ_BNDRY_64 |
16104 DMA_RWCTRL_WRITE_BNDRY_64);
16105 break;
16106 }
16107 /* fallthrough */
16108 case 128:
16109 if (goal == BOUNDARY_SINGLE_CACHELINE) {
16110 val |= (DMA_RWCTRL_READ_BNDRY_128 |
16111 DMA_RWCTRL_WRITE_BNDRY_128);
16112 break;
16113 }
16114 /* fallthrough */
16115 case 256:
16116 val |= (DMA_RWCTRL_READ_BNDRY_256 |
16117 DMA_RWCTRL_WRITE_BNDRY_256);
16118 break;
16119 case 512:
16120 val |= (DMA_RWCTRL_READ_BNDRY_512 |
16121 DMA_RWCTRL_WRITE_BNDRY_512);
16122 break;
16123 case 1024:
16124 default:
16125 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
16126 DMA_RWCTRL_WRITE_BNDRY_1024);
16127 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070016128 }
David S. Miller59e6b432005-05-18 22:50:10 -070016129 }
16130
16131out:
16132 return val;
16133}
16134
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016135static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
16136 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016137{
16138 struct tg3_internal_buffer_desc test_desc;
16139 u32 sram_dma_descs;
16140 int i, ret;
16141
16142 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
16143
16144 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
16145 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
16146 tw32(RDMAC_STATUS, 0);
16147 tw32(WDMAC_STATUS, 0);
16148
16149 tw32(BUFMGR_MODE, 0);
16150 tw32(FTQ_RESET, 0);
16151
16152 test_desc.addr_hi = ((u64) buf_dma) >> 32;
16153 test_desc.addr_lo = buf_dma & 0xffffffff;
16154 test_desc.nic_mbuf = 0x00002100;
16155 test_desc.len = size;
16156
16157 /*
16158 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
16159 * the *second* time the tg3 driver was getting loaded after an
16160 * initial scan.
16161 *
16162 * Broadcom tells me:
16163 * ...the DMA engine is connected to the GRC block and a DMA
16164 * reset may affect the GRC block in some unpredictable way...
16165 * The behavior of resets to individual blocks has not been tested.
16166 *
16167 * Broadcom noted the GRC reset will also reset all sub-components.
16168 */
16169 if (to_device) {
16170 test_desc.cqid_sqid = (13 << 8) | 2;
16171
16172 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
16173 udelay(40);
16174 } else {
16175 test_desc.cqid_sqid = (16 << 8) | 7;
16176
16177 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
16178 udelay(40);
16179 }
16180 test_desc.flags = 0x00000005;
16181
16182 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
16183 u32 val;
16184
16185 val = *(((u32 *)&test_desc) + i);
16186 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
16187 sram_dma_descs + (i * sizeof(u32)));
16188 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
16189 }
16190 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
16191
Matt Carlson859a588792010-04-05 10:19:28 +000016192 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016193 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000016194 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070016195 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016196
16197 ret = -ENODEV;
16198 for (i = 0; i < 40; i++) {
16199 u32 val;
16200
16201 if (to_device)
16202 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
16203 else
16204 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
16205 if ((val & 0xffff) == sram_dma_descs) {
16206 ret = 0;
16207 break;
16208 }
16209
16210 udelay(100);
16211 }
16212
16213 return ret;
16214}
16215
David S. Millerded73402005-05-23 13:59:47 -070016216#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016217
Matt Carlson41434702011-03-09 16:58:22 +000016218static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080016219 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
16220 { },
16221};
16222
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016223static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016224{
16225 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070016226 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016227 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016228
Matt Carlson4bae65c2010-11-24 08:31:52 +000016229 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
16230 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016231 if (!buf) {
16232 ret = -ENOMEM;
16233 goto out_nofree;
16234 }
16235
16236 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
16237 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16238
David S. Miller59e6b432005-05-18 22:50:10 -070016239 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016240
Joe Perches63c3a662011-04-26 08:12:10 +000016241 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016242 goto out;
16243
Joe Perches63c3a662011-04-26 08:12:10 +000016244 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016245 /* DMA read watermark not used on PCIE */
16246 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016247 } else if (!tg3_flag(tp, PCIX_MODE)) {
Joe Perches41535772013-02-16 11:20:04 +000016248 if (tg3_asic_rev(tp) == ASIC_REV_5705 ||
16249 tg3_asic_rev(tp) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016250 tp->dma_rwctrl |= 0x003f0000;
16251 else
16252 tp->dma_rwctrl |= 0x003f000f;
16253 } else {
Joe Perches41535772013-02-16 11:20:04 +000016254 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16255 tg3_asic_rev(tp) == ASIC_REV_5704) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016256 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016257 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016258
Michael Chan4a29cc22006-03-19 13:21:12 -080016259 /* If the 5704 is behind the EPB bridge, we can
16260 * do the less restrictive ONE_DMA workaround for
16261 * better performance.
16262 */
Joe Perches63c3a662011-04-26 08:12:10 +000016263 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Joe Perches41535772013-02-16 11:20:04 +000016264 tg3_asic_rev(tp) == ASIC_REV_5704)
Michael Chan4a29cc22006-03-19 13:21:12 -080016265 tp->dma_rwctrl |= 0x8000;
16266 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016267 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16268
Joe Perches41535772013-02-16 11:20:04 +000016269 if (tg3_asic_rev(tp) == ASIC_REV_5703)
Michael Chan49afdeb2007-02-13 12:17:03 -080016270 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016271 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016272 tp->dma_rwctrl |=
16273 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16274 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16275 (1 << 23);
Joe Perches41535772013-02-16 11:20:04 +000016276 } else if (tg3_asic_rev(tp) == ASIC_REV_5780) {
Michael Chan4cf78e42005-07-25 12:29:19 -070016277 /* 5780 always in PCIX mode */
16278 tp->dma_rwctrl |= 0x00144000;
Joe Perches41535772013-02-16 11:20:04 +000016279 } else if (tg3_asic_rev(tp) == ASIC_REV_5714) {
Michael Chana4e2b342005-10-26 15:46:52 -070016280 /* 5714 always in PCIX mode */
16281 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016282 } else {
16283 tp->dma_rwctrl |= 0x001b000f;
16284 }
16285 }
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016286 if (tg3_flag(tp, ONE_DMA_AT_ONCE))
16287 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016288
Joe Perches41535772013-02-16 11:20:04 +000016289 if (tg3_asic_rev(tp) == ASIC_REV_5703 ||
16290 tg3_asic_rev(tp) == ASIC_REV_5704)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016291 tp->dma_rwctrl &= 0xfffffff0;
16292
Joe Perches41535772013-02-16 11:20:04 +000016293 if (tg3_asic_rev(tp) == ASIC_REV_5700 ||
16294 tg3_asic_rev(tp) == ASIC_REV_5701) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016295 /* Remove this if it causes problems for some boards. */
16296 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16297
16298 /* On 5700/5701 chips, we need to set this bit.
16299 * Otherwise the chip will issue cacheline transactions
16300 * to streamable DMA memory with not all the byte
16301 * enables turned on. This is an error on several
16302 * RISC PCI controllers, in particular sparc64.
16303 *
16304 * On 5703/5704 chips, this bit has been reassigned
16305 * a different meaning. In particular, it is used
16306 * on those chips to enable a PCI-X workaround.
16307 */
16308 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16309 }
16310
16311 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16312
16313#if 0
16314 /* Unneeded, already done by tg3_get_invariants. */
16315 tg3_switch_clocks(tp);
16316#endif
16317
Joe Perches41535772013-02-16 11:20:04 +000016318 if (tg3_asic_rev(tp) != ASIC_REV_5700 &&
16319 tg3_asic_rev(tp) != ASIC_REV_5701)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016320 goto out;
16321
David S. Miller59e6b432005-05-18 22:50:10 -070016322 /* It is best to perform DMA test with maximum write burst size
16323 * to expose the 5700/5701 write DMA bug.
16324 */
16325 saved_dma_rwctrl = tp->dma_rwctrl;
16326 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16327 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16328
Linus Torvalds1da177e2005-04-16 15:20:36 -070016329 while (1) {
16330 u32 *p = buf, i;
16331
16332 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16333 p[i] = i;
16334
16335 /* Send the buffer to the chip. */
16336 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16337 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016338 dev_err(&tp->pdev->dev,
16339 "%s: Buffer write failed. err = %d\n",
16340 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016341 break;
16342 }
16343
16344#if 0
16345 /* validate data reached card RAM correctly. */
16346 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16347 u32 val;
16348 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16349 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016350 dev_err(&tp->pdev->dev,
16351 "%s: Buffer corrupted on device! "
16352 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016353 /* ret = -ENODEV here? */
16354 }
16355 p[i] = 0;
16356 }
16357#endif
16358 /* Now read it back. */
16359 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16360 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016361 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16362 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016363 break;
16364 }
16365
16366 /* Verify it. */
16367 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16368 if (p[i] == i)
16369 continue;
16370
David S. Miller59e6b432005-05-18 22:50:10 -070016371 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16372 DMA_RWCTRL_WRITE_BNDRY_16) {
16373 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016374 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16375 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16376 break;
16377 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016378 dev_err(&tp->pdev->dev,
16379 "%s: Buffer corrupted on read back! "
16380 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016381 ret = -ENODEV;
16382 goto out;
16383 }
16384 }
16385
16386 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16387 /* Success. */
16388 ret = 0;
16389 break;
16390 }
16391 }
David S. Miller59e6b432005-05-18 22:50:10 -070016392 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16393 DMA_RWCTRL_WRITE_BNDRY_16) {
16394 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016395 * now look for chipsets that are known to expose the
16396 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016397 */
Matt Carlson41434702011-03-09 16:58:22 +000016398 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016399 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16400 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016401 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016402 /* Safe to use the calculated DMA boundary. */
16403 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016404 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016405
David S. Miller59e6b432005-05-18 22:50:10 -070016406 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016408
16409out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016410 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016411out_nofree:
16412 return ret;
16413}
16414
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016415static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416{
Joe Perches63c3a662011-04-26 08:12:10 +000016417 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016418 tp->bufmgr_config.mbuf_read_dma_low_water =
16419 DEFAULT_MB_RDMA_LOW_WATER_5705;
16420 tp->bufmgr_config.mbuf_mac_rx_low_water =
16421 DEFAULT_MB_MACRX_LOW_WATER_57765;
16422 tp->bufmgr_config.mbuf_high_water =
16423 DEFAULT_MB_HIGH_WATER_57765;
16424
16425 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16426 DEFAULT_MB_RDMA_LOW_WATER_5705;
16427 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16428 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16429 tp->bufmgr_config.mbuf_high_water_jumbo =
16430 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016431 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016432 tp->bufmgr_config.mbuf_read_dma_low_water =
16433 DEFAULT_MB_RDMA_LOW_WATER_5705;
16434 tp->bufmgr_config.mbuf_mac_rx_low_water =
16435 DEFAULT_MB_MACRX_LOW_WATER_5705;
16436 tp->bufmgr_config.mbuf_high_water =
16437 DEFAULT_MB_HIGH_WATER_5705;
Joe Perches41535772013-02-16 11:20:04 +000016438 if (tg3_asic_rev(tp) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070016439 tp->bufmgr_config.mbuf_mac_rx_low_water =
16440 DEFAULT_MB_MACRX_LOW_WATER_5906;
16441 tp->bufmgr_config.mbuf_high_water =
16442 DEFAULT_MB_HIGH_WATER_5906;
16443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444
Michael Chanfdfec1722005-07-25 12:31:48 -070016445 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16446 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16447 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16448 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16449 tp->bufmgr_config.mbuf_high_water_jumbo =
16450 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16451 } else {
16452 tp->bufmgr_config.mbuf_read_dma_low_water =
16453 DEFAULT_MB_RDMA_LOW_WATER;
16454 tp->bufmgr_config.mbuf_mac_rx_low_water =
16455 DEFAULT_MB_MACRX_LOW_WATER;
16456 tp->bufmgr_config.mbuf_high_water =
16457 DEFAULT_MB_HIGH_WATER;
16458
16459 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16460 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16461 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16462 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16463 tp->bufmgr_config.mbuf_high_water_jumbo =
16464 DEFAULT_MB_HIGH_WATER_JUMBO;
16465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016466
16467 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16468 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16469}
16470
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016471static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016472{
Matt Carlson79eb6902010-02-17 15:17:03 +000016473 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16474 case TG3_PHY_ID_BCM5400: return "5400";
16475 case TG3_PHY_ID_BCM5401: return "5401";
16476 case TG3_PHY_ID_BCM5411: return "5411";
16477 case TG3_PHY_ID_BCM5701: return "5701";
16478 case TG3_PHY_ID_BCM5703: return "5703";
16479 case TG3_PHY_ID_BCM5704: return "5704";
16480 case TG3_PHY_ID_BCM5705: return "5705";
16481 case TG3_PHY_ID_BCM5750: return "5750";
16482 case TG3_PHY_ID_BCM5752: return "5752";
16483 case TG3_PHY_ID_BCM5714: return "5714";
16484 case TG3_PHY_ID_BCM5780: return "5780";
16485 case TG3_PHY_ID_BCM5755: return "5755";
16486 case TG3_PHY_ID_BCM5787: return "5787";
16487 case TG3_PHY_ID_BCM5784: return "5784";
16488 case TG3_PHY_ID_BCM5756: return "5722/5756";
16489 case TG3_PHY_ID_BCM5906: return "5906";
16490 case TG3_PHY_ID_BCM5761: return "5761";
16491 case TG3_PHY_ID_BCM5718C: return "5718C";
16492 case TG3_PHY_ID_BCM5718S: return "5718S";
16493 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016494 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016495 case TG3_PHY_ID_BCM5720C: return "5720C";
Michael Chanc65a17f2013-01-06 12:51:07 +000016496 case TG3_PHY_ID_BCM5762: return "5762C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016497 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016498 case 0: return "serdes";
16499 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016501}
16502
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016503static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016504{
Joe Perches63c3a662011-04-26 08:12:10 +000016505 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016506 strcpy(str, "PCI Express");
16507 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016508 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016509 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16510
16511 strcpy(str, "PCIX:");
16512
16513 if ((clock_ctrl == 7) ||
16514 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16515 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16516 strcat(str, "133MHz");
16517 else if (clock_ctrl == 0)
16518 strcat(str, "33MHz");
16519 else if (clock_ctrl == 2)
16520 strcat(str, "50MHz");
16521 else if (clock_ctrl == 4)
16522 strcat(str, "66MHz");
16523 else if (clock_ctrl == 6)
16524 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016525 } else {
16526 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016527 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016528 strcat(str, "66MHz");
16529 else
16530 strcat(str, "33MHz");
16531 }
Joe Perches63c3a662011-04-26 08:12:10 +000016532 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016533 strcat(str, ":32-bit");
16534 else
16535 strcat(str, ":64-bit");
16536 return str;
16537}
16538
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016539static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016540{
16541 struct ethtool_coalesce *ec = &tp->coal;
16542
16543 memset(ec, 0, sizeof(*ec));
16544 ec->cmd = ETHTOOL_GCOALESCE;
16545 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16546 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16547 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16548 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16549 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16550 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16551 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16552 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16553 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16554
16555 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16556 HOSTCC_MODE_CLRTICK_TXBD)) {
16557 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16558 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16559 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16560 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16561 }
Michael Chand244c892005-07-05 14:42:33 -070016562
Joe Perches63c3a662011-04-26 08:12:10 +000016563 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016564 ec->rx_coalesce_usecs_irq = 0;
16565 ec->tx_coalesce_usecs_irq = 0;
16566 ec->stats_block_coalesce_usecs = 0;
16567 }
David S. Miller15f98502005-05-18 22:49:26 -070016568}
16569
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016570static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016571 const struct pci_device_id *ent)
16572{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016573 struct net_device *dev;
16574 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016575 int i, err, pm_cap;
16576 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016577 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016578 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016579 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016580
Joe Perches05dbe002010-02-17 19:44:19 +000016581 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016582
16583 err = pci_enable_device(pdev);
16584 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016585 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016586 return err;
16587 }
16588
Linus Torvalds1da177e2005-04-16 15:20:36 -070016589 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16590 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016591 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016592 goto err_out_disable_pdev;
16593 }
16594
16595 pci_set_master(pdev);
16596
16597 /* Find power-management capability. */
16598 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16599 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016600 dev_err(&pdev->dev,
16601 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016602 err = -EIO;
16603 goto err_out_free_res;
16604 }
16605
Matt Carlson16821282011-07-13 09:27:28 +000016606 err = pci_set_power_state(pdev, PCI_D0);
16607 if (err) {
16608 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16609 goto err_out_free_res;
16610 }
16611
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016612 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016613 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016614 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016615 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016616 }
16617
Linus Torvalds1da177e2005-04-16 15:20:36 -070016618 SET_NETDEV_DEV(dev, &pdev->dev);
16619
Linus Torvalds1da177e2005-04-16 15:20:36 -070016620 tp = netdev_priv(dev);
16621 tp->pdev = pdev;
16622 tp->dev = dev;
16623 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016624 tp->rx_mode = TG3_DEF_RX_MODE;
16625 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016626 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016627
Linus Torvalds1da177e2005-04-16 15:20:36 -070016628 if (tg3_debug > 0)
16629 tp->msg_enable = tg3_debug;
16630 else
16631 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16632
Hauke Mehrtens7e6c63f2013-02-07 05:37:39 +000016633 if (pdev_is_ssb_gige_core(pdev)) {
16634 tg3_flag_set(tp, IS_SSB_CORE);
16635 if (ssb_gige_must_flush_posted_writes(pdev))
16636 tg3_flag_set(tp, FLUSH_POSTED_WRITES);
16637 if (ssb_gige_one_dma_at_once(pdev))
16638 tg3_flag_set(tp, ONE_DMA_AT_ONCE);
16639 if (ssb_gige_have_roboswitch(pdev))
16640 tg3_flag_set(tp, ROBOSWITCH);
16641 if (ssb_gige_is_rgmii(pdev))
16642 tg3_flag_set(tp, RGMII_MODE);
16643 }
16644
Linus Torvalds1da177e2005-04-16 15:20:36 -070016645 /* The word/byte swap controls here control register access byte
16646 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16647 * setting below.
16648 */
16649 tp->misc_host_ctrl =
16650 MISC_HOST_CTRL_MASK_PCI_INT |
16651 MISC_HOST_CTRL_WORD_SWAP |
16652 MISC_HOST_CTRL_INDIR_ACCESS |
16653 MISC_HOST_CTRL_PCISTATE_RW;
16654
16655 /* The NONFRM (non-frame) byte/word swap controls take effect
16656 * on descriptor entries, anything which isn't packet data.
16657 *
16658 * The StrongARM chips on the board (one for tx, one for rx)
16659 * are running in big-endian mode.
16660 */
16661 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16662 GRC_MODE_WSWAP_NONFRM_DATA);
16663#ifdef __BIG_ENDIAN
16664 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16665#endif
16666 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016667 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016668 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016669
Matt Carlsond5fe4882008-11-21 17:20:32 -080016670 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016671 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016672 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016673 err = -ENOMEM;
16674 goto err_out_free_dev;
16675 }
16676
Matt Carlsonc9cab242011-07-13 09:27:27 +000016677 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16678 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16679 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16680 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16681 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016682 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016683 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16684 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
Michael Chanc65a17f2013-01-06 12:51:07 +000016685 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720 ||
16686 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5762 ||
16687 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5725 ||
16688 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5727) {
Matt Carlsonc9cab242011-07-13 09:27:27 +000016689 tg3_flag_set(tp, ENABLE_APE);
16690 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16691 if (!tp->aperegs) {
16692 dev_err(&pdev->dev,
16693 "Cannot map APE registers, aborting\n");
16694 err = -ENOMEM;
16695 goto err_out_iounmap;
16696 }
16697 }
16698
Linus Torvalds1da177e2005-04-16 15:20:36 -070016699 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16700 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016701
Linus Torvalds1da177e2005-04-16 15:20:36 -070016702 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016703 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016704 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016705 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016706
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016707 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016708 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016709 dev_err(&pdev->dev,
16710 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016711 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016712 }
16713
Michael Chan4a29cc22006-03-19 13:21:12 -080016714 /* The EPB bridge inside 5714, 5715, and 5780 and any
16715 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016716 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16717 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16718 * do DMA address check in tg3_start_xmit().
16719 */
Joe Perches63c3a662011-04-26 08:12:10 +000016720 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016721 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016722 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016723 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016724#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016725 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016726#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016727 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016728 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016729
16730 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016731 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016732 err = pci_set_dma_mask(pdev, dma_mask);
16733 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016734 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016735 err = pci_set_consistent_dma_mask(pdev,
16736 persist_dma_mask);
16737 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016738 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16739 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016740 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016741 }
16742 }
16743 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016744 if (err || dma_mask == DMA_BIT_MASK(32)) {
16745 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016746 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016747 dev_err(&pdev->dev,
16748 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016749 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016750 }
16751 }
16752
Michael Chanfdfec1722005-07-25 12:31:48 -070016753 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016754
Matt Carlson0da06062011-05-19 12:12:53 +000016755 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16756
16757 /* 5700 B0 chips do not support checksumming correctly due
16758 * to hardware bugs.
16759 */
Joe Perches41535772013-02-16 11:20:04 +000016760 if (tg3_chip_rev_id(tp) != CHIPREV_ID_5700_B0) {
Matt Carlson0da06062011-05-19 12:12:53 +000016761 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16762
16763 if (tg3_flag(tp, 5755_PLUS))
16764 features |= NETIF_F_IPV6_CSUM;
16765 }
16766
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016767 /* TSO is on by default on chips that support hardware TSO.
16768 * Firmware TSO on older chips gives lower performance, so it
16769 * is off by default, but can be enabled using ethtool.
16770 */
Joe Perches63c3a662011-04-26 08:12:10 +000016771 if ((tg3_flag(tp, HW_TSO_1) ||
16772 tg3_flag(tp, HW_TSO_2) ||
16773 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016774 (features & NETIF_F_IP_CSUM))
16775 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016776 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016777 if (features & NETIF_F_IPV6_CSUM)
16778 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016779 if (tg3_flag(tp, HW_TSO_3) ||
Joe Perches41535772013-02-16 11:20:04 +000016780 tg3_asic_rev(tp) == ASIC_REV_5761 ||
16781 (tg3_asic_rev(tp) == ASIC_REV_5784 &&
16782 tg3_chip_rev(tp) != CHIPREV_5784_AX) ||
16783 tg3_asic_rev(tp) == ASIC_REV_5785 ||
16784 tg3_asic_rev(tp) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016785 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016787
Matt Carlsond542fe22011-05-19 16:02:43 +000016788 dev->features |= features;
16789 dev->vlan_features |= features;
16790
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016791 /*
16792 * Add loopback capability only for a subset of devices that support
16793 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16794 * loopback for the remaining devices.
16795 */
Joe Perches41535772013-02-16 11:20:04 +000016796 if (tg3_asic_rev(tp) != ASIC_REV_5780 &&
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016797 !tg3_flag(tp, CPMU_PRESENT))
16798 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016799 features |= NETIF_F_LOOPBACK;
16800
Matt Carlson0da06062011-05-19 12:12:53 +000016801 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016802
Joe Perches41535772013-02-16 11:20:04 +000016803 if (tg3_chip_rev_id(tp) == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016804 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016805 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016806 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016807 tp->rx_pending = 63;
16808 }
16809
Linus Torvalds1da177e2005-04-16 15:20:36 -070016810 err = tg3_get_device_address(tp);
16811 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016812 dev_err(&pdev->dev,
16813 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016814 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016815 }
16816
Matt Carlsonc88864d2007-11-12 21:07:01 -080016817 /*
16818 * Reset chip in case UNDI or EFI driver did not shutdown
16819 * DMA self test will enable WDMAC and we'll see (spurious)
16820 * pending DMA on the PCI bus at that point.
16821 */
16822 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16823 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16824 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16825 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16826 }
16827
16828 err = tg3_test_dma(tp);
16829 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016830 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016831 goto err_out_apeunmap;
16832 }
16833
Matt Carlson78f90dc2009-11-13 13:03:42 +000016834 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16835 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16836 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016837 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016838 struct tg3_napi *tnapi = &tp->napi[i];
16839
16840 tnapi->tp = tp;
16841 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16842
16843 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016844 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016845 intmbx += 0x8;
16846 else
16847 intmbx += 0x4;
16848
16849 tnapi->consmbox = rcvmbx;
16850 tnapi->prodmbox = sndmbx;
16851
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016852 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016853 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016854 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016855 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016856
Joe Perches63c3a662011-04-26 08:12:10 +000016857 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016858 break;
16859
16860 /*
16861 * If we support MSIX, we'll be using RSS. If we're using
16862 * RSS, the first vector only handles link interrupts and the
16863 * remaining vectors handle rx and tx interrupts. Reuse the
16864 * mailbox values for the next iteration. The values we setup
16865 * above are still useful for the single vectored mode.
16866 */
16867 if (!i)
16868 continue;
16869
16870 rcvmbx += 0x8;
16871
16872 if (sndmbx & 0x4)
16873 sndmbx -= 0x4;
16874 else
16875 sndmbx += 0xc;
16876 }
16877
Matt Carlsonc88864d2007-11-12 21:07:01 -080016878 tg3_init_coal(tp);
16879
Michael Chanc49a1562006-12-17 17:07:29 -080016880 pci_set_drvdata(pdev, dev);
16881
Joe Perches41535772013-02-16 11:20:04 +000016882 if (tg3_asic_rev(tp) == ASIC_REV_5719 ||
16883 tg3_asic_rev(tp) == ASIC_REV_5720 ||
16884 tg3_asic_rev(tp) == ASIC_REV_5762)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016885 tg3_flag_set(tp, PTP_CAPABLE);
16886
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016887 if (tg3_flag(tp, 5717_PLUS)) {
16888 /* Resume a low-power mode */
16889 tg3_frob_aux_power(tp, false);
16890 }
16891
Matt Carlson21f76382012-02-22 12:35:21 +000016892 tg3_timer_init(tp);
16893
Michael Chan402e1392013-02-14 12:13:41 +000016894 tg3_carrier_off(tp);
16895
Linus Torvalds1da177e2005-04-16 15:20:36 -070016896 err = register_netdev(dev);
16897 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016898 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016899 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016900 }
16901
Joe Perches05dbe002010-02-17 19:44:19 +000016902 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16903 tp->board_part_number,
Joe Perches41535772013-02-16 11:20:04 +000016904 tg3_chip_rev_id(tp),
Joe Perches05dbe002010-02-17 19:44:19 +000016905 tg3_bus_string(tp, str),
16906 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016907
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016908 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016909 struct phy_device *phydev;
16910 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016911 netdev_info(dev,
16912 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016913 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016914 } else {
16915 char *ethtype;
16916
16917 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16918 ethtype = "10/100Base-TX";
16919 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16920 ethtype = "1000Base-SX";
16921 else
16922 ethtype = "10/100/1000Base-T";
16923
Matt Carlson5129c3a2010-04-05 10:19:23 +000016924 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016925 "(WireSpeed[%d], EEE[%d])\n",
16926 tg3_phy_string(tp), ethtype,
16927 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16928 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016929 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016930
Joe Perches05dbe002010-02-17 19:44:19 +000016931 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016932 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016933 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016934 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016935 tg3_flag(tp, ENABLE_ASF) != 0,
16936 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016937 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16938 tp->dma_rwctrl,
16939 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16940 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016941
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016942 pci_save_state(pdev);
16943
Linus Torvalds1da177e2005-04-16 15:20:36 -070016944 return 0;
16945
Matt Carlson0d3031d2007-10-10 18:02:43 -070016946err_out_apeunmap:
16947 if (tp->aperegs) {
16948 iounmap(tp->aperegs);
16949 tp->aperegs = NULL;
16950 }
16951
Linus Torvalds1da177e2005-04-16 15:20:36 -070016952err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016953 if (tp->regs) {
16954 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016955 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016957
16958err_out_free_dev:
16959 free_netdev(dev);
16960
Matt Carlson16821282011-07-13 09:27:28 +000016961err_out_power_down:
16962 pci_set_power_state(pdev, PCI_D3hot);
16963
Linus Torvalds1da177e2005-04-16 15:20:36 -070016964err_out_free_res:
16965 pci_release_regions(pdev);
16966
16967err_out_disable_pdev:
16968 pci_disable_device(pdev);
16969 pci_set_drvdata(pdev, NULL);
16970 return err;
16971}
16972
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016973static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016974{
16975 struct net_device *dev = pci_get_drvdata(pdev);
16976
16977 if (dev) {
16978 struct tg3 *tp = netdev_priv(dev);
16979
Jesper Juhle3c55302012-04-09 22:50:15 +020016980 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016981
Matt Carlsondb219972011-11-04 09:15:03 +000016982 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016983
David S. Miller1805b2f2011-10-24 18:18:09 -040016984 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016985 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016986 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016987 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016988
Linus Torvalds1da177e2005-04-16 15:20:36 -070016989 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016990 if (tp->aperegs) {
16991 iounmap(tp->aperegs);
16992 tp->aperegs = NULL;
16993 }
Michael Chan68929142005-08-09 20:17:14 -070016994 if (tp->regs) {
16995 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016996 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016998 free_netdev(dev);
16999 pci_release_regions(pdev);
17000 pci_disable_device(pdev);
17001 pci_set_drvdata(pdev, NULL);
17002 }
17003}
17004
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017005#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017006static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017007{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017008 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017009 struct net_device *dev = pci_get_drvdata(pdev);
17010 struct tg3 *tp = netdev_priv(dev);
17011 int err;
17012
17013 if (!netif_running(dev))
17014 return 0;
17015
Matt Carlsondb219972011-11-04 09:15:03 +000017016 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017017 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017018 tg3_netif_stop(tp);
17019
Matt Carlson21f76382012-02-22 12:35:21 +000017020 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017021
David S. Millerf47c11e2005-06-24 20:18:35 -070017022 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017023 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070017024 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017025
17026 netif_device_detach(dev);
17027
David S. Millerf47c11e2005-06-24 20:18:35 -070017028 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070017029 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000017030 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070017031 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017032
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017033 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017034 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017035 int err2;
17036
David S. Millerf47c11e2005-06-24 20:18:35 -070017037 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017038
Joe Perches63c3a662011-04-26 08:12:10 +000017039 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017040 err2 = tg3_restart_hw(tp, 1);
17041 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070017042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017043
Matt Carlson21f76382012-02-22 12:35:21 +000017044 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017045
17046 netif_device_attach(dev);
17047 tg3_netif_start(tp);
17048
Michael Chanb9ec6c12006-07-25 16:37:27 -070017049out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017050 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017051
17052 if (!err2)
17053 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017054 }
17055
17056 return err;
17057}
17058
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017059static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017060{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017061 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017062 struct net_device *dev = pci_get_drvdata(pdev);
17063 struct tg3 *tp = netdev_priv(dev);
17064 int err;
17065
17066 if (!netif_running(dev))
17067 return 0;
17068
Linus Torvalds1da177e2005-04-16 15:20:36 -070017069 netif_device_attach(dev);
17070
David S. Millerf47c11e2005-06-24 20:18:35 -070017071 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017072
Joe Perches63c3a662011-04-26 08:12:10 +000017073 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070017074 err = tg3_restart_hw(tp, 1);
17075 if (err)
17076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017077
Matt Carlson21f76382012-02-22 12:35:21 +000017078 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017079
Linus Torvalds1da177e2005-04-16 15:20:36 -070017080 tg3_netif_start(tp);
17081
Michael Chanb9ec6c12006-07-25 16:37:27 -070017082out:
David S. Millerf47c11e2005-06-24 20:18:35 -070017083 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017084
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070017085 if (!err)
17086 tg3_phy_start(tp);
17087
Michael Chanb9ec6c12006-07-25 16:37:27 -070017088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017089}
17090
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017091static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017092#define TG3_PM_OPS (&tg3_pm_ops)
17093
17094#else
17095
17096#define TG3_PM_OPS NULL
17097
17098#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000017099
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017100/**
17101 * tg3_io_error_detected - called when PCI error is detected
17102 * @pdev: Pointer to PCI device
17103 * @state: The current pci connection state
17104 *
17105 * This function is called after a PCI bus error affecting
17106 * this device has been detected.
17107 */
17108static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
17109 pci_channel_state_t state)
17110{
17111 struct net_device *netdev = pci_get_drvdata(pdev);
17112 struct tg3 *tp = netdev_priv(netdev);
17113 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
17114
17115 netdev_info(netdev, "PCI I/O error detected\n");
17116
17117 rtnl_lock();
17118
17119 if (!netif_running(netdev))
17120 goto done;
17121
17122 tg3_phy_stop(tp);
17123
17124 tg3_netif_stop(tp);
17125
Matt Carlson21f76382012-02-22 12:35:21 +000017126 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017127
17128 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000017129 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017130
17131 netif_device_detach(netdev);
17132
17133 /* Clean up software state, even if MMIO is blocked */
17134 tg3_full_lock(tp, 0);
17135 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
17136 tg3_full_unlock(tp);
17137
17138done:
17139 if (state == pci_channel_io_perm_failure)
17140 err = PCI_ERS_RESULT_DISCONNECT;
17141 else
17142 pci_disable_device(pdev);
17143
17144 rtnl_unlock();
17145
17146 return err;
17147}
17148
17149/**
17150 * tg3_io_slot_reset - called after the pci bus has been reset.
17151 * @pdev: Pointer to PCI device
17152 *
17153 * Restart the card from scratch, as if from a cold-boot.
17154 * At this point, the card has exprienced a hard reset,
17155 * followed by fixups by BIOS, and has its config space
17156 * set up identically to what it was at cold boot.
17157 */
17158static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
17159{
17160 struct net_device *netdev = pci_get_drvdata(pdev);
17161 struct tg3 *tp = netdev_priv(netdev);
17162 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
17163 int err;
17164
17165 rtnl_lock();
17166
17167 if (pci_enable_device(pdev)) {
17168 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
17169 goto done;
17170 }
17171
17172 pci_set_master(pdev);
17173 pci_restore_state(pdev);
17174 pci_save_state(pdev);
17175
17176 if (!netif_running(netdev)) {
17177 rc = PCI_ERS_RESULT_RECOVERED;
17178 goto done;
17179 }
17180
17181 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000017182 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017183 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017184
17185 rc = PCI_ERS_RESULT_RECOVERED;
17186
17187done:
17188 rtnl_unlock();
17189
17190 return rc;
17191}
17192
17193/**
17194 * tg3_io_resume - called when traffic can start flowing again.
17195 * @pdev: Pointer to PCI device
17196 *
17197 * This callback is called when the error recovery driver tells
17198 * us that its OK to resume normal operation.
17199 */
17200static void tg3_io_resume(struct pci_dev *pdev)
17201{
17202 struct net_device *netdev = pci_get_drvdata(pdev);
17203 struct tg3 *tp = netdev_priv(netdev);
17204 int err;
17205
17206 rtnl_lock();
17207
17208 if (!netif_running(netdev))
17209 goto done;
17210
17211 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000017212 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017213 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017214 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017215 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017216 netdev_err(netdev, "Cannot restart hardware after reset.\n");
17217 goto done;
17218 }
17219
17220 netif_device_attach(netdev);
17221
Matt Carlson21f76382012-02-22 12:35:21 +000017222 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017223
17224 tg3_netif_start(tp);
17225
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000017226 tg3_full_unlock(tp);
17227
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017228 tg3_phy_start(tp);
17229
17230done:
17231 rtnl_unlock();
17232}
17233
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070017234static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017235 .error_detected = tg3_io_error_detected,
17236 .slot_reset = tg3_io_slot_reset,
17237 .resume = tg3_io_resume
17238};
17239
Linus Torvalds1da177e2005-04-16 15:20:36 -070017240static struct pci_driver tg3_driver = {
17241 .name = DRV_MODULE_NAME,
17242 .id_table = tg3_pci_tbl,
17243 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050017244 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000017245 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000017246 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070017247};
17248
17249static int __init tg3_init(void)
17250{
Jeff Garzik29917622006-08-19 17:48:59 -040017251 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017252}
17253
17254static void __exit tg3_cleanup(void)
17255{
17256 pci_unregister_driver(&tg3_driver);
17257}
17258
17259module_init(tg3_init);
17260module_exit(tg3_cleanup);