blob: bdb086934cd95de72cd1aa02cf1cb4b6baaaafc3 [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.
Matt Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 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>
Michael Chanaed93e02012-07-16 16:24:02 +000047#include <linux/hwmon.h>
48#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030051#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Matt Carlsonbe947302012-12-03 19:36:57 +000057#include <uapi/linux/net_tstamp.h>
58#include <linux/ptp_clock_kernel.h>
59
David S. Miller49b6e95f2007-03-29 01:38:42 -070060#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070062#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Matt Carlson63532392008-11-03 16:49:57 -080065#define BAR_0 0
66#define BAR_2 2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "tg3.h"
69
Joe Perches63c3a662011-04-26 08:12:10 +000070/* Functions & macros to verify TG3_FLAGS types */
71
72static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 return test_bit(flag, bits);
75}
76
77static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 set_bit(flag, bits);
80}
81
82static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
83{
84 clear_bit(flag, bits);
85}
86
87#define tg3_flag(tp, flag) \
88 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
89#define tg3_flag_set(tp, flag) \
90 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
91#define tg3_flag_clear(tp, flag) \
92 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000095#define TG3_MAJ_NUM 3
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000096#define TG3_MIN_NUM 128
Matt Carlson6867c842010-07-11 09:31:44 +000097#define DRV_MODULE_VERSION \
98 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000099#define DRV_MODULE_RELDATE "December 03, 2012"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000101#define RESET_KIND_SHUTDOWN 0
102#define RESET_KIND_INIT 1
103#define RESET_KIND_SUSPEND 2
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define TG3_DEF_RX_MODE 0
106#define TG3_DEF_TX_MODE 0
107#define TG3_DEF_MSG_ENABLE \
108 (NETIF_MSG_DRV | \
109 NETIF_MSG_PROBE | \
110 NETIF_MSG_LINK | \
111 NETIF_MSG_TIMER | \
112 NETIF_MSG_IFDOWN | \
113 NETIF_MSG_IFUP | \
114 NETIF_MSG_RX_ERR | \
115 NETIF_MSG_TX_ERR)
116
Matt Carlson520b2752011-06-13 13:39:02 +0000117#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* length of time before we decide the hardware is borked,
120 * and dev->tx_timeout() should be called to fix the problem
121 */
Joe Perches63c3a662011-04-26 08:12:10 +0000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TG3_TX_TIMEOUT (5 * HZ)
124
125/* hardware minimum and maximum for a single frame's data payload */
126#define TG3_MIN_MTU 60
127#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000128 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* These numbers seem to be hard coded in the NIC firmware somehow.
131 * You can't change the ring sizes, but you can change where you place
132 * them in the NIC onboard memory.
133 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000138#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000139 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000140 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define TG3_DEF_RX_JUMBO_RING_PENDING 100
142
143/* Do not place this n-ring entries value into the tp struct itself,
144 * we really want to expose these constants to GCC so that modulo et
145 * al. operations are done with shifts and masks instead of with
146 * hw multiply/modulo instructions. Another solution would be to
147 * replace things like '% foo' with '& (foo - 1)'.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150#define TG3_TX_RING_SIZE 512
151#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
152
Matt Carlson2c49a442010-09-30 10:34:35 +0000153#define TG3_RX_STD_RING_BYTES(tp) \
154 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
155#define TG3_RX_JMB_RING_BYTES(tp) \
156 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
157#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000158 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
160 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
162
Matt Carlson287be122009-08-28 13:58:46 +0000163#define TG3_DMA_BYTE_ENAB 64
164
165#define TG3_RX_STD_DMA_SZ 1536
166#define TG3_RX_JMB_DMA_SZ 9046
167
168#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
169
170#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
171#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlson2c49a442010-09-30 10:34:35 +0000176#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
177 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000178
Matt Carlsond2757fc2010-04-12 06:58:27 +0000179/* Due to a hardware bug, the 5701 can only DMA to memory addresses
180 * that are at least dword aligned when used in PCIX mode. The driver
181 * works around this bug by double copying the packet. This workaround
182 * is built into the normal double copy length check for efficiency.
183 *
184 * However, the double copy is only necessary on those architectures
185 * where unaligned memory accesses are inefficient. For those architectures
186 * where unaligned memory accesses incur little penalty, we can reintegrate
187 * the 5701 in the normal rx path. Doing so saves a device structure
188 * dereference by hardcoding the double copy threshold in place.
189 */
190#define TG3_RX_COPY_THRESHOLD 256
191#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
192 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
193#else
194 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
195#endif
196
Matt Carlson81389f52011-08-31 11:44:49 +0000197#if (NET_IP_ALIGN != 0)
198#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
199#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000200#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000201#endif
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000204#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000205#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000206#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Matt Carlsonad829262008-11-21 17:16:16 -0800208#define TG3_RAW_IP_ALIGN 2
209
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000210#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000211#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000212
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800213#define FIRMWARE_TG3 "tigon/tg3.bin"
214#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
215#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
216
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500217static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000218 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
221MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
222MODULE_LICENSE("GPL");
223MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800224MODULE_FIRMWARE(FIRMWARE_TG3);
225MODULE_FIRMWARE(FIRMWARE_TG3TSO);
226MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
229module_param(tg3_debug, int, 0);
230MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
231
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000232#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
233#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
255 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
256 TG3_DRV_DATA_FLAG_5705_10_100},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
258 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
259 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
262 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
263 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
269 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000283 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
284 PCI_VENDOR_ID_LENOVO,
285 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
286 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000308 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
309 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
310 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
317 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
327 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
329 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700333 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
334 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
335 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
336 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
337 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
338 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
339 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000340 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700341 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
345
Andreas Mohr50da8592006-08-14 23:54:30 -0700346static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000348} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 { "rx_octets" },
350 { "rx_fragments" },
351 { "rx_ucast_packets" },
352 { "rx_mcast_packets" },
353 { "rx_bcast_packets" },
354 { "rx_fcs_errors" },
355 { "rx_align_errors" },
356 { "rx_xon_pause_rcvd" },
357 { "rx_xoff_pause_rcvd" },
358 { "rx_mac_ctrl_rcvd" },
359 { "rx_xoff_entered" },
360 { "rx_frame_too_long_errors" },
361 { "rx_jabbers" },
362 { "rx_undersize_packets" },
363 { "rx_in_length_errors" },
364 { "rx_out_length_errors" },
365 { "rx_64_or_less_octet_packets" },
366 { "rx_65_to_127_octet_packets" },
367 { "rx_128_to_255_octet_packets" },
368 { "rx_256_to_511_octet_packets" },
369 { "rx_512_to_1023_octet_packets" },
370 { "rx_1024_to_1522_octet_packets" },
371 { "rx_1523_to_2047_octet_packets" },
372 { "rx_2048_to_4095_octet_packets" },
373 { "rx_4096_to_8191_octet_packets" },
374 { "rx_8192_to_9022_octet_packets" },
375
376 { "tx_octets" },
377 { "tx_collisions" },
378
379 { "tx_xon_sent" },
380 { "tx_xoff_sent" },
381 { "tx_flow_control" },
382 { "tx_mac_errors" },
383 { "tx_single_collisions" },
384 { "tx_mult_collisions" },
385 { "tx_deferred" },
386 { "tx_excessive_collisions" },
387 { "tx_late_collisions" },
388 { "tx_collide_2times" },
389 { "tx_collide_3times" },
390 { "tx_collide_4times" },
391 { "tx_collide_5times" },
392 { "tx_collide_6times" },
393 { "tx_collide_7times" },
394 { "tx_collide_8times" },
395 { "tx_collide_9times" },
396 { "tx_collide_10times" },
397 { "tx_collide_11times" },
398 { "tx_collide_12times" },
399 { "tx_collide_13times" },
400 { "tx_collide_14times" },
401 { "tx_collide_15times" },
402 { "tx_ucast_packets" },
403 { "tx_mcast_packets" },
404 { "tx_bcast_packets" },
405 { "tx_carrier_sense_errors" },
406 { "tx_discards" },
407 { "tx_errors" },
408
409 { "dma_writeq_full" },
410 { "dma_write_prioq_full" },
411 { "rxbds_empty" },
412 { "rx_discards" },
413 { "rx_errors" },
414 { "rx_threshold_hit" },
415
416 { "dma_readq_full" },
417 { "dma_read_prioq_full" },
418 { "tx_comp_queue_full" },
419
420 { "ring_set_send_prod_index" },
421 { "ring_status_update" },
422 { "nic_irqs" },
423 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000424 { "nic_tx_threshold_hit" },
425
426 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Matt Carlson48fa55a2011-04-13 11:05:06 +0000429#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000430#define TG3_NVRAM_TEST 0
431#define TG3_LINK_TEST 1
432#define TG3_REGISTER_TEST 2
433#define TG3_MEMORY_TEST 3
434#define TG3_MAC_LOOPB_TEST 4
435#define TG3_PHY_LOOPB_TEST 5
436#define TG3_EXT_LOOPB_TEST 6
437#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000438
439
Andreas Mohr50da8592006-08-14 23:54:30 -0700440static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700441 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000442} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000443 [TG3_NVRAM_TEST] = { "nvram test (online) " },
444 [TG3_LINK_TEST] = { "link test (online) " },
445 [TG3_REGISTER_TEST] = { "register test (offline)" },
446 [TG3_MEMORY_TEST] = { "memory test (offline)" },
447 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
448 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
449 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
450 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700451};
452
Matt Carlson48fa55a2011-04-13 11:05:06 +0000453#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
454
455
Michael Chanb401e9e2005-12-19 16:27:04 -0800456static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
457{
458 writel(val, tp->regs + off);
459}
460
461static u32 tg3_read32(struct tg3 *tp, u32 off)
462{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000463 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800464}
465
Matt Carlson0d3031d2007-10-10 18:02:43 -0700466static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
467{
468 writel(val, tp->aperegs + off);
469}
470
471static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
472{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000473 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
477{
Michael Chan68929142005-08-09 20:17:14 -0700478 unsigned long flags;
479
480 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700481 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
482 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700483 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700484}
485
486static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
487{
488 writel(val, tp->regs + off);
489 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Michael Chan68929142005-08-09 20:17:14 -0700492static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
493{
494 unsigned long flags;
495 u32 val;
496
497 spin_lock_irqsave(&tp->indirect_lock, flags);
498 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
499 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
500 spin_unlock_irqrestore(&tp->indirect_lock, flags);
501 return val;
502}
503
504static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
505{
506 unsigned long flags;
507
508 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
509 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
510 TG3_64BIT_REG_LOW, val);
511 return;
512 }
Matt Carlson66711e662009-11-13 13:03:49 +0000513 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700514 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
515 TG3_64BIT_REG_LOW, val);
516 return;
517 }
518
519 spin_lock_irqsave(&tp->indirect_lock, flags);
520 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
521 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
522 spin_unlock_irqrestore(&tp->indirect_lock, flags);
523
524 /* In indirect mode when disabling interrupts, we also need
525 * to clear the interrupt bit in the GRC local ctrl register.
526 */
527 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
528 (val == 0x1)) {
529 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
530 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
531 }
532}
533
534static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
535{
536 unsigned long flags;
537 u32 val;
538
539 spin_lock_irqsave(&tp->indirect_lock, flags);
540 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
541 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
542 spin_unlock_irqrestore(&tp->indirect_lock, flags);
543 return val;
544}
545
Michael Chanb401e9e2005-12-19 16:27:04 -0800546/* usec_wait specifies the wait time in usec when writing to certain registers
547 * where it is unsafe to read back the register without some delay.
548 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
549 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
550 */
551static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Joe Perches63c3a662011-04-26 08:12:10 +0000553 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800554 /* Non-posted methods */
555 tp->write32(tp, off, val);
556 else {
557 /* Posted method */
558 tg3_write32(tp, off, val);
559 if (usec_wait)
560 udelay(usec_wait);
561 tp->read32(tp, off);
562 }
563 /* Wait again after the read for the posted method to guarantee that
564 * the wait time is met.
565 */
566 if (usec_wait)
567 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Michael Chan09ee9292005-08-09 20:17:00 -0700570static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
571{
572 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000573 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700574 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700575}
576
Michael Chan20094932005-08-09 20:16:32 -0700577static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 void __iomem *mbox = tp->regs + off;
580 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000581 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000583 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 readl(mbox);
585}
586
Michael Chanb5d37722006-09-27 16:06:21 -0700587static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
588{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000589 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700590}
591
592static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
593{
594 writel(val, tp->regs + off + GRCMBOX_BASE);
595}
596
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000597#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700598#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000599#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
600#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
601#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700602
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000603#define tw32(reg, val) tp->write32(tp, reg, val)
604#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
605#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
606#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
609{
Michael Chan68929142005-08-09 20:17:14 -0700610 unsigned long flags;
611
Matt Carlson6ff6f812011-05-19 12:12:54 +0000612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700613 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
614 return;
615
Michael Chan68929142005-08-09 20:17:14 -0700616 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000617 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700618 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
619 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Michael Chanbbadf502006-04-06 21:46:34 -0700621 /* Always leave this as zero. */
622 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
623 } else {
624 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
625 tw32_f(TG3PCI_MEM_WIN_DATA, val);
626
627 /* Always leave this as zero. */
628 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
629 }
Michael Chan68929142005-08-09 20:17:14 -0700630 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
634{
Michael Chan68929142005-08-09 20:17:14 -0700635 unsigned long flags;
636
Matt Carlson6ff6f812011-05-19 12:12:54 +0000637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700638 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
639 *val = 0;
640 return;
641 }
642
Michael Chan68929142005-08-09 20:17:14 -0700643 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000644 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700645 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
646 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Michael Chanbbadf502006-04-06 21:46:34 -0700648 /* Always leave this as zero. */
649 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
650 } else {
651 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
652 *val = tr32(TG3PCI_MEM_WIN_DATA);
653
654 /* Always leave this as zero. */
655 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
656 }
Michael Chan68929142005-08-09 20:17:14 -0700657 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660static void tg3_ape_lock_init(struct tg3 *tp)
661{
662 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000663 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000664
665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
666 regbase = TG3_APE_LOCK_GRANT;
667 else
668 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669
670 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000671 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
672 switch (i) {
673 case TG3_APE_LOCK_PHY0:
674 case TG3_APE_LOCK_PHY1:
675 case TG3_APE_LOCK_PHY2:
676 case TG3_APE_LOCK_PHY3:
677 bit = APE_LOCK_GRANT_DRIVER;
678 break;
679 default:
680 if (!tp->pci_fn)
681 bit = APE_LOCK_GRANT_DRIVER;
682 else
683 bit = 1 << tp->pci_fn;
684 }
685 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000686 }
687
Matt Carlson0d3031d2007-10-10 18:02:43 -0700688}
689
690static int tg3_ape_lock(struct tg3 *tp, int locknum)
691{
692 int i, off;
693 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695
Joe Perches63c3a662011-04-26 08:12:10 +0000696 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 return 0;
698
699 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000700 case TG3_APE_LOCK_GPIO:
701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
702 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000703 case TG3_APE_LOCK_GRC:
704 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000705 if (!tp->pci_fn)
706 bit = APE_LOCK_REQ_DRIVER;
707 else
708 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000709 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000710 case TG3_APE_LOCK_PHY0:
711 case TG3_APE_LOCK_PHY1:
712 case TG3_APE_LOCK_PHY2:
713 case TG3_APE_LOCK_PHY3:
714 bit = APE_LOCK_REQ_DRIVER;
715 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000716 default:
717 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700718 }
719
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
721 req = TG3_APE_LOCK_REQ;
722 gnt = TG3_APE_LOCK_GRANT;
723 } else {
724 req = TG3_APE_PER_LOCK_REQ;
725 gnt = TG3_APE_PER_LOCK_GRANT;
726 }
727
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 off = 4 * locknum;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731
732 /* Wait for up to 1 millisecond to acquire lock. */
733 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000734 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000735 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700736 break;
737 udelay(10);
738 }
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000742 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 ret = -EBUSY;
744 }
745
746 return ret;
747}
748
749static void tg3_ape_unlock(struct tg3 *tp, int locknum)
750{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000751 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700752
Joe Perches63c3a662011-04-26 08:12:10 +0000753 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 return;
755
756 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000757 case TG3_APE_LOCK_GPIO:
758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
759 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000760 case TG3_APE_LOCK_GRC:
761 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000762 if (!tp->pci_fn)
763 bit = APE_LOCK_GRANT_DRIVER;
764 else
765 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000766 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000767 case TG3_APE_LOCK_PHY0:
768 case TG3_APE_LOCK_PHY1:
769 case TG3_APE_LOCK_PHY2:
770 case TG3_APE_LOCK_PHY3:
771 bit = APE_LOCK_GRANT_DRIVER;
772 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000773 default:
774 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700775 }
776
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000777 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
778 gnt = TG3_APE_LOCK_GRANT;
779 else
780 gnt = TG3_APE_PER_LOCK_GRANT;
781
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000782 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700783}
784
Matt Carlsonb65a3722012-07-16 16:24:00 +0000785static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000786{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000787 u32 apedata;
788
Matt Carlsonb65a3722012-07-16 16:24:00 +0000789 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000790 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000791 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000792
793 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000794 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
795 break;
796
Matt Carlsonb65a3722012-07-16 16:24:00 +0000797 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
798
799 udelay(10);
800 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000801 }
802
Matt Carlsonb65a3722012-07-16 16:24:00 +0000803 return timeout_us ? 0 : -EBUSY;
804}
805
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000806static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
807{
808 u32 i, apedata;
809
810 for (i = 0; i < timeout_us / 10; i++) {
811 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
812
813 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
814 break;
815
816 udelay(10);
817 }
818
819 return i == timeout_us / 10;
820}
821
Michael Chan86449942012-10-02 20:31:14 -0700822static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
823 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000824{
825 int err;
826 u32 i, bufoff, msgoff, maxlen, apedata;
827
828 if (!tg3_flag(tp, APE_HAS_NCSI))
829 return 0;
830
831 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
832 if (apedata != APE_SEG_SIG_MAGIC)
833 return -ENODEV;
834
835 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
836 if (!(apedata & APE_FW_STATUS_READY))
837 return -EAGAIN;
838
839 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
840 TG3_APE_SHMEM_BASE;
841 msgoff = bufoff + 2 * sizeof(u32);
842 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
843
844 while (len) {
845 u32 length;
846
847 /* Cap xfer sizes to scratchpad limits. */
848 length = (len > maxlen) ? maxlen : len;
849 len -= length;
850
851 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
852 if (!(apedata & APE_FW_STATUS_READY))
853 return -EAGAIN;
854
855 /* Wait for up to 1 msec for APE to service previous event. */
856 err = tg3_ape_event_lock(tp, 1000);
857 if (err)
858 return err;
859
860 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
861 APE_EVENT_STATUS_SCRTCHPD_READ |
862 APE_EVENT_STATUS_EVENT_PENDING;
863 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
864
865 tg3_ape_write32(tp, bufoff, base_off);
866 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
867
868 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
869 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
870
871 base_off += length;
872
873 if (tg3_ape_wait_for_event(tp, 30000))
874 return -EAGAIN;
875
876 for (i = 0; length; i += 4, length -= 4) {
877 u32 val = tg3_ape_read32(tp, msgoff + i);
878 memcpy(data, &val, sizeof(u32));
879 data++;
880 }
881 }
882
883 return 0;
884}
885
Matt Carlsonb65a3722012-07-16 16:24:00 +0000886static int tg3_ape_send_event(struct tg3 *tp, u32 event)
887{
888 int err;
889 u32 apedata;
890
891 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
892 if (apedata != APE_SEG_SIG_MAGIC)
893 return -EAGAIN;
894
895 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
896 if (!(apedata & APE_FW_STATUS_READY))
897 return -EAGAIN;
898
899 /* Wait for up to 1 millisecond for APE to service previous event. */
900 err = tg3_ape_event_lock(tp, 1000);
901 if (err)
902 return err;
903
904 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
905 event | APE_EVENT_STATUS_EVENT_PENDING);
906
907 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
908 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
909
910 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000911}
912
913static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
914{
915 u32 event;
916 u32 apedata;
917
918 if (!tg3_flag(tp, ENABLE_APE))
919 return;
920
921 switch (kind) {
922 case RESET_KIND_INIT:
923 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
924 APE_HOST_SEG_SIG_MAGIC);
925 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
926 APE_HOST_SEG_LEN_MAGIC);
927 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
928 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
929 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
930 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
931 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
932 APE_HOST_BEHAV_NO_PHYLOCK);
933 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
934 TG3_APE_HOST_DRVR_STATE_START);
935
936 event = APE_EVENT_STATUS_STATE_START;
937 break;
938 case RESET_KIND_SHUTDOWN:
939 /* With the interface we are currently using,
940 * APE does not track driver state. Wiping
941 * out the HOST SEGMENT SIGNATURE forces
942 * the APE to assume OS absent status.
943 */
944 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
945
946 if (device_may_wakeup(&tp->pdev->dev) &&
947 tg3_flag(tp, WOL_ENABLE)) {
948 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
949 TG3_APE_HOST_WOL_SPEED_AUTO);
950 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
951 } else
952 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
953
954 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
955
956 event = APE_EVENT_STATUS_STATE_UNLOAD;
957 break;
958 case RESET_KIND_SUSPEND:
959 event = APE_EVENT_STATUS_STATE_SUSPEND;
960 break;
961 default:
962 return;
963 }
964
965 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
966
967 tg3_ape_send_event(tp, event);
968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static void tg3_disable_ints(struct tg3 *tp)
971{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000972 int i;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 tw32(TG3PCI_MISC_HOST_CTRL,
975 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000976 for (i = 0; i < tp->irq_max; i++)
977 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static void tg3_enable_ints(struct tg3 *tp)
981{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000982 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000983
Michael Chanbbe832c2005-06-24 20:20:04 -0700984 tp->irq_sync = 0;
985 wmb();
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 tw32(TG3PCI_MISC_HOST_CTRL,
988 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000989
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000990 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 for (i = 0; i < tp->irq_cnt; i++) {
992 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000993
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000995 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000996 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
997
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000998 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000999 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001000
1001 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001002 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001003 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1004 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1005 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001006 tw32(HOSTCC_MODE, tp->coal_now);
1007
1008 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Matt Carlson17375d22009-08-28 14:02:18 +00001011static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001012{
Matt Carlson17375d22009-08-28 14:02:18 +00001013 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001014 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001015 unsigned int work_exists = 0;
1016
1017 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001018 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001019 if (sblk->status & SD_STATUS_LINK_CHG)
1020 work_exists = 1;
1021 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001022
1023 /* check for TX work to do */
1024 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1025 work_exists = 1;
1026
1027 /* check for RX work to do */
1028 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001029 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001030 work_exists = 1;
1031
1032 return work_exists;
1033}
1034
Matt Carlson17375d22009-08-28 14:02:18 +00001035/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001036 * similar to tg3_enable_ints, but it accurately determines whether there
1037 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001038 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Matt Carlson17375d22009-08-28 14:02:18 +00001040static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Matt Carlson17375d22009-08-28 14:02:18 +00001042 struct tg3 *tp = tnapi->tp;
1043
Matt Carlson898a56f2009-08-28 14:02:40 +00001044 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 mmiowb();
1046
David S. Millerfac9b832005-05-18 22:46:34 -07001047 /* When doing tagged status, this work check is unnecessary.
1048 * The last_tag we write above tells the chip which piece of
1049 * work we've completed.
1050 */
Joe Perches63c3a662011-04-26 08:12:10 +00001051 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001052 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001053 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static void tg3_switch_clocks(struct tg3 *tp)
1057{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001058 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 u32 orig_clock_ctrl;
1060
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001062 return;
1063
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001064 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 orig_clock_ctrl = clock_ctrl;
1067 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1068 CLOCK_CTRL_CLKRUN_OENABLE |
1069 0x1f);
1070 tp->pci_clock_ctrl = clock_ctrl;
1071
Joe Perches63c3a662011-04-26 08:12:10 +00001072 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001074 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1075 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001078 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1079 clock_ctrl |
1080 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1081 40);
1082 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1083 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1084 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001086 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089#define PHY_BUSY_LOOPS 5000
1090
1091static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1092{
1093 u32 frame_val;
1094 unsigned int loops;
1095 int ret;
1096
1097 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1098 tw32_f(MAC_MI_MODE,
1099 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1100 udelay(80);
1101 }
1102
Michael Chan8151ad52012-07-29 19:15:41 +00001103 tg3_ape_lock(tp, tp->phy_ape_lock);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *val = 0x0;
1106
Matt Carlson882e9792009-09-01 13:21:36 +00001107 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 MI_COM_PHY_ADDR_MASK);
1109 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1110 MI_COM_REG_ADDR_MASK);
1111 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 tw32_f(MAC_MI_COM, frame_val);
1114
1115 loops = PHY_BUSY_LOOPS;
1116 while (loops != 0) {
1117 udelay(10);
1118 frame_val = tr32(MAC_MI_COM);
1119
1120 if ((frame_val & MI_COM_BUSY) == 0) {
1121 udelay(5);
1122 frame_val = tr32(MAC_MI_COM);
1123 break;
1124 }
1125 loops -= 1;
1126 }
1127
1128 ret = -EBUSY;
1129 if (loops != 0) {
1130 *val = frame_val & MI_COM_DATA_MASK;
1131 ret = 0;
1132 }
1133
1134 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1135 tw32_f(MAC_MI_MODE, tp->mi_mode);
1136 udelay(80);
1137 }
1138
Michael Chan8151ad52012-07-29 19:15:41 +00001139 tg3_ape_unlock(tp, tp->phy_ape_lock);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return ret;
1142}
1143
1144static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1145{
1146 u32 frame_val;
1147 unsigned int loops;
1148 int ret;
1149
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001150 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001151 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001152 return 0;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1155 tw32_f(MAC_MI_MODE,
1156 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1157 udelay(80);
1158 }
1159
Michael Chan8151ad52012-07-29 19:15:41 +00001160 tg3_ape_lock(tp, tp->phy_ape_lock);
1161
Matt Carlson882e9792009-09-01 13:21:36 +00001162 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 MI_COM_PHY_ADDR_MASK);
1164 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1165 MI_COM_REG_ADDR_MASK);
1166 frame_val |= (val & MI_COM_DATA_MASK);
1167 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 tw32_f(MAC_MI_COM, frame_val);
1170
1171 loops = PHY_BUSY_LOOPS;
1172 while (loops != 0) {
1173 udelay(10);
1174 frame_val = tr32(MAC_MI_COM);
1175 if ((frame_val & MI_COM_BUSY) == 0) {
1176 udelay(5);
1177 frame_val = tr32(MAC_MI_COM);
1178 break;
1179 }
1180 loops -= 1;
1181 }
1182
1183 ret = -EBUSY;
1184 if (loops != 0)
1185 ret = 0;
1186
1187 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1188 tw32_f(MAC_MI_MODE, tp->mi_mode);
1189 udelay(80);
1190 }
1191
Michael Chan8151ad52012-07-29 19:15:41 +00001192 tg3_ape_unlock(tp, tp->phy_ape_lock);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ret;
1195}
1196
Matt Carlsonb0988c12011-04-20 07:57:39 +00001197static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1198{
1199 int err;
1200
1201 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1202 if (err)
1203 goto done;
1204
1205 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1206 if (err)
1207 goto done;
1208
1209 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1210 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1211 if (err)
1212 goto done;
1213
1214 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1215
1216done:
1217 return err;
1218}
1219
1220static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1221{
1222 int err;
1223
1224 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1225 if (err)
1226 goto done;
1227
1228 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1229 if (err)
1230 goto done;
1231
1232 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1233 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1234 if (err)
1235 goto done;
1236
1237 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1238
1239done:
1240 return err;
1241}
1242
1243static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1244{
1245 int err;
1246
1247 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1248 if (!err)
1249 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1250
1251 return err;
1252}
1253
1254static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1255{
1256 int err;
1257
1258 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1259 if (!err)
1260 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1261
1262 return err;
1263}
1264
Matt Carlson15ee95c2011-04-20 07:57:40 +00001265static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1270 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1271 MII_TG3_AUXCTL_SHDWSEL_MISC);
1272 if (!err)
1273 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1274
1275 return err;
1276}
1277
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001278static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1279{
1280 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1281 set |= MII_TG3_AUXCTL_MISC_WREN;
1282
1283 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1284}
1285
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001286static int tg3_phy_toggle_auxctl_smdsp(struct tg3 *tp, bool enable)
1287{
1288 u32 val;
1289 int err;
Matt Carlson1d36ba42011-04-20 07:57:42 +00001290
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00001291 err = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1292
1293 if (err)
1294 return err;
1295 if (enable)
1296
1297 val |= MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1298 else
1299 val &= ~MII_TG3_AUXCTL_ACTL_SMDSP_ENA;
1300
1301 err = tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1302 val | MII_TG3_AUXCTL_ACTL_TX_6DB);
1303
1304 return err;
1305}
Matt Carlson1d36ba42011-04-20 07:57:42 +00001306
Matt Carlson95e28692008-05-25 23:44:14 -07001307static int tg3_bmcr_reset(struct tg3 *tp)
1308{
1309 u32 phy_control;
1310 int limit, err;
1311
1312 /* OK, reset it, and poll the BMCR_RESET bit until it
1313 * clears or we time out.
1314 */
1315 phy_control = BMCR_RESET;
1316 err = tg3_writephy(tp, MII_BMCR, phy_control);
1317 if (err != 0)
1318 return -EBUSY;
1319
1320 limit = 5000;
1321 while (limit--) {
1322 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1323 if (err != 0)
1324 return -EBUSY;
1325
1326 if ((phy_control & BMCR_RESET) == 0) {
1327 udelay(40);
1328 break;
1329 }
1330 udelay(10);
1331 }
Roel Kluind4675b52009-02-12 16:33:27 -08001332 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001333 return -EBUSY;
1334
1335 return 0;
1336}
1337
Matt Carlson158d7ab2008-05-29 01:37:54 -07001338static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1339{
Francois Romieu3d165432009-01-19 16:56:50 -08001340 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001341 u32 val;
1342
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001343 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001344
1345 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001346 val = -EIO;
1347
1348 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001349
1350 return val;
1351}
1352
1353static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1354{
Francois Romieu3d165432009-01-19 16:56:50 -08001355 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001356 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001357
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001358 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001359
1360 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001361 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001362
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001363 spin_unlock_bh(&tp->lock);
1364
1365 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001366}
1367
1368static int tg3_mdio_reset(struct mii_bus *bp)
1369{
1370 return 0;
1371}
1372
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001373static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001374{
1375 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001376 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001377
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001378 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001379 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001380 case PHY_ID_BCM50610:
1381 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001382 val = MAC_PHYCFG2_50610_LED_MODES;
1383 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001384 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 val = MAC_PHYCFG2_AC131_LED_MODES;
1386 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001387 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001388 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1389 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001390 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001391 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1392 break;
1393 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001395 }
1396
1397 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1398 tw32(MAC_PHYCFG2, val);
1399
1400 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001401 val &= ~(MAC_PHYCFG1_RGMII_INT |
1402 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1403 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001404 tw32(MAC_PHYCFG1, val);
1405
1406 return;
1407 }
1408
Joe Perches63c3a662011-04-26 08:12:10 +00001409 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001410 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1411 MAC_PHYCFG2_FMODE_MASK_MASK |
1412 MAC_PHYCFG2_GMODE_MASK_MASK |
1413 MAC_PHYCFG2_ACT_MASK_MASK |
1414 MAC_PHYCFG2_QUAL_MASK_MASK |
1415 MAC_PHYCFG2_INBAND_ENABLE;
1416
1417 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001418
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001419 val = tr32(MAC_PHYCFG1);
1420 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1421 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001422 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1423 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001424 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001425 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001426 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1427 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001428 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1429 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1430 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001431
Matt Carlsona9daf362008-05-25 23:49:44 -07001432 val = tr32(MAC_EXT_RGMII_MODE);
1433 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1434 MAC_RGMII_MODE_RX_QUALITY |
1435 MAC_RGMII_MODE_RX_ACTIVITY |
1436 MAC_RGMII_MODE_RX_ENG_DET |
1437 MAC_RGMII_MODE_TX_ENABLE |
1438 MAC_RGMII_MODE_TX_LOWPWR |
1439 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001440 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1441 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001442 val |= MAC_RGMII_MODE_RX_INT_B |
1443 MAC_RGMII_MODE_RX_QUALITY |
1444 MAC_RGMII_MODE_RX_ACTIVITY |
1445 MAC_RGMII_MODE_RX_ENG_DET;
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_RGMII_MODE_TX_ENABLE |
1448 MAC_RGMII_MODE_TX_LOWPWR |
1449 MAC_RGMII_MODE_TX_RESET;
1450 }
1451 tw32(MAC_EXT_RGMII_MODE, val);
1452}
1453
Matt Carlson158d7ab2008-05-29 01:37:54 -07001454static void tg3_mdio_start(struct tg3 *tp)
1455{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001456 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1457 tw32_f(MAC_MI_MODE, tp->mi_mode);
1458 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001459
Joe Perches63c3a662011-04-26 08:12:10 +00001460 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001461 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1462 tg3_mdio_config_5785(tp);
1463}
1464
1465static int tg3_mdio_init(struct tg3 *tp)
1466{
1467 int i;
1468 u32 reg;
1469 struct phy_device *phydev;
1470
Joe Perches63c3a662011-04-26 08:12:10 +00001471 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001472 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001473
Matt Carlson69f11c92011-07-13 09:27:30 +00001474 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001475
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001476 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1477 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1478 else
1479 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1480 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001481 if (is_serdes)
1482 tp->phy_addr += 7;
1483 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001484 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001485
Matt Carlson158d7ab2008-05-29 01:37:54 -07001486 tg3_mdio_start(tp);
1487
Joe Perches63c3a662011-04-26 08:12:10 +00001488 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001489 return 0;
1490
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001491 tp->mdio_bus = mdiobus_alloc();
1492 if (tp->mdio_bus == NULL)
1493 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001494
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001495 tp->mdio_bus->name = "tg3 mdio bus";
1496 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001497 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001498 tp->mdio_bus->priv = tp;
1499 tp->mdio_bus->parent = &tp->pdev->dev;
1500 tp->mdio_bus->read = &tg3_mdio_read;
1501 tp->mdio_bus->write = &tg3_mdio_write;
1502 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001503 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001504 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001505
1506 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001507 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001508
1509 /* The bus registration will look for all the PHYs on the mdio bus.
1510 * Unfortunately, it does not ensure the PHY is powered up before
1511 * accessing the PHY ID registers. A chip reset is the
1512 * quickest way to bring the device back to an operational state..
1513 */
1514 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1515 tg3_bmcr_reset(tp);
1516
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001517 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001518 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001519 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001520 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001521 return i;
1522 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001523
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001524 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001525
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001526 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001527 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001528 mdiobus_unregister(tp->mdio_bus);
1529 mdiobus_free(tp->mdio_bus);
1530 return -ENODEV;
1531 }
1532
1533 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001534 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001535 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001536 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001537 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001538 case PHY_ID_BCM50610:
1539 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001540 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001541 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001542 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001543 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001544 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001545 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001546 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001547 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001548 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001549 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001550 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001551 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001552 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001553 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001554 case PHY_ID_RTL8201E:
1555 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001556 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001557 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001558 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001559 break;
1560 }
1561
Joe Perches63c3a662011-04-26 08:12:10 +00001562 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001563
1564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1565 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001566
1567 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001568}
1569
1570static void tg3_mdio_fini(struct tg3 *tp)
1571{
Joe Perches63c3a662011-04-26 08:12:10 +00001572 if (tg3_flag(tp, MDIOBUS_INITED)) {
1573 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001574 mdiobus_unregister(tp->mdio_bus);
1575 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001576 }
1577}
1578
Matt Carlson95e28692008-05-25 23:44:14 -07001579/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001580static inline void tg3_generate_fw_event(struct tg3 *tp)
1581{
1582 u32 val;
1583
1584 val = tr32(GRC_RX_CPU_EVENT);
1585 val |= GRC_RX_CPU_DRIVER_EVENT;
1586 tw32_f(GRC_RX_CPU_EVENT, val);
1587
1588 tp->last_event_jiffies = jiffies;
1589}
1590
1591#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1592
1593/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001594static void tg3_wait_for_event_ack(struct tg3 *tp)
1595{
1596 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001597 unsigned int delay_cnt;
1598 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001599
Matt Carlson4ba526c2008-08-15 14:10:04 -07001600 /* If enough time has passed, no wait is necessary. */
1601 time_remain = (long)(tp->last_event_jiffies + 1 +
1602 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1603 (long)jiffies;
1604 if (time_remain < 0)
1605 return;
1606
1607 /* Check if we can shorten the wait time. */
1608 delay_cnt = jiffies_to_usecs(time_remain);
1609 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1610 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1611 delay_cnt = (delay_cnt >> 3) + 1;
1612
1613 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001614 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1615 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001616 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001617 }
1618}
1619
1620/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001621static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001622{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001623 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001624
1625 val = 0;
1626 if (!tg3_readphy(tp, MII_BMCR, &reg))
1627 val = reg << 16;
1628 if (!tg3_readphy(tp, MII_BMSR, &reg))
1629 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001630 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001631
1632 val = 0;
1633 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1634 val = reg << 16;
1635 if (!tg3_readphy(tp, MII_LPA, &reg))
1636 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001637 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001638
1639 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001640 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001641 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1642 val = reg << 16;
1643 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1644 val |= (reg & 0xffff);
1645 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001646 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001647
1648 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1649 val = reg << 16;
1650 else
1651 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001652 *data++ = val;
1653}
1654
1655/* tp->lock is held. */
1656static void tg3_ump_link_report(struct tg3 *tp)
1657{
1658 u32 data[4];
1659
1660 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1661 return;
1662
1663 tg3_phy_gather_ump_data(tp, data);
1664
1665 tg3_wait_for_event_ack(tp);
1666
1667 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1668 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1669 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1670 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1671 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1672 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001673
Matt Carlson4ba526c2008-08-15 14:10:04 -07001674 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001675}
1676
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001677/* tp->lock is held. */
1678static void tg3_stop_fw(struct tg3 *tp)
1679{
1680 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1681 /* Wait for RX cpu to ACK the previous event. */
1682 tg3_wait_for_event_ack(tp);
1683
1684 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1685
1686 tg3_generate_fw_event(tp);
1687
1688 /* Wait for RX cpu to ACK this event. */
1689 tg3_wait_for_event_ack(tp);
1690 }
1691}
1692
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001693/* tp->lock is held. */
1694static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1695{
1696 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1697 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1698
1699 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1700 switch (kind) {
1701 case RESET_KIND_INIT:
1702 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1703 DRV_STATE_START);
1704 break;
1705
1706 case RESET_KIND_SHUTDOWN:
1707 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1708 DRV_STATE_UNLOAD);
1709 break;
1710
1711 case RESET_KIND_SUSPEND:
1712 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1713 DRV_STATE_SUSPEND);
1714 break;
1715
1716 default:
1717 break;
1718 }
1719 }
1720
1721 if (kind == RESET_KIND_INIT ||
1722 kind == RESET_KIND_SUSPEND)
1723 tg3_ape_driver_state_change(tp, kind);
1724}
1725
1726/* tp->lock is held. */
1727static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1728{
1729 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1730 switch (kind) {
1731 case RESET_KIND_INIT:
1732 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1733 DRV_STATE_START_DONE);
1734 break;
1735
1736 case RESET_KIND_SHUTDOWN:
1737 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1738 DRV_STATE_UNLOAD_DONE);
1739 break;
1740
1741 default:
1742 break;
1743 }
1744 }
1745
1746 if (kind == RESET_KIND_SHUTDOWN)
1747 tg3_ape_driver_state_change(tp, kind);
1748}
1749
1750/* tp->lock is held. */
1751static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1752{
1753 if (tg3_flag(tp, ENABLE_ASF)) {
1754 switch (kind) {
1755 case RESET_KIND_INIT:
1756 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1757 DRV_STATE_START);
1758 break;
1759
1760 case RESET_KIND_SHUTDOWN:
1761 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1762 DRV_STATE_UNLOAD);
1763 break;
1764
1765 case RESET_KIND_SUSPEND:
1766 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1767 DRV_STATE_SUSPEND);
1768 break;
1769
1770 default:
1771 break;
1772 }
1773 }
1774}
1775
1776static int tg3_poll_fw(struct tg3 *tp)
1777{
1778 int i;
1779 u32 val;
1780
1781 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1782 /* Wait up to 20ms for init done. */
1783 for (i = 0; i < 200; i++) {
1784 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1785 return 0;
1786 udelay(100);
1787 }
1788 return -ENODEV;
1789 }
1790
1791 /* Wait for firmware initialization to complete. */
1792 for (i = 0; i < 100000; i++) {
1793 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1794 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1795 break;
1796 udelay(10);
1797 }
1798
1799 /* Chip might not be fitted with firmware. Some Sun onboard
1800 * parts are configured like that. So don't signal the timeout
1801 * of the above loop as an error, but do report the lack of
1802 * running firmware once.
1803 */
1804 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1805 tg3_flag_set(tp, NO_FWARE_REPORTED);
1806
1807 netdev_info(tp->dev, "No firmware running\n");
1808 }
1809
1810 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1811 /* The 57765 A0 needs a little more
1812 * time to do some important work.
1813 */
1814 mdelay(10);
1815 }
1816
1817 return 0;
1818}
1819
Matt Carlson95e28692008-05-25 23:44:14 -07001820static void tg3_link_report(struct tg3 *tp)
1821{
1822 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001823 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001824 tg3_ump_link_report(tp);
1825 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001826 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1827 (tp->link_config.active_speed == SPEED_1000 ?
1828 1000 :
1829 (tp->link_config.active_speed == SPEED_100 ?
1830 100 : 10)),
1831 (tp->link_config.active_duplex == DUPLEX_FULL ?
1832 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001833
Joe Perches05dbe002010-02-17 19:44:19 +00001834 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1835 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1836 "on" : "off",
1837 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1838 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001839
1840 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1841 netdev_info(tp->dev, "EEE is %s\n",
1842 tp->setlpicnt ? "enabled" : "disabled");
1843
Matt Carlson95e28692008-05-25 23:44:14 -07001844 tg3_ump_link_report(tp);
1845 }
1846}
1847
Matt Carlson95e28692008-05-25 23:44:14 -07001848static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1849{
1850 u16 miireg;
1851
Steve Glendinninge18ce342008-12-16 02:00:00 -08001852 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001853 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001854 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001855 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001856 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001857 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1858 else
1859 miireg = 0;
1860
1861 return miireg;
1862}
1863
Matt Carlson95e28692008-05-25 23:44:14 -07001864static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1865{
1866 u8 cap = 0;
1867
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001868 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1869 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1870 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1871 if (lcladv & ADVERTISE_1000XPAUSE)
1872 cap = FLOW_CTRL_RX;
1873 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001874 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001875 }
1876
1877 return cap;
1878}
1879
Matt Carlsonf51f3562008-05-25 23:45:08 -07001880static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001881{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001882 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001883 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001884 u32 old_rx_mode = tp->rx_mode;
1885 u32 old_tx_mode = tp->tx_mode;
1886
Joe Perches63c3a662011-04-26 08:12:10 +00001887 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001888 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001889 else
1890 autoneg = tp->link_config.autoneg;
1891
Joe Perches63c3a662011-04-26 08:12:10 +00001892 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001893 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001894 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001895 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001896 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001897 } else
1898 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001899
Matt Carlsonf51f3562008-05-25 23:45:08 -07001900 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001901
Steve Glendinninge18ce342008-12-16 02:00:00 -08001902 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001903 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1904 else
1905 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1906
Matt Carlsonf51f3562008-05-25 23:45:08 -07001907 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001908 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001909
Steve Glendinninge18ce342008-12-16 02:00:00 -08001910 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001911 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1912 else
1913 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1914
Matt Carlsonf51f3562008-05-25 23:45:08 -07001915 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001916 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001917}
1918
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001919static void tg3_adjust_link(struct net_device *dev)
1920{
1921 u8 oldflowctrl, linkmesg = 0;
1922 u32 mac_mode, lcl_adv, rmt_adv;
1923 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001924 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001925
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001926 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001927
1928 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1929 MAC_MODE_HALF_DUPLEX);
1930
1931 oldflowctrl = tp->link_config.active_flowctrl;
1932
1933 if (phydev->link) {
1934 lcl_adv = 0;
1935 rmt_adv = 0;
1936
1937 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1938 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001939 else if (phydev->speed == SPEED_1000 ||
1940 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001941 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001942 else
1943 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001944
1945 if (phydev->duplex == DUPLEX_HALF)
1946 mac_mode |= MAC_MODE_HALF_DUPLEX;
1947 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001948 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001949 tp->link_config.flowctrl);
1950
1951 if (phydev->pause)
1952 rmt_adv = LPA_PAUSE_CAP;
1953 if (phydev->asym_pause)
1954 rmt_adv |= LPA_PAUSE_ASYM;
1955 }
1956
1957 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1958 } else
1959 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1960
1961 if (mac_mode != tp->mac_mode) {
1962 tp->mac_mode = mac_mode;
1963 tw32_f(MAC_MODE, tp->mac_mode);
1964 udelay(40);
1965 }
1966
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1968 if (phydev->speed == SPEED_10)
1969 tw32(MAC_MI_STAT,
1970 MAC_MI_STAT_10MBPS_MODE |
1971 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1972 else
1973 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1974 }
1975
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1977 tw32(MAC_TX_LENGTHS,
1978 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1979 (6 << TX_LENGTHS_IPG_SHIFT) |
1980 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1981 else
1982 tw32(MAC_TX_LENGTHS,
1983 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1984 (6 << TX_LENGTHS_IPG_SHIFT) |
1985 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1986
Matt Carlson34655ad2012-02-22 12:35:18 +00001987 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001988 phydev->speed != tp->link_config.active_speed ||
1989 phydev->duplex != tp->link_config.active_duplex ||
1990 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001991 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001992
Matt Carlson34655ad2012-02-22 12:35:18 +00001993 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001994 tp->link_config.active_speed = phydev->speed;
1995 tp->link_config.active_duplex = phydev->duplex;
1996
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001997 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001998
1999 if (linkmesg)
2000 tg3_link_report(tp);
2001}
2002
2003static int tg3_phy_init(struct tg3 *tp)
2004{
2005 struct phy_device *phydev;
2006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002007 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002008 return 0;
2009
2010 /* Bring the PHY back to a known state. */
2011 tg3_bmcr_reset(tp);
2012
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002013 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002014
2015 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08002016 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07002017 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002018 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002019 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002020 return PTR_ERR(phydev);
2021 }
2022
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002023 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002024 switch (phydev->interface) {
2025 case PHY_INTERFACE_MODE_GMII:
2026 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002027 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002028 phydev->supported &= (PHY_GBIT_FEATURES |
2029 SUPPORTED_Pause |
2030 SUPPORTED_Asym_Pause);
2031 break;
2032 }
2033 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002034 case PHY_INTERFACE_MODE_MII:
2035 phydev->supported &= (PHY_BASIC_FEATURES |
2036 SUPPORTED_Pause |
2037 SUPPORTED_Asym_Pause);
2038 break;
2039 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002040 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002041 return -EINVAL;
2042 }
2043
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002044 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002045
2046 phydev->advertising = phydev->supported;
2047
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002048 return 0;
2049}
2050
2051static void tg3_phy_start(struct tg3 *tp)
2052{
2053 struct phy_device *phydev;
2054
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002055 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002056 return;
2057
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002058 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002059
Matt Carlson80096062010-08-02 11:26:06 +00002060 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2061 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002062 phydev->speed = tp->link_config.speed;
2063 phydev->duplex = tp->link_config.duplex;
2064 phydev->autoneg = tp->link_config.autoneg;
2065 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002066 }
2067
2068 phy_start(phydev);
2069
2070 phy_start_aneg(phydev);
2071}
2072
2073static void tg3_phy_stop(struct tg3 *tp)
2074{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002075 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002076 return;
2077
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002078 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002079}
2080
2081static void tg3_phy_fini(struct tg3 *tp)
2082{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002083 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002084 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002085 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002086 }
2087}
2088
Matt Carlson941ec902011-08-19 13:58:23 +00002089static int tg3_phy_set_extloopbk(struct tg3 *tp)
2090{
2091 int err;
2092 u32 val;
2093
2094 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2095 return 0;
2096
2097 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2098 /* Cannot do read-modify-write on 5401 */
2099 err = tg3_phy_auxctl_write(tp,
2100 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2101 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2102 0x4c20);
2103 goto done;
2104 }
2105
2106 err = tg3_phy_auxctl_read(tp,
2107 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2108 if (err)
2109 return err;
2110
2111 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2112 err = tg3_phy_auxctl_write(tp,
2113 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2114
2115done:
2116 return err;
2117}
2118
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002119static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2120{
2121 u32 phytest;
2122
2123 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2124 u32 phy;
2125
2126 tg3_writephy(tp, MII_TG3_FET_TEST,
2127 phytest | MII_TG3_FET_SHADOW_EN);
2128 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2129 if (enable)
2130 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2131 else
2132 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2133 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2134 }
2135 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2136 }
2137}
2138
Matt Carlson6833c042008-11-21 17:18:59 -08002139static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2140{
2141 u32 reg;
2142
Joe Perches63c3a662011-04-26 08:12:10 +00002143 if (!tg3_flag(tp, 5705_PLUS) ||
2144 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002145 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002146 return;
2147
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002148 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002149 tg3_phy_fet_toggle_apd(tp, enable);
2150 return;
2151 }
2152
Matt Carlson6833c042008-11-21 17:18:59 -08002153 reg = MII_TG3_MISC_SHDW_WREN |
2154 MII_TG3_MISC_SHDW_SCR5_SEL |
2155 MII_TG3_MISC_SHDW_SCR5_LPED |
2156 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2157 MII_TG3_MISC_SHDW_SCR5_SDTL |
2158 MII_TG3_MISC_SHDW_SCR5_C125OE;
2159 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2160 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2161
2162 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2163
2164
2165 reg = MII_TG3_MISC_SHDW_WREN |
2166 MII_TG3_MISC_SHDW_APD_SEL |
2167 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2168 if (enable)
2169 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2170
2171 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2172}
2173
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002174static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2175{
2176 u32 phy;
2177
Joe Perches63c3a662011-04-26 08:12:10 +00002178 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002179 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002180 return;
2181
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002182 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002183 u32 ephy;
2184
Matt Carlson535ef6e2009-08-25 10:09:36 +00002185 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2186 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2187
2188 tg3_writephy(tp, MII_TG3_FET_TEST,
2189 ephy | MII_TG3_FET_SHADOW_EN);
2190 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002191 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002192 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002193 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002194 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2195 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002196 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002197 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002198 }
2199 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002200 int ret;
2201
2202 ret = tg3_phy_auxctl_read(tp,
2203 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2204 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002205 if (enable)
2206 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2207 else
2208 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002209 tg3_phy_auxctl_write(tp,
2210 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002211 }
2212 }
2213}
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215static void tg3_phy_set_wirespeed(struct tg3 *tp)
2216{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002217 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 u32 val;
2219
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002220 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return;
2222
Matt Carlson15ee95c2011-04-20 07:57:40 +00002223 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2224 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002225 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2226 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002229static void tg3_phy_apply_otp(struct tg3 *tp)
2230{
2231 u32 otp, phy;
2232
2233 if (!tp->phy_otp)
2234 return;
2235
2236 otp = tp->phy_otp;
2237
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002238 if (tg3_phy_toggle_auxctl_smdsp(tp, true))
Matt Carlson1d36ba42011-04-20 07:57:42 +00002239 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002240
2241 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2242 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2243 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2244
2245 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2246 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2247 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2248
2249 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2250 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2251 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2252
2253 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2254 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2255
2256 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2257 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2258
2259 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2260 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2261 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2262
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002263 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002264}
2265
Matt Carlson52b02d02010-10-14 10:37:41 +00002266static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2267{
2268 u32 val;
2269
2270 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2271 return;
2272
2273 tp->setlpicnt = 0;
2274
2275 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2276 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002277 tp->link_config.active_duplex == DUPLEX_FULL &&
2278 (tp->link_config.active_speed == SPEED_100 ||
2279 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002280 u32 eeectl;
2281
2282 if (tp->link_config.active_speed == SPEED_1000)
2283 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2284 else
2285 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2286
2287 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2288
Matt Carlson3110f5f52010-12-06 08:28:50 +00002289 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2290 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002291
Matt Carlsonb0c59432011-05-19 12:12:48 +00002292 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2293 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002294 tp->setlpicnt = 2;
2295 }
2296
2297 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002298 if (current_link_up == 1 &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002299 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002300 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002301 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb715ce92011-07-20 10:20:52 +00002302 }
2303
Matt Carlson52b02d02010-10-14 10:37:41 +00002304 val = tr32(TG3_CPMU_EEE_MODE);
2305 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2306 }
2307}
2308
Matt Carlsonb0c59432011-05-19 12:12:48 +00002309static void tg3_phy_eee_enable(struct tg3 *tp)
2310{
2311 u32 val;
2312
2313 if (tp->link_config.active_speed == SPEED_1000 &&
2314 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002316 tg3_flag(tp, 57765_CLASS)) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002317 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002318 val = MII_TG3_DSP_TAP26_ALNOKO |
2319 MII_TG3_DSP_TAP26_RMRXSTO;
2320 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002321 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002322 }
2323
2324 val = tr32(TG3_CPMU_EEE_MODE);
2325 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328static int tg3_wait_macro_done(struct tg3 *tp)
2329{
2330 int limit = 100;
2331
2332 while (limit--) {
2333 u32 tmp32;
2334
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002335 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if ((tmp32 & 0x1000) == 0)
2337 break;
2338 }
2339 }
Roel Kluind4675b52009-02-12 16:33:27 -08002340 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return -EBUSY;
2342
2343 return 0;
2344}
2345
2346static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2347{
2348 static const u32 test_pat[4][6] = {
2349 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2350 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2351 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2352 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2353 };
2354 int chan;
2355
2356 for (chan = 0; chan < 4; chan++) {
2357 int i;
2358
2359 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2360 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002361 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 for (i = 0; i < 6; i++)
2364 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2365 test_pat[chan][i]);
2366
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002367 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (tg3_wait_macro_done(tp)) {
2369 *resetp = 1;
2370 return -EBUSY;
2371 }
2372
2373 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2374 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002375 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (tg3_wait_macro_done(tp)) {
2377 *resetp = 1;
2378 return -EBUSY;
2379 }
2380
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002381 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (tg3_wait_macro_done(tp)) {
2383 *resetp = 1;
2384 return -EBUSY;
2385 }
2386
2387 for (i = 0; i < 6; i += 2) {
2388 u32 low, high;
2389
2390 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2391 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2392 tg3_wait_macro_done(tp)) {
2393 *resetp = 1;
2394 return -EBUSY;
2395 }
2396 low &= 0x7fff;
2397 high &= 0x000f;
2398 if (low != test_pat[chan][i] ||
2399 high != test_pat[chan][i+1]) {
2400 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2401 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2402 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2403
2404 return -EBUSY;
2405 }
2406 }
2407 }
2408
2409 return 0;
2410}
2411
2412static int tg3_phy_reset_chanpat(struct tg3 *tp)
2413{
2414 int chan;
2415
2416 for (chan = 0; chan < 4; chan++) {
2417 int i;
2418
2419 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2420 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002421 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 for (i = 0; i < 6; i++)
2423 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002424 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (tg3_wait_macro_done(tp))
2426 return -EBUSY;
2427 }
2428
2429 return 0;
2430}
2431
2432static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2433{
2434 u32 reg32, phy9_orig;
2435 int retries, do_phy_reset, err;
2436
2437 retries = 10;
2438 do_phy_reset = 1;
2439 do {
2440 if (do_phy_reset) {
2441 err = tg3_bmcr_reset(tp);
2442 if (err)
2443 return err;
2444 do_phy_reset = 0;
2445 }
2446
2447 /* Disable transmitter and interrupt. */
2448 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2449 continue;
2450
2451 reg32 |= 0x3000;
2452 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2453
2454 /* Set full-duplex, 1000 mbps. */
2455 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002456 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002459 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 continue;
2461
Matt Carlson221c5632011-06-13 13:39:01 +00002462 tg3_writephy(tp, MII_CTRL1000,
2463 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002465 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002466 if (err)
2467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002470 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2473 if (!err)
2474 break;
2475 } while (--retries);
2476
2477 err = tg3_phy_reset_chanpat(tp);
2478 if (err)
2479 return err;
2480
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002481 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002484 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002486 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Matt Carlson221c5632011-06-13 13:39:01 +00002488 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2491 reg32 &= ~0x3000;
2492 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2493 } else if (!err)
2494 err = -EBUSY;
2495
2496 return err;
2497}
2498
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002499static void tg3_carrier_on(struct tg3 *tp)
2500{
2501 netif_carrier_on(tp->dev);
2502 tp->link_up = true;
2503}
2504
2505static void tg3_carrier_off(struct tg3 *tp)
2506{
2507 netif_carrier_off(tp->dev);
2508 tp->link_up = false;
2509}
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511/* This will reset the tigon3 PHY if there is no valid
2512 * link unless the FORCE argument is non-zero.
2513 */
2514static int tg3_phy_reset(struct tg3 *tp)
2515{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002516 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 int err;
2518
Michael Chan60189dd2006-12-17 17:08:07 -08002519 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002520 val = tr32(GRC_MISC_CFG);
2521 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2522 udelay(40);
2523 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002524 err = tg3_readphy(tp, MII_BMSR, &val);
2525 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (err != 0)
2527 return -EBUSY;
2528
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002529 if (netif_running(tp->dev) && tp->link_up) {
2530 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002531 tg3_link_report(tp);
2532 }
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2536 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2537 err = tg3_phy_reset_5703_4_5(tp);
2538 if (err)
2539 return err;
2540 goto out;
2541 }
2542
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002543 cpmuctrl = 0;
2544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2545 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2546 cpmuctrl = tr32(TG3_CPMU_CTRL);
2547 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2548 tw32(TG3_CPMU_CTRL,
2549 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2550 }
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 err = tg3_bmcr_reset(tp);
2553 if (err)
2554 return err;
2555
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002556 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002557 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2558 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002559
2560 tw32(TG3_CPMU_CTRL, cpmuctrl);
2561 }
2562
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002563 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2564 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002565 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2566 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2567 CPMU_LSPD_1000MB_MACCLK_12_5) {
2568 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2569 udelay(40);
2570 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2571 }
2572 }
2573
Joe Perches63c3a662011-04-26 08:12:10 +00002574 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002575 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002576 return 0;
2577
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002578 tg3_phy_apply_otp(tp);
2579
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002580 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002581 tg3_phy_toggle_apd(tp, true);
2582 else
2583 tg3_phy_toggle_apd(tp, false);
2584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002586 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002587 !tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002588 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2589 tg3_phydsp_write(tp, 0x000a, 0x0323);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002590 tg3_phy_toggle_auxctl_smdsp(tp, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002592
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002593 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002594 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2595 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002597
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002598 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002599 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002600 tg3_phydsp_write(tp, 0x000a, 0x310b);
2601 tg3_phydsp_write(tp, 0x201f, 0x9506);
2602 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002603 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002604 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002605 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002606 if (!tg3_phy_toggle_auxctl_smdsp(tp, true)) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002607 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2608 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2609 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2610 tg3_writephy(tp, MII_TG3_TEST1,
2611 MII_TG3_TEST1_TRIM_EN | 0x4);
2612 } else
2613 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2614
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00002615 tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002616 }
Michael Chanc424cb22006-04-29 18:56:34 -07002617 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /* Set Extended packet length bit (bit 14) on all chips that */
2620 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002621 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002623 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002624 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002626 err = tg3_phy_auxctl_read(tp,
2627 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2628 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002629 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2630 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
2633 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2634 * jumbo frames transmission.
2635 */
Joe Perches63c3a662011-04-26 08:12:10 +00002636 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002637 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002638 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002639 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641
Michael Chan715116a2006-09-27 16:09:25 -07002642 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002643 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002644 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002645 }
2646
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002647 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 tg3_phy_set_wirespeed(tp);
2649 return 0;
2650}
2651
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002652#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2653#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2654#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2655 TG3_GPIO_MSG_NEED_VAUX)
2656#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2657 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2658 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2659 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2660 (TG3_GPIO_MSG_DRVR_PRES << 12))
2661
2662#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2663 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2664 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2665 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2666 (TG3_GPIO_MSG_NEED_VAUX << 12))
2667
2668static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2669{
2670 u32 status, shift;
2671
2672 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2674 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2675 else
2676 status = tr32(TG3_CPMU_DRV_STATUS);
2677
2678 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2679 status &= ~(TG3_GPIO_MSG_MASK << shift);
2680 status |= (newstat << shift);
2681
2682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2684 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2685 else
2686 tw32(TG3_CPMU_DRV_STATUS, status);
2687
2688 return status >> TG3_APE_GPIO_MSG_SHIFT;
2689}
2690
Matt Carlson520b2752011-06-13 13:39:02 +00002691static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2692{
2693 if (!tg3_flag(tp, IS_NIC))
2694 return 0;
2695
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002696 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2697 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2698 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2699 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2700 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002701
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002702 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2703
2704 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2705 TG3_GRC_LCLCTL_PWRSW_DELAY);
2706
2707 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2708 } else {
2709 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2710 TG3_GRC_LCLCTL_PWRSW_DELAY);
2711 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002712
Matt Carlson520b2752011-06-13 13:39:02 +00002713 return 0;
2714}
2715
2716static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2717{
2718 u32 grc_local_ctrl;
2719
2720 if (!tg3_flag(tp, IS_NIC) ||
2721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2723 return;
2724
2725 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2726
2727 tw32_wait_f(GRC_LOCAL_CTRL,
2728 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2729 TG3_GRC_LCLCTL_PWRSW_DELAY);
2730
2731 tw32_wait_f(GRC_LOCAL_CTRL,
2732 grc_local_ctrl,
2733 TG3_GRC_LCLCTL_PWRSW_DELAY);
2734
2735 tw32_wait_f(GRC_LOCAL_CTRL,
2736 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2737 TG3_GRC_LCLCTL_PWRSW_DELAY);
2738}
2739
2740static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2741{
2742 if (!tg3_flag(tp, IS_NIC))
2743 return;
2744
2745 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2746 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2747 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2748 (GRC_LCLCTRL_GPIO_OE0 |
2749 GRC_LCLCTRL_GPIO_OE1 |
2750 GRC_LCLCTRL_GPIO_OE2 |
2751 GRC_LCLCTRL_GPIO_OUTPUT0 |
2752 GRC_LCLCTRL_GPIO_OUTPUT1),
2753 TG3_GRC_LCLCTL_PWRSW_DELAY);
2754 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2755 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2756 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2757 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2758 GRC_LCLCTRL_GPIO_OE1 |
2759 GRC_LCLCTRL_GPIO_OE2 |
2760 GRC_LCLCTRL_GPIO_OUTPUT0 |
2761 GRC_LCLCTRL_GPIO_OUTPUT1 |
2762 tp->grc_local_ctrl;
2763 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2764 TG3_GRC_LCLCTL_PWRSW_DELAY);
2765
2766 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2767 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2768 TG3_GRC_LCLCTL_PWRSW_DELAY);
2769
2770 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2771 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2772 TG3_GRC_LCLCTL_PWRSW_DELAY);
2773 } else {
2774 u32 no_gpio2;
2775 u32 grc_local_ctrl = 0;
2776
2777 /* Workaround to prevent overdrawing Amps. */
2778 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2779 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2780 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2781 grc_local_ctrl,
2782 TG3_GRC_LCLCTL_PWRSW_DELAY);
2783 }
2784
2785 /* On 5753 and variants, GPIO2 cannot be used. */
2786 no_gpio2 = tp->nic_sram_data_cfg &
2787 NIC_SRAM_DATA_CFG_NO_GPIO2;
2788
2789 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2790 GRC_LCLCTRL_GPIO_OE1 |
2791 GRC_LCLCTRL_GPIO_OE2 |
2792 GRC_LCLCTRL_GPIO_OUTPUT1 |
2793 GRC_LCLCTRL_GPIO_OUTPUT2;
2794 if (no_gpio2) {
2795 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2796 GRC_LCLCTRL_GPIO_OUTPUT2);
2797 }
2798 tw32_wait_f(GRC_LOCAL_CTRL,
2799 tp->grc_local_ctrl | grc_local_ctrl,
2800 TG3_GRC_LCLCTL_PWRSW_DELAY);
2801
2802 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2803
2804 tw32_wait_f(GRC_LOCAL_CTRL,
2805 tp->grc_local_ctrl | grc_local_ctrl,
2806 TG3_GRC_LCLCTL_PWRSW_DELAY);
2807
2808 if (!no_gpio2) {
2809 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2810 tw32_wait_f(GRC_LOCAL_CTRL,
2811 tp->grc_local_ctrl | grc_local_ctrl,
2812 TG3_GRC_LCLCTL_PWRSW_DELAY);
2813 }
2814 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002815}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002816
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002817static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002818{
2819 u32 msg = 0;
2820
2821 /* Serialize power state transitions */
2822 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2823 return;
2824
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002825 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002826 msg = TG3_GPIO_MSG_NEED_VAUX;
2827
2828 msg = tg3_set_function_status(tp, msg);
2829
2830 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2831 goto done;
2832
2833 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2834 tg3_pwrsrc_switch_to_vaux(tp);
2835 else
2836 tg3_pwrsrc_die_with_vmain(tp);
2837
2838done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002839 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002840}
2841
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002842static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Matt Carlson683644b2011-03-09 16:58:23 +00002844 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Matt Carlson334355a2010-01-20 16:58:10 +00002846 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002847 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return;
2849
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2851 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002853 tg3_frob_aux_power_5717(tp, include_wol ?
2854 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002855 return;
2856 }
2857
2858 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002859 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002861 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002862
Michael Chanbc1c7562006-03-20 17:48:03 -08002863 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002864 if (dev_peer) {
2865 struct tg3 *tp_peer = netdev_priv(dev_peer);
2866
Joe Perches63c3a662011-04-26 08:12:10 +00002867 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002868 return;
2869
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002870 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002871 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002872 need_vaux = true;
2873 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002876 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2877 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002878 need_vaux = true;
2879
Matt Carlson520b2752011-06-13 13:39:02 +00002880 if (need_vaux)
2881 tg3_pwrsrc_switch_to_vaux(tp);
2882 else
2883 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002886static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2887{
2888 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2889 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002890 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002891 if (speed != SPEED_10)
2892 return 1;
2893 } else if (speed == SPEED_10)
2894 return 1;
2895
2896 return 0;
2897}
2898
Matt Carlson0a459aa2008-11-03 16:54:15 -08002899static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002900{
Matt Carlsonce057f02007-11-12 21:08:03 -08002901 u32 val;
2902
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002903 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2905 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2906 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2907
2908 sg_dig_ctrl |=
2909 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2910 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2911 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2912 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002913 return;
Michael Chan51297242007-02-13 12:17:57 -08002914 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002915
Michael Chan60189dd2006-12-17 17:08:07 -08002916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002917 tg3_bmcr_reset(tp);
2918 val = tr32(GRC_MISC_CFG);
2919 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2920 udelay(40);
2921 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002922 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002923 u32 phytest;
2924 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2925 u32 phy;
2926
2927 tg3_writephy(tp, MII_ADVERTISE, 0);
2928 tg3_writephy(tp, MII_BMCR,
2929 BMCR_ANENABLE | BMCR_ANRESTART);
2930
2931 tg3_writephy(tp, MII_TG3_FET_TEST,
2932 phytest | MII_TG3_FET_SHADOW_EN);
2933 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2934 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2935 tg3_writephy(tp,
2936 MII_TG3_FET_SHDW_AUXMODE4,
2937 phy);
2938 }
2939 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2940 }
2941 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002942 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002943 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2944 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002945
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002946 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2947 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2948 MII_TG3_AUXCTL_PCTL_VREG_11V;
2949 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002950 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002951
Michael Chan15c3b692006-03-22 01:06:52 -08002952 /* The PHY should not be powered down on some chips because
2953 * of bugs.
2954 */
2955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2957 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002958 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2959 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2960 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002961 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002962
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002963 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2964 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002965 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2966 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2967 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2968 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2969 }
2970
Michael Chan15c3b692006-03-22 01:06:52 -08002971 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2972}
2973
Matt Carlson3f007892008-11-03 16:51:36 -08002974/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002975static int tg3_nvram_lock(struct tg3 *tp)
2976{
Joe Perches63c3a662011-04-26 08:12:10 +00002977 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002978 int i;
2979
2980 if (tp->nvram_lock_cnt == 0) {
2981 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2982 for (i = 0; i < 8000; i++) {
2983 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2984 break;
2985 udelay(20);
2986 }
2987 if (i == 8000) {
2988 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2989 return -ENODEV;
2990 }
2991 }
2992 tp->nvram_lock_cnt++;
2993 }
2994 return 0;
2995}
2996
2997/* tp->lock is held. */
2998static void tg3_nvram_unlock(struct tg3 *tp)
2999{
Joe Perches63c3a662011-04-26 08:12:10 +00003000 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003001 if (tp->nvram_lock_cnt > 0)
3002 tp->nvram_lock_cnt--;
3003 if (tp->nvram_lock_cnt == 0)
3004 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
3005 }
3006}
3007
3008/* tp->lock is held. */
3009static void tg3_enable_nvram_access(struct tg3 *tp)
3010{
Joe Perches63c3a662011-04-26 08:12:10 +00003011 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003012 u32 nvaccess = tr32(NVRAM_ACCESS);
3013
3014 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3015 }
3016}
3017
3018/* tp->lock is held. */
3019static void tg3_disable_nvram_access(struct tg3 *tp)
3020{
Joe Perches63c3a662011-04-26 08:12:10 +00003021 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003022 u32 nvaccess = tr32(NVRAM_ACCESS);
3023
3024 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3025 }
3026}
3027
3028static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3029 u32 offset, u32 *val)
3030{
3031 u32 tmp;
3032 int i;
3033
3034 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3035 return -EINVAL;
3036
3037 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3038 EEPROM_ADDR_DEVID_MASK |
3039 EEPROM_ADDR_READ);
3040 tw32(GRC_EEPROM_ADDR,
3041 tmp |
3042 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3043 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3044 EEPROM_ADDR_ADDR_MASK) |
3045 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3046
3047 for (i = 0; i < 1000; i++) {
3048 tmp = tr32(GRC_EEPROM_ADDR);
3049
3050 if (tmp & EEPROM_ADDR_COMPLETE)
3051 break;
3052 msleep(1);
3053 }
3054 if (!(tmp & EEPROM_ADDR_COMPLETE))
3055 return -EBUSY;
3056
Matt Carlson62cedd12009-04-20 14:52:29 -07003057 tmp = tr32(GRC_EEPROM_DATA);
3058
3059 /*
3060 * The data will always be opposite the native endian
3061 * format. Perform a blind byteswap to compensate.
3062 */
3063 *val = swab32(tmp);
3064
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003065 return 0;
3066}
3067
3068#define NVRAM_CMD_TIMEOUT 10000
3069
3070static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3071{
3072 int i;
3073
3074 tw32(NVRAM_CMD, nvram_cmd);
3075 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3076 udelay(10);
3077 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3078 udelay(10);
3079 break;
3080 }
3081 }
3082
3083 if (i == NVRAM_CMD_TIMEOUT)
3084 return -EBUSY;
3085
3086 return 0;
3087}
3088
3089static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3090{
Joe Perches63c3a662011-04-26 08:12:10 +00003091 if (tg3_flag(tp, NVRAM) &&
3092 tg3_flag(tp, NVRAM_BUFFERED) &&
3093 tg3_flag(tp, FLASH) &&
3094 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003095 (tp->nvram_jedecnum == JEDEC_ATMEL))
3096
3097 addr = ((addr / tp->nvram_pagesize) <<
3098 ATMEL_AT45DB0X1B_PAGE_POS) +
3099 (addr % tp->nvram_pagesize);
3100
3101 return addr;
3102}
3103
3104static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3105{
Joe Perches63c3a662011-04-26 08:12:10 +00003106 if (tg3_flag(tp, NVRAM) &&
3107 tg3_flag(tp, NVRAM_BUFFERED) &&
3108 tg3_flag(tp, FLASH) &&
3109 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003110 (tp->nvram_jedecnum == JEDEC_ATMEL))
3111
3112 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3113 tp->nvram_pagesize) +
3114 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3115
3116 return addr;
3117}
3118
Matt Carlsone4f34112009-02-25 14:25:00 +00003119/* NOTE: Data read in from NVRAM is byteswapped according to
3120 * the byteswapping settings for all other register accesses.
3121 * tg3 devices are BE devices, so on a BE machine, the data
3122 * returned will be exactly as it is seen in NVRAM. On a LE
3123 * machine, the 32-bit value will be byteswapped.
3124 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003125static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3126{
3127 int ret;
3128
Joe Perches63c3a662011-04-26 08:12:10 +00003129 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003130 return tg3_nvram_read_using_eeprom(tp, offset, val);
3131
3132 offset = tg3_nvram_phys_addr(tp, offset);
3133
3134 if (offset > NVRAM_ADDR_MSK)
3135 return -EINVAL;
3136
3137 ret = tg3_nvram_lock(tp);
3138 if (ret)
3139 return ret;
3140
3141 tg3_enable_nvram_access(tp);
3142
3143 tw32(NVRAM_ADDR, offset);
3144 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3145 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3146
3147 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003148 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003149
3150 tg3_disable_nvram_access(tp);
3151
3152 tg3_nvram_unlock(tp);
3153
3154 return ret;
3155}
3156
Matt Carlsona9dc5292009-02-25 14:25:30 +00003157/* Ensures NVRAM data is in bytestream format. */
3158static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003159{
3160 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003161 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003162 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003163 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003164 return res;
3165}
3166
Matt Carlsondbe9b922012-02-13 10:20:09 +00003167static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3168 u32 offset, u32 len, u8 *buf)
3169{
3170 int i, j, rc = 0;
3171 u32 val;
3172
3173 for (i = 0; i < len; i += 4) {
3174 u32 addr;
3175 __be32 data;
3176
3177 addr = offset + i;
3178
3179 memcpy(&data, buf + i, 4);
3180
3181 /*
3182 * The SEEPROM interface expects the data to always be opposite
3183 * the native endian format. We accomplish this by reversing
3184 * all the operations that would have been performed on the
3185 * data from a call to tg3_nvram_read_be32().
3186 */
3187 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3188
3189 val = tr32(GRC_EEPROM_ADDR);
3190 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3191
3192 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3193 EEPROM_ADDR_READ);
3194 tw32(GRC_EEPROM_ADDR, val |
3195 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3196 (addr & EEPROM_ADDR_ADDR_MASK) |
3197 EEPROM_ADDR_START |
3198 EEPROM_ADDR_WRITE);
3199
3200 for (j = 0; j < 1000; j++) {
3201 val = tr32(GRC_EEPROM_ADDR);
3202
3203 if (val & EEPROM_ADDR_COMPLETE)
3204 break;
3205 msleep(1);
3206 }
3207 if (!(val & EEPROM_ADDR_COMPLETE)) {
3208 rc = -EBUSY;
3209 break;
3210 }
3211 }
3212
3213 return rc;
3214}
3215
3216/* offset and length are dword aligned */
3217static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3218 u8 *buf)
3219{
3220 int ret = 0;
3221 u32 pagesize = tp->nvram_pagesize;
3222 u32 pagemask = pagesize - 1;
3223 u32 nvram_cmd;
3224 u8 *tmp;
3225
3226 tmp = kmalloc(pagesize, GFP_KERNEL);
3227 if (tmp == NULL)
3228 return -ENOMEM;
3229
3230 while (len) {
3231 int j;
3232 u32 phy_addr, page_off, size;
3233
3234 phy_addr = offset & ~pagemask;
3235
3236 for (j = 0; j < pagesize; j += 4) {
3237 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3238 (__be32 *) (tmp + j));
3239 if (ret)
3240 break;
3241 }
3242 if (ret)
3243 break;
3244
3245 page_off = offset & pagemask;
3246 size = pagesize;
3247 if (len < size)
3248 size = len;
3249
3250 len -= size;
3251
3252 memcpy(tmp + page_off, buf, size);
3253
3254 offset = offset + (pagesize - page_off);
3255
3256 tg3_enable_nvram_access(tp);
3257
3258 /*
3259 * Before we can erase the flash page, we need
3260 * to issue a special "write enable" command.
3261 */
3262 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3263
3264 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3265 break;
3266
3267 /* Erase the target page */
3268 tw32(NVRAM_ADDR, phy_addr);
3269
3270 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3271 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3272
3273 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3274 break;
3275
3276 /* Issue another write enable to start the write. */
3277 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3278
3279 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3280 break;
3281
3282 for (j = 0; j < pagesize; j += 4) {
3283 __be32 data;
3284
3285 data = *((__be32 *) (tmp + j));
3286
3287 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3288
3289 tw32(NVRAM_ADDR, phy_addr + j);
3290
3291 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3292 NVRAM_CMD_WR;
3293
3294 if (j == 0)
3295 nvram_cmd |= NVRAM_CMD_FIRST;
3296 else if (j == (pagesize - 4))
3297 nvram_cmd |= NVRAM_CMD_LAST;
3298
3299 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3300 if (ret)
3301 break;
3302 }
3303 if (ret)
3304 break;
3305 }
3306
3307 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3308 tg3_nvram_exec_cmd(tp, nvram_cmd);
3309
3310 kfree(tmp);
3311
3312 return ret;
3313}
3314
3315/* offset and length are dword aligned */
3316static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3317 u8 *buf)
3318{
3319 int i, ret = 0;
3320
3321 for (i = 0; i < len; i += 4, offset += 4) {
3322 u32 page_off, phy_addr, nvram_cmd;
3323 __be32 data;
3324
3325 memcpy(&data, buf + i, 4);
3326 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3327
3328 page_off = offset % tp->nvram_pagesize;
3329
3330 phy_addr = tg3_nvram_phys_addr(tp, offset);
3331
Matt Carlsondbe9b922012-02-13 10:20:09 +00003332 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3333
3334 if (page_off == 0 || i == 0)
3335 nvram_cmd |= NVRAM_CMD_FIRST;
3336 if (page_off == (tp->nvram_pagesize - 4))
3337 nvram_cmd |= NVRAM_CMD_LAST;
3338
3339 if (i == (len - 4))
3340 nvram_cmd |= NVRAM_CMD_LAST;
3341
Matt Carlson42278222012-02-13 15:20:11 +00003342 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3343 !tg3_flag(tp, FLASH) ||
3344 !tg3_flag(tp, 57765_PLUS))
3345 tw32(NVRAM_ADDR, phy_addr);
3346
Matt Carlsondbe9b922012-02-13 10:20:09 +00003347 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3348 !tg3_flag(tp, 5755_PLUS) &&
3349 (tp->nvram_jedecnum == JEDEC_ST) &&
3350 (nvram_cmd & NVRAM_CMD_FIRST)) {
3351 u32 cmd;
3352
3353 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3354 ret = tg3_nvram_exec_cmd(tp, cmd);
3355 if (ret)
3356 break;
3357 }
3358 if (!tg3_flag(tp, FLASH)) {
3359 /* We always do complete word writes to eeprom. */
3360 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3361 }
3362
3363 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3364 if (ret)
3365 break;
3366 }
3367 return ret;
3368}
3369
3370/* offset and length are dword aligned */
3371static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3372{
3373 int ret;
3374
3375 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3376 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3377 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3378 udelay(40);
3379 }
3380
3381 if (!tg3_flag(tp, NVRAM)) {
3382 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3383 } else {
3384 u32 grc_mode;
3385
3386 ret = tg3_nvram_lock(tp);
3387 if (ret)
3388 return ret;
3389
3390 tg3_enable_nvram_access(tp);
3391 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3392 tw32(NVRAM_WRITE1, 0x406);
3393
3394 grc_mode = tr32(GRC_MODE);
3395 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3396
3397 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3398 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3399 buf);
3400 } else {
3401 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3402 buf);
3403 }
3404
3405 grc_mode = tr32(GRC_MODE);
3406 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3407
3408 tg3_disable_nvram_access(tp);
3409 tg3_nvram_unlock(tp);
3410 }
3411
3412 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3413 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3414 udelay(40);
3415 }
3416
3417 return ret;
3418}
3419
Matt Carlson997b4f12011-08-31 11:44:53 +00003420#define RX_CPU_SCRATCH_BASE 0x30000
3421#define RX_CPU_SCRATCH_SIZE 0x04000
3422#define TX_CPU_SCRATCH_BASE 0x34000
3423#define TX_CPU_SCRATCH_SIZE 0x04000
3424
3425/* tp->lock is held. */
3426static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3427{
3428 int i;
3429
3430 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3431
3432 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3433 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3434
3435 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3436 return 0;
3437 }
3438 if (offset == RX_CPU_BASE) {
3439 for (i = 0; i < 10000; i++) {
3440 tw32(offset + CPU_STATE, 0xffffffff);
3441 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3442 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3443 break;
3444 }
3445
3446 tw32(offset + CPU_STATE, 0xffffffff);
3447 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3448 udelay(10);
3449 } else {
3450 for (i = 0; i < 10000; i++) {
3451 tw32(offset + CPU_STATE, 0xffffffff);
3452 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3453 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3454 break;
3455 }
3456 }
3457
3458 if (i >= 10000) {
3459 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3460 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3461 return -ENODEV;
3462 }
3463
3464 /* Clear firmware's nvram arbitration. */
3465 if (tg3_flag(tp, NVRAM))
3466 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3467 return 0;
3468}
3469
3470struct fw_info {
3471 unsigned int fw_base;
3472 unsigned int fw_len;
3473 const __be32 *fw_data;
3474};
3475
3476/* tp->lock is held. */
3477static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3478 u32 cpu_scratch_base, int cpu_scratch_size,
3479 struct fw_info *info)
3480{
3481 int err, lock_err, i;
3482 void (*write_op)(struct tg3 *, u32, u32);
3483
3484 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3485 netdev_err(tp->dev,
3486 "%s: Trying to load TX cpu firmware which is 5705\n",
3487 __func__);
3488 return -EINVAL;
3489 }
3490
3491 if (tg3_flag(tp, 5705_PLUS))
3492 write_op = tg3_write_mem;
3493 else
3494 write_op = tg3_write_indirect_reg32;
3495
3496 /* It is possible that bootcode is still loading at this point.
3497 * Get the nvram lock first before halting the cpu.
3498 */
3499 lock_err = tg3_nvram_lock(tp);
3500 err = tg3_halt_cpu(tp, cpu_base);
3501 if (!lock_err)
3502 tg3_nvram_unlock(tp);
3503 if (err)
3504 goto out;
3505
3506 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3507 write_op(tp, cpu_scratch_base + i, 0);
3508 tw32(cpu_base + CPU_STATE, 0xffffffff);
3509 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3510 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3511 write_op(tp, (cpu_scratch_base +
3512 (info->fw_base & 0xffff) +
3513 (i * sizeof(u32))),
3514 be32_to_cpu(info->fw_data[i]));
3515
3516 err = 0;
3517
3518out:
3519 return err;
3520}
3521
3522/* tp->lock is held. */
3523static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3524{
3525 struct fw_info info;
3526 const __be32 *fw_data;
3527 int err, i;
3528
3529 fw_data = (void *)tp->fw->data;
3530
3531 /* Firmware blob starts with version numbers, followed by
3532 start address and length. We are setting complete length.
3533 length = end_address_of_bss - start_address_of_text.
3534 Remainder is the blob to be loaded contiguously
3535 from start address. */
3536
3537 info.fw_base = be32_to_cpu(fw_data[1]);
3538 info.fw_len = tp->fw->size - 12;
3539 info.fw_data = &fw_data[3];
3540
3541 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3542 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3543 &info);
3544 if (err)
3545 return err;
3546
3547 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3548 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3549 &info);
3550 if (err)
3551 return err;
3552
3553 /* Now startup only the RX cpu. */
3554 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3555 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3556
3557 for (i = 0; i < 5; i++) {
3558 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3559 break;
3560 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3561 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3562 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3563 udelay(1000);
3564 }
3565 if (i >= 5) {
3566 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3567 "should be %08x\n", __func__,
3568 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3569 return -ENODEV;
3570 }
3571 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3572 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3573
3574 return 0;
3575}
3576
3577/* tp->lock is held. */
3578static int tg3_load_tso_firmware(struct tg3 *tp)
3579{
3580 struct fw_info info;
3581 const __be32 *fw_data;
3582 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3583 int err, i;
3584
3585 if (tg3_flag(tp, HW_TSO_1) ||
3586 tg3_flag(tp, HW_TSO_2) ||
3587 tg3_flag(tp, HW_TSO_3))
3588 return 0;
3589
3590 fw_data = (void *)tp->fw->data;
3591
3592 /* Firmware blob starts with version numbers, followed by
3593 start address and length. We are setting complete length.
3594 length = end_address_of_bss - start_address_of_text.
3595 Remainder is the blob to be loaded contiguously
3596 from start address. */
3597
3598 info.fw_base = be32_to_cpu(fw_data[1]);
3599 cpu_scratch_size = tp->fw_len;
3600 info.fw_len = tp->fw->size - 12;
3601 info.fw_data = &fw_data[3];
3602
3603 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3604 cpu_base = RX_CPU_BASE;
3605 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3606 } else {
3607 cpu_base = TX_CPU_BASE;
3608 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3609 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3610 }
3611
3612 err = tg3_load_firmware_cpu(tp, cpu_base,
3613 cpu_scratch_base, cpu_scratch_size,
3614 &info);
3615 if (err)
3616 return err;
3617
3618 /* Now startup the cpu. */
3619 tw32(cpu_base + CPU_STATE, 0xffffffff);
3620 tw32_f(cpu_base + CPU_PC, info.fw_base);
3621
3622 for (i = 0; i < 5; i++) {
3623 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3624 break;
3625 tw32(cpu_base + CPU_STATE, 0xffffffff);
3626 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3627 tw32_f(cpu_base + CPU_PC, info.fw_base);
3628 udelay(1000);
3629 }
3630 if (i >= 5) {
3631 netdev_err(tp->dev,
3632 "%s fails to set CPU PC, is %08x should be %08x\n",
3633 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3634 return -ENODEV;
3635 }
3636 tw32(cpu_base + CPU_STATE, 0xffffffff);
3637 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3638 return 0;
3639}
3640
3641
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003642/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003643static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3644{
3645 u32 addr_high, addr_low;
3646 int i;
3647
3648 addr_high = ((tp->dev->dev_addr[0] << 8) |
3649 tp->dev->dev_addr[1]);
3650 addr_low = ((tp->dev->dev_addr[2] << 24) |
3651 (tp->dev->dev_addr[3] << 16) |
3652 (tp->dev->dev_addr[4] << 8) |
3653 (tp->dev->dev_addr[5] << 0));
3654 for (i = 0; i < 4; i++) {
3655 if (i == 1 && skip_mac_1)
3656 continue;
3657 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3658 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3659 }
3660
3661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3662 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3663 for (i = 0; i < 12; i++) {
3664 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3665 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3666 }
3667 }
3668
3669 addr_high = (tp->dev->dev_addr[0] +
3670 tp->dev->dev_addr[1] +
3671 tp->dev->dev_addr[2] +
3672 tp->dev->dev_addr[3] +
3673 tp->dev->dev_addr[4] +
3674 tp->dev->dev_addr[5]) &
3675 TX_BACKOFF_SEED_MASK;
3676 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3677}
3678
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003679static void tg3_enable_register_access(struct tg3 *tp)
3680{
3681 /*
3682 * Make sure register accesses (indirect or otherwise) will function
3683 * correctly.
3684 */
3685 pci_write_config_dword(tp->pdev,
3686 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3687}
3688
3689static int tg3_power_up(struct tg3 *tp)
3690{
Matt Carlsonbed98292011-07-13 09:27:29 +00003691 int err;
3692
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003693 tg3_enable_register_access(tp);
3694
Matt Carlsonbed98292011-07-13 09:27:29 +00003695 err = pci_set_power_state(tp->pdev, PCI_D0);
3696 if (!err) {
3697 /* Switch out of Vaux if it is a NIC */
3698 tg3_pwrsrc_switch_to_vmain(tp);
3699 } else {
3700 netdev_err(tp->dev, "Transition to D0 failed\n");
3701 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003702
Matt Carlsonbed98292011-07-13 09:27:29 +00003703 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003704}
3705
Matt Carlson4b409522012-02-13 10:20:11 +00003706static int tg3_setup_phy(struct tg3 *, int);
3707
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003708static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
3710 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003711 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003713 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003714
3715 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003716 if (tg3_flag(tp, CLKREQ_BUG))
3717 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3718 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3721 tw32(TG3PCI_MISC_HOST_CTRL,
3722 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3723
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003724 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003725 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003726
Joe Perches63c3a662011-04-26 08:12:10 +00003727 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003728 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003729 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003730 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003731 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003732 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003733
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003734 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003735
Matt Carlson80096062010-08-02 11:26:06 +00003736 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003737
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003738 tp->link_config.speed = phydev->speed;
3739 tp->link_config.duplex = phydev->duplex;
3740 tp->link_config.autoneg = phydev->autoneg;
3741 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003742
3743 advertising = ADVERTISED_TP |
3744 ADVERTISED_Pause |
3745 ADVERTISED_Autoneg |
3746 ADVERTISED_10baseT_Half;
3747
Joe Perches63c3a662011-04-26 08:12:10 +00003748 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3749 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003750 advertising |=
3751 ADVERTISED_100baseT_Half |
3752 ADVERTISED_100baseT_Full |
3753 ADVERTISED_10baseT_Full;
3754 else
3755 advertising |= ADVERTISED_10baseT_Full;
3756 }
3757
3758 phydev->advertising = advertising;
3759
3760 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003761
3762 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003763 if (phyid != PHY_ID_BCMAC131) {
3764 phyid &= PHY_BCM_OUI_MASK;
3765 if (phyid == PHY_BCM_OUI_1 ||
3766 phyid == PHY_BCM_OUI_2 ||
3767 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003768 do_low_power = true;
3769 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003770 }
Matt Carlsondd477002008-05-25 23:45:58 -07003771 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003772 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003773
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003774 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003775 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Matt Carlson2855b9f2012-02-13 15:20:14 +00003777 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003778 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780
Michael Chanb5d37722006-09-27 16:06:21 -07003781 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3782 u32 val;
3783
3784 val = tr32(GRC_VCPU_EXT_CTRL);
3785 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003786 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003787 int i;
3788 u32 val;
3789
3790 for (i = 0; i < 200; i++) {
3791 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3792 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3793 break;
3794 msleep(1);
3795 }
3796 }
Joe Perches63c3a662011-04-26 08:12:10 +00003797 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003798 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3799 WOL_DRV_STATE_SHUTDOWN |
3800 WOL_DRV_WOL |
3801 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003802
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003803 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 u32 mac_mode;
3805
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003806 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003807 if (do_low_power &&
3808 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3809 tg3_phy_auxctl_write(tp,
3810 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3811 MII_TG3_AUXCTL_PCTL_WOL_EN |
3812 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3813 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003814 udelay(40);
3815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003817 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003818 mac_mode = MAC_MODE_PORT_MODE_GMII;
3819 else
3820 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003822 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3823 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3824 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003825 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003826 SPEED_100 : SPEED_10;
3827 if (tg3_5700_link_polarity(tp, speed))
3828 mac_mode |= MAC_MODE_LINK_POLARITY;
3829 else
3830 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 } else {
3833 mac_mode = MAC_MODE_PORT_MODE_TBI;
3834 }
3835
Joe Perches63c3a662011-04-26 08:12:10 +00003836 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 tw32(MAC_LED_CTRL, tp->led_ctrl);
3838
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003839 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003840 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3841 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003842 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Joe Perches63c3a662011-04-26 08:12:10 +00003844 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003845 mac_mode |= MAC_MODE_APE_TX_EN |
3846 MAC_MODE_APE_RX_EN |
3847 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 tw32_f(MAC_MODE, mac_mode);
3850 udelay(100);
3851
3852 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3853 udelay(10);
3854 }
3855
Joe Perches63c3a662011-04-26 08:12:10 +00003856 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3859 u32 base_val;
3860
3861 base_val = tp->pci_clock_ctrl;
3862 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3863 CLOCK_CTRL_TXCLK_DISABLE);
3864
Michael Chanb401e9e2005-12-19 16:27:04 -08003865 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3866 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003867 } else if (tg3_flag(tp, 5780_CLASS) ||
3868 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003869 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003870 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003871 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 u32 newbits1, newbits2;
3873
3874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3875 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3876 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3877 CLOCK_CTRL_TXCLK_DISABLE |
3878 CLOCK_CTRL_ALTCLK);
3879 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003880 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 newbits1 = CLOCK_CTRL_625_CORE;
3882 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3883 } else {
3884 newbits1 = CLOCK_CTRL_ALTCLK;
3885 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3886 }
3887
Michael Chanb401e9e2005-12-19 16:27:04 -08003888 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3889 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Michael Chanb401e9e2005-12-19 16:27:04 -08003891 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3892 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Joe Perches63c3a662011-04-26 08:12:10 +00003894 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 u32 newbits3;
3896
3897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3899 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3900 CLOCK_CTRL_TXCLK_DISABLE |
3901 CLOCK_CTRL_44MHZ_CORE);
3902 } else {
3903 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3904 }
3905
Michael Chanb401e9e2005-12-19 16:27:04 -08003906 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3907 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909 }
3910
Joe Perches63c3a662011-04-26 08:12:10 +00003911 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003912 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003913
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003914 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 /* Workaround for unstable PLL clock */
3917 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3918 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3919 u32 val = tr32(0x7d00);
3920
3921 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3922 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003923 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003924 int err;
3925
3926 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003928 if (!err)
3929 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
3932
Michael Chanbbadf502006-04-06 21:46:34 -07003933 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return 0;
3936}
3937
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003938static void tg3_power_down(struct tg3 *tp)
3939{
3940 tg3_power_down_prepare(tp);
3941
Joe Perches63c3a662011-04-26 08:12:10 +00003942 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003943 pci_set_power_state(tp->pdev, PCI_D3hot);
3944}
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3947{
3948 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3949 case MII_TG3_AUX_STAT_10HALF:
3950 *speed = SPEED_10;
3951 *duplex = DUPLEX_HALF;
3952 break;
3953
3954 case MII_TG3_AUX_STAT_10FULL:
3955 *speed = SPEED_10;
3956 *duplex = DUPLEX_FULL;
3957 break;
3958
3959 case MII_TG3_AUX_STAT_100HALF:
3960 *speed = SPEED_100;
3961 *duplex = DUPLEX_HALF;
3962 break;
3963
3964 case MII_TG3_AUX_STAT_100FULL:
3965 *speed = SPEED_100;
3966 *duplex = DUPLEX_FULL;
3967 break;
3968
3969 case MII_TG3_AUX_STAT_1000HALF:
3970 *speed = SPEED_1000;
3971 *duplex = DUPLEX_HALF;
3972 break;
3973
3974 case MII_TG3_AUX_STAT_1000FULL:
3975 *speed = SPEED_1000;
3976 *duplex = DUPLEX_FULL;
3977 break;
3978
3979 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003980 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003981 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3982 SPEED_10;
3983 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3984 DUPLEX_HALF;
3985 break;
3986 }
Matt Carlsone7405222012-02-13 15:20:16 +00003987 *speed = SPEED_UNKNOWN;
3988 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Matt Carlson42b64a42011-05-19 12:12:49 +00003993static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Matt Carlson42b64a42011-05-19 12:12:49 +00003995 int err = 0;
3996 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Matt Carlson42b64a42011-05-19 12:12:49 +00003998 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003999 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00004000 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Matt Carlson42b64a42011-05-19 12:12:49 +00004002 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
4003 if (err)
4004 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Matt Carlson4f272092011-12-14 11:09:57 +00004006 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4007 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004008
Matt Carlson4f272092011-12-14 11:09:57 +00004009 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4010 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
4011 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004012
Matt Carlson4f272092011-12-14 11:09:57 +00004013 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4014 if (err)
4015 goto done;
4016 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004017
Matt Carlson42b64a42011-05-19 12:12:49 +00004018 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4019 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Matt Carlson42b64a42011-05-19 12:12:49 +00004021 tw32(TG3_CPMU_EEE_MODE,
4022 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004023
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004024 err = tg3_phy_toggle_auxctl_smdsp(tp, true);
Matt Carlson42b64a42011-05-19 12:12:49 +00004025 if (!err) {
4026 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004027
Matt Carlsona6b68da2010-12-06 08:28:52 +00004028 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004029 /* Advertise 100-BaseTX EEE ability */
4030 if (advertise & ADVERTISED_100baseT_Full)
4031 val |= MDIO_AN_EEE_ADV_100TX;
4032 /* Advertise 1000-BaseT EEE ability */
4033 if (advertise & ADVERTISED_1000baseT_Full)
4034 val |= MDIO_AN_EEE_ADV_1000T;
4035 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004036 if (err)
4037 val = 0;
4038
4039 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
4040 case ASIC_REV_5717:
4041 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004042 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004043 case ASIC_REV_5719:
4044 /* If we advertised any eee advertisements above... */
4045 if (val)
4046 val = MII_TG3_DSP_TAP26_ALNOKO |
4047 MII_TG3_DSP_TAP26_RMRXSTO |
4048 MII_TG3_DSP_TAP26_OPCSINPT;
4049 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4050 /* Fall through */
4051 case ASIC_REV_5720:
4052 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4053 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4054 MII_TG3_DSP_CH34TP2_HIBW01);
4055 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004056
Nithin Nayak Sujirdaf3ec62013-01-14 17:11:00 +00004057 err2 = tg3_phy_toggle_auxctl_smdsp(tp, false);
Matt Carlson42b64a42011-05-19 12:12:49 +00004058 if (!err)
4059 err = err2;
4060 }
4061
4062done:
4063 return err;
4064}
4065
4066static void tg3_phy_copper_begin(struct tg3 *tp)
4067{
Matt Carlsond13ba512012-02-22 12:35:19 +00004068 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4069 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4070 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004071
Matt Carlsond13ba512012-02-22 12:35:19 +00004072 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4073 adv = ADVERTISED_10baseT_Half |
4074 ADVERTISED_10baseT_Full;
4075 if (tg3_flag(tp, WOL_SPEED_100MB))
4076 adv |= ADVERTISED_100baseT_Half |
4077 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004078
Matt Carlsond13ba512012-02-22 12:35:19 +00004079 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004080 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004081 adv = tp->link_config.advertising;
4082 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4083 adv &= ~(ADVERTISED_1000baseT_Half |
4084 ADVERTISED_1000baseT_Full);
4085
4086 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004087 }
4088
Matt Carlsond13ba512012-02-22 12:35:19 +00004089 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004090
Matt Carlsond13ba512012-02-22 12:35:19 +00004091 tg3_writephy(tp, MII_BMCR,
4092 BMCR_ANENABLE | BMCR_ANRESTART);
4093 } else {
4094 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 u32 bmcr, orig_bmcr;
4096
4097 tp->link_config.active_speed = tp->link_config.speed;
4098 tp->link_config.active_duplex = tp->link_config.duplex;
4099
4100 bmcr = 0;
4101 switch (tp->link_config.speed) {
4102 default:
4103 case SPEED_10:
4104 break;
4105
4106 case SPEED_100:
4107 bmcr |= BMCR_SPEED100;
4108 break;
4109
4110 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004111 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 if (tp->link_config.duplex == DUPLEX_FULL)
4116 bmcr |= BMCR_FULLDPLX;
4117
4118 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4119 (bmcr != orig_bmcr)) {
4120 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4121 for (i = 0; i < 1500; i++) {
4122 u32 tmp;
4123
4124 udelay(10);
4125 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4126 tg3_readphy(tp, MII_BMSR, &tmp))
4127 continue;
4128 if (!(tmp & BMSR_LSTATUS)) {
4129 udelay(40);
4130 break;
4131 }
4132 }
4133 tg3_writephy(tp, MII_BMCR, bmcr);
4134 udelay(40);
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
4137}
4138
4139static int tg3_init_5401phy_dsp(struct tg3 *tp)
4140{
4141 int err;
4142
4143 /* Turn off tap power management. */
4144 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004145 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004147 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4148 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4149 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4150 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4151 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 udelay(40);
4154
4155 return err;
4156}
4157
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004158static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004160 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004161
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004162 advertising = tp->link_config.advertising;
4163 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004165 advmsk = ADVERTISE_ALL;
4166 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004167 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004168 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004171 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4172 return false;
4173
4174 if ((*lcladv & advmsk) != tgtadv)
4175 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004176
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004177 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 u32 tg3_ctrl;
4179
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004180 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004181
Matt Carlson221c5632011-06-13 13:39:01 +00004182 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004183 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Matt Carlson3198e072012-02-13 15:20:10 +00004185 if (tgtadv &&
4186 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4187 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4188 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4189 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4190 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4191 } else {
4192 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4193 }
4194
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004195 if (tg3_ctrl != tgtadv)
4196 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004198
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004199 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004200}
4201
Matt Carlson859edb22011-12-08 14:40:16 +00004202static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4203{
4204 u32 lpeth = 0;
4205
4206 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4207 u32 val;
4208
4209 if (tg3_readphy(tp, MII_STAT1000, &val))
4210 return false;
4211
4212 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4213 }
4214
4215 if (tg3_readphy(tp, MII_LPA, rmtadv))
4216 return false;
4217
4218 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4219 tp->link_config.rmt_adv = lpeth;
4220
4221 return true;
4222}
4223
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004224static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4225{
4226 if (curr_link_up != tp->link_up) {
4227 if (curr_link_up) {
4228 tg3_carrier_on(tp);
4229 } else {
4230 tg3_carrier_off(tp);
4231 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4232 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4233 }
4234
4235 tg3_link_report(tp);
4236 return true;
4237 }
4238
4239 return false;
4240}
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4243{
4244 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004245 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004246 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 u16 current_speed;
4248 u8 current_duplex;
4249 int i, err;
4250
4251 tw32(MAC_EVENT, 0);
4252
4253 tw32_f(MAC_STATUS,
4254 (MAC_STATUS_SYNC_CHANGED |
4255 MAC_STATUS_CFG_CHANGED |
4256 MAC_STATUS_MI_COMPLETION |
4257 MAC_STATUS_LNKSTATE_CHANGED));
4258 udelay(40);
4259
Matt Carlson8ef21422008-05-02 16:47:53 -07004260 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4261 tw32_f(MAC_MI_MODE,
4262 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4263 udelay(80);
4264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004266 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 /* Some third-party PHYs need to be reset on link going
4269 * down.
4270 */
4271 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004274 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 tg3_readphy(tp, MII_BMSR, &bmsr);
4276 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4277 !(bmsr & BMSR_LSTATUS))
4278 force_reset = 1;
4279 }
4280 if (force_reset)
4281 tg3_phy_reset(tp);
4282
Matt Carlson79eb6902010-02-17 15:17:03 +00004283 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 tg3_readphy(tp, MII_BMSR, &bmsr);
4285 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004286 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 bmsr = 0;
4288
4289 if (!(bmsr & BMSR_LSTATUS)) {
4290 err = tg3_init_5401phy_dsp(tp);
4291 if (err)
4292 return err;
4293
4294 tg3_readphy(tp, MII_BMSR, &bmsr);
4295 for (i = 0; i < 1000; i++) {
4296 udelay(10);
4297 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4298 (bmsr & BMSR_LSTATUS)) {
4299 udelay(40);
4300 break;
4301 }
4302 }
4303
Matt Carlson79eb6902010-02-17 15:17:03 +00004304 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4305 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 !(bmsr & BMSR_LSTATUS) &&
4307 tp->link_config.active_speed == SPEED_1000) {
4308 err = tg3_phy_reset(tp);
4309 if (!err)
4310 err = tg3_init_5401phy_dsp(tp);
4311 if (err)
4312 return err;
4313 }
4314 }
4315 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4316 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4317 /* 5701 {A0,B0} CRC bug workaround */
4318 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004319 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4320 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4321 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323
4324 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004325 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4326 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004328 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004330 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4332
4333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4335 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4336 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4337 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4338 else
4339 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4340 }
4341
4342 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004343 current_speed = SPEED_UNKNOWN;
4344 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004345 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004346 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004348 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004349 err = tg3_phy_auxctl_read(tp,
4350 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4351 &val);
4352 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004353 tg3_phy_auxctl_write(tp,
4354 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4355 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 goto relink;
4357 }
4358 }
4359
4360 bmsr = 0;
4361 for (i = 0; i < 100; i++) {
4362 tg3_readphy(tp, MII_BMSR, &bmsr);
4363 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4364 (bmsr & BMSR_LSTATUS))
4365 break;
4366 udelay(40);
4367 }
4368
4369 if (bmsr & BMSR_LSTATUS) {
4370 u32 aux_stat, bmcr;
4371
4372 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4373 for (i = 0; i < 2000; i++) {
4374 udelay(10);
4375 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4376 aux_stat)
4377 break;
4378 }
4379
4380 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4381 &current_speed,
4382 &current_duplex);
4383
4384 bmcr = 0;
4385 for (i = 0; i < 200; i++) {
4386 tg3_readphy(tp, MII_BMCR, &bmcr);
4387 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4388 continue;
4389 if (bmcr && bmcr != 0x7fff)
4390 break;
4391 udelay(10);
4392 }
4393
Matt Carlsonef167e22007-12-20 20:10:01 -08004394 lcl_adv = 0;
4395 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Matt Carlsonef167e22007-12-20 20:10:01 -08004397 tp->link_config.active_speed = current_speed;
4398 tp->link_config.active_duplex = current_duplex;
4399
4400 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4401 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004402 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004403 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004404 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 } else {
4406 if (!(bmcr & BMCR_ANENABLE) &&
4407 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004408 tp->link_config.duplex == current_duplex &&
4409 tp->link_config.flowctrl ==
4410 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
4413 }
4414
Matt Carlsonef167e22007-12-20 20:10:01 -08004415 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004416 tp->link_config.active_duplex == DUPLEX_FULL) {
4417 u32 reg, bit;
4418
4419 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4420 reg = MII_TG3_FET_GEN_STAT;
4421 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4422 } else {
4423 reg = MII_TG3_EXT_STAT;
4424 bit = MII_TG3_EXT_STAT_MDIX;
4425 }
4426
4427 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4428 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4429
Matt Carlsonef167e22007-12-20 20:10:01 -08004430 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 }
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434relink:
Matt Carlson80096062010-08-02 11:26:06 +00004435 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 tg3_phy_copper_begin(tp);
4437
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004438 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004439 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4440 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 current_link_up = 1;
4442 }
4443
4444 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4445 if (current_link_up == 1) {
4446 if (tp->link_config.active_speed == SPEED_100 ||
4447 tp->link_config.active_speed == SPEED_10)
4448 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4449 else
4450 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004451 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004452 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4453 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4455
4456 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4457 if (tp->link_config.active_duplex == DUPLEX_HALF)
4458 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004461 if (current_link_up == 1 &&
4462 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004464 else
4465 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467
4468 /* ??? Without this setting Netgear GA302T PHY does not
4469 * ??? send/receive packets...
4470 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004471 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4473 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4474 tw32_f(MAC_MI_MODE, tp->mi_mode);
4475 udelay(80);
4476 }
4477
4478 tw32_f(MAC_MODE, tp->mac_mode);
4479 udelay(40);
4480
Matt Carlson52b02d02010-10-14 10:37:41 +00004481 tg3_phy_eee_adjust(tp, current_link_up);
4482
Joe Perches63c3a662011-04-26 08:12:10 +00004483 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 /* Polled via timer. */
4485 tw32_f(MAC_EVENT, 0);
4486 } else {
4487 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4488 }
4489 udelay(40);
4490
4491 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4492 current_link_up == 1 &&
4493 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004494 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 udelay(120);
4496 tw32_f(MAC_STATUS,
4497 (MAC_STATUS_SYNC_CHANGED |
4498 MAC_STATUS_CFG_CHANGED));
4499 udelay(40);
4500 tg3_write_mem(tp,
4501 NIC_SRAM_FIRMWARE_MBOX,
4502 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4503 }
4504
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004505 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004506 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004507 if (tp->link_config.active_speed == SPEED_100 ||
4508 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004509 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4510 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004511 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004512 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4513 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004514 }
4515
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004516 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
4518 return 0;
4519}
4520
4521struct tg3_fiber_aneginfo {
4522 int state;
4523#define ANEG_STATE_UNKNOWN 0
4524#define ANEG_STATE_AN_ENABLE 1
4525#define ANEG_STATE_RESTART_INIT 2
4526#define ANEG_STATE_RESTART 3
4527#define ANEG_STATE_DISABLE_LINK_OK 4
4528#define ANEG_STATE_ABILITY_DETECT_INIT 5
4529#define ANEG_STATE_ABILITY_DETECT 6
4530#define ANEG_STATE_ACK_DETECT_INIT 7
4531#define ANEG_STATE_ACK_DETECT 8
4532#define ANEG_STATE_COMPLETE_ACK_INIT 9
4533#define ANEG_STATE_COMPLETE_ACK 10
4534#define ANEG_STATE_IDLE_DETECT_INIT 11
4535#define ANEG_STATE_IDLE_DETECT 12
4536#define ANEG_STATE_LINK_OK 13
4537#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4538#define ANEG_STATE_NEXT_PAGE_WAIT 15
4539
4540 u32 flags;
4541#define MR_AN_ENABLE 0x00000001
4542#define MR_RESTART_AN 0x00000002
4543#define MR_AN_COMPLETE 0x00000004
4544#define MR_PAGE_RX 0x00000008
4545#define MR_NP_LOADED 0x00000010
4546#define MR_TOGGLE_TX 0x00000020
4547#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4548#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4549#define MR_LP_ADV_SYM_PAUSE 0x00000100
4550#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4551#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4552#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4553#define MR_LP_ADV_NEXT_PAGE 0x00001000
4554#define MR_TOGGLE_RX 0x00002000
4555#define MR_NP_RX 0x00004000
4556
4557#define MR_LINK_OK 0x80000000
4558
4559 unsigned long link_time, cur_time;
4560
4561 u32 ability_match_cfg;
4562 int ability_match_count;
4563
4564 char ability_match, idle_match, ack_match;
4565
4566 u32 txconfig, rxconfig;
4567#define ANEG_CFG_NP 0x00000080
4568#define ANEG_CFG_ACK 0x00000040
4569#define ANEG_CFG_RF2 0x00000020
4570#define ANEG_CFG_RF1 0x00000010
4571#define ANEG_CFG_PS2 0x00000001
4572#define ANEG_CFG_PS1 0x00008000
4573#define ANEG_CFG_HD 0x00004000
4574#define ANEG_CFG_FD 0x00002000
4575#define ANEG_CFG_INVAL 0x00001f06
4576
4577};
4578#define ANEG_OK 0
4579#define ANEG_DONE 1
4580#define ANEG_TIMER_ENAB 2
4581#define ANEG_FAILED -1
4582
4583#define ANEG_STATE_SETTLE_TIME 10000
4584
4585static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4586 struct tg3_fiber_aneginfo *ap)
4587{
Matt Carlson5be73b42007-12-20 20:09:29 -08004588 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 unsigned long delta;
4590 u32 rx_cfg_reg;
4591 int ret;
4592
4593 if (ap->state == ANEG_STATE_UNKNOWN) {
4594 ap->rxconfig = 0;
4595 ap->link_time = 0;
4596 ap->cur_time = 0;
4597 ap->ability_match_cfg = 0;
4598 ap->ability_match_count = 0;
4599 ap->ability_match = 0;
4600 ap->idle_match = 0;
4601 ap->ack_match = 0;
4602 }
4603 ap->cur_time++;
4604
4605 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4606 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4607
4608 if (rx_cfg_reg != ap->ability_match_cfg) {
4609 ap->ability_match_cfg = rx_cfg_reg;
4610 ap->ability_match = 0;
4611 ap->ability_match_count = 0;
4612 } else {
4613 if (++ap->ability_match_count > 1) {
4614 ap->ability_match = 1;
4615 ap->ability_match_cfg = rx_cfg_reg;
4616 }
4617 }
4618 if (rx_cfg_reg & ANEG_CFG_ACK)
4619 ap->ack_match = 1;
4620 else
4621 ap->ack_match = 0;
4622
4623 ap->idle_match = 0;
4624 } else {
4625 ap->idle_match = 1;
4626 ap->ability_match_cfg = 0;
4627 ap->ability_match_count = 0;
4628 ap->ability_match = 0;
4629 ap->ack_match = 0;
4630
4631 rx_cfg_reg = 0;
4632 }
4633
4634 ap->rxconfig = rx_cfg_reg;
4635 ret = ANEG_OK;
4636
Matt Carlson33f401a2010-04-05 10:19:27 +00004637 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 case ANEG_STATE_UNKNOWN:
4639 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4640 ap->state = ANEG_STATE_AN_ENABLE;
4641
4642 /* fallthru */
4643 case ANEG_STATE_AN_ENABLE:
4644 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4645 if (ap->flags & MR_AN_ENABLE) {
4646 ap->link_time = 0;
4647 ap->cur_time = 0;
4648 ap->ability_match_cfg = 0;
4649 ap->ability_match_count = 0;
4650 ap->ability_match = 0;
4651 ap->idle_match = 0;
4652 ap->ack_match = 0;
4653
4654 ap->state = ANEG_STATE_RESTART_INIT;
4655 } else {
4656 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4657 }
4658 break;
4659
4660 case ANEG_STATE_RESTART_INIT:
4661 ap->link_time = ap->cur_time;
4662 ap->flags &= ~(MR_NP_LOADED);
4663 ap->txconfig = 0;
4664 tw32(MAC_TX_AUTO_NEG, 0);
4665 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4666 tw32_f(MAC_MODE, tp->mac_mode);
4667 udelay(40);
4668
4669 ret = ANEG_TIMER_ENAB;
4670 ap->state = ANEG_STATE_RESTART;
4671
4672 /* fallthru */
4673 case ANEG_STATE_RESTART:
4674 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004675 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004677 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 break;
4680
4681 case ANEG_STATE_DISABLE_LINK_OK:
4682 ret = ANEG_DONE;
4683 break;
4684
4685 case ANEG_STATE_ABILITY_DETECT_INIT:
4686 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004687 ap->txconfig = ANEG_CFG_FD;
4688 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4689 if (flowctrl & ADVERTISE_1000XPAUSE)
4690 ap->txconfig |= ANEG_CFG_PS1;
4691 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4692 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4694 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4695 tw32_f(MAC_MODE, tp->mac_mode);
4696 udelay(40);
4697
4698 ap->state = ANEG_STATE_ABILITY_DETECT;
4699 break;
4700
4701 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004702 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 break;
4705
4706 case ANEG_STATE_ACK_DETECT_INIT:
4707 ap->txconfig |= ANEG_CFG_ACK;
4708 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4709 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4710 tw32_f(MAC_MODE, tp->mac_mode);
4711 udelay(40);
4712
4713 ap->state = ANEG_STATE_ACK_DETECT;
4714
4715 /* fallthru */
4716 case ANEG_STATE_ACK_DETECT:
4717 if (ap->ack_match != 0) {
4718 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4719 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4720 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4721 } else {
4722 ap->state = ANEG_STATE_AN_ENABLE;
4723 }
4724 } else if (ap->ability_match != 0 &&
4725 ap->rxconfig == 0) {
4726 ap->state = ANEG_STATE_AN_ENABLE;
4727 }
4728 break;
4729
4730 case ANEG_STATE_COMPLETE_ACK_INIT:
4731 if (ap->rxconfig & ANEG_CFG_INVAL) {
4732 ret = ANEG_FAILED;
4733 break;
4734 }
4735 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4736 MR_LP_ADV_HALF_DUPLEX |
4737 MR_LP_ADV_SYM_PAUSE |
4738 MR_LP_ADV_ASYM_PAUSE |
4739 MR_LP_ADV_REMOTE_FAULT1 |
4740 MR_LP_ADV_REMOTE_FAULT2 |
4741 MR_LP_ADV_NEXT_PAGE |
4742 MR_TOGGLE_RX |
4743 MR_NP_RX);
4744 if (ap->rxconfig & ANEG_CFG_FD)
4745 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4746 if (ap->rxconfig & ANEG_CFG_HD)
4747 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4748 if (ap->rxconfig & ANEG_CFG_PS1)
4749 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4750 if (ap->rxconfig & ANEG_CFG_PS2)
4751 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4752 if (ap->rxconfig & ANEG_CFG_RF1)
4753 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4754 if (ap->rxconfig & ANEG_CFG_RF2)
4755 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4756 if (ap->rxconfig & ANEG_CFG_NP)
4757 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4758
4759 ap->link_time = ap->cur_time;
4760
4761 ap->flags ^= (MR_TOGGLE_TX);
4762 if (ap->rxconfig & 0x0008)
4763 ap->flags |= MR_TOGGLE_RX;
4764 if (ap->rxconfig & ANEG_CFG_NP)
4765 ap->flags |= MR_NP_RX;
4766 ap->flags |= MR_PAGE_RX;
4767
4768 ap->state = ANEG_STATE_COMPLETE_ACK;
4769 ret = ANEG_TIMER_ENAB;
4770 break;
4771
4772 case ANEG_STATE_COMPLETE_ACK:
4773 if (ap->ability_match != 0 &&
4774 ap->rxconfig == 0) {
4775 ap->state = ANEG_STATE_AN_ENABLE;
4776 break;
4777 }
4778 delta = ap->cur_time - ap->link_time;
4779 if (delta > ANEG_STATE_SETTLE_TIME) {
4780 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4781 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4782 } else {
4783 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4784 !(ap->flags & MR_NP_RX)) {
4785 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4786 } else {
4787 ret = ANEG_FAILED;
4788 }
4789 }
4790 }
4791 break;
4792
4793 case ANEG_STATE_IDLE_DETECT_INIT:
4794 ap->link_time = ap->cur_time;
4795 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4796 tw32_f(MAC_MODE, tp->mac_mode);
4797 udelay(40);
4798
4799 ap->state = ANEG_STATE_IDLE_DETECT;
4800 ret = ANEG_TIMER_ENAB;
4801 break;
4802
4803 case ANEG_STATE_IDLE_DETECT:
4804 if (ap->ability_match != 0 &&
4805 ap->rxconfig == 0) {
4806 ap->state = ANEG_STATE_AN_ENABLE;
4807 break;
4808 }
4809 delta = ap->cur_time - ap->link_time;
4810 if (delta > ANEG_STATE_SETTLE_TIME) {
4811 /* XXX another gem from the Broadcom driver :( */
4812 ap->state = ANEG_STATE_LINK_OK;
4813 }
4814 break;
4815
4816 case ANEG_STATE_LINK_OK:
4817 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4818 ret = ANEG_DONE;
4819 break;
4820
4821 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4822 /* ??? unimplemented */
4823 break;
4824
4825 case ANEG_STATE_NEXT_PAGE_WAIT:
4826 /* ??? unimplemented */
4827 break;
4828
4829 default:
4830 ret = ANEG_FAILED;
4831 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 return ret;
4835}
4836
Matt Carlson5be73b42007-12-20 20:09:29 -08004837static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838{
4839 int res = 0;
4840 struct tg3_fiber_aneginfo aninfo;
4841 int status = ANEG_FAILED;
4842 unsigned int tick;
4843 u32 tmp;
4844
4845 tw32_f(MAC_TX_AUTO_NEG, 0);
4846
4847 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4848 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4849 udelay(40);
4850
4851 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4852 udelay(40);
4853
4854 memset(&aninfo, 0, sizeof(aninfo));
4855 aninfo.flags |= MR_AN_ENABLE;
4856 aninfo.state = ANEG_STATE_UNKNOWN;
4857 aninfo.cur_time = 0;
4858 tick = 0;
4859 while (++tick < 195000) {
4860 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4861 if (status == ANEG_DONE || status == ANEG_FAILED)
4862 break;
4863
4864 udelay(1);
4865 }
4866
4867 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4868 tw32_f(MAC_MODE, tp->mac_mode);
4869 udelay(40);
4870
Matt Carlson5be73b42007-12-20 20:09:29 -08004871 *txflags = aninfo.txconfig;
4872 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (status == ANEG_DONE &&
4875 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4876 MR_LP_ADV_FULL_DUPLEX)))
4877 res = 1;
4878
4879 return res;
4880}
4881
4882static void tg3_init_bcm8002(struct tg3 *tp)
4883{
4884 u32 mac_status = tr32(MAC_STATUS);
4885 int i;
4886
4887 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004888 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 !(mac_status & MAC_STATUS_PCS_SYNCED))
4890 return;
4891
4892 /* Set PLL lock range. */
4893 tg3_writephy(tp, 0x16, 0x8007);
4894
4895 /* SW reset */
4896 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4897
4898 /* Wait for reset to complete. */
4899 /* XXX schedule_timeout() ... */
4900 for (i = 0; i < 500; i++)
4901 udelay(10);
4902
4903 /* Config mode; select PMA/Ch 1 regs. */
4904 tg3_writephy(tp, 0x10, 0x8411);
4905
4906 /* Enable auto-lock and comdet, select txclk for tx. */
4907 tg3_writephy(tp, 0x11, 0x0a10);
4908
4909 tg3_writephy(tp, 0x18, 0x00a0);
4910 tg3_writephy(tp, 0x16, 0x41ff);
4911
4912 /* Assert and deassert POR. */
4913 tg3_writephy(tp, 0x13, 0x0400);
4914 udelay(40);
4915 tg3_writephy(tp, 0x13, 0x0000);
4916
4917 tg3_writephy(tp, 0x11, 0x0a50);
4918 udelay(40);
4919 tg3_writephy(tp, 0x11, 0x0a10);
4920
4921 /* Wait for signal to stabilize */
4922 /* XXX schedule_timeout() ... */
4923 for (i = 0; i < 15000; i++)
4924 udelay(10);
4925
4926 /* Deselect the channel register so we can read the PHYID
4927 * later.
4928 */
4929 tg3_writephy(tp, 0x10, 0x8011);
4930}
4931
4932static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4933{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004934 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 u32 sg_dig_ctrl, sg_dig_status;
4936 u32 serdes_cfg, expected_sg_dig_ctrl;
4937 int workaround, port_a;
4938 int current_link_up;
4939
4940 serdes_cfg = 0;
4941 expected_sg_dig_ctrl = 0;
4942 workaround = 0;
4943 port_a = 1;
4944 current_link_up = 0;
4945
4946 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4947 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4948 workaround = 1;
4949 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4950 port_a = 0;
4951
4952 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4953 /* preserve bits 20-23 for voltage regulator */
4954 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4955 }
4956
4957 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4958
4959 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004960 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 if (workaround) {
4962 u32 val = serdes_cfg;
4963
4964 if (port_a)
4965 val |= 0xc010000;
4966 else
4967 val |= 0x4010000;
4968 tw32_f(MAC_SERDES_CFG, val);
4969 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004970
4971 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 }
4973 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4974 tg3_setup_flow_control(tp, 0, 0);
4975 current_link_up = 1;
4976 }
4977 goto out;
4978 }
4979
4980 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004981 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Matt Carlson82cd3d12007-12-20 20:09:00 -08004983 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4984 if (flowctrl & ADVERTISE_1000XPAUSE)
4985 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4986 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4987 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
4989 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004990 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004991 tp->serdes_counter &&
4992 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4993 MAC_STATUS_RCVD_CFG)) ==
4994 MAC_STATUS_PCS_SYNCED)) {
4995 tp->serdes_counter--;
4996 current_link_up = 1;
4997 goto out;
4998 }
4999restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 if (workaround)
5001 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005002 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 udelay(5);
5004 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
5005
Michael Chan3d3ebe72006-09-27 15:59:15 -07005006 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005007 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
5009 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005010 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 mac_status = tr32(MAC_STATUS);
5012
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005013 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005015 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Matt Carlson82cd3d12007-12-20 20:09:00 -08005017 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5018 local_adv |= ADVERTISE_1000XPAUSE;
5019 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5020 local_adv |= ADVERTISE_1000XPSE_ASYM;
5021
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005022 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005023 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005024 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005025 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Matt Carlson859edb22011-12-08 14:40:16 +00005027 tp->link_config.rmt_adv =
5028 mii_adv_to_ethtool_adv_x(remote_adv);
5029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 tg3_setup_flow_control(tp, local_adv, remote_adv);
5031 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005032 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005033 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005034 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005035 if (tp->serdes_counter)
5036 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 else {
5038 if (workaround) {
5039 u32 val = serdes_cfg;
5040
5041 if (port_a)
5042 val |= 0xc010000;
5043 else
5044 val |= 0x4010000;
5045
5046 tw32_f(MAC_SERDES_CFG, val);
5047 }
5048
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005049 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 udelay(40);
5051
5052 /* Link parallel detection - link is up */
5053 /* only if we have PCS_SYNC and not */
5054 /* receiving config code words */
5055 mac_status = tr32(MAC_STATUS);
5056 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5057 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5058 tg3_setup_flow_control(tp, 0, 0);
5059 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005060 tp->phy_flags |=
5061 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005062 tp->serdes_counter =
5063 SERDES_PARALLEL_DET_TIMEOUT;
5064 } else
5065 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
5067 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005068 } else {
5069 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005070 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 }
5072
5073out:
5074 return current_link_up;
5075}
5076
5077static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5078{
5079 int current_link_up = 0;
5080
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005081 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005085 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005087
Matt Carlson5be73b42007-12-20 20:09:29 -08005088 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5089 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Matt Carlson5be73b42007-12-20 20:09:29 -08005091 if (txflags & ANEG_CFG_PS1)
5092 local_adv |= ADVERTISE_1000XPAUSE;
5093 if (txflags & ANEG_CFG_PS2)
5094 local_adv |= ADVERTISE_1000XPSE_ASYM;
5095
5096 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5097 remote_adv |= LPA_1000XPAUSE;
5098 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5099 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Matt Carlson859edb22011-12-08 14:40:16 +00005101 tp->link_config.rmt_adv =
5102 mii_adv_to_ethtool_adv_x(remote_adv);
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 tg3_setup_flow_control(tp, local_adv, remote_adv);
5105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 current_link_up = 1;
5107 }
5108 for (i = 0; i < 30; i++) {
5109 udelay(20);
5110 tw32_f(MAC_STATUS,
5111 (MAC_STATUS_SYNC_CHANGED |
5112 MAC_STATUS_CFG_CHANGED));
5113 udelay(40);
5114 if ((tr32(MAC_STATUS) &
5115 (MAC_STATUS_SYNC_CHANGED |
5116 MAC_STATUS_CFG_CHANGED)) == 0)
5117 break;
5118 }
5119
5120 mac_status = tr32(MAC_STATUS);
5121 if (current_link_up == 0 &&
5122 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5123 !(mac_status & MAC_STATUS_RCVD_CFG))
5124 current_link_up = 1;
5125 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005126 tg3_setup_flow_control(tp, 0, 0);
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 /* Forcing 1000FD link up. */
5129 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
5131 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5132 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005133
5134 tw32_f(MAC_MODE, tp->mac_mode);
5135 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 }
5137
5138out:
5139 return current_link_up;
5140}
5141
5142static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5143{
5144 u32 orig_pause_cfg;
5145 u16 orig_active_speed;
5146 u8 orig_active_duplex;
5147 u32 mac_status;
5148 int current_link_up;
5149 int i;
5150
Matt Carlson8d018622007-12-20 20:05:44 -08005151 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 orig_active_speed = tp->link_config.active_speed;
5153 orig_active_duplex = tp->link_config.active_duplex;
5154
Joe Perches63c3a662011-04-26 08:12:10 +00005155 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005156 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005157 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 mac_status = tr32(MAC_STATUS);
5159 mac_status &= (MAC_STATUS_PCS_SYNCED |
5160 MAC_STATUS_SIGNAL_DET |
5161 MAC_STATUS_CFG_CHANGED |
5162 MAC_STATUS_RCVD_CFG);
5163 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5164 MAC_STATUS_SIGNAL_DET)) {
5165 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5166 MAC_STATUS_CFG_CHANGED));
5167 return 0;
5168 }
5169 }
5170
5171 tw32_f(MAC_TX_AUTO_NEG, 0);
5172
5173 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5174 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5175 tw32_f(MAC_MODE, tp->mac_mode);
5176 udelay(40);
5177
Matt Carlson79eb6902010-02-17 15:17:03 +00005178 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 tg3_init_bcm8002(tp);
5180
5181 /* Enable link change event even when serdes polling. */
5182 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5183 udelay(40);
5184
5185 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005186 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 mac_status = tr32(MAC_STATUS);
5188
Joe Perches63c3a662011-04-26 08:12:10 +00005189 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5191 else
5192 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5193
Matt Carlson898a56f2009-08-28 14:02:40 +00005194 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005196 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
5198 for (i = 0; i < 100; i++) {
5199 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5200 MAC_STATUS_CFG_CHANGED));
5201 udelay(5);
5202 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005203 MAC_STATUS_CFG_CHANGED |
5204 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 break;
5206 }
5207
5208 mac_status = tr32(MAC_STATUS);
5209 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5210 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005211 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5212 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 tw32_f(MAC_MODE, (tp->mac_mode |
5214 MAC_MODE_SEND_CONFIGS));
5215 udelay(1);
5216 tw32_f(MAC_MODE, tp->mac_mode);
5217 }
5218 }
5219
5220 if (current_link_up == 1) {
5221 tp->link_config.active_speed = SPEED_1000;
5222 tp->link_config.active_duplex = DUPLEX_FULL;
5223 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5224 LED_CTRL_LNKLED_OVERRIDE |
5225 LED_CTRL_1000MBPS_ON));
5226 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005227 tp->link_config.active_speed = SPEED_UNKNOWN;
5228 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5230 LED_CTRL_LNKLED_OVERRIDE |
5231 LED_CTRL_TRAFFIC_OVERRIDE));
5232 }
5233
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005234 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005235 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (orig_pause_cfg != now_pause_cfg ||
5237 orig_active_speed != tp->link_config.active_speed ||
5238 orig_active_duplex != tp->link_config.active_duplex)
5239 tg3_link_report(tp);
5240 }
5241
5242 return 0;
5243}
5244
Michael Chan747e8f82005-07-25 12:33:22 -07005245static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5246{
5247 int current_link_up, err = 0;
5248 u32 bmsr, bmcr;
5249 u16 current_speed;
5250 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005251 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005252
5253 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5254 tw32_f(MAC_MODE, tp->mac_mode);
5255 udelay(40);
5256
5257 tw32(MAC_EVENT, 0);
5258
5259 tw32_f(MAC_STATUS,
5260 (MAC_STATUS_SYNC_CHANGED |
5261 MAC_STATUS_CFG_CHANGED |
5262 MAC_STATUS_MI_COMPLETION |
5263 MAC_STATUS_LNKSTATE_CHANGED));
5264 udelay(40);
5265
5266 if (force_reset)
5267 tg3_phy_reset(tp);
5268
5269 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005270 current_speed = SPEED_UNKNOWN;
5271 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005272 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005273
5274 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5275 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5277 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5278 bmsr |= BMSR_LSTATUS;
5279 else
5280 bmsr &= ~BMSR_LSTATUS;
5281 }
Michael Chan747e8f82005-07-25 12:33:22 -07005282
5283 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5284
5285 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005286 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005287 /* do nothing, just check for link up at the end */
5288 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005289 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005290
5291 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005292 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5293 ADVERTISE_1000XPAUSE |
5294 ADVERTISE_1000XPSE_ASYM |
5295 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005296
Matt Carlson28011cf2011-11-16 18:36:59 -05005297 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005298 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005299
Matt Carlson28011cf2011-11-16 18:36:59 -05005300 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5301 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005302 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5303 tg3_writephy(tp, MII_BMCR, bmcr);
5304
5305 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005306 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005307 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005308
5309 return err;
5310 }
5311 } else {
5312 u32 new_bmcr;
5313
5314 bmcr &= ~BMCR_SPEED1000;
5315 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5316
5317 if (tp->link_config.duplex == DUPLEX_FULL)
5318 new_bmcr |= BMCR_FULLDPLX;
5319
5320 if (new_bmcr != bmcr) {
5321 /* BMCR_SPEED1000 is a reserved bit that needs
5322 * to be set on write.
5323 */
5324 new_bmcr |= BMCR_SPEED1000;
5325
5326 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005327 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005328 u32 adv;
5329
5330 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5331 adv &= ~(ADVERTISE_1000XFULL |
5332 ADVERTISE_1000XHALF |
5333 ADVERTISE_SLCT);
5334 tg3_writephy(tp, MII_ADVERTISE, adv);
5335 tg3_writephy(tp, MII_BMCR, bmcr |
5336 BMCR_ANRESTART |
5337 BMCR_ANENABLE);
5338 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005339 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005340 }
5341 tg3_writephy(tp, MII_BMCR, new_bmcr);
5342 bmcr = new_bmcr;
5343 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5344 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005345 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5346 ASIC_REV_5714) {
5347 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5348 bmsr |= BMSR_LSTATUS;
5349 else
5350 bmsr &= ~BMSR_LSTATUS;
5351 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005352 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005353 }
5354 }
5355
5356 if (bmsr & BMSR_LSTATUS) {
5357 current_speed = SPEED_1000;
5358 current_link_up = 1;
5359 if (bmcr & BMCR_FULLDPLX)
5360 current_duplex = DUPLEX_FULL;
5361 else
5362 current_duplex = DUPLEX_HALF;
5363
Matt Carlsonef167e22007-12-20 20:10:01 -08005364 local_adv = 0;
5365 remote_adv = 0;
5366
Michael Chan747e8f82005-07-25 12:33:22 -07005367 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005368 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005369
5370 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5371 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5372 common = local_adv & remote_adv;
5373 if (common & (ADVERTISE_1000XHALF |
5374 ADVERTISE_1000XFULL)) {
5375 if (common & ADVERTISE_1000XFULL)
5376 current_duplex = DUPLEX_FULL;
5377 else
5378 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005379
5380 tp->link_config.rmt_adv =
5381 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005382 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005383 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005384 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005385 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005386 }
Michael Chan747e8f82005-07-25 12:33:22 -07005387 }
5388 }
5389
Matt Carlsonef167e22007-12-20 20:10:01 -08005390 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5391 tg3_setup_flow_control(tp, local_adv, remote_adv);
5392
Michael Chan747e8f82005-07-25 12:33:22 -07005393 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5394 if (tp->link_config.active_duplex == DUPLEX_HALF)
5395 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5396
5397 tw32_f(MAC_MODE, tp->mac_mode);
5398 udelay(40);
5399
5400 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5401
5402 tp->link_config.active_speed = current_speed;
5403 tp->link_config.active_duplex = current_duplex;
5404
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005405 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005406 return err;
5407}
5408
5409static void tg3_serdes_parallel_detect(struct tg3 *tp)
5410{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005411 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005412 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005413 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005414 return;
5415 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005416
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005417 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005418 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5419 u32 bmcr;
5420
5421 tg3_readphy(tp, MII_BMCR, &bmcr);
5422 if (bmcr & BMCR_ANENABLE) {
5423 u32 phy1, phy2;
5424
5425 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005426 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5427 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005428
5429 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005430 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5431 MII_TG3_DSP_EXP1_INT_STAT);
5432 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5433 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005434
5435 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5436 /* We have signal detect and not receiving
5437 * config code words, link is up by parallel
5438 * detection.
5439 */
5440
5441 bmcr &= ~BMCR_ANENABLE;
5442 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5443 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005444 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005445 }
5446 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005447 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005448 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005449 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005450 u32 phy2;
5451
5452 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005453 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5454 MII_TG3_DSP_EXP1_INT_STAT);
5455 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005456 if (phy2 & 0x20) {
5457 u32 bmcr;
5458
5459 /* Config code words received, turn on autoneg. */
5460 tg3_readphy(tp, MII_BMCR, &bmcr);
5461 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5462
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005463 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005464
5465 }
5466 }
5467}
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5470{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005471 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 int err;
5473
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005474 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005476 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005477 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005478 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005481 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005482 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005483
5484 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5485 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5486 scale = 65;
5487 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5488 scale = 6;
5489 else
5490 scale = 12;
5491
5492 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5493 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5494 tw32(GRC_MISC_CFG, val);
5495 }
5496
Matt Carlsonf2096f92011-04-05 14:22:48 +00005497 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5498 (6 << TX_LENGTHS_IPG_SHIFT);
5499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5500 val |= tr32(MAC_TX_LENGTHS) &
5501 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5502 TX_LENGTHS_CNT_DWN_VAL_MSK);
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (tp->link_config.active_speed == SPEED_1000 &&
5505 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005506 tw32(MAC_TX_LENGTHS, val |
5507 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005509 tw32(MAC_TX_LENGTHS, val |
5510 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Joe Perches63c3a662011-04-26 08:12:10 +00005512 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005513 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005515 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 } else {
5517 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5518 }
5519 }
5520
Joe Perches63c3a662011-04-26 08:12:10 +00005521 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005522 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005523 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005524 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5525 tp->pwrmgmt_thresh;
5526 else
5527 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5528 tw32(PCIE_PWR_MGMT_THRESH, val);
5529 }
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 return err;
5532}
5533
Matt Carlsonbe947302012-12-03 19:36:57 +00005534/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005535static u64 tg3_refclk_read(struct tg3 *tp)
5536{
5537 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5538 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5539}
5540
5541/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005542static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5543{
5544 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5545 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5546 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5547 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5548}
5549
Matt Carlson7d41e492012-12-03 19:36:58 +00005550static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5551static inline void tg3_full_unlock(struct tg3 *tp);
5552static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5553{
5554 struct tg3 *tp = netdev_priv(dev);
5555
5556 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5557 SOF_TIMESTAMPING_RX_SOFTWARE |
5558 SOF_TIMESTAMPING_SOFTWARE |
5559 SOF_TIMESTAMPING_TX_HARDWARE |
5560 SOF_TIMESTAMPING_RX_HARDWARE |
5561 SOF_TIMESTAMPING_RAW_HARDWARE;
5562
5563 if (tp->ptp_clock)
5564 info->phc_index = ptp_clock_index(tp->ptp_clock);
5565 else
5566 info->phc_index = -1;
5567
5568 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5569
5570 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5571 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5572 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5573 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5574 return 0;
5575}
5576
5577static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5578{
5579 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5580 bool neg_adj = false;
5581 u32 correction = 0;
5582
5583 if (ppb < 0) {
5584 neg_adj = true;
5585 ppb = -ppb;
5586 }
5587
5588 /* Frequency adjustment is performed using hardware with a 24 bit
5589 * accumulator and a programmable correction value. On each clk, the
5590 * correction value gets added to the accumulator and when it
5591 * overflows, the time counter is incremented/decremented.
5592 *
5593 * So conversion from ppb to correction value is
5594 * ppb * (1 << 24) / 1000000000
5595 */
5596 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5597 TG3_EAV_REF_CLK_CORRECT_MASK;
5598
5599 tg3_full_lock(tp, 0);
5600
5601 if (correction)
5602 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5603 TG3_EAV_REF_CLK_CORRECT_EN |
5604 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5605 else
5606 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5607
5608 tg3_full_unlock(tp);
5609
5610 return 0;
5611}
5612
5613static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5614{
5615 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5616
5617 tg3_full_lock(tp, 0);
5618 tp->ptp_adjust += delta;
5619 tg3_full_unlock(tp);
5620
5621 return 0;
5622}
5623
5624static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5625{
5626 u64 ns;
5627 u32 remainder;
5628 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5629
5630 tg3_full_lock(tp, 0);
5631 ns = tg3_refclk_read(tp);
5632 ns += tp->ptp_adjust;
5633 tg3_full_unlock(tp);
5634
5635 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5636 ts->tv_nsec = remainder;
5637
5638 return 0;
5639}
5640
5641static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5642 const struct timespec *ts)
5643{
5644 u64 ns;
5645 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5646
5647 ns = timespec_to_ns(ts);
5648
5649 tg3_full_lock(tp, 0);
5650 tg3_refclk_write(tp, ns);
5651 tp->ptp_adjust = 0;
5652 tg3_full_unlock(tp);
5653
5654 return 0;
5655}
5656
5657static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5658 struct ptp_clock_request *rq, int on)
5659{
5660 return -EOPNOTSUPP;
5661}
5662
5663static const struct ptp_clock_info tg3_ptp_caps = {
5664 .owner = THIS_MODULE,
5665 .name = "tg3 clock",
5666 .max_adj = 250000000,
5667 .n_alarm = 0,
5668 .n_ext_ts = 0,
5669 .n_per_out = 0,
5670 .pps = 0,
5671 .adjfreq = tg3_ptp_adjfreq,
5672 .adjtime = tg3_ptp_adjtime,
5673 .gettime = tg3_ptp_gettime,
5674 .settime = tg3_ptp_settime,
5675 .enable = tg3_ptp_enable,
5676};
5677
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005678static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5679 struct skb_shared_hwtstamps *timestamp)
5680{
5681 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5682 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5683 tp->ptp_adjust);
5684}
5685
Matt Carlsonbe947302012-12-03 19:36:57 +00005686/* tp->lock must be held */
5687static void tg3_ptp_init(struct tg3 *tp)
5688{
5689 if (!tg3_flag(tp, PTP_CAPABLE))
5690 return;
5691
5692 /* Initialize the hardware clock to the system time. */
5693 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5694 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005695 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005696}
5697
5698/* tp->lock must be held */
5699static void tg3_ptp_resume(struct tg3 *tp)
5700{
5701 if (!tg3_flag(tp, PTP_CAPABLE))
5702 return;
5703
5704 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5705 tp->ptp_adjust = 0;
5706}
5707
5708static void tg3_ptp_fini(struct tg3 *tp)
5709{
5710 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5711 return;
5712
Matt Carlson7d41e492012-12-03 19:36:58 +00005713 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005714 tp->ptp_clock = NULL;
5715 tp->ptp_adjust = 0;
5716}
5717
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005718static inline int tg3_irq_sync(struct tg3 *tp)
5719{
5720 return tp->irq_sync;
5721}
5722
Matt Carlson97bd8e42011-04-13 11:05:04 +00005723static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5724{
5725 int i;
5726
5727 dst = (u32 *)((u8 *)dst + off);
5728 for (i = 0; i < len; i += sizeof(u32))
5729 *dst++ = tr32(off + i);
5730}
5731
5732static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5733{
5734 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5735 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5736 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5737 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5738 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5739 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5740 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5741 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5742 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5743 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5744 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5745 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5746 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5747 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5748 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5749 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5750 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5751 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5752 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5753
Joe Perches63c3a662011-04-26 08:12:10 +00005754 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005755 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5756
5757 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5758 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5759 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5760 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5761 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5762 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5763 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5764 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5765
Joe Perches63c3a662011-04-26 08:12:10 +00005766 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005767 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5768 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5769 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5770 }
5771
5772 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5773 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5774 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5775 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5776 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5777
Joe Perches63c3a662011-04-26 08:12:10 +00005778 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005779 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5780}
5781
5782static void tg3_dump_state(struct tg3 *tp)
5783{
5784 int i;
5785 u32 *regs;
5786
5787 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5788 if (!regs) {
5789 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5790 return;
5791 }
5792
Joe Perches63c3a662011-04-26 08:12:10 +00005793 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005794 /* Read up to but not including private PCI registers */
5795 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5796 regs[i / sizeof(u32)] = tr32(i);
5797 } else
5798 tg3_dump_legacy_regs(tp, regs);
5799
5800 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5801 if (!regs[i + 0] && !regs[i + 1] &&
5802 !regs[i + 2] && !regs[i + 3])
5803 continue;
5804
5805 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5806 i * 4,
5807 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5808 }
5809
5810 kfree(regs);
5811
5812 for (i = 0; i < tp->irq_cnt; i++) {
5813 struct tg3_napi *tnapi = &tp->napi[i];
5814
5815 /* SW status block */
5816 netdev_err(tp->dev,
5817 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5818 i,
5819 tnapi->hw_status->status,
5820 tnapi->hw_status->status_tag,
5821 tnapi->hw_status->rx_jumbo_consumer,
5822 tnapi->hw_status->rx_consumer,
5823 tnapi->hw_status->rx_mini_consumer,
5824 tnapi->hw_status->idx[0].rx_producer,
5825 tnapi->hw_status->idx[0].tx_consumer);
5826
5827 netdev_err(tp->dev,
5828 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5829 i,
5830 tnapi->last_tag, tnapi->last_irq_tag,
5831 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5832 tnapi->rx_rcb_ptr,
5833 tnapi->prodring.rx_std_prod_idx,
5834 tnapi->prodring.rx_std_cons_idx,
5835 tnapi->prodring.rx_jmb_prod_idx,
5836 tnapi->prodring.rx_jmb_cons_idx);
5837 }
5838}
5839
Michael Chandf3e6542006-05-26 17:48:07 -07005840/* This is called whenever we suspect that the system chipset is re-
5841 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5842 * is bogus tx completions. We try to recover by setting the
5843 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5844 * in the workqueue.
5845 */
5846static void tg3_tx_recover(struct tg3 *tp)
5847{
Joe Perches63c3a662011-04-26 08:12:10 +00005848 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005849 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5850
Matt Carlson5129c3a2010-04-05 10:19:23 +00005851 netdev_warn(tp->dev,
5852 "The system may be re-ordering memory-mapped I/O "
5853 "cycles to the network device, attempting to recover. "
5854 "Please report the problem to the driver maintainer "
5855 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005856
5857 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005858 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005859 spin_unlock(&tp->lock);
5860}
5861
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005862static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005863{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005864 /* Tell compiler to fetch tx indices from memory. */
5865 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005866 return tnapi->tx_pending -
5867 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005868}
5869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870/* Tigon3 never reports partial packet sends. So we do not
5871 * need special logic to handle SKBs that have not had all
5872 * of their frags sent yet, like SunGEM does.
5873 */
Matt Carlson17375d22009-08-28 14:02:18 +00005874static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875{
Matt Carlson17375d22009-08-28 14:02:18 +00005876 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005877 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005878 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005879 struct netdev_queue *txq;
5880 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005881 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005882
Joe Perches63c3a662011-04-26 08:12:10 +00005883 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005884 index--;
5885
5886 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
5888 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005889 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005891 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
Michael Chandf3e6542006-05-26 17:48:07 -07005893 if (unlikely(skb == NULL)) {
5894 tg3_tx_recover(tp);
5895 return;
5896 }
5897
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005898 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
5899 struct skb_shared_hwtstamps timestamp;
5900 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
5901 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
5902
5903 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
5904
5905 skb_tstamp_tx(skb, &timestamp);
5906 }
5907
Alexander Duyckf4188d82009-12-02 16:48:38 +00005908 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005909 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005910 skb_headlen(skb),
5911 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
5913 ri->skb = NULL;
5914
Matt Carlsone01ee142011-07-27 14:20:50 +00005915 while (ri->fragmented) {
5916 ri->fragmented = false;
5917 sw_idx = NEXT_TX(sw_idx);
5918 ri = &tnapi->tx_buffers[sw_idx];
5919 }
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 sw_idx = NEXT_TX(sw_idx);
5922
5923 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005924 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005925 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5926 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005927
5928 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005929 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005930 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005931 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005932
5933 while (ri->fragmented) {
5934 ri->fragmented = false;
5935 sw_idx = NEXT_TX(sw_idx);
5936 ri = &tnapi->tx_buffers[sw_idx];
5937 }
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 sw_idx = NEXT_TX(sw_idx);
5940 }
5941
Tom Herbert298376d2011-11-28 16:33:30 +00005942 pkts_compl++;
5943 bytes_compl += skb->len;
5944
David S. Millerf47c11e2005-06-24 20:18:35 -07005945 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005946
5947 if (unlikely(tx_bug)) {
5948 tg3_tx_recover(tp);
5949 return;
5950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 }
5952
Tom Herbert5cb917b2012-03-05 19:53:50 +00005953 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005954
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005955 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
Michael Chan1b2a7202006-08-07 21:46:02 -07005957 /* Need to make the tx_cons update visible to tg3_start_xmit()
5958 * before checking for netif_queue_stopped(). Without the
5959 * memory barrier, there is a small possibility that tg3_start_xmit()
5960 * will miss it and cause the queue to be stopped forever.
5961 */
5962 smp_mb();
5963
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005964 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005965 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005966 __netif_tx_lock(txq, smp_processor_id());
5967 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005968 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005969 netif_tx_wake_queue(txq);
5970 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972}
5973
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005974static void tg3_frag_free(bool is_frag, void *data)
5975{
5976 if (is_frag)
5977 put_page(virt_to_head_page(data));
5978 else
5979 kfree(data);
5980}
5981
Eric Dumazet9205fd92011-11-18 06:47:01 +00005982static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005983{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005984 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5985 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5986
Eric Dumazet9205fd92011-11-18 06:47:01 +00005987 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005988 return;
5989
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005990 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005991 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005992 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005993 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005994}
5995
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997/* Returns size of skb allocated or < 0 on error.
5998 *
5999 * We only need to fill in the address because the other members
6000 * of the RX descriptor are invariant, see tg3_init_rings.
6001 *
6002 * Note the purposeful assymetry of cpu vs. chip accesses. For
6003 * posting buffers we only dirty the first cache line of the RX
6004 * descriptor (containing the address). Whereas for the RX status
6005 * buffers the cpu only reads the last cacheline of the RX descriptor
6006 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
6007 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006008static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006009 u32 opaque_key, u32 dest_idx_unmasked,
6010 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011{
6012 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006013 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006014 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006016 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 switch (opaque_key) {
6019 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006020 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006021 desc = &tpr->rx_std[dest_idx];
6022 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006023 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 break;
6025
6026 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006027 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006028 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006029 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006030 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 break;
6032
6033 default:
6034 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
6037 /* Do not overwrite any of the map or rp information
6038 * until we are sure we can commit to a new buffer.
6039 *
6040 * Callers depend upon this behavior and assume that
6041 * we leave everything unchanged if we fail.
6042 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006043 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6044 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006045 if (skb_size <= PAGE_SIZE) {
6046 data = netdev_alloc_frag(skb_size);
6047 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006048 } else {
6049 data = kmalloc(skb_size, GFP_ATOMIC);
6050 *frag_size = 0;
6051 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006052 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 return -ENOMEM;
6054
Eric Dumazet9205fd92011-11-18 06:47:01 +00006055 mapping = pci_map_single(tp->pdev,
6056 data + TG3_RX_OFFSET(tp),
6057 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006059 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006060 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006061 return -EIO;
6062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
Eric Dumazet9205fd92011-11-18 06:47:01 +00006064 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006065 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 desc->addr_hi = ((u64)mapping >> 32);
6068 desc->addr_lo = ((u64)mapping & 0xffffffff);
6069
Eric Dumazet9205fd92011-11-18 06:47:01 +00006070 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071}
6072
6073/* We only need to move over in the address because the other
6074 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006075 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 */
Matt Carlsona3896162009-11-13 13:03:44 +00006077static void tg3_recycle_rx(struct tg3_napi *tnapi,
6078 struct tg3_rx_prodring_set *dpr,
6079 u32 opaque_key, int src_idx,
6080 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081{
Matt Carlson17375d22009-08-28 14:02:18 +00006082 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6084 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006085 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006086 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
6088 switch (opaque_key) {
6089 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006090 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006091 dest_desc = &dpr->rx_std[dest_idx];
6092 dest_map = &dpr->rx_std_buffers[dest_idx];
6093 src_desc = &spr->rx_std[src_idx];
6094 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 break;
6096
6097 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006098 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006099 dest_desc = &dpr->rx_jmb[dest_idx].std;
6100 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6101 src_desc = &spr->rx_jmb[src_idx].std;
6102 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 break;
6104
6105 default:
6106 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Eric Dumazet9205fd92011-11-18 06:47:01 +00006109 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006110 dma_unmap_addr_set(dest_map, mapping,
6111 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 dest_desc->addr_hi = src_desc->addr_hi;
6113 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006114
6115 /* Ensure that the update to the skb happens after the physical
6116 * addresses have been transferred to the new BD location.
6117 */
6118 smp_wmb();
6119
Eric Dumazet9205fd92011-11-18 06:47:01 +00006120 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121}
6122
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123/* The RX ring scheme is composed of multiple rings which post fresh
6124 * buffers to the chip, and one special ring the chip uses to report
6125 * status back to the host.
6126 *
6127 * The special ring reports the status of received packets to the
6128 * host. The chip does not write into the original descriptor the
6129 * RX buffer was obtained from. The chip simply takes the original
6130 * descriptor as provided by the host, updates the status and length
6131 * field, then writes this into the next status ring entry.
6132 *
6133 * Each ring the host uses to post buffers to the chip is described
6134 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6135 * it is first placed into the on-chip ram. When the packet's length
6136 * is known, it walks down the TG3_BDINFO entries to select the ring.
6137 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6138 * which is within the range of the new packet's length is chosen.
6139 *
6140 * The "separate ring for rx status" scheme may sound queer, but it makes
6141 * sense from a cache coherency perspective. If only the host writes
6142 * to the buffer post rings, and only the chip writes to the rx status
6143 * rings, then cache lines never move beyond shared-modified state.
6144 * If both the host and chip were to write into the same ring, cache line
6145 * eviction could occur since both entities want it in an exclusive state.
6146 */
Matt Carlson17375d22009-08-28 14:02:18 +00006147static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
Matt Carlson17375d22009-08-28 14:02:18 +00006149 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006150 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006151 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006152 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006153 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006155 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006157 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 /*
6159 * We need to order the read of hw_idx and the read of
6160 * the opaque cookie.
6161 */
6162 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 work_mask = 0;
6164 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006165 std_prod_idx = tpr->rx_std_prod_idx;
6166 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006168 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006169 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 unsigned int len;
6171 struct sk_buff *skb;
6172 dma_addr_t dma_addr;
6173 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006174 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006175 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6178 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6179 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006180 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006181 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006182 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006183 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006184 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006186 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006187 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006188 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006189 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006190 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
6193 work_mask |= opaque_key;
6194
6195 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6196 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6197 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006198 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 desc_idx, *post_ptr);
6200 drop_it_no_recycle:
6201 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006202 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 goto next_pkt;
6204 }
6205
Eric Dumazet9205fd92011-11-18 06:47:01 +00006206 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006207 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6208 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006210 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6211 RXD_FLAG_PTPSTAT_PTPV1 ||
6212 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6213 RXD_FLAG_PTPSTAT_PTPV2) {
6214 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6215 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6216 }
6217
Matt Carlsond2757fc2010-04-12 06:58:27 +00006218 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006220 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
Eric Dumazet9205fd92011-11-18 06:47:01 +00006222 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006223 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 if (skb_size < 0)
6225 goto drop_it;
6226
Matt Carlson287be122009-08-28 13:58:46 +00006227 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 PCI_DMA_FROMDEVICE);
6229
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006230 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006231 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006232 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006233 goto drop_it_no_recycle;
6234 }
6235 skb_reserve(skb, TG3_RX_OFFSET(tp));
6236 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006237 * after the usage of the old DMA mapping.
6238 */
6239 smp_wmb();
6240
Eric Dumazet9205fd92011-11-18 06:47:01 +00006241 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006244 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 desc_idx, *post_ptr);
6246
Eric Dumazet9205fd92011-11-18 06:47:01 +00006247 skb = netdev_alloc_skb(tp->dev,
6248 len + TG3_RAW_IP_ALIGN);
6249 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 goto drop_it_no_recycle;
6251
Eric Dumazet9205fd92011-11-18 06:47:01 +00006252 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006254 memcpy(skb->data,
6255 data + TG3_RX_OFFSET(tp),
6256 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 }
6259
Eric Dumazet9205fd92011-11-18 06:47:01 +00006260 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006261 if (tstamp)
6262 tg3_hwclock_to_timestamp(tp, tstamp,
6263 skb_hwtstamps(skb));
6264
Michał Mirosławdc668912011-04-07 03:35:07 +00006265 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6267 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6268 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6269 skb->ip_summed = CHECKSUM_UNNECESSARY;
6270 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006271 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006274
6275 if (len > (tp->dev->mtu + ETH_HLEN) &&
6276 skb->protocol != htons(ETH_P_8021Q)) {
6277 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006278 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006279 }
6280
Matt Carlson9dc7a112010-04-12 06:58:28 +00006281 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006282 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6283 __vlan_hwaccel_put_tag(skb,
6284 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006285
Matt Carlsonbf933c82011-01-25 15:58:49 +00006286 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 received++;
6289 budget--;
6290
6291next_pkt:
6292 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006293
6294 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006295 tpr->rx_std_prod_idx = std_prod_idx &
6296 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006297 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6298 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006299 work_mask &= ~RXD_OPAQUE_RING_STD;
6300 rx_std_posted = 0;
6301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006303 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006304 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006305
6306 /* Refresh hw_idx to see if there is new work */
6307 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006308 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006309 rmb();
6310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 }
6312
6313 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006314 tnapi->rx_rcb_ptr = sw_idx;
6315 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
6317 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006318 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006319 /* Sync BD data before updating mailbox */
6320 wmb();
6321
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006322 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006323 tpr->rx_std_prod_idx = std_prod_idx &
6324 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006325 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6326 tpr->rx_std_prod_idx);
6327 }
6328 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006329 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6330 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006331 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6332 tpr->rx_jmb_prod_idx);
6333 }
6334 mmiowb();
6335 } else if (work_mask) {
6336 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6337 * updated before the producer indices can be updated.
6338 */
6339 smp_wmb();
6340
Matt Carlson2c49a442010-09-30 10:34:35 +00006341 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6342 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006343
Michael Chan7ae52892012-03-21 15:38:33 +00006344 if (tnapi != &tp->napi[1]) {
6345 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006346 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 return received;
6351}
6352
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006353static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006356 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006357 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 if (sblk->status & SD_STATUS_LINK_CHG) {
6360 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006361 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006362 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006363 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006364 tw32_f(MAC_STATUS,
6365 (MAC_STATUS_SYNC_CHANGED |
6366 MAC_STATUS_CFG_CHANGED |
6367 MAC_STATUS_MI_COMPLETION |
6368 MAC_STATUS_LNKSTATE_CHANGED));
6369 udelay(40);
6370 } else
6371 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006372 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 }
6374 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006375}
6376
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006377static int tg3_rx_prodring_xfer(struct tg3 *tp,
6378 struct tg3_rx_prodring_set *dpr,
6379 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006380{
6381 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006382 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006383
6384 while (1) {
6385 src_prod_idx = spr->rx_std_prod_idx;
6386
6387 /* Make sure updates to the rx_std_buffers[] entries and the
6388 * standard producer index are seen in the correct order.
6389 */
6390 smp_rmb();
6391
6392 if (spr->rx_std_cons_idx == src_prod_idx)
6393 break;
6394
6395 if (spr->rx_std_cons_idx < src_prod_idx)
6396 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6397 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006398 cpycnt = tp->rx_std_ring_mask + 1 -
6399 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006400
Matt Carlson2c49a442010-09-30 10:34:35 +00006401 cpycnt = min(cpycnt,
6402 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006403
6404 si = spr->rx_std_cons_idx;
6405 di = dpr->rx_std_prod_idx;
6406
Matt Carlsone92967b2010-02-12 14:47:06 +00006407 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006408 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006409 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006410 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006411 break;
6412 }
6413 }
6414
6415 if (!cpycnt)
6416 break;
6417
6418 /* Ensure that updates to the rx_std_buffers ring and the
6419 * shadowed hardware producer ring from tg3_recycle_skb() are
6420 * ordered correctly WRT the skb check above.
6421 */
6422 smp_rmb();
6423
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006424 memcpy(&dpr->rx_std_buffers[di],
6425 &spr->rx_std_buffers[si],
6426 cpycnt * sizeof(struct ring_info));
6427
6428 for (i = 0; i < cpycnt; i++, di++, si++) {
6429 struct tg3_rx_buffer_desc *sbd, *dbd;
6430 sbd = &spr->rx_std[si];
6431 dbd = &dpr->rx_std[di];
6432 dbd->addr_hi = sbd->addr_hi;
6433 dbd->addr_lo = sbd->addr_lo;
6434 }
6435
Matt Carlson2c49a442010-09-30 10:34:35 +00006436 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6437 tp->rx_std_ring_mask;
6438 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6439 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006440 }
6441
6442 while (1) {
6443 src_prod_idx = spr->rx_jmb_prod_idx;
6444
6445 /* Make sure updates to the rx_jmb_buffers[] entries and
6446 * the jumbo producer index are seen in the correct order.
6447 */
6448 smp_rmb();
6449
6450 if (spr->rx_jmb_cons_idx == src_prod_idx)
6451 break;
6452
6453 if (spr->rx_jmb_cons_idx < src_prod_idx)
6454 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6455 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006456 cpycnt = tp->rx_jmb_ring_mask + 1 -
6457 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006458
6459 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006460 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006461
6462 si = spr->rx_jmb_cons_idx;
6463 di = dpr->rx_jmb_prod_idx;
6464
Matt Carlsone92967b2010-02-12 14:47:06 +00006465 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006466 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006467 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006468 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006469 break;
6470 }
6471 }
6472
6473 if (!cpycnt)
6474 break;
6475
6476 /* Ensure that updates to the rx_jmb_buffers ring and the
6477 * shadowed hardware producer ring from tg3_recycle_skb() are
6478 * ordered correctly WRT the skb check above.
6479 */
6480 smp_rmb();
6481
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006482 memcpy(&dpr->rx_jmb_buffers[di],
6483 &spr->rx_jmb_buffers[si],
6484 cpycnt * sizeof(struct ring_info));
6485
6486 for (i = 0; i < cpycnt; i++, di++, si++) {
6487 struct tg3_rx_buffer_desc *sbd, *dbd;
6488 sbd = &spr->rx_jmb[si].std;
6489 dbd = &dpr->rx_jmb[di].std;
6490 dbd->addr_hi = sbd->addr_hi;
6491 dbd->addr_lo = sbd->addr_lo;
6492 }
6493
Matt Carlson2c49a442010-09-30 10:34:35 +00006494 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6495 tp->rx_jmb_ring_mask;
6496 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6497 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006498 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006499
6500 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006501}
6502
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006503static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6504{
6505 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
6507 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006508 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006509 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006510 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006511 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 }
6513
Matt Carlsonf891ea12012-04-24 13:37:01 +00006514 if (!tnapi->rx_rcb_prod_idx)
6515 return work_done;
6516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 /* run RX thread, within the bounds set by NAPI.
6518 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006519 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006521 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006522 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
Joe Perches63c3a662011-04-26 08:12:10 +00006524 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006525 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006526 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006527 u32 std_prod_idx = dpr->rx_std_prod_idx;
6528 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006529
Michael Chan7ae52892012-03-21 15:38:33 +00006530 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006531 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006532 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006533 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006534
6535 wmb();
6536
Matt Carlsone4af1af2010-02-12 14:47:05 +00006537 if (std_prod_idx != dpr->rx_std_prod_idx)
6538 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6539 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006540
Matt Carlsone4af1af2010-02-12 14:47:05 +00006541 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6542 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6543 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006544
6545 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006546
6547 if (err)
6548 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006549 }
6550
David S. Miller6f535762007-10-11 18:08:29 -07006551 return work_done;
6552}
David S. Millerf7383c222005-05-18 22:50:53 -07006553
Matt Carlsondb219972011-11-04 09:15:03 +00006554static inline void tg3_reset_task_schedule(struct tg3 *tp)
6555{
6556 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6557 schedule_work(&tp->reset_task);
6558}
6559
6560static inline void tg3_reset_task_cancel(struct tg3 *tp)
6561{
6562 cancel_work_sync(&tp->reset_task);
6563 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006564 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006565}
6566
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006567static int tg3_poll_msix(struct napi_struct *napi, int budget)
6568{
6569 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6570 struct tg3 *tp = tnapi->tp;
6571 int work_done = 0;
6572 struct tg3_hw_status *sblk = tnapi->hw_status;
6573
6574 while (1) {
6575 work_done = tg3_poll_work(tnapi, work_done, budget);
6576
Joe Perches63c3a662011-04-26 08:12:10 +00006577 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006578 goto tx_recovery;
6579
6580 if (unlikely(work_done >= budget))
6581 break;
6582
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006583 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006584 * to tell the hw how much work has been processed,
6585 * so we must read it before checking for more work.
6586 */
6587 tnapi->last_tag = sblk->status_tag;
6588 tnapi->last_irq_tag = tnapi->last_tag;
6589 rmb();
6590
6591 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006592 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6593 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006594
6595 /* This test here is not race free, but will reduce
6596 * the number of interrupts by looping again.
6597 */
6598 if (tnapi == &tp->napi[1] && tp->rx_refill)
6599 continue;
6600
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006601 napi_complete(napi);
6602 /* Reenable interrupts. */
6603 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006604
6605 /* This test here is synchronized by napi_schedule()
6606 * and napi_complete() to close the race condition.
6607 */
6608 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6609 tw32(HOSTCC_MODE, tp->coalesce_mode |
6610 HOSTCC_MODE_ENABLE |
6611 tnapi->coal_now);
6612 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006613 mmiowb();
6614 break;
6615 }
6616 }
6617
6618 return work_done;
6619
6620tx_recovery:
6621 /* work_done is guaranteed to be less than budget. */
6622 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006623 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006624 return work_done;
6625}
6626
Matt Carlsone64de4e2011-04-13 11:05:05 +00006627static void tg3_process_error(struct tg3 *tp)
6628{
6629 u32 val;
6630 bool real_error = false;
6631
Joe Perches63c3a662011-04-26 08:12:10 +00006632 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006633 return;
6634
6635 /* Check Flow Attention register */
6636 val = tr32(HOSTCC_FLOW_ATTN);
6637 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6638 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6639 real_error = true;
6640 }
6641
6642 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6643 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6644 real_error = true;
6645 }
6646
6647 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6648 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6649 real_error = true;
6650 }
6651
6652 if (!real_error)
6653 return;
6654
6655 tg3_dump_state(tp);
6656
Joe Perches63c3a662011-04-26 08:12:10 +00006657 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006658 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006659}
6660
David S. Miller6f535762007-10-11 18:08:29 -07006661static int tg3_poll(struct napi_struct *napi, int budget)
6662{
Matt Carlson8ef04422009-08-28 14:01:37 +00006663 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6664 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006665 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006666 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006667
6668 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006669 if (sblk->status & SD_STATUS_ERROR)
6670 tg3_process_error(tp);
6671
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006672 tg3_poll_link(tp);
6673
Matt Carlson17375d22009-08-28 14:02:18 +00006674 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006675
Joe Perches63c3a662011-04-26 08:12:10 +00006676 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006677 goto tx_recovery;
6678
6679 if (unlikely(work_done >= budget))
6680 break;
6681
Joe Perches63c3a662011-04-26 08:12:10 +00006682 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006683 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006684 * to tell the hw how much work has been processed,
6685 * so we must read it before checking for more work.
6686 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006687 tnapi->last_tag = sblk->status_tag;
6688 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006689 rmb();
6690 } else
6691 sblk->status &= ~SD_STATUS_UPDATED;
6692
Matt Carlson17375d22009-08-28 14:02:18 +00006693 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006694 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006695 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006696 break;
6697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 }
6699
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006700 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006701
6702tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006703 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006704 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006705 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006706 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707}
6708
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006709static void tg3_napi_disable(struct tg3 *tp)
6710{
6711 int i;
6712
6713 for (i = tp->irq_cnt - 1; i >= 0; i--)
6714 napi_disable(&tp->napi[i].napi);
6715}
6716
6717static void tg3_napi_enable(struct tg3 *tp)
6718{
6719 int i;
6720
6721 for (i = 0; i < tp->irq_cnt; i++)
6722 napi_enable(&tp->napi[i].napi);
6723}
6724
6725static void tg3_napi_init(struct tg3 *tp)
6726{
6727 int i;
6728
6729 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6730 for (i = 1; i < tp->irq_cnt; i++)
6731 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6732}
6733
6734static void tg3_napi_fini(struct tg3 *tp)
6735{
6736 int i;
6737
6738 for (i = 0; i < tp->irq_cnt; i++)
6739 netif_napi_del(&tp->napi[i].napi);
6740}
6741
6742static inline void tg3_netif_stop(struct tg3 *tp)
6743{
6744 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6745 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006746 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006747 netif_tx_disable(tp->dev);
6748}
6749
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006750/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006751static inline void tg3_netif_start(struct tg3 *tp)
6752{
Matt Carlsonbe947302012-12-03 19:36:57 +00006753 tg3_ptp_resume(tp);
6754
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006755 /* NOTE: unconditional netif_tx_wake_all_queues is only
6756 * appropriate so long as all callers are assured to
6757 * have free tx slots (such as after tg3_init_hw)
6758 */
6759 netif_tx_wake_all_queues(tp->dev);
6760
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006761 if (tp->link_up)
6762 netif_carrier_on(tp->dev);
6763
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006764 tg3_napi_enable(tp);
6765 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6766 tg3_enable_ints(tp);
6767}
6768
David S. Millerf47c11e2005-06-24 20:18:35 -07006769static void tg3_irq_quiesce(struct tg3 *tp)
6770{
Matt Carlson4f125f42009-09-01 12:55:02 +00006771 int i;
6772
David S. Millerf47c11e2005-06-24 20:18:35 -07006773 BUG_ON(tp->irq_sync);
6774
6775 tp->irq_sync = 1;
6776 smp_mb();
6777
Matt Carlson4f125f42009-09-01 12:55:02 +00006778 for (i = 0; i < tp->irq_cnt; i++)
6779 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006780}
6781
David S. Millerf47c11e2005-06-24 20:18:35 -07006782/* Fully shutdown all tg3 driver activity elsewhere in the system.
6783 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6784 * with as well. Most of the time, this is not necessary except when
6785 * shutting down the device.
6786 */
6787static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6788{
Michael Chan46966542007-07-11 19:47:19 -07006789 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006790 if (irq_sync)
6791 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006792}
6793
6794static inline void tg3_full_unlock(struct tg3 *tp)
6795{
David S. Millerf47c11e2005-06-24 20:18:35 -07006796 spin_unlock_bh(&tp->lock);
6797}
6798
Michael Chanfcfa0a32006-03-20 22:28:41 -08006799/* One-shot MSI handler - Chip automatically disables interrupt
6800 * after sending MSI so driver doesn't have to do it.
6801 */
David Howells7d12e782006-10-05 14:55:46 +01006802static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006803{
Matt Carlson09943a12009-08-28 14:01:57 +00006804 struct tg3_napi *tnapi = dev_id;
6805 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006806
Matt Carlson898a56f2009-08-28 14:02:40 +00006807 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006808 if (tnapi->rx_rcb)
6809 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006810
6811 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006812 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006813
6814 return IRQ_HANDLED;
6815}
6816
Michael Chan88b06bc22005-04-21 17:13:25 -07006817/* MSI ISR - No need to check for interrupt sharing and no need to
6818 * flush status block and interrupt mailbox. PCI ordering rules
6819 * guarantee that MSI will arrive after the status block.
6820 */
David Howells7d12e782006-10-05 14:55:46 +01006821static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006822{
Matt Carlson09943a12009-08-28 14:01:57 +00006823 struct tg3_napi *tnapi = dev_id;
6824 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006825
Matt Carlson898a56f2009-08-28 14:02:40 +00006826 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006827 if (tnapi->rx_rcb)
6828 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006829 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006830 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006831 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006832 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006833 * NIC to stop sending us irqs, engaging "in-intr-handler"
6834 * event coalescing.
6835 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006836 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006837 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006838 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006839
Michael Chan88b06bc22005-04-21 17:13:25 -07006840 return IRQ_RETVAL(1);
6841}
6842
David Howells7d12e782006-10-05 14:55:46 +01006843static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844{
Matt Carlson09943a12009-08-28 14:01:57 +00006845 struct tg3_napi *tnapi = dev_id;
6846 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006847 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 unsigned int handled = 1;
6849
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 /* In INTx mode, it is possible for the interrupt to arrive at
6851 * the CPU before the status block posted prior to the interrupt.
6852 * Reading the PCI State register will confirm whether the
6853 * interrupt is ours and will flush the status block.
6854 */
Michael Chand18edcb2007-03-24 20:57:11 -07006855 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006856 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006857 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6858 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006859 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006860 }
Michael Chand18edcb2007-03-24 20:57:11 -07006861 }
6862
6863 /*
6864 * Writing any value to intr-mbox-0 clears PCI INTA# and
6865 * chip-internal interrupt pending events.
6866 * Writing non-zero to intr-mbox-0 additional tells the
6867 * NIC to stop sending us irqs, engaging "in-intr-handler"
6868 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006869 *
6870 * Flush the mailbox to de-assert the IRQ immediately to prevent
6871 * spurious interrupts. The flush impacts performance but
6872 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006873 */
Michael Chanc04cb342007-05-07 00:26:15 -07006874 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006875 if (tg3_irq_sync(tp))
6876 goto out;
6877 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006878 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006879 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006880 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006881 } else {
6882 /* No work, shared interrupt perhaps? re-enable
6883 * interrupts, and flush that PCI write
6884 */
6885 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6886 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006887 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006888out:
David S. Millerfac9b832005-05-18 22:46:34 -07006889 return IRQ_RETVAL(handled);
6890}
6891
David Howells7d12e782006-10-05 14:55:46 +01006892static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006893{
Matt Carlson09943a12009-08-28 14:01:57 +00006894 struct tg3_napi *tnapi = dev_id;
6895 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006896 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006897 unsigned int handled = 1;
6898
David S. Millerfac9b832005-05-18 22:46:34 -07006899 /* In INTx mode, it is possible for the interrupt to arrive at
6900 * the CPU before the status block posted prior to the interrupt.
6901 * Reading the PCI State register will confirm whether the
6902 * interrupt is ours and will flush the status block.
6903 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006904 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006905 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006906 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6907 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 }
Michael Chand18edcb2007-03-24 20:57:11 -07006910 }
6911
6912 /*
6913 * writing any value to intr-mbox-0 clears PCI INTA# and
6914 * chip-internal interrupt pending events.
6915 * writing non-zero to intr-mbox-0 additional tells the
6916 * NIC to stop sending us irqs, engaging "in-intr-handler"
6917 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006918 *
6919 * Flush the mailbox to de-assert the IRQ immediately to prevent
6920 * spurious interrupts. The flush impacts performance but
6921 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006922 */
Michael Chanc04cb342007-05-07 00:26:15 -07006923 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006924
6925 /*
6926 * In a shared interrupt configuration, sometimes other devices'
6927 * interrupts will scream. We record the current status tag here
6928 * so that the above check can report that the screaming interrupts
6929 * are unhandled. Eventually they will be silenced.
6930 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006931 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006932
Michael Chand18edcb2007-03-24 20:57:11 -07006933 if (tg3_irq_sync(tp))
6934 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006935
Matt Carlson72334482009-08-28 14:03:01 +00006936 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006937
Matt Carlson09943a12009-08-28 14:01:57 +00006938 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006939
David S. Millerf47c11e2005-06-24 20:18:35 -07006940out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 return IRQ_RETVAL(handled);
6942}
6943
Michael Chan79381092005-04-21 17:13:59 -07006944/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006945static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006946{
Matt Carlson09943a12009-08-28 14:01:57 +00006947 struct tg3_napi *tnapi = dev_id;
6948 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006949 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006950
Michael Chanf9804dd2005-09-27 12:13:10 -07006951 if ((sblk->status & SD_STATUS_UPDATED) ||
6952 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006953 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006954 return IRQ_RETVAL(1);
6955 }
6956 return IRQ_RETVAL(0);
6957}
6958
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959#ifdef CONFIG_NET_POLL_CONTROLLER
6960static void tg3_poll_controller(struct net_device *dev)
6961{
Matt Carlson4f125f42009-09-01 12:55:02 +00006962 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006963 struct tg3 *tp = netdev_priv(dev);
6964
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00006965 if (tg3_irq_sync(tp))
6966 return;
6967
Matt Carlson4f125f42009-09-01 12:55:02 +00006968 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006969 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971#endif
6972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973static void tg3_tx_timeout(struct net_device *dev)
6974{
6975 struct tg3 *tp = netdev_priv(dev);
6976
Michael Chanb0408752007-02-13 12:18:30 -08006977 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006978 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006979 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Matt Carlsondb219972011-11-04 09:15:03 +00006982 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983}
6984
Michael Chanc58ec932005-09-17 00:46:27 -07006985/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6986static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6987{
6988 u32 base = (u32) mapping & 0xffffffff;
6989
Eric Dumazet807540b2010-09-23 05:40:09 +00006990 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006991}
6992
Michael Chan72f2afb2006-03-06 19:28:35 -08006993/* Test for DMA addresses > 40-bit */
6994static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6995 int len)
6996{
6997#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006998 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006999 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08007000 return 0;
7001#else
7002 return 0;
7003#endif
7004}
7005
Matt Carlsond1a3b732011-07-27 14:20:51 +00007006static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007007 dma_addr_t mapping, u32 len, u32 flags,
7008 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00007009{
Matt Carlson92cd3a12011-07-27 14:20:47 +00007010 txbd->addr_hi = ((u64) mapping >> 32);
7011 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7012 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7013 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015
Matt Carlson84b67b22011-07-27 14:20:52 +00007016static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007017 dma_addr_t map, u32 len, u32 flags,
7018 u32 mss, u32 vlan)
7019{
7020 struct tg3 *tp = tnapi->tp;
7021 bool hwbug = false;
7022
7023 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007024 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007025
7026 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007027 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007028
7029 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007030 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007031
Matt Carlsona4cb4282011-12-14 11:09:58 +00007032 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007033 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007034 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007035 while (len > tp->dma_limit && *budget) {
7036 u32 frag_len = tp->dma_limit;
7037 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007038
Matt Carlsonb9e45482011-11-04 09:14:59 +00007039 /* Avoid the 8byte DMA problem */
7040 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007041 len += tp->dma_limit / 2;
7042 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007043 }
7044
Matt Carlsonb9e45482011-11-04 09:14:59 +00007045 tnapi->tx_buffers[*entry].fragmented = true;
7046
7047 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7048 frag_len, tmp_flag, mss, vlan);
7049 *budget -= 1;
7050 prvidx = *entry;
7051 *entry = NEXT_TX(*entry);
7052
Matt Carlsone31aa982011-07-27 14:20:53 +00007053 map += frag_len;
7054 }
7055
7056 if (len) {
7057 if (*budget) {
7058 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7059 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007060 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007061 *entry = NEXT_TX(*entry);
7062 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007063 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007064 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007065 }
7066 }
7067 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007068 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7069 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007070 *entry = NEXT_TX(*entry);
7071 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007072
7073 return hwbug;
7074}
7075
Matt Carlson0d681b22011-07-27 14:20:49 +00007076static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007077{
7078 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007079 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007080 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007081
Matt Carlson0d681b22011-07-27 14:20:49 +00007082 skb = txb->skb;
7083 txb->skb = NULL;
7084
Matt Carlson432aa7e2011-05-19 12:12:45 +00007085 pci_unmap_single(tnapi->tp->pdev,
7086 dma_unmap_addr(txb, mapping),
7087 skb_headlen(skb),
7088 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007089
7090 while (txb->fragmented) {
7091 txb->fragmented = false;
7092 entry = NEXT_TX(entry);
7093 txb = &tnapi->tx_buffers[entry];
7094 }
7095
Matt Carlsonba1142e2011-11-04 09:15:00 +00007096 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007097 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007098
7099 entry = NEXT_TX(entry);
7100 txb = &tnapi->tx_buffers[entry];
7101
7102 pci_unmap_page(tnapi->tp->pdev,
7103 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007104 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007105
7106 while (txb->fragmented) {
7107 txb->fragmented = false;
7108 entry = NEXT_TX(entry);
7109 txb = &tnapi->tx_buffers[entry];
7110 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007111 }
7112}
7113
Michael Chan72f2afb2006-03-06 19:28:35 -08007114/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007115static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007116 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007117 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007118 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007120 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007121 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007122 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007123 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
Matt Carlson41588ba12008-04-19 18:12:33 -07007125 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
7126 new_skb = skb_copy(skb, GFP_ATOMIC);
7127 else {
7128 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7129
7130 new_skb = skb_copy_expand(skb,
7131 skb_headroom(skb) + more_headroom,
7132 skb_tailroom(skb), GFP_ATOMIC);
7133 }
7134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007136 ret = -1;
7137 } else {
7138 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007139 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7140 PCI_DMA_TODEVICE);
7141 /* Make sure the mapping succeeded */
7142 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007143 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007144 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007145 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007146 u32 save_entry = *entry;
7147
Matt Carlson92cd3a12011-07-27 14:20:47 +00007148 base_flags |= TXD_FLAG_END;
7149
Matt Carlson84b67b22011-07-27 14:20:52 +00007150 tnapi->tx_buffers[*entry].skb = new_skb;
7151 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007152 mapping, new_addr);
7153
Matt Carlson84b67b22011-07-27 14:20:52 +00007154 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007155 new_skb->len, base_flags,
7156 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007157 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007158 dev_kfree_skb(new_skb);
7159 ret = -1;
7160 }
Michael Chanc58ec932005-09-17 00:46:27 -07007161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 }
7163
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007165 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167}
7168
Matt Carlson2ffcc982011-05-19 12:12:44 +00007169static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007170
7171/* Use GSO to workaround a rare TSO bug that may be triggered when the
7172 * TSO header is greater than 80 bytes.
7173 */
7174static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7175{
7176 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007177 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007178
7179 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007180 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007181 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007182
7183 /* netif_tx_stop_queue() must be done before checking
7184 * checking tx index in tg3_tx_avail() below, because in
7185 * tg3_tx(), we update tx index before checking for
7186 * netif_tx_queue_stopped().
7187 */
7188 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007189 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007190 return NETDEV_TX_BUSY;
7191
7192 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007193 }
7194
7195 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007196 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007197 goto tg3_tso_bug_end;
7198
7199 do {
7200 nskb = segs;
7201 segs = segs->next;
7202 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007203 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007204 } while (segs);
7205
7206tg3_tso_bug_end:
7207 dev_kfree_skb(skb);
7208
7209 return NETDEV_TX_OK;
7210}
Michael Chan52c0fd82006-06-29 20:15:54 -07007211
Michael Chan5a6f3072006-03-20 22:28:05 -08007212/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007213 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007214 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007215static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007216{
7217 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007218 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007219 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007220 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007221 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007222 struct tg3_napi *tnapi;
7223 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007224 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007225
Matt Carlson24f4efd2009-11-13 13:03:35 +00007226 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7227 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007228 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007229 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
Matt Carlson84b67b22011-07-27 14:20:52 +00007231 budget = tg3_tx_avail(tnapi);
7232
Michael Chan00b70502006-06-17 21:58:45 -07007233 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007234 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007235 * interrupt. Furthermore, IRQ processing runs lockless so we have
7236 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007238 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007239 if (!netif_tx_queue_stopped(txq)) {
7240 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007241
7242 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007243 netdev_err(dev,
7244 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 return NETDEV_TX_BUSY;
7247 }
7248
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007249 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007251 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007253
Matt Carlsonbe98da62010-07-11 09:31:46 +00007254 mss = skb_shinfo(skb)->gso_size;
7255 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007256 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007257 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
7259 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007260 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7261 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
Matt Carlson34195c32010-07-11 09:31:42 +00007263 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007264 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265
Eric Dumazeta5a11952012-01-23 01:22:09 +00007266 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007267
Eric Dumazeta5a11952012-01-23 01:22:09 +00007268 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007269 iph->check = 0;
7270 iph->tot_len = htons(mss + hdr_len);
7271 }
7272
Michael Chan52c0fd82006-06-29 20:15:54 -07007273 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007274 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007275 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007276
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7278 TXD_FLAG_CPU_POST_DMA);
7279
Joe Perches63c3a662011-04-26 08:12:10 +00007280 if (tg3_flag(tp, HW_TSO_1) ||
7281 tg3_flag(tp, HW_TSO_2) ||
7282 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007283 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007285 } else
7286 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7287 iph->daddr, 0,
7288 IPPROTO_TCP,
7289 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Joe Perches63c3a662011-04-26 08:12:10 +00007291 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007292 mss |= (hdr_len & 0xc) << 12;
7293 if (hdr_len & 0x10)
7294 base_flags |= 0x00000010;
7295 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007296 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007297 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007298 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007299 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007300 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 int tsflags;
7302
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007303 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 mss |= (tsflags << 11);
7305 }
7306 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007307 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 int tsflags;
7309
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007310 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311 base_flags |= tsflags << 12;
7312 }
7313 }
7314 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007315
Matt Carlson93a700a2011-08-31 11:44:54 +00007316 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7317 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7318 base_flags |= TXD_FLAG_JMB_PKT;
7319
Matt Carlson92cd3a12011-07-27 14:20:47 +00007320 if (vlan_tx_tag_present(skb)) {
7321 base_flags |= TXD_FLAG_VLAN;
7322 vlan = vlan_tx_tag_get(skb);
7323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007325 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7326 tg3_flag(tp, TX_TSTAMP_EN)) {
7327 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7328 base_flags |= TXD_FLAG_HWTSTAMP;
7329 }
7330
Alexander Duyckf4188d82009-12-02 16:48:38 +00007331 len = skb_headlen(skb);
7332
7333 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007334 if (pci_dma_mapping_error(tp->pdev, mapping))
7335 goto drop;
7336
David S. Miller90079ce2008-09-11 04:52:51 -07007337
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007338 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007339 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
7341 would_hit_hwbug = 0;
7342
Joe Perches63c3a662011-04-26 08:12:10 +00007343 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007344 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
Matt Carlson84b67b22011-07-27 14:20:52 +00007346 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007347 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007348 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007349 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007350 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007351 u32 tmp_mss = mss;
7352
7353 if (!tg3_flag(tp, HW_TSO_1) &&
7354 !tg3_flag(tp, HW_TSO_2) &&
7355 !tg3_flag(tp, HW_TSO_3))
7356 tmp_mss = 0;
7357
Matt Carlsonc5665a52012-02-13 10:20:12 +00007358 /* Now loop through additional data
7359 * fragments, and queue them.
7360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 last = skb_shinfo(skb)->nr_frags - 1;
7362 for (i = 0; i <= last; i++) {
7363 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7364
Eric Dumazet9e903e02011-10-18 21:00:24 +00007365 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007366 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007367 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007369 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007370 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007371 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007372 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007373 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Matt Carlsonb9e45482011-11-04 09:14:59 +00007375 if (!budget ||
7376 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007377 len, base_flags |
7378 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007379 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007380 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007381 break;
7382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 }
7384 }
7385
7386 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007387 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388
7389 /* If the workaround fails due to memory/mapping
7390 * failure, silently drop this packet.
7391 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007392 entry = tnapi->tx_prod;
7393 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007394 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007395 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007396 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 }
7398
Richard Cochrand515b452011-06-19 03:31:41 +00007399 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007400 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007401
Michael Chan6541b802012-03-04 14:48:14 +00007402 /* Sync BD data before updating mailbox */
7403 wmb();
7404
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007406 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007408 tnapi->tx_prod = entry;
7409 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007410 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007411
7412 /* netif_tx_stop_queue() must be done before checking
7413 * checking tx index in tg3_tx_avail() below, because in
7414 * tg3_tx(), we update tx index before checking for
7415 * netif_tx_queue_stopped().
7416 */
7417 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007418 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007419 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007422 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007424
7425dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007426 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007427 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007428drop:
7429 dev_kfree_skb(skb);
7430drop_nofree:
7431 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007432 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433}
7434
Matt Carlson6e01b202011-08-19 13:58:20 +00007435static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7436{
7437 if (enable) {
7438 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7439 MAC_MODE_PORT_MODE_MASK);
7440
7441 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7442
7443 if (!tg3_flag(tp, 5705_PLUS))
7444 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7445
7446 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7447 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7448 else
7449 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7450 } else {
7451 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7452
7453 if (tg3_flag(tp, 5705_PLUS) ||
7454 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7455 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7456 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7457 }
7458
7459 tw32(MAC_MODE, tp->mac_mode);
7460 udelay(40);
7461}
7462
Matt Carlson941ec902011-08-19 13:58:23 +00007463static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007464{
Matt Carlson941ec902011-08-19 13:58:23 +00007465 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007466
7467 tg3_phy_toggle_apd(tp, false);
7468 tg3_phy_toggle_automdix(tp, 0);
7469
Matt Carlson941ec902011-08-19 13:58:23 +00007470 if (extlpbk && tg3_phy_set_extloopbk(tp))
7471 return -EIO;
7472
7473 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007474 switch (speed) {
7475 case SPEED_10:
7476 break;
7477 case SPEED_100:
7478 bmcr |= BMCR_SPEED100;
7479 break;
7480 case SPEED_1000:
7481 default:
7482 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7483 speed = SPEED_100;
7484 bmcr |= BMCR_SPEED100;
7485 } else {
7486 speed = SPEED_1000;
7487 bmcr |= BMCR_SPEED1000;
7488 }
7489 }
7490
Matt Carlson941ec902011-08-19 13:58:23 +00007491 if (extlpbk) {
7492 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7493 tg3_readphy(tp, MII_CTRL1000, &val);
7494 val |= CTL1000_AS_MASTER |
7495 CTL1000_ENABLE_MASTER;
7496 tg3_writephy(tp, MII_CTRL1000, val);
7497 } else {
7498 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7499 MII_TG3_FET_PTEST_TRIM_2;
7500 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7501 }
7502 } else
7503 bmcr |= BMCR_LOOPBACK;
7504
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007505 tg3_writephy(tp, MII_BMCR, bmcr);
7506
7507 /* The write needs to be flushed for the FETs */
7508 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7509 tg3_readphy(tp, MII_BMCR, &bmcr);
7510
7511 udelay(40);
7512
7513 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7514 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007515 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007516 MII_TG3_FET_PTEST_FRC_TX_LINK |
7517 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7518
7519 /* The write needs to be flushed for the AC131 */
7520 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7521 }
7522
7523 /* Reset to prevent losing 1st rx packet intermittently */
7524 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7525 tg3_flag(tp, 5780_CLASS)) {
7526 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7527 udelay(10);
7528 tw32_f(MAC_RX_MODE, tp->rx_mode);
7529 }
7530
7531 mac_mode = tp->mac_mode &
7532 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7533 if (speed == SPEED_1000)
7534 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7535 else
7536 mac_mode |= MAC_MODE_PORT_MODE_MII;
7537
7538 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7539 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7540
7541 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7542 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7543 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7544 mac_mode |= MAC_MODE_LINK_POLARITY;
7545
7546 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7547 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7548 }
7549
7550 tw32(MAC_MODE, mac_mode);
7551 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007552
7553 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007554}
7555
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007556static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007557{
7558 struct tg3 *tp = netdev_priv(dev);
7559
7560 if (features & NETIF_F_LOOPBACK) {
7561 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7562 return;
7563
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007564 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007565 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007566 netif_carrier_on(tp->dev);
7567 spin_unlock_bh(&tp->lock);
7568 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7569 } else {
7570 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7571 return;
7572
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007573 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007574 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007575 /* Force link status check */
7576 tg3_setup_phy(tp, 1);
7577 spin_unlock_bh(&tp->lock);
7578 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7579 }
7580}
7581
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007582static netdev_features_t tg3_fix_features(struct net_device *dev,
7583 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007584{
7585 struct tg3 *tp = netdev_priv(dev);
7586
Joe Perches63c3a662011-04-26 08:12:10 +00007587 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007588 features &= ~NETIF_F_ALL_TSO;
7589
7590 return features;
7591}
7592
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007593static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007594{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007595 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007596
7597 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7598 tg3_set_loopback(dev, features);
7599
7600 return 0;
7601}
7602
Matt Carlson21f581a2009-08-28 14:00:25 +00007603static void tg3_rx_prodring_free(struct tg3 *tp,
7604 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606 int i;
7607
Matt Carlson8fea32b2010-09-15 08:59:58 +00007608 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007609 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007610 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007611 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007612 tp->rx_pkt_map_sz);
7613
Joe Perches63c3a662011-04-26 08:12:10 +00007614 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007615 for (i = tpr->rx_jmb_cons_idx;
7616 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007617 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007618 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007619 TG3_RX_JMB_MAP_SZ);
7620 }
7621 }
7622
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007623 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
Matt Carlson2c49a442010-09-30 10:34:35 +00007626 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007627 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007628 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
Joe Perches63c3a662011-04-26 08:12:10 +00007630 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007631 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007632 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007633 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 }
7635}
7636
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007637/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007638 *
7639 * The chip has been shut down and the driver detached from
7640 * the networking, so no interrupts or new tx packets will
7641 * end up in the driver. tp->{tx,}lock are held and thus
7642 * we may not sleep.
7643 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007644static int tg3_rx_prodring_alloc(struct tg3 *tp,
7645 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646{
Matt Carlson287be122009-08-28 13:58:46 +00007647 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007649 tpr->rx_std_cons_idx = 0;
7650 tpr->rx_std_prod_idx = 0;
7651 tpr->rx_jmb_cons_idx = 0;
7652 tpr->rx_jmb_prod_idx = 0;
7653
Matt Carlson8fea32b2010-09-15 08:59:58 +00007654 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007655 memset(&tpr->rx_std_buffers[0], 0,
7656 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007657 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007658 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007659 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007660 goto done;
7661 }
7662
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007664 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665
Matt Carlson287be122009-08-28 13:58:46 +00007666 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007667 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007668 tp->dev->mtu > ETH_DATA_LEN)
7669 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7670 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07007671
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 /* Initialize invariants of the rings, we only set this
7673 * stuff once. This works because the card does not
7674 * write into the rx buffer posting rings.
7675 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007676 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 struct tg3_rx_buffer_desc *rxd;
7678
Matt Carlson21f581a2009-08-28 14:00:25 +00007679 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007680 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7682 rxd->opaque = (RXD_OPAQUE_RING_STD |
7683 (i << RXD_OPAQUE_INDEX_SHIFT));
7684 }
7685
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007686 /* Now allocate fresh SKBs for each rx ring. */
7687 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007688 unsigned int frag_size;
7689
7690 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7691 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007692 netdev_warn(tp->dev,
7693 "Using a smaller RX standard ring. Only "
7694 "%d out of %d buffers were allocated "
7695 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007696 if (i == 0)
7697 goto initfail;
7698 tp->rx_pending = i;
7699 break;
7700 }
7701 }
7702
Joe Perches63c3a662011-04-26 08:12:10 +00007703 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007704 goto done;
7705
Matt Carlson2c49a442010-09-30 10:34:35 +00007706 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007707
Joe Perches63c3a662011-04-26 08:12:10 +00007708 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007709 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710
Matt Carlson2c49a442010-09-30 10:34:35 +00007711 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007712 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713
Matt Carlson0d86df82010-02-17 15:17:00 +00007714 rxd = &tpr->rx_jmb[i].std;
7715 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7716 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7717 RXD_FLAG_JUMBO;
7718 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7719 (i << RXD_OPAQUE_INDEX_SHIFT));
7720 }
7721
7722 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007723 unsigned int frag_size;
7724
7725 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7726 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007727 netdev_warn(tp->dev,
7728 "Using a smaller RX jumbo ring. Only %d "
7729 "out of %d buffers were allocated "
7730 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007731 if (i == 0)
7732 goto initfail;
7733 tp->rx_jumbo_pending = i;
7734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007735 }
7736 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007737
7738done:
Michael Chan32d8c572006-07-25 16:38:29 -07007739 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007740
7741initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007742 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007743 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744}
7745
Matt Carlson21f581a2009-08-28 14:00:25 +00007746static void tg3_rx_prodring_fini(struct tg3 *tp,
7747 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748{
Matt Carlson21f581a2009-08-28 14:00:25 +00007749 kfree(tpr->rx_std_buffers);
7750 tpr->rx_std_buffers = NULL;
7751 kfree(tpr->rx_jmb_buffers);
7752 tpr->rx_jmb_buffers = NULL;
7753 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007754 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7755 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007756 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007758 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007759 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7760 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007761 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007762 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007763}
7764
Matt Carlson21f581a2009-08-28 14:00:25 +00007765static int tg3_rx_prodring_init(struct tg3 *tp,
7766 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007767{
Matt Carlson2c49a442010-09-30 10:34:35 +00007768 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7769 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007770 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007771 return -ENOMEM;
7772
Matt Carlson4bae65c2010-11-24 08:31:52 +00007773 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7774 TG3_RX_STD_RING_BYTES(tp),
7775 &tpr->rx_std_mapping,
7776 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007777 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007778 goto err_out;
7779
Joe Perches63c3a662011-04-26 08:12:10 +00007780 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007781 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007782 GFP_KERNEL);
7783 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007784 goto err_out;
7785
Matt Carlson4bae65c2010-11-24 08:31:52 +00007786 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7787 TG3_RX_JMB_RING_BYTES(tp),
7788 &tpr->rx_jmb_mapping,
7789 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007790 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007791 goto err_out;
7792 }
7793
7794 return 0;
7795
7796err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007797 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007798 return -ENOMEM;
7799}
7800
7801/* Free up pending packets in all rx/tx rings.
7802 *
7803 * The chip has been shut down and the driver detached from
7804 * the networking, so no interrupts or new tx packets will
7805 * end up in the driver. tp->{tx,}lock is not held and we are not
7806 * in an interrupt context and thus may sleep.
7807 */
7808static void tg3_free_rings(struct tg3 *tp)
7809{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007810 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007811
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007812 for (j = 0; j < tp->irq_cnt; j++) {
7813 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007814
Matt Carlson8fea32b2010-09-15 08:59:58 +00007815 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007816
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007817 if (!tnapi->tx_buffers)
7818 continue;
7819
Matt Carlson0d681b22011-07-27 14:20:49 +00007820 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7821 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007822
Matt Carlson0d681b22011-07-27 14:20:49 +00007823 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007824 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007825
Matt Carlsonba1142e2011-11-04 09:15:00 +00007826 tg3_tx_skb_unmap(tnapi, i,
7827 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007828
7829 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007830 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007831 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007832 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007833}
7834
7835/* Initialize tx/rx rings for packet processing.
7836 *
7837 * The chip has been shut down and the driver detached from
7838 * the networking, so no interrupts or new tx packets will
7839 * end up in the driver. tp->{tx,}lock are held and thus
7840 * we may not sleep.
7841 */
7842static int tg3_init_rings(struct tg3 *tp)
7843{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007844 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007845
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007846 /* Free up all the SKBs. */
7847 tg3_free_rings(tp);
7848
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007849 for (i = 0; i < tp->irq_cnt; i++) {
7850 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007851
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007852 tnapi->last_tag = 0;
7853 tnapi->last_irq_tag = 0;
7854 tnapi->hw_status->status = 0;
7855 tnapi->hw_status->status_tag = 0;
7856 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7857
7858 tnapi->tx_prod = 0;
7859 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007860 if (tnapi->tx_ring)
7861 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007862
7863 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007864 if (tnapi->rx_rcb)
7865 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007866
Matt Carlson8fea32b2010-09-15 08:59:58 +00007867 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007868 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007869 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007870 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007871 }
Matt Carlson72334482009-08-28 14:03:01 +00007872
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007873 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007874}
7875
Michael Chan49a359e2012-09-28 07:12:37 +00007876static void tg3_mem_tx_release(struct tg3 *tp)
7877{
7878 int i;
7879
7880 for (i = 0; i < tp->irq_max; i++) {
7881 struct tg3_napi *tnapi = &tp->napi[i];
7882
7883 if (tnapi->tx_ring) {
7884 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7885 tnapi->tx_ring, tnapi->tx_desc_mapping);
7886 tnapi->tx_ring = NULL;
7887 }
7888
7889 kfree(tnapi->tx_buffers);
7890 tnapi->tx_buffers = NULL;
7891 }
7892}
7893
7894static int tg3_mem_tx_acquire(struct tg3 *tp)
7895{
7896 int i;
7897 struct tg3_napi *tnapi = &tp->napi[0];
7898
7899 /* If multivector TSS is enabled, vector 0 does not handle
7900 * tx interrupts. Don't allocate any resources for it.
7901 */
7902 if (tg3_flag(tp, ENABLE_TSS))
7903 tnapi++;
7904
7905 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7906 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7907 TG3_TX_RING_SIZE, GFP_KERNEL);
7908 if (!tnapi->tx_buffers)
7909 goto err_out;
7910
7911 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7912 TG3_TX_RING_BYTES,
7913 &tnapi->tx_desc_mapping,
7914 GFP_KERNEL);
7915 if (!tnapi->tx_ring)
7916 goto err_out;
7917 }
7918
7919 return 0;
7920
7921err_out:
7922 tg3_mem_tx_release(tp);
7923 return -ENOMEM;
7924}
7925
7926static void tg3_mem_rx_release(struct tg3 *tp)
7927{
7928 int i;
7929
7930 for (i = 0; i < tp->irq_max; i++) {
7931 struct tg3_napi *tnapi = &tp->napi[i];
7932
7933 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7934
7935 if (!tnapi->rx_rcb)
7936 continue;
7937
7938 dma_free_coherent(&tp->pdev->dev,
7939 TG3_RX_RCB_RING_BYTES(tp),
7940 tnapi->rx_rcb,
7941 tnapi->rx_rcb_mapping);
7942 tnapi->rx_rcb = NULL;
7943 }
7944}
7945
7946static int tg3_mem_rx_acquire(struct tg3 *tp)
7947{
7948 unsigned int i, limit;
7949
7950 limit = tp->rxq_cnt;
7951
7952 /* If RSS is enabled, we need a (dummy) producer ring
7953 * set on vector zero. This is the true hw prodring.
7954 */
7955 if (tg3_flag(tp, ENABLE_RSS))
7956 limit++;
7957
7958 for (i = 0; i < limit; i++) {
7959 struct tg3_napi *tnapi = &tp->napi[i];
7960
7961 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7962 goto err_out;
7963
7964 /* If multivector RSS is enabled, vector 0
7965 * does not handle rx or tx interrupts.
7966 * Don't allocate any resources for it.
7967 */
7968 if (!i && tg3_flag(tp, ENABLE_RSS))
7969 continue;
7970
7971 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7972 TG3_RX_RCB_RING_BYTES(tp),
7973 &tnapi->rx_rcb_mapping,
7974 GFP_KERNEL);
7975 if (!tnapi->rx_rcb)
7976 goto err_out;
7977
7978 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
7979 }
7980
7981 return 0;
7982
7983err_out:
7984 tg3_mem_rx_release(tp);
7985 return -ENOMEM;
7986}
7987
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007988/*
7989 * Must not be invoked with interrupt sources disabled and
7990 * the hardware shutdown down.
7991 */
7992static void tg3_free_consistent(struct tg3 *tp)
7993{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007994 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007995
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007996 for (i = 0; i < tp->irq_cnt; i++) {
7997 struct tg3_napi *tnapi = &tp->napi[i];
7998
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007999 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008000 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
8001 tnapi->hw_status,
8002 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008003 tnapi->hw_status = NULL;
8004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008006
Michael Chan49a359e2012-09-28 07:12:37 +00008007 tg3_mem_rx_release(tp);
8008 tg3_mem_tx_release(tp);
8009
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00008011 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8012 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013 tp->hw_stats = NULL;
8014 }
8015}
8016
8017/*
8018 * Must not be invoked with interrupt sources disabled and
8019 * the hardware shutdown down. Can sleep.
8020 */
8021static int tg3_alloc_consistent(struct tg3 *tp)
8022{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008023 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008024
Matt Carlson4bae65c2010-11-24 08:31:52 +00008025 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8026 sizeof(struct tg3_hw_stats),
8027 &tp->stats_mapping,
8028 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 if (!tp->hw_stats)
8030 goto err_out;
8031
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8033
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008034 for (i = 0; i < tp->irq_cnt; i++) {
8035 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008036 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008037
Matt Carlson4bae65c2010-11-24 08:31:52 +00008038 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8039 TG3_HW_STATUS_SIZE,
8040 &tnapi->status_mapping,
8041 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008042 if (!tnapi->hw_status)
8043 goto err_out;
8044
8045 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008046 sblk = tnapi->hw_status;
8047
Michael Chan49a359e2012-09-28 07:12:37 +00008048 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008049 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008050
Michael Chan49a359e2012-09-28 07:12:37 +00008051 /*
8052 * When RSS is enabled, the status block format changes
8053 * slightly. The "rx_jumbo_consumer", "reserved",
8054 * and "rx_mini_consumer" members get mapped to the
8055 * other three rx return ring producer indexes.
8056 */
8057 switch (i) {
8058 case 1:
8059 prodptr = &sblk->idx[0].rx_producer;
8060 break;
8061 case 2:
8062 prodptr = &sblk->rx_jumbo_consumer;
8063 break;
8064 case 3:
8065 prodptr = &sblk->reserved;
8066 break;
8067 case 4:
8068 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008069 break;
8070 }
Michael Chan49a359e2012-09-28 07:12:37 +00008071 tnapi->rx_rcb_prod_idx = prodptr;
8072 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008073 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008074 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008075 }
8076
Michael Chan49a359e2012-09-28 07:12:37 +00008077 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8078 goto err_out;
8079
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 return 0;
8081
8082err_out:
8083 tg3_free_consistent(tp);
8084 return -ENOMEM;
8085}
8086
8087#define MAX_WAIT_CNT 1000
8088
8089/* To stop a block, clear the enable bit and poll till it
8090 * clears. tp->lock is held.
8091 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008092static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093{
8094 unsigned int i;
8095 u32 val;
8096
Joe Perches63c3a662011-04-26 08:12:10 +00008097 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098 switch (ofs) {
8099 case RCVLSC_MODE:
8100 case DMAC_MODE:
8101 case MBFREE_MODE:
8102 case BUFMGR_MODE:
8103 case MEMARB_MODE:
8104 /* We can't enable/disable these bits of the
8105 * 5705/5750, just say success.
8106 */
8107 return 0;
8108
8109 default:
8110 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 }
8113
8114 val = tr32(ofs);
8115 val &= ~enable_bit;
8116 tw32_f(ofs, val);
8117
8118 for (i = 0; i < MAX_WAIT_CNT; i++) {
8119 udelay(100);
8120 val = tr32(ofs);
8121 if ((val & enable_bit) == 0)
8122 break;
8123 }
8124
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008125 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008126 dev_err(&tp->pdev->dev,
8127 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8128 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 return -ENODEV;
8130 }
8131
8132 return 0;
8133}
8134
8135/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008136static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137{
8138 int i, err;
8139
8140 tg3_disable_ints(tp);
8141
8142 tp->rx_mode &= ~RX_MODE_ENABLE;
8143 tw32_f(MAC_RX_MODE, tp->rx_mode);
8144 udelay(10);
8145
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008146 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8147 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8148 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8149 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8150 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8151 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008153 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8154 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8155 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8156 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8157 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8158 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8159 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160
8161 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8162 tw32_f(MAC_MODE, tp->mac_mode);
8163 udelay(40);
8164
8165 tp->tx_mode &= ~TX_MODE_ENABLE;
8166 tw32_f(MAC_TX_MODE, tp->tx_mode);
8167
8168 for (i = 0; i < MAX_WAIT_CNT; i++) {
8169 udelay(100);
8170 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8171 break;
8172 }
8173 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008174 dev_err(&tp->pdev->dev,
8175 "%s timed out, TX_MODE_ENABLE will not clear "
8176 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008177 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 }
8179
Michael Chane6de8ad2005-05-05 14:42:41 -07008180 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008181 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8182 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183
8184 tw32(FTQ_RESET, 0xffffffff);
8185 tw32(FTQ_RESET, 0x00000000);
8186
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008187 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8188 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008190 for (i = 0; i < tp->irq_cnt; i++) {
8191 struct tg3_napi *tnapi = &tp->napi[i];
8192 if (tnapi->hw_status)
8193 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 return err;
8197}
8198
Michael Chanee6a99b2007-07-18 21:49:10 -07008199/* Save PCI command register before chip reset */
8200static void tg3_save_pci_state(struct tg3 *tp)
8201{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008202 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008203}
8204
8205/* Restore PCI state after chip reset */
8206static void tg3_restore_pci_state(struct tg3 *tp)
8207{
8208 u32 val;
8209
8210 /* Re-enable indirect register accesses. */
8211 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8212 tp->misc_host_ctrl);
8213
8214 /* Set MAX PCI retry to zero. */
8215 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
8216 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008217 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008218 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008219 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008221 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008222 PCISTATE_ALLOW_APE_SHMEM_WR |
8223 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008224 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8225
Matt Carlson8a6eac92007-10-21 16:17:55 -07008226 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008227
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008228 if (!tg3_flag(tp, PCI_EXPRESS)) {
8229 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8230 tp->pci_cacheline_sz);
8231 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8232 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008233 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008234
Michael Chanee6a99b2007-07-18 21:49:10 -07008235 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008237 u16 pcix_cmd;
8238
8239 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8240 &pcix_cmd);
8241 pcix_cmd &= ~PCI_X_CMD_ERO;
8242 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8243 pcix_cmd);
8244 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008245
Joe Perches63c3a662011-04-26 08:12:10 +00008246 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008247
8248 /* Chip reset on 5780 will reset MSI enable bit,
8249 * so need to restore it.
8250 */
Joe Perches63c3a662011-04-26 08:12:10 +00008251 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008252 u16 ctrl;
8253
8254 pci_read_config_word(tp->pdev,
8255 tp->msi_cap + PCI_MSI_FLAGS,
8256 &ctrl);
8257 pci_write_config_word(tp->pdev,
8258 tp->msi_cap + PCI_MSI_FLAGS,
8259 ctrl | PCI_MSI_FLAGS_ENABLE);
8260 val = tr32(MSGINT_MODE);
8261 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8262 }
8263 }
8264}
8265
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266/* tp->lock is held. */
8267static int tg3_chip_reset(struct tg3 *tp)
8268{
8269 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008270 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008271 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008272
David S. Millerf49639e2006-06-09 11:58:36 -07008273 tg3_nvram_lock(tp);
8274
Matt Carlson77b483f2008-08-15 14:07:24 -07008275 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8276
David S. Millerf49639e2006-06-09 11:58:36 -07008277 /* No matching tg3_nvram_unlock() after this because
8278 * chip reset below will undo the nvram lock.
8279 */
8280 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008281
Michael Chanee6a99b2007-07-18 21:49:10 -07008282 /* GRC_MISC_CFG core clock reset will clear the memory
8283 * enable bit in PCI register 4 and the MSI enable bit
8284 * on some chips, so we save relevant registers here.
8285 */
8286 tg3_save_pci_state(tp);
8287
Michael Chand9ab5ad12006-03-20 22:27:35 -08008288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008289 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008290 tw32(GRC_FASTBOOT_PC, 0);
8291
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 /*
8293 * We must avoid the readl() that normally takes place.
8294 * It locks machines, causes machine checks, and other
8295 * fun things. So, temporarily disable the 5701
8296 * hardware workaround, while we do the reset.
8297 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008298 write_op = tp->write32;
8299 if (write_op == tg3_write_flush_reg32)
8300 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301
Michael Chand18edcb2007-03-24 20:57:11 -07008302 /* Prevent the irq handler from reading or writing PCI registers
8303 * during chip reset when the memory enable bit in the PCI command
8304 * register may be cleared. The chip does not generate interrupt
8305 * at this time, but the irq handler may still be called due to irq
8306 * sharing or irqpoll.
8307 */
Joe Perches63c3a662011-04-26 08:12:10 +00008308 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008309 for (i = 0; i < tp->irq_cnt; i++) {
8310 struct tg3_napi *tnapi = &tp->napi[i];
8311 if (tnapi->hw_status) {
8312 tnapi->hw_status->status = 0;
8313 tnapi->hw_status->status_tag = 0;
8314 }
8315 tnapi->last_tag = 0;
8316 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008317 }
Michael Chand18edcb2007-03-24 20:57:11 -07008318 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008319
8320 for (i = 0; i < tp->irq_cnt; i++)
8321 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008322
Matt Carlson255ca312009-08-25 10:07:27 +00008323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8324 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8325 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8326 }
8327
Linus Torvalds1da177e2005-04-16 15:20:36 -07008328 /* do the reset */
8329 val = GRC_MISC_CFG_CORECLK_RESET;
8330
Joe Perches63c3a662011-04-26 08:12:10 +00008331 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008332 /* Force PCIe 1.0a mode */
8333 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008334 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008335 tr32(TG3_PCIE_PHY_TSTCTL) ==
8336 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8337 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8338
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
8340 tw32(GRC_MISC_CFG, (1 << 29));
8341 val |= (1 << 29);
8342 }
8343 }
8344
Michael Chanb5d37722006-09-27 16:06:21 -07008345 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8346 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8347 tw32(GRC_VCPU_EXT_CTRL,
8348 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8349 }
8350
Matt Carlsonf37500d2010-08-02 11:25:59 +00008351 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008352 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008354
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 tw32(GRC_MISC_CFG, val);
8356
Michael Chan1ee582d2005-08-09 20:16:46 -07008357 /* restore 5701 hardware bug workaround write method */
8358 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359
8360 /* Unfortunately, we have to delay before the PCI read back.
8361 * Some 575X chips even will not respond to a PCI cfg access
8362 * when the reset command is given to the chip.
8363 *
8364 * How do these hardware designers expect things to work
8365 * properly if the PCI write is posted for a long period
8366 * of time? It is always necessary to have some method by
8367 * which a register read back can occur to push the write
8368 * out which does the reset.
8369 *
8370 * For most tg3 variants the trick below was working.
8371 * Ho hum...
8372 */
8373 udelay(120);
8374
8375 /* Flush PCI posted writes. The normal MMIO registers
8376 * are inaccessible at this time so this is the only
8377 * way to make this reliably (actually, this is no longer
8378 * the case, see above). I tried to use indirect
8379 * register read/write but this upset some 5701 variants.
8380 */
8381 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8382
8383 udelay(120);
8384
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008385 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008386 u16 val16;
8387
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008389 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 u32 cfg_val;
8391
8392 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008393 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 udelay(100);
8395
8396 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8397 pci_write_config_dword(tp->pdev, 0xc4,
8398 cfg_val | (1 << 15));
8399 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008400
Matt Carlsone7126992009-08-25 10:08:16 +00008401 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008402 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008403 /*
8404 * Older PCIe devices only support the 128 byte
8405 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008406 */
Joe Perches63c3a662011-04-26 08:12:10 +00008407 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008408 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8409 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008410
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008411 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008412 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008413 PCI_EXP_DEVSTA_CED |
8414 PCI_EXP_DEVSTA_NFED |
8415 PCI_EXP_DEVSTA_FED |
8416 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417 }
8418
Michael Chanee6a99b2007-07-18 21:49:10 -07008419 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420
Joe Perches63c3a662011-04-26 08:12:10 +00008421 tg3_flag_clear(tp, CHIP_RESETTING);
8422 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008423
Michael Chanee6a99b2007-07-18 21:49:10 -07008424 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008425 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008426 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008427 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008428
8429 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8430 tg3_stop_fw(tp);
8431 tw32(0x5000, 0x400);
8432 }
8433
8434 tw32(GRC_MODE, tp->grc_mode);
8435
8436 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008437 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438
8439 tw32(0xc4, val | (1 << 15));
8440 }
8441
8442 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8443 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8444 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8445 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8446 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8447 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8448 }
8449
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008450 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008451 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008452 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008453 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008454 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008455 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008457 val = 0;
8458
8459 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 udelay(40);
8461
Matt Carlson77b483f2008-08-15 14:07:24 -07008462 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8463
Michael Chan7a6f4362006-09-27 16:03:31 -07008464 err = tg3_poll_fw(tp);
8465 if (err)
8466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467
Matt Carlson0a9140c2009-08-28 12:27:50 +00008468 tg3_mdio_start(tp);
8469
Joe Perches63c3a662011-04-26 08:12:10 +00008470 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008471 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8472 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008473 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008474 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475
8476 tw32(0x7c00, val | (1 << 25));
8477 }
8478
Matt Carlsond78b59f2011-04-05 14:22:46 +00008479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8480 val = tr32(TG3_CPMU_CLCK_ORIDE);
8481 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8482 }
8483
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008485 tg3_flag_clear(tp, ENABLE_ASF);
8486 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8488 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8489 u32 nic_cfg;
8490
8491 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8492 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008493 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008494 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008495 if (tg3_flag(tp, 5750_PLUS))
8496 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497 }
8498 }
8499
8500 return 0;
8501}
8502
Matt Carlson65ec6982012-02-28 23:33:37 +00008503static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8504static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008505
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008507static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508{
8509 int err;
8510
8511 tg3_stop_fw(tp);
8512
Michael Chan944d9802005-05-29 14:57:48 -07008513 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008515 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 err = tg3_chip_reset(tp);
8517
Matt Carlsondaba2a62009-04-20 06:58:52 +00008518 __tg3_set_mac_addr(tp, 0);
8519
Michael Chan944d9802005-05-29 14:57:48 -07008520 tg3_write_sig_legacy(tp, kind);
8521 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522
Matt Carlson92feeab2011-12-08 14:40:14 +00008523 if (tp->hw_stats) {
8524 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008525 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008526 tg3_get_estats(tp, &tp->estats_prev);
8527
8528 /* And make sure the next sample is new data */
8529 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8530 }
8531
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 if (err)
8533 return err;
8534
8535 return 0;
8536}
8537
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538static int tg3_set_mac_addr(struct net_device *dev, void *p)
8539{
8540 struct tg3 *tp = netdev_priv(dev);
8541 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008542 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543
Michael Chanf9804dd2005-09-27 12:13:10 -07008544 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008545 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008546
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8548
Michael Chane75f7c92006-03-20 21:33:26 -08008549 if (!netif_running(dev))
8550 return 0;
8551
Joe Perches63c3a662011-04-26 08:12:10 +00008552 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008553 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008554
Michael Chan986e0ae2007-05-05 12:10:20 -07008555 addr0_high = tr32(MAC_ADDR_0_HIGH);
8556 addr0_low = tr32(MAC_ADDR_0_LOW);
8557 addr1_high = tr32(MAC_ADDR_1_HIGH);
8558 addr1_low = tr32(MAC_ADDR_1_LOW);
8559
8560 /* Skip MAC addr 1 if ASF is using it. */
8561 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8562 !(addr1_high == 0 && addr1_low == 0))
8563 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008564 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008565 spin_lock_bh(&tp->lock);
8566 __tg3_set_mac_addr(tp, skip_mac_1);
8567 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568
Michael Chanb9ec6c12006-07-25 16:37:27 -07008569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570}
8571
8572/* tp->lock is held. */
8573static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8574 dma_addr_t mapping, u32 maxlen_flags,
8575 u32 nic_addr)
8576{
8577 tg3_write_mem(tp,
8578 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8579 ((u64) mapping >> 32));
8580 tg3_write_mem(tp,
8581 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8582 ((u64) mapping & 0xffffffff));
8583 tg3_write_mem(tp,
8584 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8585 maxlen_flags);
8586
Joe Perches63c3a662011-04-26 08:12:10 +00008587 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588 tg3_write_mem(tp,
8589 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8590 nic_addr);
8591}
8592
Michael Chana489b6d2012-09-28 07:12:39 +00008593
8594static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008595{
Michael Chana489b6d2012-09-28 07:12:39 +00008596 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008597
Joe Perches63c3a662011-04-26 08:12:10 +00008598 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008599 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8600 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8601 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008602 } else {
8603 tw32(HOSTCC_TXCOL_TICKS, 0);
8604 tw32(HOSTCC_TXMAX_FRAMES, 0);
8605 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008606
8607 for (; i < tp->txq_cnt; i++) {
8608 u32 reg;
8609
8610 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8611 tw32(reg, ec->tx_coalesce_usecs);
8612 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8613 tw32(reg, ec->tx_max_coalesced_frames);
8614 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8615 tw32(reg, ec->tx_max_coalesced_frames_irq);
8616 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008617 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008618
Michael Chana489b6d2012-09-28 07:12:39 +00008619 for (; i < tp->irq_max - 1; i++) {
8620 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8621 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8622 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8623 }
8624}
8625
8626static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8627{
8628 int i = 0;
8629 u32 limit = tp->rxq_cnt;
8630
Joe Perches63c3a662011-04-26 08:12:10 +00008631 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008632 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8633 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8634 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008635 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008636 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008637 tw32(HOSTCC_RXCOL_TICKS, 0);
8638 tw32(HOSTCC_RXMAX_FRAMES, 0);
8639 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008640 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008641
Michael Chana489b6d2012-09-28 07:12:39 +00008642 for (; i < limit; i++) {
8643 u32 reg;
8644
8645 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8646 tw32(reg, ec->rx_coalesce_usecs);
8647 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8648 tw32(reg, ec->rx_max_coalesced_frames);
8649 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8650 tw32(reg, ec->rx_max_coalesced_frames_irq);
8651 }
8652
8653 for (; i < tp->irq_max - 1; i++) {
8654 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8655 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8656 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8657 }
8658}
8659
8660static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8661{
8662 tg3_coal_tx_init(tp, ec);
8663 tg3_coal_rx_init(tp, ec);
8664
Joe Perches63c3a662011-04-26 08:12:10 +00008665 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008666 u32 val = ec->stats_block_coalesce_usecs;
8667
Matt Carlsonb6080e12009-09-01 13:12:00 +00008668 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8669 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8670
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008671 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008672 val = 0;
8673
8674 tw32(HOSTCC_STAT_COAL_TICKS, val);
8675 }
8676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008677
8678/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008679static void tg3_rings_reset(struct tg3 *tp)
8680{
8681 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008682 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008683 struct tg3_napi *tnapi = &tp->napi[0];
8684
8685 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008686 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008687 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008688 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008689 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008690 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008691 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008692 else
8693 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8694
8695 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8696 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8697 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8698 BDINFO_FLAGS_DISABLED);
8699
8700
8701 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008702 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008703 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008704 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008705 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008706 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008707 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008708 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8709 else
8710 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8711
8712 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8713 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8714 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8715 BDINFO_FLAGS_DISABLED);
8716
8717 /* Disable interrupts */
8718 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008719 tp->napi[0].chk_msi_cnt = 0;
8720 tp->napi[0].last_rx_cons = 0;
8721 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008722
8723 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008724 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008725 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008726 tp->napi[i].tx_prod = 0;
8727 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008728 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008729 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008730 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8731 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008732 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008733 tp->napi[i].last_rx_cons = 0;
8734 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008735 }
Joe Perches63c3a662011-04-26 08:12:10 +00008736 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008737 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008738 } else {
8739 tp->napi[0].tx_prod = 0;
8740 tp->napi[0].tx_cons = 0;
8741 tw32_mailbox(tp->napi[0].prodmbox, 0);
8742 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8743 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008744
8745 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008746 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008747 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8748 for (i = 0; i < 16; i++)
8749 tw32_tx_mbox(mbox + i * 8, 0);
8750 }
8751
8752 txrcb = NIC_SRAM_SEND_RCB;
8753 rxrcb = NIC_SRAM_RCV_RET_RCB;
8754
8755 /* Clear status block in ram. */
8756 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8757
8758 /* Set status block DMA address */
8759 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8760 ((u64) tnapi->status_mapping >> 32));
8761 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8762 ((u64) tnapi->status_mapping & 0xffffffff));
8763
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008764 if (tnapi->tx_ring) {
8765 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8766 (TG3_TX_RING_SIZE <<
8767 BDINFO_FLAGS_MAXLEN_SHIFT),
8768 NIC_SRAM_TX_BUFFER_DESC);
8769 txrcb += TG3_BDINFO_SIZE;
8770 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008771
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008772 if (tnapi->rx_rcb) {
8773 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008774 (tp->rx_ret_ring_mask + 1) <<
8775 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008776 rxrcb += TG3_BDINFO_SIZE;
8777 }
8778
8779 stblk = HOSTCC_STATBLCK_RING1;
8780
8781 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8782 u64 mapping = (u64)tnapi->status_mapping;
8783 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8784 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8785
8786 /* Clear status block in ram. */
8787 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8788
Matt Carlson19cfaec2009-12-03 08:36:20 +00008789 if (tnapi->tx_ring) {
8790 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8791 (TG3_TX_RING_SIZE <<
8792 BDINFO_FLAGS_MAXLEN_SHIFT),
8793 NIC_SRAM_TX_BUFFER_DESC);
8794 txrcb += TG3_BDINFO_SIZE;
8795 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008796
8797 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008798 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008799 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8800
8801 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008802 rxrcb += TG3_BDINFO_SIZE;
8803 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008804}
8805
Matt Carlsoneb07a942011-04-20 07:57:36 +00008806static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8807{
8808 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8809
Joe Perches63c3a662011-04-26 08:12:10 +00008810 if (!tg3_flag(tp, 5750_PLUS) ||
8811 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008813 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8814 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008815 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8816 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8817 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8818 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8819 else
8820 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8821
8822 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8823 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8824
8825 val = min(nic_rep_thresh, host_rep_thresh);
8826 tw32(RCVBDI_STD_THRESH, val);
8827
Joe Perches63c3a662011-04-26 08:12:10 +00008828 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008829 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8830
Joe Perches63c3a662011-04-26 08:12:10 +00008831 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008832 return;
8833
Matt Carlson513aa6e2011-11-21 15:01:18 +00008834 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008835
8836 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8837
8838 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8839 tw32(RCVBDI_JUMBO_THRESH, val);
8840
Joe Perches63c3a662011-04-26 08:12:10 +00008841 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008842 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8843}
8844
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008845static inline u32 calc_crc(unsigned char *buf, int len)
8846{
8847 u32 reg;
8848 u32 tmp;
8849 int j, k;
8850
8851 reg = 0xffffffff;
8852
8853 for (j = 0; j < len; j++) {
8854 reg ^= buf[j];
8855
8856 for (k = 0; k < 8; k++) {
8857 tmp = reg & 0x01;
8858
8859 reg >>= 1;
8860
8861 if (tmp)
8862 reg ^= 0xedb88320;
8863 }
8864 }
8865
8866 return ~reg;
8867}
8868
8869static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8870{
8871 /* accept or reject all multicast frames */
8872 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8873 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8874 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8875 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8876}
8877
8878static void __tg3_set_rx_mode(struct net_device *dev)
8879{
8880 struct tg3 *tp = netdev_priv(dev);
8881 u32 rx_mode;
8882
8883 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8884 RX_MODE_KEEP_VLAN_TAG);
8885
8886#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8887 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8888 * flag clear.
8889 */
8890 if (!tg3_flag(tp, ENABLE_ASF))
8891 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8892#endif
8893
8894 if (dev->flags & IFF_PROMISC) {
8895 /* Promiscuous mode. */
8896 rx_mode |= RX_MODE_PROMISC;
8897 } else if (dev->flags & IFF_ALLMULTI) {
8898 /* Accept all multicast. */
8899 tg3_set_multi(tp, 1);
8900 } else if (netdev_mc_empty(dev)) {
8901 /* Reject all multicast. */
8902 tg3_set_multi(tp, 0);
8903 } else {
8904 /* Accept one or more multicast(s). */
8905 struct netdev_hw_addr *ha;
8906 u32 mc_filter[4] = { 0, };
8907 u32 regidx;
8908 u32 bit;
8909 u32 crc;
8910
8911 netdev_for_each_mc_addr(ha, dev) {
8912 crc = calc_crc(ha->addr, ETH_ALEN);
8913 bit = ~crc & 0x7f;
8914 regidx = (bit & 0x60) >> 5;
8915 bit &= 0x1f;
8916 mc_filter[regidx] |= (1 << bit);
8917 }
8918
8919 tw32(MAC_HASH_REG_0, mc_filter[0]);
8920 tw32(MAC_HASH_REG_1, mc_filter[1]);
8921 tw32(MAC_HASH_REG_2, mc_filter[2]);
8922 tw32(MAC_HASH_REG_3, mc_filter[3]);
8923 }
8924
8925 if (rx_mode != tp->rx_mode) {
8926 tp->rx_mode = rx_mode;
8927 tw32_f(MAC_RX_MODE, rx_mode);
8928 udelay(10);
8929 }
8930}
8931
Michael Chan91024262012-09-28 07:12:38 +00008932static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00008933{
8934 int i;
8935
8936 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00008937 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00008938}
8939
8940static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008941{
8942 int i;
8943
8944 if (!tg3_flag(tp, SUPPORT_MSIX))
8945 return;
8946
Michael Chan0b3ba052012-11-14 14:44:29 +00008947 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008948 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008949 return;
8950 }
8951
8952 /* Validate table against current IRQ count */
8953 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00008954 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00008955 break;
8956 }
8957
8958 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00008959 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008960}
8961
Matt Carlson90415472011-12-16 13:33:23 +00008962static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008963{
8964 int i = 0;
8965 u32 reg = MAC_RSS_INDIR_TBL_0;
8966
8967 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8968 u32 val = tp->rss_ind_tbl[i];
8969 i++;
8970 for (; i % 8; i++) {
8971 val <<= 4;
8972 val |= tp->rss_ind_tbl[i];
8973 }
8974 tw32(reg, val);
8975 reg += 4;
8976 }
8977}
8978
Matt Carlson2d31eca2009-09-01 12:53:31 +00008979/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008980static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981{
8982 u32 val, rdmac_mode;
8983 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008984 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985
8986 tg3_disable_ints(tp);
8987
8988 tg3_stop_fw(tp);
8989
8990 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8991
Joe Perches63c3a662011-04-26 08:12:10 +00008992 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008993 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994
Matt Carlson699c0192010-12-06 08:28:51 +00008995 /* Enable MAC control of LPI */
8996 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8997 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8998 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8999 TG3_CPMU_EEE_LNKIDL_UART_IDL);
9000
9001 tw32_f(TG3_CPMU_EEE_CTRL,
9002 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
9003
Matt Carlsona386b902010-12-06 08:28:53 +00009004 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
9005 TG3_CPMU_EEEMD_LPI_IN_TX |
9006 TG3_CPMU_EEEMD_LPI_IN_RX |
9007 TG3_CPMU_EEEMD_EEE_ENABLE;
9008
9009 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
9010 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
9011
Joe Perches63c3a662011-04-26 08:12:10 +00009012 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009013 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9014
9015 tw32_f(TG3_CPMU_EEE_MODE, val);
9016
9017 tw32_f(TG3_CPMU_EEE_DBTMR1,
9018 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9019 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9020
9021 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009022 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009023 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009024 }
9025
Matt Carlson603f1172010-02-12 14:47:10 +00009026 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009027 tg3_phy_reset(tp);
9028
Linus Torvalds1da177e2005-04-16 15:20:36 -07009029 err = tg3_chip_reset(tp);
9030 if (err)
9031 return err;
9032
9033 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9034
Matt Carlsonbcb37f62008-11-03 16:52:09 -08009035 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009036 val = tr32(TG3_CPMU_CTRL);
9037 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9038 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009039
9040 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9041 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9042 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9043 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9044
9045 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9046 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9047 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9048 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9049
9050 val = tr32(TG3_CPMU_HST_ACC);
9051 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9052 val |= CPMU_HST_ACC_MACCLK_6_25;
9053 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009054 }
9055
Matt Carlson33466d932009-04-20 06:57:41 +00009056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
9057 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9058 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9059 PCIE_PWR_MGMT_L1_THRESH_4MS;
9060 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009061
9062 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9063 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9064
9065 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009066
Matt Carlsonf40386c2009-11-02 14:24:02 +00009067 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9068 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009069 }
9070
Joe Perches63c3a662011-04-26 08:12:10 +00009071 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009072 u32 grc_mode = tr32(GRC_MODE);
9073
9074 /* Access the lower 1K of PL PCIE block registers. */
9075 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9076 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9077
9078 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9079 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9080 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9081
9082 tw32(GRC_MODE, grc_mode);
9083 }
9084
Matt Carlson55086ad2011-12-14 11:09:59 +00009085 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009086 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
9087 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009088
Matt Carlson5093eed2010-11-24 08:31:45 +00009089 /* Access the lower 1K of PL PCIE block registers. */
9090 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9091 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009092
Matt Carlson5093eed2010-11-24 08:31:45 +00009093 val = tr32(TG3_PCIE_TLDLPL_PORT +
9094 TG3_PCIE_PL_LO_PHYCTL5);
9095 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9096 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009097
Matt Carlson5093eed2010-11-24 08:31:45 +00009098 tw32(GRC_MODE, grc_mode);
9099 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009100
Matt Carlson1ff30a52011-05-19 12:12:46 +00009101 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
9102 u32 grc_mode = tr32(GRC_MODE);
9103
9104 /* Access the lower 1K of DL PCIE block registers. */
9105 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9106 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9107
9108 val = tr32(TG3_PCIE_TLDLPL_PORT +
9109 TG3_PCIE_DL_LO_FTSMAX);
9110 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9111 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9112 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9113
9114 tw32(GRC_MODE, grc_mode);
9115 }
9116
Matt Carlsona977dbe2010-04-12 06:58:26 +00009117 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9118 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9119 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9120 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009121 }
9122
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 /* This works around an issue with Athlon chipsets on
9124 * B3 tigon3 silicon. This bit has no effect on any
9125 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009126 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 */
Joe Perches63c3a662011-04-26 08:12:10 +00009128 if (!tg3_flag(tp, CPMU_PRESENT)) {
9129 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009130 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9131 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009133
9134 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009135 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 val = tr32(TG3PCI_PCISTATE);
9137 val |= PCISTATE_RETRY_SAME_DMA;
9138 tw32(TG3PCI_PCISTATE, val);
9139 }
9140
Joe Perches63c3a662011-04-26 08:12:10 +00009141 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009142 /* Allow reads and writes to the
9143 * APE register and memory space.
9144 */
9145 val = tr32(TG3PCI_PCISTATE);
9146 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009147 PCISTATE_ALLOW_APE_SHMEM_WR |
9148 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009149 tw32(TG3PCI_PCISTATE, val);
9150 }
9151
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
9153 /* Enable some hw fixes. */
9154 val = tr32(TG3PCI_MSI_DATA);
9155 val |= (1 << 26) | (1 << 28) | (1 << 29);
9156 tw32(TG3PCI_MSI_DATA, val);
9157 }
9158
9159 /* Descriptor ring init may make accesses to the
9160 * NIC SRAM area to setup the TX descriptors, so we
9161 * can only do this after the hardware has been
9162 * successfully reset.
9163 */
Michael Chan32d8c572006-07-25 16:38:29 -07009164 err = tg3_init_rings(tp);
9165 if (err)
9166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009167
Joe Perches63c3a662011-04-26 08:12:10 +00009168 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009169 val = tr32(TG3PCI_DMA_RW_CTRL) &
9170 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00009171 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
9172 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009173 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00009174 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
9175 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009176 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
9177 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
9178 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009179 /* This value is determined during the probe time DMA
9180 * engine test, tg3_test_dma.
9181 */
9182 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184
9185 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9186 GRC_MODE_4X_NIC_SEND_RINGS |
9187 GRC_MODE_NO_TX_PHDR_CSUM |
9188 GRC_MODE_NO_RX_PHDR_CSUM);
9189 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009190
9191 /* Pseudo-header checksum is done by hardware logic and not
9192 * the offload processers, so make the chip do the pseudo-
9193 * header checksums on receive. For transmit it is more
9194 * convenient to do the pseudo-header checksum in software
9195 * as Linux does that on transmit for us in all cases.
9196 */
9197 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009199 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9200 if (tp->rxptpctl)
9201 tw32(TG3_RX_PTP_CTL,
9202 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9203
9204 if (tg3_flag(tp, PTP_CAPABLE))
9205 val |= GRC_MODE_TIME_SYNC_ENABLE;
9206
9207 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208
9209 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9210 val = tr32(GRC_MISC_CFG);
9211 val &= ~0xff;
9212 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9213 tw32(GRC_MISC_CFG, val);
9214
9215 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009216 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 /* Do nothing. */
9218 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
9219 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
9220 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
9221 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9222 else
9223 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9224 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9225 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009226 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009227 int fw_len;
9228
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009229 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9231 tw32(BUFMGR_MB_POOL_ADDR,
9232 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9233 tw32(BUFMGR_MB_POOL_SIZE,
9234 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236
Michael Chan0f893dc2005-07-25 12:30:38 -07009237 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9239 tp->bufmgr_config.mbuf_read_dma_low_water);
9240 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9241 tp->bufmgr_config.mbuf_mac_rx_low_water);
9242 tw32(BUFMGR_MB_HIGH_WATER,
9243 tp->bufmgr_config.mbuf_high_water);
9244 } else {
9245 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9246 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9247 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9248 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9249 tw32(BUFMGR_MB_HIGH_WATER,
9250 tp->bufmgr_config.mbuf_high_water_jumbo);
9251 }
9252 tw32(BUFMGR_DMA_LOW_WATER,
9253 tp->bufmgr_config.dma_low_water);
9254 tw32(BUFMGR_DMA_HIGH_WATER,
9255 tp->bufmgr_config.dma_high_water);
9256
Matt Carlsond309a462010-09-30 10:34:31 +00009257 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
9258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
9259 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00009260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9261 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9262 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
9263 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009264 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009265 for (i = 0; i < 2000; i++) {
9266 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9267 break;
9268 udelay(10);
9269 }
9270 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009271 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009272 return -ENODEV;
9273 }
9274
Matt Carlsoneb07a942011-04-20 07:57:36 +00009275 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
9276 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009277
Matt Carlsoneb07a942011-04-20 07:57:36 +00009278 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009279
9280 /* Initialize TG3_BDINFO's at:
9281 * RCVDBDI_STD_BD: standard eth size rx ring
9282 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9283 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9284 *
9285 * like so:
9286 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9287 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9288 * ring attribute flags
9289 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9290 *
9291 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9292 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9293 *
9294 * The size of each ring is fixed in the firmware, but the location is
9295 * configurable.
9296 */
9297 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009298 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009300 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009301 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009302 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9303 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009305 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009306 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9308 BDINFO_FLAGS_DISABLED);
9309
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009310 /* Program the jumbo buffer descriptor ring control
9311 * blocks on those devices that have them.
9312 */
Matt Carlsona0512942011-07-27 14:20:54 +00009313 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009314 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315
Joe Perches63c3a662011-04-26 08:12:10 +00009316 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009318 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009320 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009321 val = TG3_RX_JMB_RING_SIZE(tp) <<
9322 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009323 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009324 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009325 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009326 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00009327 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9328 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 } else {
9330 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9331 BDINFO_FLAGS_DISABLED);
9332 }
9333
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009335 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009336 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9337 val |= (TG3_RX_STD_DMA_SZ << 2);
9338 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009339 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009340 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009341 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009342
9343 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
Matt Carlson411da642009-11-13 13:03:46 +00009345 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00009346 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347
Joe Perches63c3a662011-04-26 08:12:10 +00009348 tpr->rx_jmb_prod_idx =
9349 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00009350 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351
Matt Carlson2d31eca2009-09-01 12:53:31 +00009352 tg3_rings_reset(tp);
9353
Linus Torvalds1da177e2005-04-16 15:20:36 -07009354 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009355 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
9357 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009358 tw32(MAC_RX_MTU_SIZE,
9359 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009360
9361 /* The slot time is changed by tg3_setup_phy if we
9362 * run at gigabit with half duplex.
9363 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009364 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9365 (6 << TX_LENGTHS_IPG_SHIFT) |
9366 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9367
9368 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9369 val |= tr32(MAC_TX_LENGTHS) &
9370 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9371 TX_LENGTHS_CNT_DWN_VAL_MSK);
9372
9373 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374
9375 /* Receive rules. */
9376 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9377 tw32(RCVLPC_CONFIG, 0x0181);
9378
9379 /* Calculate RDMAC_MODE setting early, we need it to determine
9380 * the RCVLPC_STATE_ENABLE mask.
9381 */
9382 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9383 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9384 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9385 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9386 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009387
Matt Carlsondeabaac2010-11-24 08:31:50 +00009388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009389 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9390
Matt Carlson57e69832008-05-25 23:48:31 -07009391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9393 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009394 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9395 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9396 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9397
Matt Carlsonc5908932011-03-09 16:58:25 +00009398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9399 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009400 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07009401 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9403 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009404 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9406 }
9407 }
9408
Joe Perches63c3a662011-04-26 08:12:10 +00009409 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009410 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9411
Joe Perches63c3a662011-04-26 08:12:10 +00009412 if (tg3_flag(tp, HW_TSO_1) ||
9413 tg3_flag(tp, HW_TSO_2) ||
9414 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009415 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9416
Matt Carlson108a6c12011-05-19 12:12:47 +00009417 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009418 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08009419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9420 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421
Matt Carlsonf2096f92011-04-05 14:22:48 +00009422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9423 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9424
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009425 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9426 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9427 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9428 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009429 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009430 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Michael Chan10ce95d2012-07-29 19:15:42 +00009431 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009432 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9433 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9434 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9435 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9436 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9437 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009438 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009439 tw32(TG3_RDMA_RSRVCTRL_REG,
9440 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9441 }
9442
Matt Carlsond78b59f2011-04-05 14:22:46 +00009443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009445 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9446 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9447 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9448 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9449 }
9450
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009453 val = tr32(RCVLPC_STATS_ENABLE);
9454 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9455 tw32(RCVLPC_STATS_ENABLE, val);
9456 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009457 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458 val = tr32(RCVLPC_STATS_ENABLE);
9459 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9460 tw32(RCVLPC_STATS_ENABLE, val);
9461 } else {
9462 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9463 }
9464 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9465 tw32(SNDDATAI_STATSENAB, 0xffffff);
9466 tw32(SNDDATAI_STATSCTRL,
9467 (SNDDATAI_SCTRL_ENABLE |
9468 SNDDATAI_SCTRL_FASTUPD));
9469
9470 /* Setup host coalescing engine. */
9471 tw32(HOSTCC_MODE, 0);
9472 for (i = 0; i < 2000; i++) {
9473 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9474 break;
9475 udelay(10);
9476 }
9477
Michael Chand244c892005-07-05 14:42:33 -07009478 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479
Joe Perches63c3a662011-04-26 08:12:10 +00009480 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481 /* Status/statistics block address. See tg3_timer,
9482 * the tg3_periodic_fetch_stats call there, and
9483 * tg3_get_stats to see how this works for 5705/5750 chips.
9484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9486 ((u64) tp->stats_mapping >> 32));
9487 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9488 ((u64) tp->stats_mapping & 0xffffffff));
9489 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009490
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009492
9493 /* Clear statistics and status block memory areas */
9494 for (i = NIC_SRAM_STATS_BLK;
9495 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9496 i += sizeof(u32)) {
9497 tg3_write_mem(tp, i, 0);
9498 udelay(40);
9499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009500 }
9501
9502 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9503
9504 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9505 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009506 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9508
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009509 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9510 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009511 /* reset to prevent losing 1st rx packet intermittently */
9512 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9513 udelay(10);
9514 }
9515
Matt Carlson3bda1252008-08-15 14:08:22 -07009516 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009517 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9518 MAC_MODE_FHDE_ENABLE;
9519 if (tg3_flag(tp, ENABLE_APE))
9520 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009521 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009522 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009523 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9524 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9526 udelay(40);
9527
Michael Chan314fba32005-04-21 17:07:04 -07009528 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009529 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009530 * register to preserve the GPIO settings for LOMs. The GPIOs,
9531 * whether used as inputs or outputs, are set by boot code after
9532 * reset.
9533 */
Joe Perches63c3a662011-04-26 08:12:10 +00009534 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009535 u32 gpio_mask;
9536
Michael Chan9d26e212006-12-07 00:21:14 -08009537 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9538 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9539 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009540
9541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9542 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9543 GRC_LCLCTRL_GPIO_OUTPUT3;
9544
Michael Chanaf36e6b2006-03-23 01:28:06 -08009545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9546 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9547
Gary Zambranoaaf84462007-05-05 11:51:45 -07009548 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009549 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9550
9551 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009552 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009553 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9554 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9557 udelay(100);
9558
Matt Carlsonc3b50032012-01-17 15:27:23 +00009559 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009560 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009561 val |= MSGINT_MODE_ENABLE;
9562 if (tp->irq_cnt > 1)
9563 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009564 if (!tg3_flag(tp, 1SHOT_MSI))
9565 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009566 tw32(MSGINT_MODE, val);
9567 }
9568
Joe Perches63c3a662011-04-26 08:12:10 +00009569 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9571 udelay(40);
9572 }
9573
9574 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9575 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9576 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9577 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9578 WDMAC_MODE_LNGREAD_ENAB);
9579
Matt Carlsonc5908932011-03-09 16:58:25 +00009580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9581 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009582 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009583 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9584 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9585 /* nothing */
9586 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009587 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 val |= WDMAC_MODE_RX_ACCEL;
9589 }
9590 }
9591
Michael Chand9ab5ad12006-03-20 22:27:35 -08009592 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009593 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009594 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009595
Matt Carlson788a0352009-11-02 14:26:03 +00009596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9597 val |= WDMAC_MODE_BURST_ALL_DATA;
9598
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 tw32_f(WDMAC_MODE, val);
9600 udelay(40);
9601
Joe Perches63c3a662011-04-26 08:12:10 +00009602 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009603 u16 pcix_cmd;
9604
9605 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9606 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009608 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9609 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009610 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009611 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9612 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613 }
Matt Carlson9974a352007-10-07 23:27:28 -07009614 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9615 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 }
9617
9618 tw32_f(RDMAC_MODE, rdmac_mode);
9619 udelay(40);
9620
Michael Chan091f0ea2012-07-29 19:15:43 +00009621 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9622 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9623 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9624 break;
9625 }
9626 if (i < TG3_NUM_RDMA_CHANNELS) {
9627 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9628 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9629 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9630 tg3_flag_set(tp, 5719_RDMA_BUG);
9631 }
9632 }
9633
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009635 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009637
9638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9639 tw32(SNDDATAC_MODE,
9640 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9641 else
9642 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9643
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9645 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009646 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009647 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009648 val |= RCVDBDI_MODE_LRG_RING_SZ;
9649 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009651 if (tg3_flag(tp, HW_TSO_1) ||
9652 tg3_flag(tp, HW_TSO_2) ||
9653 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009655 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009656 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009657 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9658 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9660
9661 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9662 err = tg3_load_5701_a0_firmware_fix(tp);
9663 if (err)
9664 return err;
9665 }
9666
Joe Perches63c3a662011-04-26 08:12:10 +00009667 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668 err = tg3_load_tso_firmware(tp);
9669 if (err)
9670 return err;
9671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672
9673 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009674
Joe Perches63c3a662011-04-26 08:12:10 +00009675 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009676 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9677 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009678
9679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9680 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9681 tp->tx_mode &= ~val;
9682 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9683 }
9684
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 tw32_f(MAC_TX_MODE, tp->tx_mode);
9686 udelay(100);
9687
Joe Perches63c3a662011-04-26 08:12:10 +00009688 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009689 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009690
9691 /* Setup the "secret" hash key. */
9692 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9693 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9694 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9695 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9696 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9697 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9698 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9699 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9700 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9701 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9702 }
9703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009705 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009706 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9707
Joe Perches63c3a662011-04-26 08:12:10 +00009708 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009709 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9710 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9711 RX_MODE_RSS_IPV6_HASH_EN |
9712 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9713 RX_MODE_RSS_IPV4_HASH_EN |
9714 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9715
Linus Torvalds1da177e2005-04-16 15:20:36 -07009716 tw32_f(MAC_RX_MODE, tp->rx_mode);
9717 udelay(10);
9718
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719 tw32(MAC_LED_CTRL, tp->led_ctrl);
9720
9721 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009722 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9724 udelay(10);
9725 }
9726 tw32_f(MAC_RX_MODE, tp->rx_mode);
9727 udelay(10);
9728
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009729 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009730 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009731 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 /* Set drive transmission level to 1.2V */
9733 /* only if the signal pre-emphasis bit is not set */
9734 val = tr32(MAC_SERDES_CFG);
9735 val &= 0xfffff000;
9736 val |= 0x880;
9737 tw32(MAC_SERDES_CFG, val);
9738 }
9739 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9740 tw32(MAC_SERDES_CFG, 0x616000);
9741 }
9742
9743 /* Prevent chip from dropping frames when flow control
9744 * is enabled.
9745 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009746 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009747 val = 1;
9748 else
9749 val = 2;
9750 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751
9752 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009753 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009755 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009756 }
9757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009758 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009759 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009760 u32 tmp;
9761
9762 tmp = tr32(SERDES_RX_CTRL);
9763 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9764 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9765 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9766 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9767 }
9768
Joe Perches63c3a662011-04-26 08:12:10 +00009769 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009770 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009771 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772
Matt Carlsondd477002008-05-25 23:45:58 -07009773 err = tg3_setup_phy(tp, 0);
9774 if (err)
9775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009777 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9778 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009779 u32 tmp;
9780
9781 /* Clear CRC stats. */
9782 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9783 tg3_writephy(tp, MII_TG3_TEST1,
9784 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009785 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787 }
9788 }
9789
9790 __tg3_set_rx_mode(tp->dev);
9791
9792 /* Initialize receive rules. */
9793 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9794 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9795 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9796 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9797
Joe Perches63c3a662011-04-26 08:12:10 +00009798 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799 limit = 8;
9800 else
9801 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009802 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 limit -= 4;
9804 switch (limit) {
9805 case 16:
9806 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9807 case 15:
9808 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9809 case 14:
9810 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9811 case 13:
9812 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9813 case 12:
9814 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9815 case 11:
9816 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9817 case 10:
9818 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9819 case 9:
9820 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9821 case 8:
9822 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9823 case 7:
9824 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9825 case 6:
9826 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9827 case 5:
9828 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9829 case 4:
9830 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9831 case 3:
9832 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9833 case 2:
9834 case 1:
9835
9836 default:
9837 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839
Joe Perches63c3a662011-04-26 08:12:10 +00009840 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009841 /* Write our heartbeat update interval to APE. */
9842 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9843 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009844
Linus Torvalds1da177e2005-04-16 15:20:36 -07009845 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9846
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847 return 0;
9848}
9849
9850/* Called at device open time to get the chip ready for
9851 * packet processing. Invoked with tp->lock held.
9852 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009853static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 tg3_switch_clocks(tp);
9856
9857 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9858
Matt Carlson2f751b62008-08-04 23:17:34 -07009859 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860}
9861
Michael Chanaed93e02012-07-16 16:24:02 +00009862static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9863{
9864 int i;
9865
9866 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9867 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9868
9869 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9870 off += len;
9871
9872 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9873 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9874 memset(ocir, 0, TG3_OCIR_LEN);
9875 }
9876}
9877
9878/* sysfs attributes for hwmon */
9879static ssize_t tg3_show_temp(struct device *dev,
9880 struct device_attribute *devattr, char *buf)
9881{
9882 struct pci_dev *pdev = to_pci_dev(dev);
9883 struct net_device *netdev = pci_get_drvdata(pdev);
9884 struct tg3 *tp = netdev_priv(netdev);
9885 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9886 u32 temperature;
9887
9888 spin_lock_bh(&tp->lock);
9889 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
9890 sizeof(temperature));
9891 spin_unlock_bh(&tp->lock);
9892 return sprintf(buf, "%u\n", temperature);
9893}
9894
9895
9896static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
9897 TG3_TEMP_SENSOR_OFFSET);
9898static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
9899 TG3_TEMP_CAUTION_OFFSET);
9900static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
9901 TG3_TEMP_MAX_OFFSET);
9902
9903static struct attribute *tg3_attributes[] = {
9904 &sensor_dev_attr_temp1_input.dev_attr.attr,
9905 &sensor_dev_attr_temp1_crit.dev_attr.attr,
9906 &sensor_dev_attr_temp1_max.dev_attr.attr,
9907 NULL
9908};
9909
9910static const struct attribute_group tg3_group = {
9911 .attrs = tg3_attributes,
9912};
9913
Michael Chanaed93e02012-07-16 16:24:02 +00009914static void tg3_hwmon_close(struct tg3 *tp)
9915{
Michael Chanaed93e02012-07-16 16:24:02 +00009916 if (tp->hwmon_dev) {
9917 hwmon_device_unregister(tp->hwmon_dev);
9918 tp->hwmon_dev = NULL;
9919 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
9920 }
Michael Chanaed93e02012-07-16 16:24:02 +00009921}
9922
9923static void tg3_hwmon_open(struct tg3 *tp)
9924{
Michael Chanaed93e02012-07-16 16:24:02 +00009925 int i, err;
9926 u32 size = 0;
9927 struct pci_dev *pdev = tp->pdev;
9928 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
9929
9930 tg3_sd_scan_scratchpad(tp, ocirs);
9931
9932 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
9933 if (!ocirs[i].src_data_length)
9934 continue;
9935
9936 size += ocirs[i].src_hdr_length;
9937 size += ocirs[i].src_data_length;
9938 }
9939
9940 if (!size)
9941 return;
9942
9943 /* Register hwmon sysfs hooks */
9944 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
9945 if (err) {
9946 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
9947 return;
9948 }
9949
9950 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
9951 if (IS_ERR(tp->hwmon_dev)) {
9952 tp->hwmon_dev = NULL;
9953 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
9954 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
9955 }
Michael Chanaed93e02012-07-16 16:24:02 +00009956}
9957
9958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959#define TG3_STAT_ADD32(PSTAT, REG) \
9960do { u32 __val = tr32(REG); \
9961 (PSTAT)->low += __val; \
9962 if ((PSTAT)->low < __val) \
9963 (PSTAT)->high += 1; \
9964} while (0)
9965
9966static void tg3_periodic_fetch_stats(struct tg3 *tp)
9967{
9968 struct tg3_hw_stats *sp = tp->hw_stats;
9969
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009970 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 return;
9972
9973 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9974 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9975 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9976 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9977 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9978 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9979 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9980 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9981 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9982 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9983 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9984 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9985 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +00009986 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
9987 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
9988 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
9989 u32 val;
9990
9991 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9992 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
9993 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9994 tg3_flag_clear(tp, 5719_RDMA_BUG);
9995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996
9997 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9998 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9999 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
10000 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
10001 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
10002 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
10003 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
10004 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
10005 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
10006 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
10007 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
10008 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
10009 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
10010 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -070010011
10012 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +000010013 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10014 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
10015 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010016 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10017 } else {
10018 u32 val = tr32(HOSTCC_FLOW_ATTN);
10019 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10020 if (val) {
10021 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10022 sp->rx_discards.low += val;
10023 if (sp->rx_discards.low < val)
10024 sp->rx_discards.high += 1;
10025 }
10026 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10027 }
Michael Chan463d3052006-05-22 16:36:27 -070010028 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029}
10030
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010031static void tg3_chk_missed_msi(struct tg3 *tp)
10032{
10033 u32 i;
10034
10035 for (i = 0; i < tp->irq_cnt; i++) {
10036 struct tg3_napi *tnapi = &tp->napi[i];
10037
10038 if (tg3_has_work(tnapi)) {
10039 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10040 tnapi->last_tx_cons == tnapi->tx_cons) {
10041 if (tnapi->chk_msi_cnt < 1) {
10042 tnapi->chk_msi_cnt++;
10043 return;
10044 }
Matt Carlson7f230732011-08-31 11:44:48 +000010045 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010046 }
10047 }
10048 tnapi->chk_msi_cnt = 0;
10049 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10050 tnapi->last_tx_cons = tnapi->tx_cons;
10051 }
10052}
10053
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054static void tg3_timer(unsigned long __opaque)
10055{
10056 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057
Matt Carlson5b190622011-11-04 09:15:04 +000010058 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010059 goto restart_timer;
10060
David S. Millerf47c11e2005-06-24 20:18:35 -070010061 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010064 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010065 tg3_chk_missed_msi(tp);
10066
Joe Perches63c3a662011-04-26 08:12:10 +000010067 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010068 /* All of this garbage is because when using non-tagged
10069 * IRQ status the mailbox/status_block protocol the chip
10070 * uses with the cpu is race prone.
10071 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010072 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010073 tw32(GRC_LOCAL_CTRL,
10074 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10075 } else {
10076 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010077 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079
David S. Millerfac9b832005-05-18 22:46:34 -070010080 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010081 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010082 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010083 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010085 }
10086
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 /* This part only runs once per second. */
10088 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010089 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010090 tg3_periodic_fetch_stats(tp);
10091
Matt Carlsonb0c59432011-05-19 12:12:48 +000010092 if (tp->setlpicnt && !--tp->setlpicnt)
10093 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010094
Joe Perches63c3a662011-04-26 08:12:10 +000010095 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 u32 mac_stat;
10097 int phy_event;
10098
10099 mac_stat = tr32(MAC_STATUS);
10100
10101 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010102 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10104 phy_event = 1;
10105 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10106 phy_event = 1;
10107
10108 if (phy_event)
10109 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010110 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 u32 mac_stat = tr32(MAC_STATUS);
10112 int need_setup = 0;
10113
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010114 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10116 need_setup = 1;
10117 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010118 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10120 MAC_STATUS_SIGNAL_DET))) {
10121 need_setup = 1;
10122 }
10123 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010124 if (!tp->serdes_counter) {
10125 tw32_f(MAC_MODE,
10126 (tp->mac_mode &
10127 ~MAC_MODE_PORT_MODE_MASK));
10128 udelay(40);
10129 tw32_f(MAC_MODE, tp->mac_mode);
10130 udelay(40);
10131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010132 tg3_setup_phy(tp, 0);
10133 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010134 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010135 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010136 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138
10139 tp->timer_counter = tp->timer_multiplier;
10140 }
10141
Michael Chan130b8e42006-09-27 16:00:40 -070010142 /* Heartbeat is only sent once every 2 seconds.
10143 *
10144 * The heartbeat is to tell the ASF firmware that the host
10145 * driver is still alive. In the event that the OS crashes,
10146 * ASF needs to reset the hardware to free up the FIFO space
10147 * that may be filled with rx packets destined for the host.
10148 * If the FIFO is full, ASF will no longer function properly.
10149 *
10150 * Unintended resets have been reported on real time kernels
10151 * where the timer doesn't run on time. Netpoll will also have
10152 * same problem.
10153 *
10154 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10155 * to check the ring condition when the heartbeat is expiring
10156 * before doing the reset. This will prevent most unintended
10157 * resets.
10158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010160 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010161 tg3_wait_for_event_ack(tp);
10162
Michael Chanbbadf502006-04-06 21:46:34 -070010163 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010164 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010165 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010166 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10167 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010168
10169 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170 }
10171 tp->asf_counter = tp->asf_multiplier;
10172 }
10173
David S. Millerf47c11e2005-06-24 20:18:35 -070010174 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175
Michael Chanf475f162006-03-27 23:20:14 -080010176restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 tp->timer.expires = jiffies + tp->timer_offset;
10178 add_timer(&tp->timer);
10179}
10180
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010181static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010182{
10183 if (tg3_flag(tp, TAGGED_STATUS) &&
10184 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10185 !tg3_flag(tp, 57765_CLASS))
10186 tp->timer_offset = HZ;
10187 else
10188 tp->timer_offset = HZ / 10;
10189
10190 BUG_ON(tp->timer_offset > HZ);
10191
10192 tp->timer_multiplier = (HZ / tp->timer_offset);
10193 tp->asf_multiplier = (HZ / tp->timer_offset) *
10194 TG3_FW_UPDATE_FREQ_SEC;
10195
10196 init_timer(&tp->timer);
10197 tp->timer.data = (unsigned long) tp;
10198 tp->timer.function = tg3_timer;
10199}
10200
10201static void tg3_timer_start(struct tg3 *tp)
10202{
10203 tp->asf_counter = tp->asf_multiplier;
10204 tp->timer_counter = tp->timer_multiplier;
10205
10206 tp->timer.expires = jiffies + tp->timer_offset;
10207 add_timer(&tp->timer);
10208}
10209
10210static void tg3_timer_stop(struct tg3 *tp)
10211{
10212 del_timer_sync(&tp->timer);
10213}
10214
10215/* Restart hardware after configuration changes, self-test, etc.
10216 * Invoked with tp->lock held.
10217 */
10218static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10219 __releases(tp->lock)
10220 __acquires(tp->lock)
10221{
10222 int err;
10223
10224 err = tg3_init_hw(tp, reset_phy);
10225 if (err) {
10226 netdev_err(tp->dev,
10227 "Failed to re-initialize device, aborting\n");
10228 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10229 tg3_full_unlock(tp);
10230 tg3_timer_stop(tp);
10231 tp->irq_sync = 0;
10232 tg3_napi_enable(tp);
10233 dev_close(tp->dev);
10234 tg3_full_lock(tp, 0);
10235 }
10236 return err;
10237}
10238
10239static void tg3_reset_task(struct work_struct *work)
10240{
10241 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10242 int err;
10243
10244 tg3_full_lock(tp, 0);
10245
10246 if (!netif_running(tp->dev)) {
10247 tg3_flag_clear(tp, RESET_TASK_PENDING);
10248 tg3_full_unlock(tp);
10249 return;
10250 }
10251
10252 tg3_full_unlock(tp);
10253
10254 tg3_phy_stop(tp);
10255
10256 tg3_netif_stop(tp);
10257
10258 tg3_full_lock(tp, 1);
10259
10260 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10261 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10262 tp->write32_rx_mbox = tg3_write_flush_reg32;
10263 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10264 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10265 }
10266
10267 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10268 err = tg3_init_hw(tp, 1);
10269 if (err)
10270 goto out;
10271
10272 tg3_netif_start(tp);
10273
10274out:
10275 tg3_full_unlock(tp);
10276
10277 if (!err)
10278 tg3_phy_start(tp);
10279
10280 tg3_flag_clear(tp, RESET_TASK_PENDING);
10281}
10282
Matt Carlson4f125f42009-09-01 12:55:02 +000010283static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010284{
David Howells7d12e782006-10-05 14:55:46 +010010285 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010286 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010287 char *name;
10288 struct tg3_napi *tnapi = &tp->napi[irq_num];
10289
10290 if (tp->irq_cnt == 1)
10291 name = tp->dev->name;
10292 else {
10293 name = &tnapi->irq_lbl[0];
10294 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10295 name[IFNAMSIZ-1] = 0;
10296 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010297
Joe Perches63c3a662011-04-26 08:12:10 +000010298 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010299 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010300 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010301 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010302 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010303 } else {
10304 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010305 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010306 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010307 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010308 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010309
10310 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010311}
10312
Michael Chan79381092005-04-21 17:13:59 -070010313static int tg3_test_interrupt(struct tg3 *tp)
10314{
Matt Carlson09943a12009-08-28 14:01:57 +000010315 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010316 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010317 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010318 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010319
Michael Chand4bc3922005-05-29 14:59:20 -070010320 if (!netif_running(dev))
10321 return -ENODEV;
10322
Michael Chan79381092005-04-21 17:13:59 -070010323 tg3_disable_ints(tp);
10324
Matt Carlson4f125f42009-09-01 12:55:02 +000010325 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010326
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010327 /*
10328 * Turn off MSI one shot mode. Otherwise this test has no
10329 * observable way to know whether the interrupt was delivered.
10330 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010331 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010332 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10333 tw32(MSGINT_MODE, val);
10334 }
10335
Matt Carlson4f125f42009-09-01 12:55:02 +000010336 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010337 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010338 if (err)
10339 return err;
10340
Matt Carlson898a56f2009-08-28 14:02:40 +000010341 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010342 tg3_enable_ints(tp);
10343
10344 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010345 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010346
10347 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010348 u32 int_mbox, misc_host_ctrl;
10349
Matt Carlson898a56f2009-08-28 14:02:40 +000010350 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010351 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10352
10353 if ((int_mbox != 0) ||
10354 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10355 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010356 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010357 }
10358
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010359 if (tg3_flag(tp, 57765_PLUS) &&
10360 tnapi->hw_status->status_tag != tnapi->last_tag)
10361 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10362
Michael Chan79381092005-04-21 17:13:59 -070010363 msleep(10);
10364 }
10365
10366 tg3_disable_ints(tp);
10367
Matt Carlson4f125f42009-09-01 12:55:02 +000010368 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010369
Matt Carlson4f125f42009-09-01 12:55:02 +000010370 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010371
10372 if (err)
10373 return err;
10374
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010375 if (intr_ok) {
10376 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010377 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010378 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10379 tw32(MSGINT_MODE, val);
10380 }
Michael Chan79381092005-04-21 17:13:59 -070010381 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010382 }
Michael Chan79381092005-04-21 17:13:59 -070010383
10384 return -EIO;
10385}
10386
10387/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10388 * successfully restored
10389 */
10390static int tg3_test_msi(struct tg3 *tp)
10391{
Michael Chan79381092005-04-21 17:13:59 -070010392 int err;
10393 u16 pci_cmd;
10394
Joe Perches63c3a662011-04-26 08:12:10 +000010395 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010396 return 0;
10397
10398 /* Turn off SERR reporting in case MSI terminates with Master
10399 * Abort.
10400 */
10401 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10402 pci_write_config_word(tp->pdev, PCI_COMMAND,
10403 pci_cmd & ~PCI_COMMAND_SERR);
10404
10405 err = tg3_test_interrupt(tp);
10406
10407 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10408
10409 if (!err)
10410 return 0;
10411
10412 /* other failures */
10413 if (err != -EIO)
10414 return err;
10415
10416 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010417 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10418 "to INTx mode. Please report this failure to the PCI "
10419 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010420
Matt Carlson4f125f42009-09-01 12:55:02 +000010421 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010422
Michael Chan79381092005-04-21 17:13:59 -070010423 pci_disable_msi(tp->pdev);
10424
Joe Perches63c3a662011-04-26 08:12:10 +000010425 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010426 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010427
Matt Carlson4f125f42009-09-01 12:55:02 +000010428 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010429 if (err)
10430 return err;
10431
10432 /* Need to reset the chip because the MSI cycle may have terminated
10433 * with Master Abort.
10434 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010435 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010436
Michael Chan944d9802005-05-29 14:57:48 -070010437 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010438 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010439
David S. Millerf47c11e2005-06-24 20:18:35 -070010440 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010441
10442 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010443 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010444
10445 return err;
10446}
10447
Matt Carlson9e9fd122009-01-19 16:57:45 -080010448static int tg3_request_firmware(struct tg3 *tp)
10449{
10450 const __be32 *fw_data;
10451
10452 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010453 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10454 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010455 return -ENOENT;
10456 }
10457
10458 fw_data = (void *)tp->fw->data;
10459
10460 /* Firmware blob starts with version numbers, followed by
10461 * start address and _full_ length including BSS sections
10462 * (which must be longer than the actual data, of course
10463 */
10464
10465 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10466 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010467 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10468 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010469 release_firmware(tp->fw);
10470 tp->fw = NULL;
10471 return -EINVAL;
10472 }
10473
10474 /* We no longer need firmware; we have it. */
10475 tp->fw_needed = NULL;
10476 return 0;
10477}
10478
Michael Chan91024262012-09-28 07:12:38 +000010479static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010480{
Michael Chan91024262012-09-28 07:12:38 +000010481 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010482
Michael Chan91024262012-09-28 07:12:38 +000010483 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010484 /* We want as many rx rings enabled as there are cpus.
10485 * In multiqueue MSI-X mode, the first MSI-X vector
10486 * only deals with link interrupts, etc, so we add
10487 * one to the number of vectors we are requesting.
10488 */
Michael Chan91024262012-09-28 07:12:38 +000010489 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010490 }
Matt Carlson679563f2009-09-01 12:55:46 +000010491
Michael Chan91024262012-09-28 07:12:38 +000010492 return irq_cnt;
10493}
10494
10495static bool tg3_enable_msix(struct tg3 *tp)
10496{
10497 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010498 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010499
Michael Chan09681692012-09-28 07:12:42 +000010500 tp->txq_cnt = tp->txq_req;
10501 tp->rxq_cnt = tp->rxq_req;
10502 if (!tp->rxq_cnt)
10503 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010504 if (tp->rxq_cnt > tp->rxq_max)
10505 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010506
10507 /* Disable multiple TX rings by default. Simple round-robin hardware
10508 * scheduling of the TX rings can cause starvation of rings with
10509 * small packets when other rings have TSO or jumbo packets.
10510 */
10511 if (!tp->txq_req)
10512 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010513
10514 tp->irq_cnt = tg3_irq_count(tp);
10515
Matt Carlson679563f2009-09-01 12:55:46 +000010516 for (i = 0; i < tp->irq_max; i++) {
10517 msix_ent[i].entry = i;
10518 msix_ent[i].vector = 0;
10519 }
10520
10521 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010522 if (rc < 0) {
10523 return false;
10524 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010525 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10526 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010527 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10528 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010529 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010530 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010531 if (tp->txq_cnt)
10532 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010533 }
10534
10535 for (i = 0; i < tp->irq_max; i++)
10536 tp->napi[i].irq_vec = msix_ent[i].vector;
10537
Michael Chan49a359e2012-09-28 07:12:37 +000010538 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010539 pci_disable_msix(tp->pdev);
10540 return false;
10541 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010542
Michael Chan91024262012-09-28 07:12:38 +000010543 if (tp->irq_cnt == 1)
10544 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010545
Michael Chan91024262012-09-28 07:12:38 +000010546 tg3_flag_set(tp, ENABLE_RSS);
10547
10548 if (tp->txq_cnt > 1)
10549 tg3_flag_set(tp, ENABLE_TSS);
10550
10551 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010552
Matt Carlson679563f2009-09-01 12:55:46 +000010553 return true;
10554}
10555
Matt Carlson07b01732009-08-28 14:01:15 +000010556static void tg3_ints_init(struct tg3 *tp)
10557{
Joe Perches63c3a662011-04-26 08:12:10 +000010558 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10559 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010560 /* All MSI supporting chips should support tagged
10561 * status. Assert that this is the case.
10562 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010563 netdev_warn(tp->dev,
10564 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010565 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010566 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010567
Joe Perches63c3a662011-04-26 08:12:10 +000010568 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10569 tg3_flag_set(tp, USING_MSIX);
10570 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10571 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010572
Joe Perches63c3a662011-04-26 08:12:10 +000010573 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010574 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010575 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010576 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010577 if (!tg3_flag(tp, 1SHOT_MSI))
10578 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010579 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10580 }
10581defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010582 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010583 tp->irq_cnt = 1;
10584 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010585 }
10586
10587 if (tp->irq_cnt == 1) {
10588 tp->txq_cnt = 1;
10589 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010590 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010591 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010592 }
Matt Carlson07b01732009-08-28 14:01:15 +000010593}
10594
10595static void tg3_ints_fini(struct tg3 *tp)
10596{
Joe Perches63c3a662011-04-26 08:12:10 +000010597 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010598 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010599 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010600 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010601 tg3_flag_clear(tp, USING_MSI);
10602 tg3_flag_clear(tp, USING_MSIX);
10603 tg3_flag_clear(tp, ENABLE_RSS);
10604 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010605}
10606
Matt Carlsonbe947302012-12-03 19:36:57 +000010607static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10608 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010609{
Michael Chand8f4cd32012-09-28 07:12:40 +000010610 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010611 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010612
Matt Carlson679563f2009-09-01 12:55:46 +000010613 /*
10614 * Setup interrupts first so we know how
10615 * many NAPI resources to allocate
10616 */
10617 tg3_ints_init(tp);
10618
Matt Carlson90415472011-12-16 13:33:23 +000010619 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010620
Linus Torvalds1da177e2005-04-16 15:20:36 -070010621 /* The placement of this call is tied
10622 * to the setup and use of Host TX descriptors.
10623 */
10624 err = tg3_alloc_consistent(tp);
10625 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010626 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010627
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010628 tg3_napi_init(tp);
10629
Matt Carlsonfed97812009-09-01 13:10:19 +000010630 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010631
Matt Carlson4f125f42009-09-01 12:55:02 +000010632 for (i = 0; i < tp->irq_cnt; i++) {
10633 struct tg3_napi *tnapi = &tp->napi[i];
10634 err = tg3_request_irq(tp, i);
10635 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010636 for (i--; i >= 0; i--) {
10637 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010638 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010639 }
10640 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010641 }
10642 }
Matt Carlson07b01732009-08-28 14:01:15 +000010643
David S. Millerf47c11e2005-06-24 20:18:35 -070010644 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645
Michael Chand8f4cd32012-09-28 07:12:40 +000010646 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010648 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010649 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010650 }
10651
David S. Millerf47c11e2005-06-24 20:18:35 -070010652 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010653
Matt Carlson07b01732009-08-28 14:01:15 +000010654 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010655 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010656
Michael Chand8f4cd32012-09-28 07:12:40 +000010657 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010658 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010659
Michael Chan79381092005-04-21 17:13:59 -070010660 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010661 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010662 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010663 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010664 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010665
Matt Carlson679563f2009-09-01 12:55:46 +000010666 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010667 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010668
Joe Perches63c3a662011-04-26 08:12:10 +000010669 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010670 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010671
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010672 tw32(PCIE_TRANSACTION_CFG,
10673 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010674 }
Michael Chan79381092005-04-21 17:13:59 -070010675 }
10676
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 tg3_phy_start(tp);
10678
Michael Chanaed93e02012-07-16 16:24:02 +000010679 tg3_hwmon_open(tp);
10680
David S. Millerf47c11e2005-06-24 20:18:35 -070010681 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682
Matt Carlson21f76382012-02-22 12:35:21 +000010683 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010684 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010685 tg3_enable_ints(tp);
10686
Matt Carlsonbe947302012-12-03 19:36:57 +000010687 if (init)
10688 tg3_ptp_init(tp);
10689 else
10690 tg3_ptp_resume(tp);
10691
10692
David S. Millerf47c11e2005-06-24 20:18:35 -070010693 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010694
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010695 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010696
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010697 /*
10698 * Reset loopback feature if it was turned on while the device was down
10699 * make sure that it's installed properly now.
10700 */
10701 if (dev->features & NETIF_F_LOOPBACK)
10702 tg3_set_loopback(dev, dev->features);
10703
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010705
Matt Carlson679563f2009-09-01 12:55:46 +000010706err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010707 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10708 struct tg3_napi *tnapi = &tp->napi[i];
10709 free_irq(tnapi->irq_vec, tnapi);
10710 }
Matt Carlson07b01732009-08-28 14:01:15 +000010711
Matt Carlson679563f2009-09-01 12:55:46 +000010712err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010713 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010714 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010715 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010716
10717err_out1:
10718 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010719
Matt Carlson07b01732009-08-28 14:01:15 +000010720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010721}
10722
Michael Chan65138592012-09-28 07:12:41 +000010723static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724{
Matt Carlson4f125f42009-09-01 12:55:02 +000010725 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010726
Matt Carlsondb219972011-11-04 09:15:03 +000010727 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010728 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010729
Matt Carlson21f76382012-02-22 12:35:21 +000010730 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
Michael Chanaed93e02012-07-16 16:24:02 +000010732 tg3_hwmon_close(tp);
10733
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010734 tg3_phy_stop(tp);
10735
David S. Millerf47c11e2005-06-24 20:18:35 -070010736 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010737
10738 tg3_disable_ints(tp);
10739
Michael Chan944d9802005-05-29 14:57:48 -070010740 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010742 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743
David S. Millerf47c11e2005-06-24 20:18:35 -070010744 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010745
Matt Carlson4f125f42009-09-01 12:55:02 +000010746 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10747 struct tg3_napi *tnapi = &tp->napi[i];
10748 free_irq(tnapi->irq_vec, tnapi);
10749 }
Matt Carlson07b01732009-08-28 14:01:15 +000010750
10751 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010752
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010753 tg3_napi_fini(tp);
10754
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010756}
10757
Michael Chand8f4cd32012-09-28 07:12:40 +000010758static int tg3_open(struct net_device *dev)
10759{
10760 struct tg3 *tp = netdev_priv(dev);
10761 int err;
10762
10763 if (tp->fw_needed) {
10764 err = tg3_request_firmware(tp);
10765 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10766 if (err)
10767 return err;
10768 } else if (err) {
10769 netdev_warn(tp->dev, "TSO capability disabled\n");
10770 tg3_flag_clear(tp, TSO_CAPABLE);
10771 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10772 netdev_notice(tp->dev, "TSO capability restored\n");
10773 tg3_flag_set(tp, TSO_CAPABLE);
10774 }
10775 }
10776
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010777 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010778
10779 err = tg3_power_up(tp);
10780 if (err)
10781 return err;
10782
10783 tg3_full_lock(tp, 0);
10784
10785 tg3_disable_ints(tp);
10786 tg3_flag_clear(tp, INIT_COMPLETE);
10787
10788 tg3_full_unlock(tp);
10789
Matt Carlsonbe947302012-12-03 19:36:57 +000010790 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010791 if (err) {
10792 tg3_frob_aux_power(tp, false);
10793 pci_set_power_state(tp->pdev, PCI_D3hot);
10794 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010795
Matt Carlson7d41e492012-12-03 19:36:58 +000010796 if (tg3_flag(tp, PTP_CAPABLE)) {
10797 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10798 &tp->pdev->dev);
10799 if (IS_ERR(tp->ptp_clock))
10800 tp->ptp_clock = NULL;
10801 }
10802
Linus Torvalds1da177e2005-04-16 15:20:36 -070010803 return err;
10804}
10805
10806static int tg3_close(struct net_device *dev)
10807{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010808 struct tg3 *tp = netdev_priv(dev);
10809
Matt Carlsonbe947302012-12-03 19:36:57 +000010810 tg3_ptp_fini(tp);
10811
Michael Chan65138592012-09-28 07:12:41 +000010812 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010813
10814 /* Clear stats across close / open calls */
10815 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10816 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010817
10818 tg3_power_down(tp);
10819
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010820 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010821
10822 return 0;
10823}
10824
10825static inline u64 get_stat64(tg3_stat64_t *val)
10826{
10827 return ((u64)val->high << 32) | ((u64)val->low);
10828}
10829
10830static u64 tg3_calc_crc_errors(struct tg3 *tp)
10831{
10832 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10833
10834 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10835 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
10837 u32 val;
10838
10839 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10840 tg3_writephy(tp, MII_TG3_TEST1,
10841 val | MII_TG3_TEST1_CRC_EN);
10842 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10843 } else
10844 val = 0;
10845
10846 tp->phy_crc_errors += val;
10847
10848 return tp->phy_crc_errors;
10849 }
10850
10851 return get_stat64(&hw_stats->rx_fcs_errors);
10852}
10853
10854#define ESTAT_ADD(member) \
10855 estats->member = old_estats->member + \
10856 get_stat64(&hw_stats->member)
10857
10858static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10859{
10860 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10861 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10862
10863 ESTAT_ADD(rx_octets);
10864 ESTAT_ADD(rx_fragments);
10865 ESTAT_ADD(rx_ucast_packets);
10866 ESTAT_ADD(rx_mcast_packets);
10867 ESTAT_ADD(rx_bcast_packets);
10868 ESTAT_ADD(rx_fcs_errors);
10869 ESTAT_ADD(rx_align_errors);
10870 ESTAT_ADD(rx_xon_pause_rcvd);
10871 ESTAT_ADD(rx_xoff_pause_rcvd);
10872 ESTAT_ADD(rx_mac_ctrl_rcvd);
10873 ESTAT_ADD(rx_xoff_entered);
10874 ESTAT_ADD(rx_frame_too_long_errors);
10875 ESTAT_ADD(rx_jabbers);
10876 ESTAT_ADD(rx_undersize_packets);
10877 ESTAT_ADD(rx_in_length_errors);
10878 ESTAT_ADD(rx_out_length_errors);
10879 ESTAT_ADD(rx_64_or_less_octet_packets);
10880 ESTAT_ADD(rx_65_to_127_octet_packets);
10881 ESTAT_ADD(rx_128_to_255_octet_packets);
10882 ESTAT_ADD(rx_256_to_511_octet_packets);
10883 ESTAT_ADD(rx_512_to_1023_octet_packets);
10884 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10885 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10886 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10887 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10888 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10889
10890 ESTAT_ADD(tx_octets);
10891 ESTAT_ADD(tx_collisions);
10892 ESTAT_ADD(tx_xon_sent);
10893 ESTAT_ADD(tx_xoff_sent);
10894 ESTAT_ADD(tx_flow_control);
10895 ESTAT_ADD(tx_mac_errors);
10896 ESTAT_ADD(tx_single_collisions);
10897 ESTAT_ADD(tx_mult_collisions);
10898 ESTAT_ADD(tx_deferred);
10899 ESTAT_ADD(tx_excessive_collisions);
10900 ESTAT_ADD(tx_late_collisions);
10901 ESTAT_ADD(tx_collide_2times);
10902 ESTAT_ADD(tx_collide_3times);
10903 ESTAT_ADD(tx_collide_4times);
10904 ESTAT_ADD(tx_collide_5times);
10905 ESTAT_ADD(tx_collide_6times);
10906 ESTAT_ADD(tx_collide_7times);
10907 ESTAT_ADD(tx_collide_8times);
10908 ESTAT_ADD(tx_collide_9times);
10909 ESTAT_ADD(tx_collide_10times);
10910 ESTAT_ADD(tx_collide_11times);
10911 ESTAT_ADD(tx_collide_12times);
10912 ESTAT_ADD(tx_collide_13times);
10913 ESTAT_ADD(tx_collide_14times);
10914 ESTAT_ADD(tx_collide_15times);
10915 ESTAT_ADD(tx_ucast_packets);
10916 ESTAT_ADD(tx_mcast_packets);
10917 ESTAT_ADD(tx_bcast_packets);
10918 ESTAT_ADD(tx_carrier_sense_errors);
10919 ESTAT_ADD(tx_discards);
10920 ESTAT_ADD(tx_errors);
10921
10922 ESTAT_ADD(dma_writeq_full);
10923 ESTAT_ADD(dma_write_prioq_full);
10924 ESTAT_ADD(rxbds_empty);
10925 ESTAT_ADD(rx_discards);
10926 ESTAT_ADD(rx_errors);
10927 ESTAT_ADD(rx_threshold_hit);
10928
10929 ESTAT_ADD(dma_readq_full);
10930 ESTAT_ADD(dma_read_prioq_full);
10931 ESTAT_ADD(tx_comp_queue_full);
10932
10933 ESTAT_ADD(ring_set_send_prod_index);
10934 ESTAT_ADD(ring_status_update);
10935 ESTAT_ADD(nic_irqs);
10936 ESTAT_ADD(nic_avoided_irqs);
10937 ESTAT_ADD(nic_tx_threshold_hit);
10938
Matt Carlson4452d092011-05-19 12:12:51 +000010939 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940}
10941
Matt Carlson65ec6982012-02-28 23:33:37 +000010942static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943{
Eric Dumazet511d2222010-07-07 20:44:24 +000010944 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010945 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10946
Linus Torvalds1da177e2005-04-16 15:20:36 -070010947 stats->rx_packets = old_stats->rx_packets +
10948 get_stat64(&hw_stats->rx_ucast_packets) +
10949 get_stat64(&hw_stats->rx_mcast_packets) +
10950 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010951
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952 stats->tx_packets = old_stats->tx_packets +
10953 get_stat64(&hw_stats->tx_ucast_packets) +
10954 get_stat64(&hw_stats->tx_mcast_packets) +
10955 get_stat64(&hw_stats->tx_bcast_packets);
10956
10957 stats->rx_bytes = old_stats->rx_bytes +
10958 get_stat64(&hw_stats->rx_octets);
10959 stats->tx_bytes = old_stats->tx_bytes +
10960 get_stat64(&hw_stats->tx_octets);
10961
10962 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010963 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010964 stats->tx_errors = old_stats->tx_errors +
10965 get_stat64(&hw_stats->tx_errors) +
10966 get_stat64(&hw_stats->tx_mac_errors) +
10967 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10968 get_stat64(&hw_stats->tx_discards);
10969
10970 stats->multicast = old_stats->multicast +
10971 get_stat64(&hw_stats->rx_mcast_packets);
10972 stats->collisions = old_stats->collisions +
10973 get_stat64(&hw_stats->tx_collisions);
10974
10975 stats->rx_length_errors = old_stats->rx_length_errors +
10976 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10977 get_stat64(&hw_stats->rx_undersize_packets);
10978
10979 stats->rx_over_errors = old_stats->rx_over_errors +
10980 get_stat64(&hw_stats->rxbds_empty);
10981 stats->rx_frame_errors = old_stats->rx_frame_errors +
10982 get_stat64(&hw_stats->rx_align_errors);
10983 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10984 get_stat64(&hw_stats->tx_discards);
10985 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10986 get_stat64(&hw_stats->tx_carrier_sense_errors);
10987
10988 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010989 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010990
John W. Linville4f63b872005-09-12 14:43:18 -070010991 stats->rx_missed_errors = old_stats->rx_missed_errors +
10992 get_stat64(&hw_stats->rx_discards);
10993
Eric Dumazetb0057c52010-10-10 19:55:52 +000010994 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010995 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010996}
10997
Linus Torvalds1da177e2005-04-16 15:20:36 -070010998static int tg3_get_regs_len(struct net_device *dev)
10999{
Matt Carlson97bd8e42011-04-13 11:05:04 +000011000 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011001}
11002
11003static void tg3_get_regs(struct net_device *dev,
11004 struct ethtool_regs *regs, void *_p)
11005{
Linus Torvalds1da177e2005-04-16 15:20:36 -070011006 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011007
11008 regs->version = 0;
11009
Matt Carlson97bd8e42011-04-13 11:05:04 +000011010 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011011
Matt Carlson80096062010-08-02 11:26:06 +000011012 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011013 return;
11014
David S. Millerf47c11e2005-06-24 20:18:35 -070011015 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011016
Matt Carlson97bd8e42011-04-13 11:05:04 +000011017 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011018
David S. Millerf47c11e2005-06-24 20:18:35 -070011019 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011020}
11021
11022static int tg3_get_eeprom_len(struct net_device *dev)
11023{
11024 struct tg3 *tp = netdev_priv(dev);
11025
11026 return tp->nvram_size;
11027}
11028
Linus Torvalds1da177e2005-04-16 15:20:36 -070011029static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11030{
11031 struct tg3 *tp = netdev_priv(dev);
11032 int ret;
11033 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011034 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011035 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036
Joe Perches63c3a662011-04-26 08:12:10 +000011037 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011038 return -EINVAL;
11039
Matt Carlson80096062010-08-02 11:26:06 +000011040 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011041 return -EAGAIN;
11042
Linus Torvalds1da177e2005-04-16 15:20:36 -070011043 offset = eeprom->offset;
11044 len = eeprom->len;
11045 eeprom->len = 0;
11046
11047 eeprom->magic = TG3_EEPROM_MAGIC;
11048
11049 if (offset & 3) {
11050 /* adjustments to start on required 4 byte boundary */
11051 b_offset = offset & 3;
11052 b_count = 4 - b_offset;
11053 if (b_count > len) {
11054 /* i.e. offset=1 len=2 */
11055 b_count = len;
11056 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011057 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058 if (ret)
11059 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011060 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011061 len -= b_count;
11062 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011063 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011064 }
11065
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011066 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011067 pd = &data[eeprom->len];
11068 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011069 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011070 if (ret) {
11071 eeprom->len += i;
11072 return ret;
11073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011074 memcpy(pd + i, &val, 4);
11075 }
11076 eeprom->len += i;
11077
11078 if (len & 3) {
11079 /* read last bytes not ending on 4 byte boundary */
11080 pd = &data[eeprom->len];
11081 b_count = len & 3;
11082 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011083 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011084 if (ret)
11085 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011086 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011087 eeprom->len += b_count;
11088 }
11089 return 0;
11090}
11091
Linus Torvalds1da177e2005-04-16 15:20:36 -070011092static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11093{
11094 struct tg3 *tp = netdev_priv(dev);
11095 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011096 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011097 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011098 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011099
Matt Carlson80096062010-08-02 11:26:06 +000011100 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011101 return -EAGAIN;
11102
Joe Perches63c3a662011-04-26 08:12:10 +000011103 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011104 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011105 return -EINVAL;
11106
11107 offset = eeprom->offset;
11108 len = eeprom->len;
11109
11110 if ((b_offset = (offset & 3))) {
11111 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011112 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011113 if (ret)
11114 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115 len += b_offset;
11116 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011117 if (len < 4)
11118 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011119 }
11120
11121 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011122 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 /* adjustments to end on required 4 byte boundary */
11124 odd_len = 1;
11125 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011126 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011127 if (ret)
11128 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011129 }
11130
11131 buf = data;
11132 if (b_offset || odd_len) {
11133 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011134 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135 return -ENOMEM;
11136 if (b_offset)
11137 memcpy(buf, &start, 4);
11138 if (odd_len)
11139 memcpy(buf+len-4, &end, 4);
11140 memcpy(buf + b_offset, data, eeprom->len);
11141 }
11142
11143 ret = tg3_nvram_write_block(tp, offset, len, buf);
11144
11145 if (buf != data)
11146 kfree(buf);
11147
11148 return ret;
11149}
11150
11151static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11152{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011153 struct tg3 *tp = netdev_priv(dev);
11154
Joe Perches63c3a662011-04-26 08:12:10 +000011155 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011156 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011157 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011158 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011159 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11160 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011161 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011162
Linus Torvalds1da177e2005-04-16 15:20:36 -070011163 cmd->supported = (SUPPORTED_Autoneg);
11164
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011165 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166 cmd->supported |= (SUPPORTED_1000baseT_Half |
11167 SUPPORTED_1000baseT_Full);
11168
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011169 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011170 cmd->supported |= (SUPPORTED_100baseT_Half |
11171 SUPPORTED_100baseT_Full |
11172 SUPPORTED_10baseT_Half |
11173 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011174 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011175 cmd->port = PORT_TP;
11176 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011177 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011178 cmd->port = PORT_FIBRE;
11179 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011180
Linus Torvalds1da177e2005-04-16 15:20:36 -070011181 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011182 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11183 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11184 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11185 cmd->advertising |= ADVERTISED_Pause;
11186 } else {
11187 cmd->advertising |= ADVERTISED_Pause |
11188 ADVERTISED_Asym_Pause;
11189 }
11190 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11191 cmd->advertising |= ADVERTISED_Asym_Pause;
11192 }
11193 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011194 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011195 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011197 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011198 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11199 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11200 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11201 else
11202 cmd->eth_tp_mdix = ETH_TP_MDI;
11203 }
Matt Carlson64c22182010-10-14 10:37:44 +000011204 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011205 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11206 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011207 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011208 }
Matt Carlson882e9792009-09-01 13:21:36 +000011209 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011210 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011211 cmd->autoneg = tp->link_config.autoneg;
11212 cmd->maxtxpkt = 0;
11213 cmd->maxrxpkt = 0;
11214 return 0;
11215}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011216
Linus Torvalds1da177e2005-04-16 15:20:36 -070011217static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11218{
11219 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011220 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011221
Joe Perches63c3a662011-04-26 08:12:10 +000011222 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011223 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011224 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011225 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011226 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11227 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011228 }
11229
Matt Carlson7e5856b2009-02-25 14:23:01 +000011230 if (cmd->autoneg != AUTONEG_ENABLE &&
11231 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011232 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011233
11234 if (cmd->autoneg == AUTONEG_DISABLE &&
11235 cmd->duplex != DUPLEX_FULL &&
11236 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011237 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011238
Matt Carlson7e5856b2009-02-25 14:23:01 +000011239 if (cmd->autoneg == AUTONEG_ENABLE) {
11240 u32 mask = ADVERTISED_Autoneg |
11241 ADVERTISED_Pause |
11242 ADVERTISED_Asym_Pause;
11243
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011244 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011245 mask |= ADVERTISED_1000baseT_Half |
11246 ADVERTISED_1000baseT_Full;
11247
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011248 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011249 mask |= ADVERTISED_100baseT_Half |
11250 ADVERTISED_100baseT_Full |
11251 ADVERTISED_10baseT_Half |
11252 ADVERTISED_10baseT_Full |
11253 ADVERTISED_TP;
11254 else
11255 mask |= ADVERTISED_FIBRE;
11256
11257 if (cmd->advertising & ~mask)
11258 return -EINVAL;
11259
11260 mask &= (ADVERTISED_1000baseT_Half |
11261 ADVERTISED_1000baseT_Full |
11262 ADVERTISED_100baseT_Half |
11263 ADVERTISED_100baseT_Full |
11264 ADVERTISED_10baseT_Half |
11265 ADVERTISED_10baseT_Full);
11266
11267 cmd->advertising &= mask;
11268 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011269 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011270 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011271 return -EINVAL;
11272
11273 if (cmd->duplex != DUPLEX_FULL)
11274 return -EINVAL;
11275 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011276 if (speed != SPEED_100 &&
11277 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011278 return -EINVAL;
11279 }
11280 }
11281
David S. Millerf47c11e2005-06-24 20:18:35 -070011282 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011283
11284 tp->link_config.autoneg = cmd->autoneg;
11285 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011286 tp->link_config.advertising = (cmd->advertising |
11287 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011288 tp->link_config.speed = SPEED_UNKNOWN;
11289 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011290 } else {
11291 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011292 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011293 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011294 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011295
Linus Torvalds1da177e2005-04-16 15:20:36 -070011296 if (netif_running(dev))
11297 tg3_setup_phy(tp, 1);
11298
David S. Millerf47c11e2005-06-24 20:18:35 -070011299 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011300
Linus Torvalds1da177e2005-04-16 15:20:36 -070011301 return 0;
11302}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011303
Linus Torvalds1da177e2005-04-16 15:20:36 -070011304static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11305{
11306 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011307
Rick Jones68aad782011-11-07 13:29:27 +000011308 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11309 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11310 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11311 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011312}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011313
Linus Torvalds1da177e2005-04-16 15:20:36 -070011314static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11315{
11316 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011317
Joe Perches63c3a662011-04-26 08:12:10 +000011318 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011319 wol->supported = WAKE_MAGIC;
11320 else
11321 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011322 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011323 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011324 wol->wolopts = WAKE_MAGIC;
11325 memset(&wol->sopass, 0, sizeof(wol->sopass));
11326}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011327
Linus Torvalds1da177e2005-04-16 15:20:36 -070011328static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11329{
11330 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011331 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011332
Linus Torvalds1da177e2005-04-16 15:20:36 -070011333 if (wol->wolopts & ~WAKE_MAGIC)
11334 return -EINVAL;
11335 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011336 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011337 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011338
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011339 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11340
David S. Millerf47c11e2005-06-24 20:18:35 -070011341 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011342 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011343 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011344 else
Joe Perches63c3a662011-04-26 08:12:10 +000011345 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011346 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011347
Linus Torvalds1da177e2005-04-16 15:20:36 -070011348 return 0;
11349}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011350
Linus Torvalds1da177e2005-04-16 15:20:36 -070011351static u32 tg3_get_msglevel(struct net_device *dev)
11352{
11353 struct tg3 *tp = netdev_priv(dev);
11354 return tp->msg_enable;
11355}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011356
Linus Torvalds1da177e2005-04-16 15:20:36 -070011357static void tg3_set_msglevel(struct net_device *dev, u32 value)
11358{
11359 struct tg3 *tp = netdev_priv(dev);
11360 tp->msg_enable = value;
11361}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011362
Linus Torvalds1da177e2005-04-16 15:20:36 -070011363static int tg3_nway_reset(struct net_device *dev)
11364{
11365 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011366 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011367
Linus Torvalds1da177e2005-04-16 15:20:36 -070011368 if (!netif_running(dev))
11369 return -EAGAIN;
11370
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011371 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011372 return -EINVAL;
11373
Joe Perches63c3a662011-04-26 08:12:10 +000011374 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011375 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011376 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011377 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011378 } else {
11379 u32 bmcr;
11380
11381 spin_lock_bh(&tp->lock);
11382 r = -EINVAL;
11383 tg3_readphy(tp, MII_BMCR, &bmcr);
11384 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11385 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011386 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011387 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11388 BMCR_ANENABLE);
11389 r = 0;
11390 }
11391 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011392 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011393
Linus Torvalds1da177e2005-04-16 15:20:36 -070011394 return r;
11395}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011396
Linus Torvalds1da177e2005-04-16 15:20:36 -070011397static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11398{
11399 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011400
Matt Carlson2c49a442010-09-30 10:34:35 +000011401 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011402 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011403 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011404 else
11405 ering->rx_jumbo_max_pending = 0;
11406
11407 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011408
11409 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011410 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011411 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11412 else
11413 ering->rx_jumbo_pending = 0;
11414
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011415 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011416}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011417
Linus Torvalds1da177e2005-04-16 15:20:36 -070011418static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11419{
11420 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011421 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011422
Matt Carlson2c49a442010-09-30 10:34:35 +000011423 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11424 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011425 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11426 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011427 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011428 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011429 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011430
Michael Chanbbe832c2005-06-24 20:20:04 -070011431 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011432 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011433 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011434 irq_sync = 1;
11435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436
Michael Chanbbe832c2005-06-24 20:20:04 -070011437 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011438
Linus Torvalds1da177e2005-04-16 15:20:36 -070011439 tp->rx_pending = ering->rx_pending;
11440
Joe Perches63c3a662011-04-26 08:12:10 +000011441 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011442 tp->rx_pending > 63)
11443 tp->rx_pending = 63;
11444 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011445
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011446 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011447 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011448
11449 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011450 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011451 err = tg3_restart_hw(tp, 1);
11452 if (!err)
11453 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011454 }
11455
David S. Millerf47c11e2005-06-24 20:18:35 -070011456 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011457
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011458 if (irq_sync && !err)
11459 tg3_phy_start(tp);
11460
Michael Chanb9ec6c12006-07-25 16:37:27 -070011461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011462}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011463
Linus Torvalds1da177e2005-04-16 15:20:36 -070011464static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11465{
11466 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011467
Joe Perches63c3a662011-04-26 08:12:10 +000011468 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011469
Matt Carlson4a2db502011-12-08 14:40:17 +000011470 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011471 epause->rx_pause = 1;
11472 else
11473 epause->rx_pause = 0;
11474
Matt Carlson4a2db502011-12-08 14:40:17 +000011475 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011476 epause->tx_pause = 1;
11477 else
11478 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011479}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011480
Linus Torvalds1da177e2005-04-16 15:20:36 -070011481static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11482{
11483 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011484 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011485
Joe Perches63c3a662011-04-26 08:12:10 +000011486 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011487 u32 newadv;
11488 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011489
Matt Carlson27121682010-02-17 15:16:57 +000011490 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011491
Matt Carlson27121682010-02-17 15:16:57 +000011492 if (!(phydev->supported & SUPPORTED_Pause) ||
11493 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011494 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011495 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011496
Matt Carlson27121682010-02-17 15:16:57 +000011497 tp->link_config.flowctrl = 0;
11498 if (epause->rx_pause) {
11499 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011500
Matt Carlson27121682010-02-17 15:16:57 +000011501 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011502 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011503 newadv = ADVERTISED_Pause;
11504 } else
11505 newadv = ADVERTISED_Pause |
11506 ADVERTISED_Asym_Pause;
11507 } else if (epause->tx_pause) {
11508 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11509 newadv = ADVERTISED_Asym_Pause;
11510 } else
11511 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011512
Matt Carlson27121682010-02-17 15:16:57 +000011513 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011514 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011515 else
Joe Perches63c3a662011-04-26 08:12:10 +000011516 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011517
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011518 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011519 u32 oldadv = phydev->advertising &
11520 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11521 if (oldadv != newadv) {
11522 phydev->advertising &=
11523 ~(ADVERTISED_Pause |
11524 ADVERTISED_Asym_Pause);
11525 phydev->advertising |= newadv;
11526 if (phydev->autoneg) {
11527 /*
11528 * Always renegotiate the link to
11529 * inform our link partner of our
11530 * flow control settings, even if the
11531 * flow control is forced. Let
11532 * tg3_adjust_link() do the final
11533 * flow control setup.
11534 */
11535 return phy_start_aneg(phydev);
11536 }
11537 }
11538
11539 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011540 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011541 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011542 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011543 ~(ADVERTISED_Pause |
11544 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011545 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011546 }
11547 } else {
11548 int irq_sync = 0;
11549
11550 if (netif_running(dev)) {
11551 tg3_netif_stop(tp);
11552 irq_sync = 1;
11553 }
11554
11555 tg3_full_lock(tp, irq_sync);
11556
11557 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011558 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011559 else
Joe Perches63c3a662011-04-26 08:12:10 +000011560 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011561 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011562 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011563 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011564 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011565 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011566 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011567 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011568 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011569
11570 if (netif_running(dev)) {
11571 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11572 err = tg3_restart_hw(tp, 1);
11573 if (!err)
11574 tg3_netif_start(tp);
11575 }
11576
11577 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011579
Michael Chanb9ec6c12006-07-25 16:37:27 -070011580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011581}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011582
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011583static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011584{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011585 switch (sset) {
11586 case ETH_SS_TEST:
11587 return TG3_NUM_TEST;
11588 case ETH_SS_STATS:
11589 return TG3_NUM_STATS;
11590 default:
11591 return -EOPNOTSUPP;
11592 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011593}
11594
Matt Carlson90415472011-12-16 13:33:23 +000011595static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11596 u32 *rules __always_unused)
11597{
11598 struct tg3 *tp = netdev_priv(dev);
11599
11600 if (!tg3_flag(tp, SUPPORT_MSIX))
11601 return -EOPNOTSUPP;
11602
11603 switch (info->cmd) {
11604 case ETHTOOL_GRXRINGS:
11605 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011606 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011607 else {
11608 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011609 if (info->data > TG3_RSS_MAX_NUM_QS)
11610 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011611 }
11612
11613 /* The first interrupt vector only
11614 * handles link interrupts.
11615 */
11616 info->data -= 1;
11617 return 0;
11618
11619 default:
11620 return -EOPNOTSUPP;
11621 }
11622}
11623
11624static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11625{
11626 u32 size = 0;
11627 struct tg3 *tp = netdev_priv(dev);
11628
11629 if (tg3_flag(tp, SUPPORT_MSIX))
11630 size = TG3_RSS_INDIR_TBL_SIZE;
11631
11632 return size;
11633}
11634
11635static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11636{
11637 struct tg3 *tp = netdev_priv(dev);
11638 int i;
11639
11640 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11641 indir[i] = tp->rss_ind_tbl[i];
11642
11643 return 0;
11644}
11645
11646static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11647{
11648 struct tg3 *tp = netdev_priv(dev);
11649 size_t i;
11650
11651 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11652 tp->rss_ind_tbl[i] = indir[i];
11653
11654 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11655 return 0;
11656
11657 /* It is legal to write the indirection
11658 * table while the device is running.
11659 */
11660 tg3_full_lock(tp, 0);
11661 tg3_rss_write_indir_tbl(tp);
11662 tg3_full_unlock(tp);
11663
11664 return 0;
11665}
11666
Michael Chan09681692012-09-28 07:12:42 +000011667static void tg3_get_channels(struct net_device *dev,
11668 struct ethtool_channels *channel)
11669{
11670 struct tg3 *tp = netdev_priv(dev);
11671 u32 deflt_qs = netif_get_num_default_rss_queues();
11672
11673 channel->max_rx = tp->rxq_max;
11674 channel->max_tx = tp->txq_max;
11675
11676 if (netif_running(dev)) {
11677 channel->rx_count = tp->rxq_cnt;
11678 channel->tx_count = tp->txq_cnt;
11679 } else {
11680 if (tp->rxq_req)
11681 channel->rx_count = tp->rxq_req;
11682 else
11683 channel->rx_count = min(deflt_qs, tp->rxq_max);
11684
11685 if (tp->txq_req)
11686 channel->tx_count = tp->txq_req;
11687 else
11688 channel->tx_count = min(deflt_qs, tp->txq_max);
11689 }
11690}
11691
11692static int tg3_set_channels(struct net_device *dev,
11693 struct ethtool_channels *channel)
11694{
11695 struct tg3 *tp = netdev_priv(dev);
11696
11697 if (!tg3_flag(tp, SUPPORT_MSIX))
11698 return -EOPNOTSUPP;
11699
11700 if (channel->rx_count > tp->rxq_max ||
11701 channel->tx_count > tp->txq_max)
11702 return -EINVAL;
11703
11704 tp->rxq_req = channel->rx_count;
11705 tp->txq_req = channel->tx_count;
11706
11707 if (!netif_running(dev))
11708 return 0;
11709
11710 tg3_stop(tp);
11711
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011712 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011713
Matt Carlsonbe947302012-12-03 19:36:57 +000011714 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011715
11716 return 0;
11717}
11718
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011719static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011720{
11721 switch (stringset) {
11722 case ETH_SS_STATS:
11723 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11724 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011725 case ETH_SS_TEST:
11726 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011728 default:
11729 WARN_ON(1); /* we need a WARN() */
11730 break;
11731 }
11732}
11733
stephen hemminger81b87092011-04-04 08:43:50 +000011734static int tg3_set_phys_id(struct net_device *dev,
11735 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011736{
11737 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011738
11739 if (!netif_running(tp->dev))
11740 return -EAGAIN;
11741
stephen hemminger81b87092011-04-04 08:43:50 +000011742 switch (state) {
11743 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011744 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011745
stephen hemminger81b87092011-04-04 08:43:50 +000011746 case ETHTOOL_ID_ON:
11747 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11748 LED_CTRL_1000MBPS_ON |
11749 LED_CTRL_100MBPS_ON |
11750 LED_CTRL_10MBPS_ON |
11751 LED_CTRL_TRAFFIC_OVERRIDE |
11752 LED_CTRL_TRAFFIC_BLINK |
11753 LED_CTRL_TRAFFIC_LED);
11754 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011755
stephen hemminger81b87092011-04-04 08:43:50 +000011756 case ETHTOOL_ID_OFF:
11757 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11758 LED_CTRL_TRAFFIC_OVERRIDE);
11759 break;
Michael Chan4009a932005-09-05 17:52:54 -070011760
stephen hemminger81b87092011-04-04 08:43:50 +000011761 case ETHTOOL_ID_INACTIVE:
11762 tw32(MAC_LED_CTRL, tp->led_ctrl);
11763 break;
Michael Chan4009a932005-09-05 17:52:54 -070011764 }
stephen hemminger81b87092011-04-04 08:43:50 +000011765
Michael Chan4009a932005-09-05 17:52:54 -070011766 return 0;
11767}
11768
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011769static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011770 struct ethtool_stats *estats, u64 *tmp_stats)
11771{
11772 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011773
Matt Carlsonb546e462012-02-13 15:20:09 +000011774 if (tp->hw_stats)
11775 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11776 else
11777 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011778}
11779
Matt Carlson535a4902011-07-20 10:20:56 +000011780static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011781{
11782 int i;
11783 __be32 *buf;
11784 u32 offset = 0, len = 0;
11785 u32 magic, val;
11786
Joe Perches63c3a662011-04-26 08:12:10 +000011787 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011788 return NULL;
11789
11790 if (magic == TG3_EEPROM_MAGIC) {
11791 for (offset = TG3_NVM_DIR_START;
11792 offset < TG3_NVM_DIR_END;
11793 offset += TG3_NVM_DIRENT_SIZE) {
11794 if (tg3_nvram_read(tp, offset, &val))
11795 return NULL;
11796
11797 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11798 TG3_NVM_DIRTYPE_EXTVPD)
11799 break;
11800 }
11801
11802 if (offset != TG3_NVM_DIR_END) {
11803 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11804 if (tg3_nvram_read(tp, offset + 4, &offset))
11805 return NULL;
11806
11807 offset = tg3_nvram_logical_addr(tp, offset);
11808 }
11809 }
11810
11811 if (!offset || !len) {
11812 offset = TG3_NVM_VPD_OFF;
11813 len = TG3_NVM_VPD_LEN;
11814 }
11815
11816 buf = kmalloc(len, GFP_KERNEL);
11817 if (buf == NULL)
11818 return NULL;
11819
11820 if (magic == TG3_EEPROM_MAGIC) {
11821 for (i = 0; i < len; i += 4) {
11822 /* The data is in little-endian format in NVRAM.
11823 * Use the big-endian read routines to preserve
11824 * the byte order as it exists in NVRAM.
11825 */
11826 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11827 goto error;
11828 }
11829 } else {
11830 u8 *ptr;
11831 ssize_t cnt;
11832 unsigned int pos = 0;
11833
11834 ptr = (u8 *)&buf[0];
11835 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11836 cnt = pci_read_vpd(tp->pdev, pos,
11837 len - pos, ptr);
11838 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11839 cnt = 0;
11840 else if (cnt < 0)
11841 goto error;
11842 }
11843 if (pos != len)
11844 goto error;
11845 }
11846
Matt Carlson535a4902011-07-20 10:20:56 +000011847 *vpdlen = len;
11848
Matt Carlsonc3e94502011-04-13 11:05:08 +000011849 return buf;
11850
11851error:
11852 kfree(buf);
11853 return NULL;
11854}
11855
Michael Chan566f86a2005-05-29 14:56:58 -070011856#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011857#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11858#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11859#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011860#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11861#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011862#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011863#define NVRAM_SELFBOOT_HW_SIZE 0x20
11864#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011865
11866static int tg3_test_nvram(struct tg3 *tp)
11867{
Matt Carlson535a4902011-07-20 10:20:56 +000011868 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011869 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011870 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011871
Joe Perches63c3a662011-04-26 08:12:10 +000011872 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011873 return 0;
11874
Matt Carlsone4f34112009-02-25 14:25:00 +000011875 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011876 return -EIO;
11877
Michael Chan1b277772006-03-20 22:27:48 -080011878 if (magic == TG3_EEPROM_MAGIC)
11879 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011880 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011881 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11882 TG3_EEPROM_SB_FORMAT_1) {
11883 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11884 case TG3_EEPROM_SB_REVISION_0:
11885 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11886 break;
11887 case TG3_EEPROM_SB_REVISION_2:
11888 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11889 break;
11890 case TG3_EEPROM_SB_REVISION_3:
11891 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11892 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011893 case TG3_EEPROM_SB_REVISION_4:
11894 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11895 break;
11896 case TG3_EEPROM_SB_REVISION_5:
11897 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11898 break;
11899 case TG3_EEPROM_SB_REVISION_6:
11900 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11901 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011902 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011903 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011904 }
11905 } else
Michael Chan1b277772006-03-20 22:27:48 -080011906 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011907 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11908 size = NVRAM_SELFBOOT_HW_SIZE;
11909 else
Michael Chan1b277772006-03-20 22:27:48 -080011910 return -EIO;
11911
11912 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011913 if (buf == NULL)
11914 return -ENOMEM;
11915
Michael Chan1b277772006-03-20 22:27:48 -080011916 err = -EIO;
11917 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011918 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11919 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011920 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011921 }
Michael Chan1b277772006-03-20 22:27:48 -080011922 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011923 goto out;
11924
Michael Chan1b277772006-03-20 22:27:48 -080011925 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011926 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011927 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011928 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011929 u8 *buf8 = (u8 *) buf, csum8 = 0;
11930
Al Virob9fc7dc2007-12-17 22:59:57 -080011931 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011932 TG3_EEPROM_SB_REVISION_2) {
11933 /* For rev 2, the csum doesn't include the MBA. */
11934 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11935 csum8 += buf8[i];
11936 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11937 csum8 += buf8[i];
11938 } else {
11939 for (i = 0; i < size; i++)
11940 csum8 += buf8[i];
11941 }
Michael Chan1b277772006-03-20 22:27:48 -080011942
Adrian Bunkad96b482006-04-05 22:21:04 -070011943 if (csum8 == 0) {
11944 err = 0;
11945 goto out;
11946 }
11947
11948 err = -EIO;
11949 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011950 }
Michael Chan566f86a2005-05-29 14:56:58 -070011951
Al Virob9fc7dc2007-12-17 22:59:57 -080011952 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011953 TG3_EEPROM_MAGIC_HW) {
11954 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011955 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011956 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011957
11958 /* Separate the parity bits and the data bytes. */
11959 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11960 if ((i == 0) || (i == 8)) {
11961 int l;
11962 u8 msk;
11963
11964 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11965 parity[k++] = buf8[i] & msk;
11966 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011967 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011968 int l;
11969 u8 msk;
11970
11971 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11972 parity[k++] = buf8[i] & msk;
11973 i++;
11974
11975 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11976 parity[k++] = buf8[i] & msk;
11977 i++;
11978 }
11979 data[j++] = buf8[i];
11980 }
11981
11982 err = -EIO;
11983 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11984 u8 hw8 = hweight8(data[i]);
11985
11986 if ((hw8 & 0x1) && parity[i])
11987 goto out;
11988 else if (!(hw8 & 0x1) && !parity[i])
11989 goto out;
11990 }
11991 err = 0;
11992 goto out;
11993 }
11994
Matt Carlson01c3a392011-03-09 16:58:20 +000011995 err = -EIO;
11996
Michael Chan566f86a2005-05-29 14:56:58 -070011997 /* Bootstrap checksum at offset 0x10 */
11998 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011999 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070012000 goto out;
12001
12002 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
12003 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000012004 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000012005 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070012006
Matt Carlsonc3e94502011-04-13 11:05:08 +000012007 kfree(buf);
12008
Matt Carlson535a4902011-07-20 10:20:56 +000012009 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000012010 if (!buf)
12011 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012012
Matt Carlson535a4902011-07-20 10:20:56 +000012013 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012014 if (i > 0) {
12015 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12016 if (j < 0)
12017 goto out;
12018
Matt Carlson535a4902011-07-20 10:20:56 +000012019 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012020 goto out;
12021
12022 i += PCI_VPD_LRDT_TAG_SIZE;
12023 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12024 PCI_VPD_RO_KEYWORD_CHKSUM);
12025 if (j > 0) {
12026 u8 csum8 = 0;
12027
12028 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12029
12030 for (i = 0; i <= j; i++)
12031 csum8 += ((u8 *)buf)[i];
12032
12033 if (csum8)
12034 goto out;
12035 }
12036 }
12037
Michael Chan566f86a2005-05-29 14:56:58 -070012038 err = 0;
12039
12040out:
12041 kfree(buf);
12042 return err;
12043}
12044
Michael Chanca430072005-05-29 14:57:23 -070012045#define TG3_SERDES_TIMEOUT_SEC 2
12046#define TG3_COPPER_TIMEOUT_SEC 6
12047
12048static int tg3_test_link(struct tg3 *tp)
12049{
12050 int i, max;
12051
12052 if (!netif_running(tp->dev))
12053 return -ENODEV;
12054
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012055 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012056 max = TG3_SERDES_TIMEOUT_SEC;
12057 else
12058 max = TG3_COPPER_TIMEOUT_SEC;
12059
12060 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012061 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012062 return 0;
12063
12064 if (msleep_interruptible(1000))
12065 break;
12066 }
12067
12068 return -EIO;
12069}
12070
Michael Chana71116d2005-05-29 14:58:11 -070012071/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012072static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012073{
Michael Chanb16250e2006-09-27 16:10:14 -070012074 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012075 u32 offset, read_mask, write_mask, val, save_val, read_val;
12076 static struct {
12077 u16 offset;
12078 u16 flags;
12079#define TG3_FL_5705 0x1
12080#define TG3_FL_NOT_5705 0x2
12081#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012082#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012083 u32 read_mask;
12084 u32 write_mask;
12085 } reg_tbl[] = {
12086 /* MAC Control Registers */
12087 { MAC_MODE, TG3_FL_NOT_5705,
12088 0x00000000, 0x00ef6f8c },
12089 { MAC_MODE, TG3_FL_5705,
12090 0x00000000, 0x01ef6b8c },
12091 { MAC_STATUS, TG3_FL_NOT_5705,
12092 0x03800107, 0x00000000 },
12093 { MAC_STATUS, TG3_FL_5705,
12094 0x03800100, 0x00000000 },
12095 { MAC_ADDR_0_HIGH, 0x0000,
12096 0x00000000, 0x0000ffff },
12097 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012098 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012099 { MAC_RX_MTU_SIZE, 0x0000,
12100 0x00000000, 0x0000ffff },
12101 { MAC_TX_MODE, 0x0000,
12102 0x00000000, 0x00000070 },
12103 { MAC_TX_LENGTHS, 0x0000,
12104 0x00000000, 0x00003fff },
12105 { MAC_RX_MODE, TG3_FL_NOT_5705,
12106 0x00000000, 0x000007fc },
12107 { MAC_RX_MODE, TG3_FL_5705,
12108 0x00000000, 0x000007dc },
12109 { MAC_HASH_REG_0, 0x0000,
12110 0x00000000, 0xffffffff },
12111 { MAC_HASH_REG_1, 0x0000,
12112 0x00000000, 0xffffffff },
12113 { MAC_HASH_REG_2, 0x0000,
12114 0x00000000, 0xffffffff },
12115 { MAC_HASH_REG_3, 0x0000,
12116 0x00000000, 0xffffffff },
12117
12118 /* Receive Data and Receive BD Initiator Control Registers. */
12119 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12120 0x00000000, 0xffffffff },
12121 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12122 0x00000000, 0xffffffff },
12123 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12124 0x00000000, 0x00000003 },
12125 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12126 0x00000000, 0xffffffff },
12127 { RCVDBDI_STD_BD+0, 0x0000,
12128 0x00000000, 0xffffffff },
12129 { RCVDBDI_STD_BD+4, 0x0000,
12130 0x00000000, 0xffffffff },
12131 { RCVDBDI_STD_BD+8, 0x0000,
12132 0x00000000, 0xffff0002 },
12133 { RCVDBDI_STD_BD+0xc, 0x0000,
12134 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012135
Michael Chana71116d2005-05-29 14:58:11 -070012136 /* Receive BD Initiator Control Registers. */
12137 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12138 0x00000000, 0xffffffff },
12139 { RCVBDI_STD_THRESH, TG3_FL_5705,
12140 0x00000000, 0x000003ff },
12141 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12142 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012143
Michael Chana71116d2005-05-29 14:58:11 -070012144 /* Host Coalescing Control Registers. */
12145 { HOSTCC_MODE, TG3_FL_NOT_5705,
12146 0x00000000, 0x00000004 },
12147 { HOSTCC_MODE, TG3_FL_5705,
12148 0x00000000, 0x000000f6 },
12149 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12150 0x00000000, 0xffffffff },
12151 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12152 0x00000000, 0x000003ff },
12153 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12154 0x00000000, 0xffffffff },
12155 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12156 0x00000000, 0x000003ff },
12157 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12158 0x00000000, 0xffffffff },
12159 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12160 0x00000000, 0x000000ff },
12161 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12162 0x00000000, 0xffffffff },
12163 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12164 0x00000000, 0x000000ff },
12165 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12166 0x00000000, 0xffffffff },
12167 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12168 0x00000000, 0xffffffff },
12169 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12170 0x00000000, 0xffffffff },
12171 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12172 0x00000000, 0x000000ff },
12173 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12174 0x00000000, 0xffffffff },
12175 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12176 0x00000000, 0x000000ff },
12177 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12178 0x00000000, 0xffffffff },
12179 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12180 0x00000000, 0xffffffff },
12181 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12182 0x00000000, 0xffffffff },
12183 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12184 0x00000000, 0xffffffff },
12185 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12186 0x00000000, 0xffffffff },
12187 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12188 0xffffffff, 0x00000000 },
12189 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12190 0xffffffff, 0x00000000 },
12191
12192 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012193 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012194 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012195 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012196 0x00000000, 0x007fffff },
12197 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12198 0x00000000, 0x0000003f },
12199 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12200 0x00000000, 0x000001ff },
12201 { BUFMGR_MB_HIGH_WATER, 0x0000,
12202 0x00000000, 0x000001ff },
12203 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12204 0xffffffff, 0x00000000 },
12205 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12206 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012207
Michael Chana71116d2005-05-29 14:58:11 -070012208 /* Mailbox Registers */
12209 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12210 0x00000000, 0x000001ff },
12211 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12212 0x00000000, 0x000001ff },
12213 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12214 0x00000000, 0x000007ff },
12215 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12216 0x00000000, 0x000001ff },
12217
12218 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12219 };
12220
Michael Chanb16250e2006-09-27 16:10:14 -070012221 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012222 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012223 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012224 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012225 is_5750 = 1;
12226 }
Michael Chana71116d2005-05-29 14:58:11 -070012227
12228 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12229 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12230 continue;
12231
12232 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12233 continue;
12234
Joe Perches63c3a662011-04-26 08:12:10 +000012235 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012236 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12237 continue;
12238
Michael Chanb16250e2006-09-27 16:10:14 -070012239 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12240 continue;
12241
Michael Chana71116d2005-05-29 14:58:11 -070012242 offset = (u32) reg_tbl[i].offset;
12243 read_mask = reg_tbl[i].read_mask;
12244 write_mask = reg_tbl[i].write_mask;
12245
12246 /* Save the original register content */
12247 save_val = tr32(offset);
12248
12249 /* Determine the read-only value. */
12250 read_val = save_val & read_mask;
12251
12252 /* Write zero to the register, then make sure the read-only bits
12253 * are not changed and the read/write bits are all zeros.
12254 */
12255 tw32(offset, 0);
12256
12257 val = tr32(offset);
12258
12259 /* Test the read-only and read/write bits. */
12260 if (((val & read_mask) != read_val) || (val & write_mask))
12261 goto out;
12262
12263 /* Write ones to all the bits defined by RdMask and WrMask, then
12264 * make sure the read-only bits are not changed and the
12265 * read/write bits are all ones.
12266 */
12267 tw32(offset, read_mask | write_mask);
12268
12269 val = tr32(offset);
12270
12271 /* Test the read-only bits. */
12272 if ((val & read_mask) != read_val)
12273 goto out;
12274
12275 /* Test the read/write bits. */
12276 if ((val & write_mask) != write_mask)
12277 goto out;
12278
12279 tw32(offset, save_val);
12280 }
12281
12282 return 0;
12283
12284out:
Michael Chan9f88f292006-12-07 00:22:54 -080012285 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012286 netdev_err(tp->dev,
12287 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012288 tw32(offset, save_val);
12289 return -EIO;
12290}
12291
Michael Chan7942e1d2005-05-29 14:58:36 -070012292static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12293{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012294 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012295 int i;
12296 u32 j;
12297
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012298 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012299 for (j = 0; j < len; j += 4) {
12300 u32 val;
12301
12302 tg3_write_mem(tp, offset + j, test_pattern[i]);
12303 tg3_read_mem(tp, offset + j, &val);
12304 if (val != test_pattern[i])
12305 return -EIO;
12306 }
12307 }
12308 return 0;
12309}
12310
12311static int tg3_test_memory(struct tg3 *tp)
12312{
12313 static struct mem_entry {
12314 u32 offset;
12315 u32 len;
12316 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012317 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012318 { 0x00002000, 0x1c000},
12319 { 0xffffffff, 0x00000}
12320 }, mem_tbl_5705[] = {
12321 { 0x00000100, 0x0000c},
12322 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012323 { 0x00004000, 0x00800},
12324 { 0x00006000, 0x01000},
12325 { 0x00008000, 0x02000},
12326 { 0x00010000, 0x0e000},
12327 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012328 }, mem_tbl_5755[] = {
12329 { 0x00000200, 0x00008},
12330 { 0x00004000, 0x00800},
12331 { 0x00006000, 0x00800},
12332 { 0x00008000, 0x02000},
12333 { 0x00010000, 0x0c000},
12334 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012335 }, mem_tbl_5906[] = {
12336 { 0x00000200, 0x00008},
12337 { 0x00004000, 0x00400},
12338 { 0x00006000, 0x00400},
12339 { 0x00008000, 0x01000},
12340 { 0x00010000, 0x01000},
12341 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012342 }, mem_tbl_5717[] = {
12343 { 0x00000200, 0x00008},
12344 { 0x00010000, 0x0a000},
12345 { 0x00020000, 0x13c00},
12346 { 0xffffffff, 0x00000}
12347 }, mem_tbl_57765[] = {
12348 { 0x00000200, 0x00008},
12349 { 0x00004000, 0x00800},
12350 { 0x00006000, 0x09800},
12351 { 0x00010000, 0x0a000},
12352 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012353 };
12354 struct mem_entry *mem_tbl;
12355 int err = 0;
12356 int i;
12357
Joe Perches63c3a662011-04-26 08:12:10 +000012358 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012359 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000012360 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012361 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012362 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012363 mem_tbl = mem_tbl_5755;
12364 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12365 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012366 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012367 mem_tbl = mem_tbl_5705;
12368 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012369 mem_tbl = mem_tbl_570x;
12370
12371 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012372 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12373 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012374 break;
12375 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012376
Michael Chan7942e1d2005-05-29 14:58:36 -070012377 return err;
12378}
12379
Matt Carlsonbb158d62011-04-25 12:42:47 +000012380#define TG3_TSO_MSS 500
12381
12382#define TG3_TSO_IP_HDR_LEN 20
12383#define TG3_TSO_TCP_HDR_LEN 20
12384#define TG3_TSO_TCP_OPT_LEN 12
12385
12386static const u8 tg3_tso_header[] = {
123870x08, 0x00,
123880x45, 0x00, 0x00, 0x00,
123890x00, 0x00, 0x40, 0x00,
123900x40, 0x06, 0x00, 0x00,
123910x0a, 0x00, 0x00, 0x01,
123920x0a, 0x00, 0x00, 0x02,
123930x0d, 0x00, 0xe0, 0x00,
123940x00, 0x00, 0x01, 0x00,
123950x00, 0x00, 0x02, 0x00,
123960x80, 0x10, 0x10, 0x00,
123970x14, 0x09, 0x00, 0x00,
123980x01, 0x01, 0x08, 0x0a,
123990x11, 0x11, 0x11, 0x11,
124000x11, 0x11, 0x11, 0x11,
12401};
Michael Chan9f40dea2005-09-05 17:53:06 -070012402
Matt Carlson28a45952011-08-19 13:58:22 +000012403static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012404{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012405 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012406 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012407 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012408 struct sk_buff *skb;
12409 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012410 dma_addr_t map;
12411 int num_pkts, tx_len, rx_len, i, err;
12412 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012413 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012414 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012415
Matt Carlsonc8873402010-02-12 14:47:11 +000012416 tnapi = &tp->napi[0];
12417 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012418 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012419 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012420 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012421 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012422 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012423 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012424 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012425
Michael Chanc76949a2005-05-29 14:58:59 -070012426 err = -EIO;
12427
Matt Carlson4852a862011-04-13 11:05:07 +000012428 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012429 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012430 if (!skb)
12431 return -ENOMEM;
12432
Michael Chanc76949a2005-05-29 14:58:59 -070012433 tx_data = skb_put(skb, tx_len);
12434 memcpy(tx_data, tp->dev->dev_addr, 6);
12435 memset(tx_data + 6, 0x0, 8);
12436
Matt Carlson4852a862011-04-13 11:05:07 +000012437 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012438
Matt Carlson28a45952011-08-19 13:58:22 +000012439 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012440 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12441
12442 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12443 TG3_TSO_TCP_OPT_LEN;
12444
12445 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12446 sizeof(tg3_tso_header));
12447 mss = TG3_TSO_MSS;
12448
12449 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12450 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12451
12452 /* Set the total length field in the IP header */
12453 iph->tot_len = htons((u16)(mss + hdr_len));
12454
12455 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12456 TXD_FLAG_CPU_POST_DMA);
12457
Joe Perches63c3a662011-04-26 08:12:10 +000012458 if (tg3_flag(tp, HW_TSO_1) ||
12459 tg3_flag(tp, HW_TSO_2) ||
12460 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012461 struct tcphdr *th;
12462 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12463 th = (struct tcphdr *)&tx_data[val];
12464 th->check = 0;
12465 } else
12466 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12467
Joe Perches63c3a662011-04-26 08:12:10 +000012468 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012469 mss |= (hdr_len & 0xc) << 12;
12470 if (hdr_len & 0x10)
12471 base_flags |= 0x00000010;
12472 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012473 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012474 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012475 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000012476 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
12477 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12478 } else {
12479 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12480 }
12481
12482 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12483 } else {
12484 num_pkts = 1;
12485 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012486
12487 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12488 tx_len > VLAN_ETH_FRAME_LEN)
12489 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012490 }
12491
12492 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012493 tx_data[i] = (u8) (i & 0xff);
12494
Alexander Duyckf4188d82009-12-02 16:48:38 +000012495 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12496 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012497 dev_kfree_skb(skb);
12498 return -EIO;
12499 }
Michael Chanc76949a2005-05-29 14:58:59 -070012500
Matt Carlson0d681b22011-07-27 14:20:49 +000012501 val = tnapi->tx_prod;
12502 tnapi->tx_buffers[val].skb = skb;
12503 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12504
Michael Chanc76949a2005-05-29 14:58:59 -070012505 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012506 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012507
12508 udelay(10);
12509
Matt Carlson898a56f2009-08-28 14:02:40 +000012510 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012511
Matt Carlson84b67b22011-07-27 14:20:52 +000012512 budget = tg3_tx_avail(tnapi);
12513 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012514 base_flags | TXD_FLAG_END, mss, 0)) {
12515 tnapi->tx_buffers[val].skb = NULL;
12516 dev_kfree_skb(skb);
12517 return -EIO;
12518 }
Michael Chanc76949a2005-05-29 14:58:59 -070012519
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012520 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012521
Michael Chan6541b802012-03-04 14:48:14 +000012522 /* Sync BD data before updating mailbox */
12523 wmb();
12524
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012525 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12526 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012527
12528 udelay(10);
12529
Matt Carlson303fc922009-11-02 14:27:34 +000012530 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12531 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012532 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012533 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012534
12535 udelay(10);
12536
Matt Carlson898a56f2009-08-28 14:02:40 +000012537 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12538 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012539 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012540 (rx_idx == (rx_start_idx + num_pkts)))
12541 break;
12542 }
12543
Matt Carlsonba1142e2011-11-04 09:15:00 +000012544 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012545 dev_kfree_skb(skb);
12546
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012547 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012548 goto out;
12549
12550 if (rx_idx != rx_start_idx + num_pkts)
12551 goto out;
12552
Matt Carlsonbb158d62011-04-25 12:42:47 +000012553 val = data_off;
12554 while (rx_idx != rx_start_idx) {
12555 desc = &rnapi->rx_rcb[rx_start_idx++];
12556 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12557 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012558
Matt Carlsonbb158d62011-04-25 12:42:47 +000012559 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12560 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012561 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012562
Matt Carlsonbb158d62011-04-25 12:42:47 +000012563 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12564 - ETH_FCS_LEN;
12565
Matt Carlson28a45952011-08-19 13:58:22 +000012566 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012567 if (rx_len != tx_len)
12568 goto out;
12569
12570 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12571 if (opaque_key != RXD_OPAQUE_RING_STD)
12572 goto out;
12573 } else {
12574 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12575 goto out;
12576 }
12577 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12578 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012579 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012580 goto out;
12581 }
12582
12583 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012584 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012585 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12586 mapping);
12587 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012588 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012589 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12590 mapping);
12591 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012592 goto out;
12593
Matt Carlsonbb158d62011-04-25 12:42:47 +000012594 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12595 PCI_DMA_FROMDEVICE);
12596
Eric Dumazet9205fd92011-11-18 06:47:01 +000012597 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012598 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012599 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012600 goto out;
12601 }
Matt Carlson4852a862011-04-13 11:05:07 +000012602 }
12603
Michael Chanc76949a2005-05-29 14:58:59 -070012604 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012605
Eric Dumazet9205fd92011-11-18 06:47:01 +000012606 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012607out:
12608 return err;
12609}
12610
Matt Carlson00c266b2011-04-25 12:42:46 +000012611#define TG3_STD_LOOPBACK_FAILED 1
12612#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012613#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012614#define TG3_LOOPBACK_FAILED \
12615 (TG3_STD_LOOPBACK_FAILED | \
12616 TG3_JMB_LOOPBACK_FAILED | \
12617 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012618
Matt Carlson941ec902011-08-19 13:58:23 +000012619static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012620{
Matt Carlson28a45952011-08-19 13:58:22 +000012621 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012622 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012623 u32 jmb_pkt_sz = 9000;
12624
12625 if (tp->dma_limit)
12626 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012627
Matt Carlsonab789042011-01-25 15:58:54 +000012628 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12629 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12630
Matt Carlson28a45952011-08-19 13:58:22 +000012631 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012632 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12633 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012634 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012635 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012636 goto done;
12637 }
12638
Michael Chanb9ec6c12006-07-25 16:37:27 -070012639 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012640 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012641 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12642 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012643 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012644 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012645 goto done;
12646 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012647
Joe Perches63c3a662011-04-26 08:12:10 +000012648 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012649 int i;
12650
12651 /* Reroute all rx packets to the 1st queue */
12652 for (i = MAC_RSS_INDIR_TBL_0;
12653 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12654 tw32(i, 0x0);
12655 }
12656
Matt Carlson6e01b202011-08-19 13:58:20 +000012657 /* HW errata - mac loopback fails in some cases on 5780.
12658 * Normal traffic and PHY loopback are not affected by
12659 * errata. Also, the MAC loopback test is deprecated for
12660 * all newer ASIC revisions.
12661 */
12662 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12663 !tg3_flag(tp, CPMU_PRESENT)) {
12664 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012665
Matt Carlson28a45952011-08-19 13:58:22 +000012666 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012667 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012668
12669 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012670 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012671 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012672
12673 tg3_mac_loopback(tp, false);
12674 }
Matt Carlson4852a862011-04-13 11:05:07 +000012675
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012676 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012677 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012678 int i;
12679
Matt Carlson941ec902011-08-19 13:58:23 +000012680 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012681
12682 /* Wait for link */
12683 for (i = 0; i < 100; i++) {
12684 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12685 break;
12686 mdelay(1);
12687 }
12688
Matt Carlson28a45952011-08-19 13:58:22 +000012689 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012690 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012691 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012692 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012693 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012694 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012695 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012696 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012697
Matt Carlson941ec902011-08-19 13:58:23 +000012698 if (do_extlpbk) {
12699 tg3_phy_lpbk_set(tp, 0, true);
12700
12701 /* All link indications report up, but the hardware
12702 * isn't really ready for about 20 msec. Double it
12703 * to be sure.
12704 */
12705 mdelay(40);
12706
12707 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012708 data[TG3_EXT_LOOPB_TEST] |=
12709 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012710 if (tg3_flag(tp, TSO_CAPABLE) &&
12711 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012712 data[TG3_EXT_LOOPB_TEST] |=
12713 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012714 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012715 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012716 data[TG3_EXT_LOOPB_TEST] |=
12717 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012718 }
12719
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012720 /* Re-enable gphy autopowerdown. */
12721 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12722 tg3_phy_toggle_apd(tp, true);
12723 }
Matt Carlson6833c042008-11-21 17:18:59 -080012724
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012725 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12726 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012727
Matt Carlsonab789042011-01-25 15:58:54 +000012728done:
12729 tp->phy_flags |= eee_cap;
12730
Michael Chan9f40dea2005-09-05 17:53:06 -070012731 return err;
12732}
12733
Michael Chan4cafd3f2005-05-29 14:56:34 -070012734static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12735 u64 *data)
12736{
Michael Chan566f86a2005-05-29 14:56:58 -070012737 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012738 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012739
Matt Carlsonbed98292011-07-13 09:27:29 +000012740 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12741 tg3_power_up(tp)) {
12742 etest->flags |= ETH_TEST_FL_FAILED;
12743 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12744 return;
12745 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012746
Michael Chan566f86a2005-05-29 14:56:58 -070012747 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12748
12749 if (tg3_test_nvram(tp) != 0) {
12750 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012751 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012752 }
Matt Carlson941ec902011-08-19 13:58:23 +000012753 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012754 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012755 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012756 }
Michael Chana71116d2005-05-29 14:58:11 -070012757 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012758 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012759
Michael Chanbbe832c2005-06-24 20:20:04 -070012760 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012761 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012762 tg3_netif_stop(tp);
12763 irq_sync = 1;
12764 }
12765
12766 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012767 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012768 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012769 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012770 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012771 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012772 if (!err)
12773 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012774
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012775 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012776 tg3_phy_reset(tp);
12777
Michael Chana71116d2005-05-29 14:58:11 -070012778 if (tg3_test_registers(tp) != 0) {
12779 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012780 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012781 }
Matt Carlson28a45952011-08-19 13:58:22 +000012782
Michael Chan7942e1d2005-05-29 14:58:36 -070012783 if (tg3_test_memory(tp) != 0) {
12784 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012785 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012786 }
Matt Carlson28a45952011-08-19 13:58:22 +000012787
Matt Carlson941ec902011-08-19 13:58:23 +000012788 if (doextlpbk)
12789 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12790
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012791 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012792 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012793
David S. Millerf47c11e2005-06-24 20:18:35 -070012794 tg3_full_unlock(tp);
12795
Michael Chand4bc3922005-05-29 14:59:20 -070012796 if (tg3_test_interrupt(tp) != 0) {
12797 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012798 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012799 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012800
12801 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012802
Michael Chana71116d2005-05-29 14:58:11 -070012803 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12804 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012805 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012806 err2 = tg3_restart_hw(tp, 1);
12807 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012808 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012809 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012810
12811 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012812
12813 if (irq_sync && !err2)
12814 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012815 }
Matt Carlson80096062010-08-02 11:26:06 +000012816 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012817 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012818
Michael Chan4cafd3f2005-05-29 14:56:34 -070012819}
12820
Matt Carlson0a633ac2012-12-03 19:36:59 +000012821static int tg3_hwtstamp_ioctl(struct net_device *dev,
12822 struct ifreq *ifr, int cmd)
12823{
12824 struct tg3 *tp = netdev_priv(dev);
12825 struct hwtstamp_config stmpconf;
12826
12827 if (!tg3_flag(tp, PTP_CAPABLE))
12828 return -EINVAL;
12829
12830 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
12831 return -EFAULT;
12832
12833 if (stmpconf.flags)
12834 return -EINVAL;
12835
12836 switch (stmpconf.tx_type) {
12837 case HWTSTAMP_TX_ON:
12838 tg3_flag_set(tp, TX_TSTAMP_EN);
12839 break;
12840 case HWTSTAMP_TX_OFF:
12841 tg3_flag_clear(tp, TX_TSTAMP_EN);
12842 break;
12843 default:
12844 return -ERANGE;
12845 }
12846
12847 switch (stmpconf.rx_filter) {
12848 case HWTSTAMP_FILTER_NONE:
12849 tp->rxptpctl = 0;
12850 break;
12851 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
12852 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12853 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
12854 break;
12855 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
12856 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12857 TG3_RX_PTP_CTL_SYNC_EVNT;
12858 break;
12859 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12860 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12861 TG3_RX_PTP_CTL_DELAY_REQ;
12862 break;
12863 case HWTSTAMP_FILTER_PTP_V2_EVENT:
12864 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12865 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12866 break;
12867 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12868 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12869 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12870 break;
12871 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12872 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12873 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12874 break;
12875 case HWTSTAMP_FILTER_PTP_V2_SYNC:
12876 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12877 TG3_RX_PTP_CTL_SYNC_EVNT;
12878 break;
12879 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
12880 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12881 TG3_RX_PTP_CTL_SYNC_EVNT;
12882 break;
12883 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
12884 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12885 TG3_RX_PTP_CTL_SYNC_EVNT;
12886 break;
12887 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
12888 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12889 TG3_RX_PTP_CTL_DELAY_REQ;
12890 break;
12891 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
12892 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12893 TG3_RX_PTP_CTL_DELAY_REQ;
12894 break;
12895 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
12896 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12897 TG3_RX_PTP_CTL_DELAY_REQ;
12898 break;
12899 default:
12900 return -ERANGE;
12901 }
12902
12903 if (netif_running(dev) && tp->rxptpctl)
12904 tw32(TG3_RX_PTP_CTL,
12905 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
12906
12907 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
12908 -EFAULT : 0;
12909}
12910
Linus Torvalds1da177e2005-04-16 15:20:36 -070012911static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12912{
12913 struct mii_ioctl_data *data = if_mii(ifr);
12914 struct tg3 *tp = netdev_priv(dev);
12915 int err;
12916
Joe Perches63c3a662011-04-26 08:12:10 +000012917 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012918 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012919 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012920 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012921 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012922 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012923 }
12924
Matt Carlson33f401a2010-04-05 10:19:27 +000012925 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012926 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012927 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012928
12929 /* fallthru */
12930 case SIOCGMIIREG: {
12931 u32 mii_regval;
12932
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012933 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 break; /* We have no PHY */
12935
Matt Carlson34eea5a2011-04-20 07:57:38 +000012936 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012937 return -EAGAIN;
12938
David S. Millerf47c11e2005-06-24 20:18:35 -070012939 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012940 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012941 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012942
12943 data->val_out = mii_regval;
12944
12945 return err;
12946 }
12947
12948 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012949 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012950 break; /* We have no PHY */
12951
Matt Carlson34eea5a2011-04-20 07:57:38 +000012952 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012953 return -EAGAIN;
12954
David S. Millerf47c11e2005-06-24 20:18:35 -070012955 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012957 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958
12959 return err;
12960
Matt Carlson0a633ac2012-12-03 19:36:59 +000012961 case SIOCSHWTSTAMP:
12962 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
12963
Linus Torvalds1da177e2005-04-16 15:20:36 -070012964 default:
12965 /* do nothing */
12966 break;
12967 }
12968 return -EOPNOTSUPP;
12969}
12970
David S. Miller15f98502005-05-18 22:49:26 -070012971static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12972{
12973 struct tg3 *tp = netdev_priv(dev);
12974
12975 memcpy(ec, &tp->coal, sizeof(*ec));
12976 return 0;
12977}
12978
Michael Chand244c892005-07-05 14:42:33 -070012979static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12980{
12981 struct tg3 *tp = netdev_priv(dev);
12982 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12983 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12984
Joe Perches63c3a662011-04-26 08:12:10 +000012985 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012986 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12987 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12988 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12989 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12990 }
12991
12992 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12993 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12994 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12995 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12996 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12997 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12998 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12999 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
13000 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
13001 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
13002 return -EINVAL;
13003
13004 /* No rx interrupts will be generated if both are zero */
13005 if ((ec->rx_coalesce_usecs == 0) &&
13006 (ec->rx_max_coalesced_frames == 0))
13007 return -EINVAL;
13008
13009 /* No tx interrupts will be generated if both are zero */
13010 if ((ec->tx_coalesce_usecs == 0) &&
13011 (ec->tx_max_coalesced_frames == 0))
13012 return -EINVAL;
13013
13014 /* Only copy relevant parameters, ignore all others. */
13015 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13016 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13017 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13018 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13019 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13020 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13021 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13022 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13023 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13024
13025 if (netif_running(dev)) {
13026 tg3_full_lock(tp, 0);
13027 __tg3_set_coalesce(tp, &tp->coal);
13028 tg3_full_unlock(tp);
13029 }
13030 return 0;
13031}
13032
Jeff Garzik7282d492006-09-13 14:30:00 -040013033static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013034 .get_settings = tg3_get_settings,
13035 .set_settings = tg3_set_settings,
13036 .get_drvinfo = tg3_get_drvinfo,
13037 .get_regs_len = tg3_get_regs_len,
13038 .get_regs = tg3_get_regs,
13039 .get_wol = tg3_get_wol,
13040 .set_wol = tg3_set_wol,
13041 .get_msglevel = tg3_get_msglevel,
13042 .set_msglevel = tg3_set_msglevel,
13043 .nway_reset = tg3_nway_reset,
13044 .get_link = ethtool_op_get_link,
13045 .get_eeprom_len = tg3_get_eeprom_len,
13046 .get_eeprom = tg3_get_eeprom,
13047 .set_eeprom = tg3_set_eeprom,
13048 .get_ringparam = tg3_get_ringparam,
13049 .set_ringparam = tg3_set_ringparam,
13050 .get_pauseparam = tg3_get_pauseparam,
13051 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013052 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013053 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013054 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013056 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013057 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013058 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013059 .get_rxnfc = tg3_get_rxnfc,
13060 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13061 .get_rxfh_indir = tg3_get_rxfh_indir,
13062 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013063 .get_channels = tg3_get_channels,
13064 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013065 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013066};
13067
David S. Millerb4017c52012-03-01 17:57:40 -050013068static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13069 struct rtnl_link_stats64 *stats)
13070{
13071 struct tg3 *tp = netdev_priv(dev);
13072
David S. Millerb4017c52012-03-01 17:57:40 -050013073 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013074 if (!tp->hw_stats) {
13075 spin_unlock_bh(&tp->lock);
13076 return &tp->net_stats_prev;
13077 }
13078
David S. Millerb4017c52012-03-01 17:57:40 -050013079 tg3_get_nstats(tp, stats);
13080 spin_unlock_bh(&tp->lock);
13081
13082 return stats;
13083}
13084
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013085static void tg3_set_rx_mode(struct net_device *dev)
13086{
13087 struct tg3 *tp = netdev_priv(dev);
13088
13089 if (!netif_running(dev))
13090 return;
13091
13092 tg3_full_lock(tp, 0);
13093 __tg3_set_rx_mode(dev);
13094 tg3_full_unlock(tp);
13095}
13096
Matt Carlsonfaf16272012-02-13 10:20:07 +000013097static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13098 int new_mtu)
13099{
13100 dev->mtu = new_mtu;
13101
13102 if (new_mtu > ETH_DATA_LEN) {
13103 if (tg3_flag(tp, 5780_CLASS)) {
13104 netdev_update_features(dev);
13105 tg3_flag_clear(tp, TSO_CAPABLE);
13106 } else {
13107 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13108 }
13109 } else {
13110 if (tg3_flag(tp, 5780_CLASS)) {
13111 tg3_flag_set(tp, TSO_CAPABLE);
13112 netdev_update_features(dev);
13113 }
13114 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13115 }
13116}
13117
13118static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13119{
13120 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013121 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013122
13123 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13124 return -EINVAL;
13125
13126 if (!netif_running(dev)) {
13127 /* We'll just catch it later when the
13128 * device is up'd.
13129 */
13130 tg3_set_mtu(dev, tp, new_mtu);
13131 return 0;
13132 }
13133
13134 tg3_phy_stop(tp);
13135
13136 tg3_netif_stop(tp);
13137
13138 tg3_full_lock(tp, 1);
13139
13140 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13141
13142 tg3_set_mtu(dev, tp, new_mtu);
13143
Michael Chan2fae5e32012-03-04 14:48:15 +000013144 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13145 * breaks all requests to 256 bytes.
13146 */
13147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13148 reset_phy = 1;
13149
13150 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013151
13152 if (!err)
13153 tg3_netif_start(tp);
13154
13155 tg3_full_unlock(tp);
13156
13157 if (!err)
13158 tg3_phy_start(tp);
13159
13160 return err;
13161}
13162
13163static const struct net_device_ops tg3_netdev_ops = {
13164 .ndo_open = tg3_open,
13165 .ndo_stop = tg3_close,
13166 .ndo_start_xmit = tg3_start_xmit,
13167 .ndo_get_stats64 = tg3_get_stats64,
13168 .ndo_validate_addr = eth_validate_addr,
13169 .ndo_set_rx_mode = tg3_set_rx_mode,
13170 .ndo_set_mac_address = tg3_set_mac_addr,
13171 .ndo_do_ioctl = tg3_ioctl,
13172 .ndo_tx_timeout = tg3_tx_timeout,
13173 .ndo_change_mtu = tg3_change_mtu,
13174 .ndo_fix_features = tg3_fix_features,
13175 .ndo_set_features = tg3_set_features,
13176#ifdef CONFIG_NET_POLL_CONTROLLER
13177 .ndo_poll_controller = tg3_poll_controller,
13178#endif
13179};
13180
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013181static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013182{
Michael Chan1b277772006-03-20 22:27:48 -080013183 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184
13185 tp->nvram_size = EEPROM_CHIP_SIZE;
13186
Matt Carlsone4f34112009-02-25 14:25:00 +000013187 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013188 return;
13189
Michael Chanb16250e2006-09-27 16:10:14 -070013190 if ((magic != TG3_EEPROM_MAGIC) &&
13191 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13192 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013193 return;
13194
13195 /*
13196 * Size the chip by reading offsets at increasing powers of two.
13197 * When we encounter our validation signature, we know the addressing
13198 * has wrapped around, and thus have our chip size.
13199 */
Michael Chan1b277772006-03-20 22:27:48 -080013200 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013201
13202 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013203 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204 return;
13205
Michael Chan18201802006-03-20 22:29:15 -080013206 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013207 break;
13208
13209 cursize <<= 1;
13210 }
13211
13212 tp->nvram_size = cursize;
13213}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013214
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013215static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013216{
13217 u32 val;
13218
Joe Perches63c3a662011-04-26 08:12:10 +000013219 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013220 return;
13221
13222 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013223 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013224 tg3_get_eeprom_size(tp);
13225 return;
13226 }
13227
Matt Carlson6d348f22009-02-25 14:25:52 +000013228 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013229 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013230 /* This is confusing. We want to operate on the
13231 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13232 * call will read from NVRAM and byteswap the data
13233 * according to the byteswapping settings for all
13234 * other register accesses. This ensures the data we
13235 * want will always reside in the lower 16-bits.
13236 * However, the data in NVRAM is in LE format, which
13237 * means the data from the NVRAM read will always be
13238 * opposite the endianness of the CPU. The 16-bit
13239 * byteswap then brings the data to CPU endianness.
13240 */
13241 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013242 return;
13243 }
13244 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013245 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013246}
13247
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013248static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013249{
13250 u32 nvcfg1;
13251
13252 nvcfg1 = tr32(NVRAM_CFG1);
13253 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013254 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013255 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013256 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13257 tw32(NVRAM_CFG1, nvcfg1);
13258 }
13259
Matt Carlson6ff6f812011-05-19 12:12:54 +000013260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013261 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013262 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013263 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13264 tp->nvram_jedecnum = JEDEC_ATMEL;
13265 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013266 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013267 break;
13268 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13269 tp->nvram_jedecnum = JEDEC_ATMEL;
13270 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13271 break;
13272 case FLASH_VENDOR_ATMEL_EEPROM:
13273 tp->nvram_jedecnum = JEDEC_ATMEL;
13274 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013275 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013276 break;
13277 case FLASH_VENDOR_ST:
13278 tp->nvram_jedecnum = JEDEC_ST;
13279 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013280 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013281 break;
13282 case FLASH_VENDOR_SAIFUN:
13283 tp->nvram_jedecnum = JEDEC_SAIFUN;
13284 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13285 break;
13286 case FLASH_VENDOR_SST_SMALL:
13287 case FLASH_VENDOR_SST_LARGE:
13288 tp->nvram_jedecnum = JEDEC_SST;
13289 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013291 }
Matt Carlson8590a602009-08-28 12:29:16 +000013292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013293 tp->nvram_jedecnum = JEDEC_ATMEL;
13294 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013295 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013296 }
13297}
13298
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013299static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013300{
13301 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13302 case FLASH_5752PAGE_SIZE_256:
13303 tp->nvram_pagesize = 256;
13304 break;
13305 case FLASH_5752PAGE_SIZE_512:
13306 tp->nvram_pagesize = 512;
13307 break;
13308 case FLASH_5752PAGE_SIZE_1K:
13309 tp->nvram_pagesize = 1024;
13310 break;
13311 case FLASH_5752PAGE_SIZE_2K:
13312 tp->nvram_pagesize = 2048;
13313 break;
13314 case FLASH_5752PAGE_SIZE_4K:
13315 tp->nvram_pagesize = 4096;
13316 break;
13317 case FLASH_5752PAGE_SIZE_264:
13318 tp->nvram_pagesize = 264;
13319 break;
13320 case FLASH_5752PAGE_SIZE_528:
13321 tp->nvram_pagesize = 528;
13322 break;
13323 }
13324}
13325
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013326static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013327{
13328 u32 nvcfg1;
13329
13330 nvcfg1 = tr32(NVRAM_CFG1);
13331
Michael Chane6af3012005-04-21 17:12:05 -070013332 /* NVRAM protection for TPM */
13333 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013334 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013335
Michael Chan361b4ac2005-04-21 17:11:21 -070013336 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013337 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13338 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13339 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013340 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013341 break;
13342 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13343 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013344 tg3_flag_set(tp, NVRAM_BUFFERED);
13345 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013346 break;
13347 case FLASH_5752VENDOR_ST_M45PE10:
13348 case FLASH_5752VENDOR_ST_M45PE20:
13349 case FLASH_5752VENDOR_ST_M45PE40:
13350 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013351 tg3_flag_set(tp, NVRAM_BUFFERED);
13352 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013353 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013354 }
13355
Joe Perches63c3a662011-04-26 08:12:10 +000013356 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013357 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013358 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013359 /* For eeprom, set pagesize to maximum eeprom size */
13360 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13361
13362 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13363 tw32(NVRAM_CFG1, nvcfg1);
13364 }
13365}
13366
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013367static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013368{
Matt Carlson989a9d22007-05-05 11:51:05 -070013369 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013370
13371 nvcfg1 = tr32(NVRAM_CFG1);
13372
13373 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013374 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013375 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013376 protect = 1;
13377 }
Michael Chand3c7b882006-03-23 01:28:25 -080013378
Matt Carlson989a9d22007-05-05 11:51:05 -070013379 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13380 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013381 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13382 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13383 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13384 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13385 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013386 tg3_flag_set(tp, NVRAM_BUFFERED);
13387 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013388 tp->nvram_pagesize = 264;
13389 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13390 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13391 tp->nvram_size = (protect ? 0x3e200 :
13392 TG3_NVRAM_SIZE_512KB);
13393 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13394 tp->nvram_size = (protect ? 0x1f200 :
13395 TG3_NVRAM_SIZE_256KB);
13396 else
13397 tp->nvram_size = (protect ? 0x1f200 :
13398 TG3_NVRAM_SIZE_128KB);
13399 break;
13400 case FLASH_5752VENDOR_ST_M45PE10:
13401 case FLASH_5752VENDOR_ST_M45PE20:
13402 case FLASH_5752VENDOR_ST_M45PE40:
13403 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013404 tg3_flag_set(tp, NVRAM_BUFFERED);
13405 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013406 tp->nvram_pagesize = 256;
13407 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13408 tp->nvram_size = (protect ?
13409 TG3_NVRAM_SIZE_64KB :
13410 TG3_NVRAM_SIZE_128KB);
13411 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13412 tp->nvram_size = (protect ?
13413 TG3_NVRAM_SIZE_64KB :
13414 TG3_NVRAM_SIZE_256KB);
13415 else
13416 tp->nvram_size = (protect ?
13417 TG3_NVRAM_SIZE_128KB :
13418 TG3_NVRAM_SIZE_512KB);
13419 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013420 }
13421}
13422
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013423static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013424{
13425 u32 nvcfg1;
13426
13427 nvcfg1 = tr32(NVRAM_CFG1);
13428
13429 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013430 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13431 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13432 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13433 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13434 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013435 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013436 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013437
Matt Carlson8590a602009-08-28 12:29:16 +000013438 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13439 tw32(NVRAM_CFG1, nvcfg1);
13440 break;
13441 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13442 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13443 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13444 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13445 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013446 tg3_flag_set(tp, NVRAM_BUFFERED);
13447 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013448 tp->nvram_pagesize = 264;
13449 break;
13450 case FLASH_5752VENDOR_ST_M45PE10:
13451 case FLASH_5752VENDOR_ST_M45PE20:
13452 case FLASH_5752VENDOR_ST_M45PE40:
13453 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013454 tg3_flag_set(tp, NVRAM_BUFFERED);
13455 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013456 tp->nvram_pagesize = 256;
13457 break;
Michael Chan1b277772006-03-20 22:27:48 -080013458 }
13459}
13460
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013461static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013462{
13463 u32 nvcfg1, protect = 0;
13464
13465 nvcfg1 = tr32(NVRAM_CFG1);
13466
13467 /* NVRAM protection for TPM */
13468 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013469 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013470 protect = 1;
13471 }
13472
13473 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13474 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013475 case FLASH_5761VENDOR_ATMEL_ADB021D:
13476 case FLASH_5761VENDOR_ATMEL_ADB041D:
13477 case FLASH_5761VENDOR_ATMEL_ADB081D:
13478 case FLASH_5761VENDOR_ATMEL_ADB161D:
13479 case FLASH_5761VENDOR_ATMEL_MDB021D:
13480 case FLASH_5761VENDOR_ATMEL_MDB041D:
13481 case FLASH_5761VENDOR_ATMEL_MDB081D:
13482 case FLASH_5761VENDOR_ATMEL_MDB161D:
13483 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013484 tg3_flag_set(tp, NVRAM_BUFFERED);
13485 tg3_flag_set(tp, FLASH);
13486 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013487 tp->nvram_pagesize = 256;
13488 break;
13489 case FLASH_5761VENDOR_ST_A_M45PE20:
13490 case FLASH_5761VENDOR_ST_A_M45PE40:
13491 case FLASH_5761VENDOR_ST_A_M45PE80:
13492 case FLASH_5761VENDOR_ST_A_M45PE16:
13493 case FLASH_5761VENDOR_ST_M_M45PE20:
13494 case FLASH_5761VENDOR_ST_M_M45PE40:
13495 case FLASH_5761VENDOR_ST_M_M45PE80:
13496 case FLASH_5761VENDOR_ST_M_M45PE16:
13497 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013498 tg3_flag_set(tp, NVRAM_BUFFERED);
13499 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013500 tp->nvram_pagesize = 256;
13501 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013502 }
13503
13504 if (protect) {
13505 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13506 } else {
13507 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013508 case FLASH_5761VENDOR_ATMEL_ADB161D:
13509 case FLASH_5761VENDOR_ATMEL_MDB161D:
13510 case FLASH_5761VENDOR_ST_A_M45PE16:
13511 case FLASH_5761VENDOR_ST_M_M45PE16:
13512 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13513 break;
13514 case FLASH_5761VENDOR_ATMEL_ADB081D:
13515 case FLASH_5761VENDOR_ATMEL_MDB081D:
13516 case FLASH_5761VENDOR_ST_A_M45PE80:
13517 case FLASH_5761VENDOR_ST_M_M45PE80:
13518 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13519 break;
13520 case FLASH_5761VENDOR_ATMEL_ADB041D:
13521 case FLASH_5761VENDOR_ATMEL_MDB041D:
13522 case FLASH_5761VENDOR_ST_A_M45PE40:
13523 case FLASH_5761VENDOR_ST_M_M45PE40:
13524 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13525 break;
13526 case FLASH_5761VENDOR_ATMEL_ADB021D:
13527 case FLASH_5761VENDOR_ATMEL_MDB021D:
13528 case FLASH_5761VENDOR_ST_A_M45PE20:
13529 case FLASH_5761VENDOR_ST_M_M45PE20:
13530 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13531 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013532 }
13533 }
13534}
13535
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013536static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013537{
13538 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013539 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013540 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13541}
13542
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013543static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013544{
13545 u32 nvcfg1;
13546
13547 nvcfg1 = tr32(NVRAM_CFG1);
13548
13549 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13550 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
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 Carlson321d32a2008-11-21 17:22:19 -080013554 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13555
13556 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13557 tw32(NVRAM_CFG1, nvcfg1);
13558 return;
13559 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13560 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13561 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13562 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13563 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13564 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13565 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13566 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013567 tg3_flag_set(tp, NVRAM_BUFFERED);
13568 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013569
13570 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13571 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13572 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13573 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13574 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13575 break;
13576 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13577 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13578 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13579 break;
13580 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13581 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13582 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13583 break;
13584 }
13585 break;
13586 case FLASH_5752VENDOR_ST_M45PE10:
13587 case FLASH_5752VENDOR_ST_M45PE20:
13588 case FLASH_5752VENDOR_ST_M45PE40:
13589 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013590 tg3_flag_set(tp, NVRAM_BUFFERED);
13591 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013592
13593 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13594 case FLASH_5752VENDOR_ST_M45PE10:
13595 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13596 break;
13597 case FLASH_5752VENDOR_ST_M45PE20:
13598 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13599 break;
13600 case FLASH_5752VENDOR_ST_M45PE40:
13601 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13602 break;
13603 }
13604 break;
13605 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013606 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013607 return;
13608 }
13609
Matt Carlsona1b950d2009-09-01 13:20:17 +000013610 tg3_nvram_get_pagesize(tp, nvcfg1);
13611 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013612 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013613}
13614
13615
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013616static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013617{
13618 u32 nvcfg1;
13619
13620 nvcfg1 = tr32(NVRAM_CFG1);
13621
13622 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13623 case FLASH_5717VENDOR_ATMEL_EEPROM:
13624 case FLASH_5717VENDOR_MICRO_EEPROM:
13625 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013626 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013627 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13628
13629 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13630 tw32(NVRAM_CFG1, nvcfg1);
13631 return;
13632 case FLASH_5717VENDOR_ATMEL_MDB011D:
13633 case FLASH_5717VENDOR_ATMEL_ADB011B:
13634 case FLASH_5717VENDOR_ATMEL_ADB011D:
13635 case FLASH_5717VENDOR_ATMEL_MDB021D:
13636 case FLASH_5717VENDOR_ATMEL_ADB021B:
13637 case FLASH_5717VENDOR_ATMEL_ADB021D:
13638 case FLASH_5717VENDOR_ATMEL_45USPT:
13639 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013640 tg3_flag_set(tp, NVRAM_BUFFERED);
13641 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013642
13643 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13644 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013645 /* Detect size with tg3_nvram_get_size() */
13646 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013647 case FLASH_5717VENDOR_ATMEL_ADB021B:
13648 case FLASH_5717VENDOR_ATMEL_ADB021D:
13649 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13650 break;
13651 default:
13652 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13653 break;
13654 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013655 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013656 case FLASH_5717VENDOR_ST_M_M25PE10:
13657 case FLASH_5717VENDOR_ST_A_M25PE10:
13658 case FLASH_5717VENDOR_ST_M_M45PE10:
13659 case FLASH_5717VENDOR_ST_A_M45PE10:
13660 case FLASH_5717VENDOR_ST_M_M25PE20:
13661 case FLASH_5717VENDOR_ST_A_M25PE20:
13662 case FLASH_5717VENDOR_ST_M_M45PE20:
13663 case FLASH_5717VENDOR_ST_A_M45PE20:
13664 case FLASH_5717VENDOR_ST_25USPT:
13665 case FLASH_5717VENDOR_ST_45USPT:
13666 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013667 tg3_flag_set(tp, NVRAM_BUFFERED);
13668 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013669
13670 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13671 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013672 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013673 /* Detect size with tg3_nvram_get_size() */
13674 break;
13675 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013676 case FLASH_5717VENDOR_ST_A_M45PE20:
13677 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13678 break;
13679 default:
13680 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13681 break;
13682 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013683 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013684 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013685 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013686 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013687 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013688
13689 tg3_nvram_get_pagesize(tp, nvcfg1);
13690 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013691 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013692}
13693
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013694static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013695{
13696 u32 nvcfg1, nvmpinstrp;
13697
13698 nvcfg1 = tr32(NVRAM_CFG1);
13699 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13700
13701 switch (nvmpinstrp) {
13702 case FLASH_5720_EEPROM_HD:
13703 case FLASH_5720_EEPROM_LD:
13704 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013705 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013706
13707 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13708 tw32(NVRAM_CFG1, nvcfg1);
13709 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13710 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13711 else
13712 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13713 return;
13714 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13715 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13716 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13717 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13718 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13719 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13720 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13721 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13722 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13723 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13724 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13725 case FLASH_5720VENDOR_ATMEL_45USPT:
13726 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013727 tg3_flag_set(tp, NVRAM_BUFFERED);
13728 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013729
13730 switch (nvmpinstrp) {
13731 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13732 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13733 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13734 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13735 break;
13736 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13737 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13738 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13739 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13740 break;
13741 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13742 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13743 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13744 break;
13745 default:
13746 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13747 break;
13748 }
13749 break;
13750 case FLASH_5720VENDOR_M_ST_M25PE10:
13751 case FLASH_5720VENDOR_M_ST_M45PE10:
13752 case FLASH_5720VENDOR_A_ST_M25PE10:
13753 case FLASH_5720VENDOR_A_ST_M45PE10:
13754 case FLASH_5720VENDOR_M_ST_M25PE20:
13755 case FLASH_5720VENDOR_M_ST_M45PE20:
13756 case FLASH_5720VENDOR_A_ST_M25PE20:
13757 case FLASH_5720VENDOR_A_ST_M45PE20:
13758 case FLASH_5720VENDOR_M_ST_M25PE40:
13759 case FLASH_5720VENDOR_M_ST_M45PE40:
13760 case FLASH_5720VENDOR_A_ST_M25PE40:
13761 case FLASH_5720VENDOR_A_ST_M45PE40:
13762 case FLASH_5720VENDOR_M_ST_M25PE80:
13763 case FLASH_5720VENDOR_M_ST_M45PE80:
13764 case FLASH_5720VENDOR_A_ST_M25PE80:
13765 case FLASH_5720VENDOR_A_ST_M45PE80:
13766 case FLASH_5720VENDOR_ST_25USPT:
13767 case FLASH_5720VENDOR_ST_45USPT:
13768 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013769 tg3_flag_set(tp, NVRAM_BUFFERED);
13770 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013771
13772 switch (nvmpinstrp) {
13773 case FLASH_5720VENDOR_M_ST_M25PE20:
13774 case FLASH_5720VENDOR_M_ST_M45PE20:
13775 case FLASH_5720VENDOR_A_ST_M25PE20:
13776 case FLASH_5720VENDOR_A_ST_M45PE20:
13777 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13778 break;
13779 case FLASH_5720VENDOR_M_ST_M25PE40:
13780 case FLASH_5720VENDOR_M_ST_M45PE40:
13781 case FLASH_5720VENDOR_A_ST_M25PE40:
13782 case FLASH_5720VENDOR_A_ST_M45PE40:
13783 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13784 break;
13785 case FLASH_5720VENDOR_M_ST_M25PE80:
13786 case FLASH_5720VENDOR_M_ST_M45PE80:
13787 case FLASH_5720VENDOR_A_ST_M25PE80:
13788 case FLASH_5720VENDOR_A_ST_M45PE80:
13789 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13790 break;
13791 default:
13792 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13793 break;
13794 }
13795 break;
13796 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013797 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013798 return;
13799 }
13800
13801 tg3_nvram_get_pagesize(tp, nvcfg1);
13802 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013803 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013804}
13805
Linus Torvalds1da177e2005-04-16 15:20:36 -070013806/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013807static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013808{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013809 tw32_f(GRC_EEPROM_ADDR,
13810 (EEPROM_ADDR_FSM_RESET |
13811 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13812 EEPROM_ADDR_CLKPERD_SHIFT)));
13813
Michael Chan9d57f012006-12-07 00:23:25 -080013814 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013815
13816 /* Enable seeprom accesses. */
13817 tw32_f(GRC_LOCAL_CTRL,
13818 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13819 udelay(100);
13820
13821 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13822 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013823 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824
Michael Chanec41c7d2006-01-17 02:40:55 -080013825 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013826 netdev_warn(tp->dev,
13827 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013828 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013829 return;
13830 }
Michael Chane6af3012005-04-21 17:12:05 -070013831 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013832
Matt Carlson989a9d22007-05-05 11:51:05 -070013833 tp->nvram_size = 0;
13834
Michael Chan361b4ac2005-04-21 17:11:21 -070013835 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13836 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013837 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13838 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013839 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013842 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013843 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13844 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013845 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13846 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013847 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013848 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013849 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013850 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13851 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013852 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013853 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13854 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013855 else
13856 tg3_get_nvram_info(tp);
13857
Matt Carlson989a9d22007-05-05 11:51:05 -070013858 if (tp->nvram_size == 0)
13859 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860
Michael Chane6af3012005-04-21 17:12:05 -070013861 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013862 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013863
13864 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013865 tg3_flag_clear(tp, NVRAM);
13866 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013867
13868 tg3_get_eeprom_size(tp);
13869 }
13870}
13871
Linus Torvalds1da177e2005-04-16 15:20:36 -070013872struct subsys_tbl_ent {
13873 u16 subsys_vendor, subsys_devid;
13874 u32 phy_id;
13875};
13876
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013877static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013878 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013879 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013880 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013882 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013883 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013884 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13886 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013888 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013890 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013894 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013896 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013898 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013899 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013900 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013901
13902 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013903 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013904 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013905 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013906 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013907 { TG3PCI_SUBVENDOR_ID_3COM,
13908 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13909 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013910 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013911 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013912 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913
13914 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013915 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013916 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013917 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013918 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013919 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013920 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013921 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013922 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923
13924 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013925 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013926 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013927 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013928 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013929 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13930 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13931 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013932 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013933 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013934 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935
13936 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013937 { TG3PCI_SUBVENDOR_ID_IBM,
13938 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013939};
13940
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013941static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013942{
13943 int i;
13944
13945 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13946 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13947 tp->pdev->subsystem_vendor) &&
13948 (subsys_id_to_phy_id[i].subsys_devid ==
13949 tp->pdev->subsystem_device))
13950 return &subsys_id_to_phy_id[i];
13951 }
13952 return NULL;
13953}
13954
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013955static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013956{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013958
Matt Carlson79eb6902010-02-17 15:17:03 +000013959 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013960 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13961
Gary Zambranoa85feb82007-05-05 11:52:19 -070013962 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013963 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13964 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013965
Michael Chanb5d37722006-09-27 16:06:21 -070013966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013967 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013968 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13969 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013970 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013971 val = tr32(VCPU_CFGSHDW);
13972 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013973 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013974 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013975 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013976 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013977 device_set_wakeup_enable(&tp->pdev->dev, true);
13978 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013979 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013980 }
13981
Linus Torvalds1da177e2005-04-16 15:20:36 -070013982 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13983 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13984 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013985 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013986 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013987
13988 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13989 tp->nic_sram_data_cfg = nic_cfg;
13990
13991 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13992 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013993 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13994 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13995 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013996 (ver > 0) && (ver < 0x100))
13997 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13998
Matt Carlsona9daf362008-05-25 23:49:44 -070013999 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
14000 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
14001
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
14003 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
14004 eeprom_phy_serdes = 1;
14005
14006 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
14007 if (nic_phy_id != 0) {
14008 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
14009 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
14010
14011 eeprom_phy_id = (id1 >> 16) << 10;
14012 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14013 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14014 } else
14015 eeprom_phy_id = 0;
14016
Michael Chan7d0c41e2005-04-21 17:06:20 -070014017 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014018 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014019 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014020 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014021 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014022 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014023 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014024
Joe Perches63c3a662011-04-26 08:12:10 +000014025 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014026 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14027 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014028 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14030
14031 switch (led_cfg) {
14032 default:
14033 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14034 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14035 break;
14036
14037 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14038 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14039 break;
14040
14041 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14042 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014043
14044 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14045 * read on some older 5700/5701 bootcode.
14046 */
14047 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14048 ASIC_REV_5700 ||
14049 GET_ASIC_REV(tp->pci_chip_rev_id) ==
14050 ASIC_REV_5701)
14051 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14052
Linus Torvalds1da177e2005-04-16 15:20:36 -070014053 break;
14054
14055 case SHASTA_EXT_LED_SHARED:
14056 tp->led_ctrl = LED_CTRL_MODE_SHARED;
14057 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
14058 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
14059 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14060 LED_CTRL_MODE_PHY_2);
14061 break;
14062
14063 case SHASTA_EXT_LED_MAC:
14064 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14065 break;
14066
14067 case SHASTA_EXT_LED_COMBO:
14068 tp->led_ctrl = LED_CTRL_MODE_COMBO;
14069 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
14070 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14071 LED_CTRL_MODE_PHY_2);
14072 break;
14073
Stephen Hemminger855e1112008-04-16 16:37:28 -070014074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014075
14076 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
14078 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14079 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14080
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014081 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
14082 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014083
Michael Chan9d26e212006-12-07 00:21:14 -080014084 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014085 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014086 if ((tp->pdev->subsystem_vendor ==
14087 PCI_VENDOR_ID_ARIMA) &&
14088 (tp->pdev->subsystem_device == 0x205a ||
14089 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014090 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014091 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014092 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14093 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095
14096 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014097 tg3_flag_set(tp, ENABLE_ASF);
14098 if (tg3_flag(tp, 5750_PLUS))
14099 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014100 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014101
14102 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014103 tg3_flag(tp, 5750_PLUS))
14104 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014105
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014106 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014107 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014108 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109
Joe Perches63c3a662011-04-26 08:12:10 +000014110 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014111 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014112 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014113 device_set_wakeup_enable(&tp->pdev->dev, true);
14114 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014115
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014117 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118
14119 /* serdes signal pre-emphasis in register 0x590 set by */
14120 /* bootcode if bit 18 is set */
14121 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014122 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014123
Joe Perches63c3a662011-04-26 08:12:10 +000014124 if ((tg3_flag(tp, 57765_PLUS) ||
14125 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14126 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014127 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014128 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014129
Joe Perches63c3a662011-04-26 08:12:10 +000014130 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000014131 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014132 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014133 u32 cfg3;
14134
14135 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14136 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014137 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014138 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014139
Matt Carlson14417062010-02-17 15:16:59 +000014140 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014141 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014142 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014143 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014144 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014145 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014146 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014147done:
Joe Perches63c3a662011-04-26 08:12:10 +000014148 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014149 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014150 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014151 else
14152 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014153}
14154
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014155static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014156{
14157 int i;
14158 u32 val;
14159
14160 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14161 tw32(OTP_CTRL, cmd);
14162
14163 /* Wait for up to 1 ms for command to execute. */
14164 for (i = 0; i < 100; i++) {
14165 val = tr32(OTP_STATUS);
14166 if (val & OTP_STATUS_CMD_DONE)
14167 break;
14168 udelay(10);
14169 }
14170
14171 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14172}
14173
14174/* Read the gphy configuration from the OTP region of the chip. The gphy
14175 * configuration is a 32-bit value that straddles the alignment boundary.
14176 * We do two 32-bit reads and then shift and merge the results.
14177 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014178static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014179{
14180 u32 bhalf_otp, thalf_otp;
14181
14182 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14183
14184 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14185 return 0;
14186
14187 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14188
14189 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14190 return 0;
14191
14192 thalf_otp = tr32(OTP_READ_DATA);
14193
14194 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14195
14196 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14197 return 0;
14198
14199 bhalf_otp = tr32(OTP_READ_DATA);
14200
14201 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14202}
14203
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014204static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014205{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014206 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014207
14208 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14209 adv |= ADVERTISED_1000baseT_Half |
14210 ADVERTISED_1000baseT_Full;
14211
14212 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14213 adv |= ADVERTISED_100baseT_Half |
14214 ADVERTISED_100baseT_Full |
14215 ADVERTISED_10baseT_Half |
14216 ADVERTISED_10baseT_Full |
14217 ADVERTISED_TP;
14218 else
14219 adv |= ADVERTISED_FIBRE;
14220
14221 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014222 tp->link_config.speed = SPEED_UNKNOWN;
14223 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014224 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014225 tp->link_config.active_speed = SPEED_UNKNOWN;
14226 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014227
14228 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014229}
14230
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014231static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014232{
14233 u32 hw_phy_id_1, hw_phy_id_2;
14234 u32 hw_phy_id, hw_phy_id_masked;
14235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014236
Matt Carlsone256f8a2011-03-09 16:58:24 +000014237 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014238 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014239 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14240
Michael Chan8151ad52012-07-29 19:15:41 +000014241 if (tg3_flag(tp, ENABLE_APE)) {
14242 switch (tp->pci_fn) {
14243 case 0:
14244 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14245 break;
14246 case 1:
14247 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14248 break;
14249 case 2:
14250 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14251 break;
14252 case 3:
14253 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14254 break;
14255 }
14256 }
14257
Joe Perches63c3a662011-04-26 08:12:10 +000014258 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014259 return tg3_phy_init(tp);
14260
Linus Torvalds1da177e2005-04-16 15:20:36 -070014261 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014262 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014263 */
14264 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014265 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014266 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267 } else {
14268 /* Now read the physical PHY_ID from the chip and verify
14269 * that it is sane. If it doesn't look good, we fall back
14270 * to either the hard-coded table based PHY_ID and failing
14271 * that the value found in the eeprom area.
14272 */
14273 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14274 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14275
14276 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14277 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14278 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14279
Matt Carlson79eb6902010-02-17 15:17:03 +000014280 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281 }
14282
Matt Carlson79eb6902010-02-17 15:17:03 +000014283 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014285 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014286 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014287 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014288 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014289 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014290 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014291 /* Do nothing, phy ID already set up in
14292 * tg3_get_eeprom_hw_cfg().
14293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014294 } else {
14295 struct subsys_tbl_ent *p;
14296
14297 /* No eeprom signature? Try the hardcoded
14298 * subsys device table.
14299 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014300 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301 if (!p)
14302 return -ENODEV;
14303
14304 tp->phy_id = p->phy_id;
14305 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014306 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014307 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308 }
14309 }
14310
Matt Carlsona6b68da2010-12-06 08:28:52 +000014311 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000014312 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
14314 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000014315 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
14316 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
14317 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014318 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14319
Matt Carlsone256f8a2011-03-09 16:58:24 +000014320 tg3_phy_init_link_config(tp);
14321
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014322 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014323 !tg3_flag(tp, ENABLE_APE) &&
14324 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014325 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326
14327 tg3_readphy(tp, MII_BMSR, &bmsr);
14328 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14329 (bmsr & BMSR_LSTATUS))
14330 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014331
Linus Torvalds1da177e2005-04-16 15:20:36 -070014332 err = tg3_phy_reset(tp);
14333 if (err)
14334 return err;
14335
Matt Carlson42b64a42011-05-19 12:12:49 +000014336 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014338 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014339 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14340 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341
14342 tg3_writephy(tp, MII_BMCR,
14343 BMCR_ANENABLE | BMCR_ANRESTART);
14344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345 }
14346
14347skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014348 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014349 err = tg3_init_5401phy_dsp(tp);
14350 if (err)
14351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353 err = tg3_init_5401phy_dsp(tp);
14354 }
14355
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 return err;
14357}
14358
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014359static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014361 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014362 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014363 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014364 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014365
Matt Carlson535a4902011-07-20 10:20:56 +000014366 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014367 if (!vpd_data)
14368 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369
Matt Carlson535a4902011-07-20 10:20:56 +000014370 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014371 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014372 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014373
14374 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14375 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14376 i += PCI_VPD_LRDT_TAG_SIZE;
14377
Matt Carlson535a4902011-07-20 10:20:56 +000014378 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014379 goto out_not_found;
14380
Matt Carlson184b8902010-04-05 10:19:25 +000014381 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14382 PCI_VPD_RO_KEYWORD_MFR_ID);
14383 if (j > 0) {
14384 len = pci_vpd_info_field_size(&vpd_data[j]);
14385
14386 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14387 if (j + len > block_end || len != 4 ||
14388 memcmp(&vpd_data[j], "1028", 4))
14389 goto partno;
14390
14391 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14392 PCI_VPD_RO_KEYWORD_VENDOR0);
14393 if (j < 0)
14394 goto partno;
14395
14396 len = pci_vpd_info_field_size(&vpd_data[j]);
14397
14398 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14399 if (j + len > block_end)
14400 goto partno;
14401
14402 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014403 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014404 }
14405
14406partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014407 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14408 PCI_VPD_RO_KEYWORD_PARTNO);
14409 if (i < 0)
14410 goto out_not_found;
14411
14412 len = pci_vpd_info_field_size(&vpd_data[i]);
14413
14414 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14415 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014416 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014417 goto out_not_found;
14418
14419 memcpy(tp->board_part_number, &vpd_data[i], len);
14420
Linus Torvalds1da177e2005-04-16 15:20:36 -070014421out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014422 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014423 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014424 return;
14425
14426out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000014427 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014428 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14429 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014430 strcpy(tp->board_part_number, "BCM5717");
14431 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14432 strcpy(tp->board_part_number, "BCM5718");
14433 else
14434 goto nomatch;
14435 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14436 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14437 strcpy(tp->board_part_number, "BCM57780");
14438 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14439 strcpy(tp->board_part_number, "BCM57760");
14440 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14441 strcpy(tp->board_part_number, "BCM57790");
14442 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14443 strcpy(tp->board_part_number, "BCM57788");
14444 else
14445 goto nomatch;
14446 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14447 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14448 strcpy(tp->board_part_number, "BCM57761");
14449 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14450 strcpy(tp->board_part_number, "BCM57765");
14451 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14452 strcpy(tp->board_part_number, "BCM57781");
14453 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14454 strcpy(tp->board_part_number, "BCM57785");
14455 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14456 strcpy(tp->board_part_number, "BCM57791");
14457 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14458 strcpy(tp->board_part_number, "BCM57795");
14459 else
14460 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000014461 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
14462 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14463 strcpy(tp->board_part_number, "BCM57762");
14464 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14465 strcpy(tp->board_part_number, "BCM57766");
14466 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14467 strcpy(tp->board_part_number, "BCM57782");
14468 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14469 strcpy(tp->board_part_number, "BCM57786");
14470 else
14471 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000014472 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014473 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014474 } else {
14475nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014476 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478}
14479
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014480static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014481{
14482 u32 val;
14483
Matt Carlsone4f34112009-02-25 14:25:00 +000014484 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014485 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014486 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014487 val != 0)
14488 return 0;
14489
14490 return 1;
14491}
14492
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014493static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014494{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014495 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014496 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014497 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014498
14499 if (tg3_nvram_read(tp, 0xc, &offset) ||
14500 tg3_nvram_read(tp, 0x4, &start))
14501 return;
14502
14503 offset = tg3_nvram_logical_addr(tp, offset);
14504
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014505 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014506 return;
14507
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014508 if ((val & 0xfc000000) == 0x0c000000) {
14509 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014510 return;
14511
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014512 if (val == 0)
14513 newver = true;
14514 }
14515
Matt Carlson75f99362010-04-05 10:19:24 +000014516 dst_off = strlen(tp->fw_ver);
14517
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014518 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014519 if (TG3_VER_SIZE - dst_off < 16 ||
14520 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014521 return;
14522
14523 offset = offset + ver_offset - start;
14524 for (i = 0; i < 16; i += 4) {
14525 __be32 v;
14526 if (tg3_nvram_read_be32(tp, offset + i, &v))
14527 return;
14528
Matt Carlson75f99362010-04-05 10:19:24 +000014529 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014530 }
14531 } else {
14532 u32 major, minor;
14533
14534 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14535 return;
14536
14537 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14538 TG3_NVM_BCVER_MAJSFT;
14539 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014540 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14541 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014542 }
14543}
14544
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014545static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014546{
14547 u32 val, major, minor;
14548
14549 /* Use native endian representation */
14550 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14551 return;
14552
14553 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14554 TG3_NVM_HWSB_CFG1_MAJSFT;
14555 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14556 TG3_NVM_HWSB_CFG1_MINSFT;
14557
14558 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14559}
14560
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014561static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014562{
14563 u32 offset, major, minor, build;
14564
Matt Carlson75f99362010-04-05 10:19:24 +000014565 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014566
14567 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14568 return;
14569
14570 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14571 case TG3_EEPROM_SB_REVISION_0:
14572 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14573 break;
14574 case TG3_EEPROM_SB_REVISION_2:
14575 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14576 break;
14577 case TG3_EEPROM_SB_REVISION_3:
14578 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14579 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014580 case TG3_EEPROM_SB_REVISION_4:
14581 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14582 break;
14583 case TG3_EEPROM_SB_REVISION_5:
14584 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14585 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014586 case TG3_EEPROM_SB_REVISION_6:
14587 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14588 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014589 default:
14590 return;
14591 }
14592
Matt Carlsone4f34112009-02-25 14:25:00 +000014593 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014594 return;
14595
14596 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14597 TG3_EEPROM_SB_EDH_BLD_SHFT;
14598 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14599 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14600 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14601
14602 if (minor > 99 || build > 26)
14603 return;
14604
Matt Carlson75f99362010-04-05 10:19:24 +000014605 offset = strlen(tp->fw_ver);
14606 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14607 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014608
14609 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014610 offset = strlen(tp->fw_ver);
14611 if (offset < TG3_VER_SIZE - 1)
14612 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014613 }
14614}
14615
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014616static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014617{
14618 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014619 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014620
14621 for (offset = TG3_NVM_DIR_START;
14622 offset < TG3_NVM_DIR_END;
14623 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014624 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014625 return;
14626
14627 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14628 break;
14629 }
14630
14631 if (offset == TG3_NVM_DIR_END)
14632 return;
14633
Joe Perches63c3a662011-04-26 08:12:10 +000014634 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014635 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014636 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014637 return;
14638
Matt Carlsone4f34112009-02-25 14:25:00 +000014639 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014640 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014641 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014642 return;
14643
14644 offset += val - start;
14645
Matt Carlsonacd9c112009-02-25 14:26:33 +000014646 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014647
Matt Carlsonacd9c112009-02-25 14:26:33 +000014648 tp->fw_ver[vlen++] = ',';
14649 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014650
14651 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014652 __be32 v;
14653 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014654 return;
14655
Al Virob9fc7dc2007-12-17 22:59:57 -080014656 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014657
Matt Carlsonacd9c112009-02-25 14:26:33 +000014658 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14659 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014660 break;
14661 }
14662
Matt Carlsonacd9c112009-02-25 14:26:33 +000014663 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14664 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014665 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014666}
14667
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014668static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014669{
Matt Carlson7fd76442009-02-25 14:27:20 +000014670 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014671
14672 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14673 if (apedata != APE_SEG_SIG_MAGIC)
14674 return;
14675
14676 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14677 if (!(apedata & APE_FW_STATUS_READY))
14678 return;
14679
Michael Chan165f4d12012-07-16 16:23:59 +000014680 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14681 tg3_flag_set(tp, APE_HAS_NCSI);
14682}
14683
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014684static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014685{
14686 int vlen;
14687 u32 apedata;
14688 char *fwtype;
14689
Matt Carlson7fd76442009-02-25 14:27:20 +000014690 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14691
Michael Chan165f4d12012-07-16 16:23:59 +000014692 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014693 fwtype = "NCSI";
Michael Chan165f4d12012-07-16 16:23:59 +000014694 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014695 fwtype = "DASH";
14696
Matt Carlson7fd76442009-02-25 14:27:20 +000014697 vlen = strlen(tp->fw_ver);
14698
Matt Carlsonecc79642010-08-02 11:26:01 +000014699 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14700 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014701 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14702 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14703 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14704 (apedata & APE_FW_VERSION_BLDMSK));
14705}
14706
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014707static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014708{
14709 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014710 bool vpd_vers = false;
14711
14712 if (tp->fw_ver[0] != 0)
14713 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014714
Joe Perches63c3a662011-04-26 08:12:10 +000014715 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014716 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000014717 return;
14718 }
14719
Matt Carlsonacd9c112009-02-25 14:26:33 +000014720 if (tg3_nvram_read(tp, 0, &val))
14721 return;
14722
14723 if (val == TG3_EEPROM_MAGIC)
14724 tg3_read_bc_ver(tp);
14725 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14726 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014727 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14728 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014729
Michael Chan165f4d12012-07-16 16:23:59 +000014730 if (tg3_flag(tp, ENABLE_ASF)) {
14731 if (tg3_flag(tp, ENABLE_APE)) {
14732 tg3_probe_ncsi(tp);
14733 if (!vpd_vers)
14734 tg3_read_dash_ver(tp);
14735 } else if (!vpd_vers) {
14736 tg3_read_mgmtfw_ver(tp);
14737 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014738 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014739
14740 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014741}
14742
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014743static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14744{
Joe Perches63c3a662011-04-26 08:12:10 +000014745 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014746 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014747 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014748 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014749 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014750 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014751}
14752
Matt Carlson41434702011-03-09 16:58:22 +000014753static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014754 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14755 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14756 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14757 { },
14758};
14759
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014760static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014761{
14762 struct pci_dev *peer;
14763 unsigned int func, devnr = tp->pdev->devfn & ~7;
14764
14765 for (func = 0; func < 8; func++) {
14766 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14767 if (peer && peer != tp->pdev)
14768 break;
14769 pci_dev_put(peer);
14770 }
14771 /* 5704 can be configured in single-port mode, set peer to
14772 * tp->pdev in that case.
14773 */
14774 if (!peer) {
14775 peer = tp->pdev;
14776 return peer;
14777 }
14778
14779 /*
14780 * We don't need to keep the refcount elevated; there's no way
14781 * to remove one half of this device without removing the other
14782 */
14783 pci_dev_put(peer);
14784
14785 return peer;
14786}
14787
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014788static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000014789{
14790 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14792 u32 reg;
14793
14794 /* All devices that use the alternate
14795 * ASIC REV location have a CPMU.
14796 */
14797 tg3_flag_set(tp, CPMU_PRESENT);
14798
14799 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000014800 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000014801 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14802 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14803 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14804 reg = TG3PCI_GEN2_PRODID_ASICREV;
14805 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14806 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14807 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14808 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14809 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14810 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14811 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14812 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14813 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14814 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14815 reg = TG3PCI_GEN15_PRODID_ASICREV;
14816 else
14817 reg = TG3PCI_PRODID_ASICREV;
14818
14819 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14820 }
14821
14822 /* Wrong chip ID in 5752 A0. This code can be removed later
14823 * as A0 is not in production.
14824 */
14825 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14826 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14827
Michael Chan79d49692012-11-05 14:26:29 +000014828 if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
14829 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
14830
Matt Carlson42b123b2012-02-13 15:20:13 +000014831 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14832 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14834 tg3_flag_set(tp, 5717_PLUS);
14835
14836 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14838 tg3_flag_set(tp, 57765_CLASS);
14839
14840 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14841 tg3_flag_set(tp, 57765_PLUS);
14842
14843 /* Intentionally exclude ASIC_REV_5906 */
14844 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14848 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14849 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14850 tg3_flag(tp, 57765_PLUS))
14851 tg3_flag_set(tp, 5755_PLUS);
14852
14853 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14854 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14855 tg3_flag_set(tp, 5780_CLASS);
14856
14857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14859 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14860 tg3_flag(tp, 5755_PLUS) ||
14861 tg3_flag(tp, 5780_CLASS))
14862 tg3_flag_set(tp, 5750_PLUS);
14863
14864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14865 tg3_flag(tp, 5750_PLUS))
14866 tg3_flag_set(tp, 5705_PLUS);
14867}
14868
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014869static bool tg3_10_100_only_device(struct tg3 *tp,
14870 const struct pci_device_id *ent)
14871{
14872 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
14873
14874 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14875 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14876 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14877 return true;
14878
14879 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
14880 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
14881 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
14882 return true;
14883 } else {
14884 return true;
14885 }
14886 }
14887
14888 return false;
14889}
14890
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000014891static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014892{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014893 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894 u32 pci_state_reg, grc_misc_cfg;
14895 u32 val;
14896 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014897 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898
Linus Torvalds1da177e2005-04-16 15:20:36 -070014899 /* Force memory write invalidate off. If we leave it on,
14900 * then on 5700_BX chips we have to enable a workaround.
14901 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14902 * to match the cacheline size. The Broadcom driver have this
14903 * workaround but turns MWI off all the times so never uses
14904 * it. This seems to suggest that the workaround is insufficient.
14905 */
14906 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14907 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14908 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14909
Matt Carlson16821282011-07-13 09:27:28 +000014910 /* Important! -- Make sure register accesses are byteswapped
14911 * correctly. Also, for those chips that require it, make
14912 * sure that indirect register accesses are enabled before
14913 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014914 */
14915 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14916 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014917 tp->misc_host_ctrl |= (misc_ctrl_reg &
14918 MISC_HOST_CTRL_CHIPREV);
14919 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14920 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921
Matt Carlson42b123b2012-02-13 15:20:13 +000014922 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014923
Michael Chan68929142005-08-09 20:17:14 -070014924 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14925 * we need to disable memory and use config. cycles
14926 * only to access all registers. The 5702/03 chips
14927 * can mistakenly decode the special cycles from the
14928 * ICH chipsets as memory write cycles, causing corruption
14929 * of register and memory space. Only certain ICH bridges
14930 * will drive special cycles with non-zero data during the
14931 * address phase which can fall within the 5703's address
14932 * range. This is not an ICH bug as the PCI spec allows
14933 * non-zero address during special cycles. However, only
14934 * these ICH bridges are known to drive non-zero addresses
14935 * during special cycles.
14936 *
14937 * Since special cycles do not cross PCI bridges, we only
14938 * enable this workaround if the 5703 is on the secondary
14939 * bus of these ICH bridges.
14940 */
14941 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14942 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14943 static struct tg3_dev_id {
14944 u32 vendor;
14945 u32 device;
14946 u32 rev;
14947 } ich_chipsets[] = {
14948 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14949 PCI_ANY_ID },
14950 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14951 PCI_ANY_ID },
14952 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14953 0xa },
14954 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14955 PCI_ANY_ID },
14956 { },
14957 };
14958 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14959 struct pci_dev *bridge = NULL;
14960
14961 while (pci_id->vendor != 0) {
14962 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14963 bridge);
14964 if (!bridge) {
14965 pci_id++;
14966 continue;
14967 }
14968 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014969 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014970 continue;
14971 }
14972 if (bridge->subordinate &&
14973 (bridge->subordinate->number ==
14974 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014975 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014976 pci_dev_put(bridge);
14977 break;
14978 }
14979 }
14980 }
14981
Matt Carlson6ff6f812011-05-19 12:12:54 +000014982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070014983 static struct tg3_dev_id {
14984 u32 vendor;
14985 u32 device;
14986 } bridge_chipsets[] = {
14987 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14988 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14989 { },
14990 };
14991 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14992 struct pci_dev *bridge = NULL;
14993
14994 while (pci_id->vendor != 0) {
14995 bridge = pci_get_device(pci_id->vendor,
14996 pci_id->device,
14997 bridge);
14998 if (!bridge) {
14999 pci_id++;
15000 continue;
15001 }
15002 if (bridge->subordinate &&
15003 (bridge->subordinate->number <=
15004 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015005 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba12008-04-19 18:12:33 -070015006 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015007 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070015008 pci_dev_put(bridge);
15009 break;
15010 }
15011 }
15012 }
15013
Michael Chan4a29cc22006-03-19 13:21:12 -080015014 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15015 * DMA addresses > 40-bit. This bridge may have other additional
15016 * 57xx devices behind it in some 4-port NIC designs for example.
15017 * Any tg3 device found behind the bridge will also need the 40-bit
15018 * DMA workaround.
15019 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015020 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015021 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015022 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015023 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015024 struct pci_dev *bridge = NULL;
15025
15026 do {
15027 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15028 PCI_DEVICE_ID_SERVERWORKS_EPB,
15029 bridge);
15030 if (bridge && bridge->subordinate &&
15031 (bridge->subordinate->number <=
15032 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015033 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015034 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015035 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015036 pci_dev_put(bridge);
15037 break;
15038 }
15039 } while (bridge);
15040 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015041
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000015043 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015044 tp->pdev_peer = tg3_find_peer(tp);
15045
Matt Carlson507399f2009-11-13 13:03:37 +000015046 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000015047 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015048 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015049 else if (tg3_flag(tp, 57765_PLUS))
15050 tg3_flag_set(tp, HW_TSO_3);
15051 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015052 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015053 tg3_flag_set(tp, HW_TSO_2);
15054 else if (tg3_flag(tp, 5750_PLUS)) {
15055 tg3_flag_set(tp, HW_TSO_1);
15056 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
15058 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015059 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015060 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15061 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
15062 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015063 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015064 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
15065 tp->fw_needed = FIRMWARE_TG3TSO5;
15066 else
15067 tp->fw_needed = FIRMWARE_TG3TSO;
15068 }
15069
Matt Carlsondabc5c62011-05-19 12:12:52 +000015070 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015071 if (tg3_flag(tp, HW_TSO_1) ||
15072 tg3_flag(tp, HW_TSO_2) ||
15073 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015074 tp->fw_needed) {
15075 /* For firmware TSO, assume ASF is disabled.
15076 * We'll disable TSO later if we discover ASF
15077 * is enabled in tg3_get_eeprom_hw_cfg().
15078 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015079 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015080 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015081 tg3_flag_clear(tp, TSO_CAPABLE);
15082 tg3_flag_clear(tp, TSO_BUG);
15083 tp->fw_needed = NULL;
15084 }
15085
15086 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
15087 tp->fw_needed = FIRMWARE_TG3;
15088
Matt Carlson507399f2009-11-13 13:03:37 +000015089 tp->irq_max = 1;
15090
Joe Perches63c3a662011-04-26 08:12:10 +000015091 if (tg3_flag(tp, 5750_PLUS)) {
15092 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015093 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
15094 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
15095 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
15096 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
15097 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015098 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015099
Joe Perches63c3a662011-04-26 08:12:10 +000015100 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070015101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015102 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015104
Joe Perches63c3a662011-04-26 08:12:10 +000015105 if (tg3_flag(tp, 57765_PLUS)) {
15106 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015107 tp->irq_max = TG3_IRQ_MAX_VECS;
15108 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015109 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015110
Michael Chan91024262012-09-28 07:12:38 +000015111 tp->txq_max = 1;
15112 tp->rxq_max = 1;
15113 if (tp->irq_max > 1) {
15114 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15115 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15116
15117 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15118 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15119 tp->txq_max = tp->irq_max - 1;
15120 }
15121
Matt Carlsonb7abee62012-06-07 12:56:54 +000015122 if (tg3_flag(tp, 5755_PLUS) ||
15123 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015124 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015125
Matt Carlsone31aa982011-07-27 14:20:53 +000015126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015127 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015128
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000015129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15131 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000015132 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015133
Joe Perches63c3a662011-04-26 08:12:10 +000015134 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000015135 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015136 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015137
Joe Perches63c3a662011-04-26 08:12:10 +000015138 if (!tg3_flag(tp, 5705_PLUS) ||
15139 tg3_flag(tp, 5780_CLASS) ||
15140 tg3_flag(tp, USE_JUMBO_BDFLAG))
15141 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015142
Matt Carlson52f44902008-11-21 17:17:04 -080015143 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15144 &pci_state_reg);
15145
Jon Mason708ebb3a2011-06-27 12:56:50 +000015146 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015147 u16 lnkctl;
15148
Joe Perches63c3a662011-04-26 08:12:10 +000015149 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015150
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015151 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015152 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000015153 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
15154 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015155 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015156 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015157 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015158 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000015160 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
15161 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015162 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000015163 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015164 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015165 }
Matt Carlson52f44902008-11-21 17:17:04 -080015166 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015167 /* BCM5785 devices are effectively PCIe devices, and should
15168 * follow PCIe codepaths, but do not have a PCIe capabilities
15169 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015170 */
Joe Perches63c3a662011-04-26 08:12:10 +000015171 tg3_flag_set(tp, PCI_EXPRESS);
15172 } else if (!tg3_flag(tp, 5705_PLUS) ||
15173 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015174 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15175 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015176 dev_err(&tp->pdev->dev,
15177 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015178 return -EIO;
15179 }
15180
15181 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015182 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184
Michael Chan399de502005-10-03 14:02:39 -070015185 /* If we have an AMD 762 or VIA K8T800 chipset, write
15186 * reordering to the mailbox registers done by the host
15187 * controller can cause major troubles. We read back from
15188 * every mailbox register write to force the writes to be
15189 * posted to the chip in order.
15190 */
Matt Carlson41434702011-03-09 16:58:22 +000015191 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015192 !tg3_flag(tp, PCI_EXPRESS))
15193 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015194
Matt Carlson69fc4052008-12-21 20:19:57 -080015195 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15196 &tp->pci_cacheline_sz);
15197 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15198 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
15200 tp->pci_lat_timer < 64) {
15201 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015202 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15203 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204 }
15205
Matt Carlson16821282011-07-13 09:27:28 +000015206 /* Important! -- It is critical that the PCI-X hw workaround
15207 * situation is decided before the first MMIO register access.
15208 */
Matt Carlson52f44902008-11-21 17:17:04 -080015209 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
15210 /* 5700 BX chips need to have their TX producer index
15211 * mailboxes written twice to workaround a bug.
15212 */
Joe Perches63c3a662011-04-26 08:12:10 +000015213 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015214
Matt Carlson52f44902008-11-21 17:17:04 -080015215 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 *
15217 * The workaround is to use indirect register accesses
15218 * for all chip writes not to mailbox registers.
15219 */
Joe Perches63c3a662011-04-26 08:12:10 +000015220 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015221 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015222
Joe Perches63c3a662011-04-26 08:12:10 +000015223 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224
15225 /* The chip can have it's power management PCI config
15226 * space registers clobbered due to this bug.
15227 * So explicitly force the chip into D0 here.
15228 */
Matt Carlson9974a352007-10-07 23:27:28 -070015229 pci_read_config_dword(tp->pdev,
15230 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015231 &pm_reg);
15232 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15233 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015234 pci_write_config_dword(tp->pdev,
15235 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015236 pm_reg);
15237
15238 /* Also, force SERR#/PERR# in PCI command. */
15239 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15240 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15241 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15242 }
15243 }
15244
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015246 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015247 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015248 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015249
15250 /* Chip-specific fixup from Broadcom driver */
15251 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
15252 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15253 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15254 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15255 }
15256
Michael Chan1ee582d2005-08-09 20:16:46 -070015257 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015258 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015259 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015260 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015261 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015262 tp->write32_tx_mbox = tg3_write32;
15263 tp->write32_rx_mbox = tg3_write32;
15264
15265 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015266 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015267 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015268 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015269 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070015270 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
15271 /*
15272 * Back to back register writes can cause problems on these
15273 * chips, the workaround is to read back all reg writes
15274 * except those to mailbox regs.
15275 *
15276 * See tg3_write_indirect_reg32().
15277 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015278 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015279 }
15280
Joe Perches63c3a662011-04-26 08:12:10 +000015281 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015282 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015283 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015284 tp->write32_rx_mbox = tg3_write_flush_reg32;
15285 }
Michael Chan20094932005-08-09 20:16:32 -070015286
Joe Perches63c3a662011-04-26 08:12:10 +000015287 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015288 tp->read32 = tg3_read_indirect_reg32;
15289 tp->write32 = tg3_write_indirect_reg32;
15290 tp->read32_mbox = tg3_read_indirect_mbox;
15291 tp->write32_mbox = tg3_write_indirect_mbox;
15292 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15293 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15294
15295 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015296 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015297
15298 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15299 pci_cmd &= ~PCI_COMMAND_MEMORY;
15300 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15301 }
Michael Chanb5d37722006-09-27 16:06:21 -070015302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15303 tp->read32_mbox = tg3_read32_mbox_5906;
15304 tp->write32_mbox = tg3_write32_mbox_5906;
15305 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15306 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15307 }
Michael Chan68929142005-08-09 20:17:14 -070015308
Michael Chanbbadf502006-04-06 21:46:34 -070015309 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015310 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070015311 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070015312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015313 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015314
Matt Carlson16821282011-07-13 09:27:28 +000015315 /* The memory arbiter has to be enabled in order for SRAM accesses
15316 * to succeed. Normally on powerup the tg3 chip firmware will make
15317 * sure it is enabled, but other entities such as system netboot
15318 * code might disable it.
15319 */
15320 val = tr32(MEMARB_MODE);
15321 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15322
Matt Carlson9dc5e342011-11-04 09:15:02 +000015323 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
15324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
15325 tg3_flag(tp, 5780_CLASS)) {
15326 if (tg3_flag(tp, PCIX_MODE)) {
15327 pci_read_config_dword(tp->pdev,
15328 tp->pcix_cap + PCI_X_STATUS,
15329 &val);
15330 tp->pci_fn = val & 0x7;
15331 }
15332 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
15333 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15334 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15335 NIC_SRAM_CPMUSTAT_SIG) {
15336 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
15337 tp->pci_fn = tp->pci_fn ? 1 : 0;
15338 }
15339 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15340 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
15341 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15342 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15343 NIC_SRAM_CPMUSTAT_SIG) {
15344 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15345 TG3_CPMU_STATUS_FSHFT_5719;
15346 }
Matt Carlson69f11c92011-07-13 09:27:30 +000015347 }
15348
Michael Chan7d0c41e2005-04-21 17:06:20 -070015349 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015350 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015351 * determined before calling tg3_set_power_state() so that
15352 * we know whether or not to switch out of Vaux power.
15353 * When the flag is set, it means that GPIO1 is used for eeprom
15354 * write protect and also implies that it is a LOM where GPIOs
15355 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015356 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015357 tg3_get_eeprom_hw_cfg(tp);
15358
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015359 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15360 tg3_flag_clear(tp, TSO_CAPABLE);
15361 tg3_flag_clear(tp, TSO_BUG);
15362 tp->fw_needed = NULL;
15363 }
15364
Joe Perches63c3a662011-04-26 08:12:10 +000015365 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015366 /* Allow reads and writes to the
15367 * APE register and memory space.
15368 */
15369 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015370 PCISTATE_ALLOW_APE_SHMEM_WR |
15371 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015372 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15373 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015374
15375 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015376 }
15377
Matt Carlson16821282011-07-13 09:27:28 +000015378 /* Set up tp->grc_local_ctrl before calling
15379 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15380 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015381 * It is also used as eeprom write protect on LOMs.
15382 */
15383 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015385 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015386 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15387 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015388 /* Unused GPIO3 must be driven as output on 5752 because there
15389 * are no pull-up resistors on unused GPIO pins.
15390 */
15391 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
15392 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015393
Matt Carlson321d32a2008-11-21 17:22:19 -080015394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000015395 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015396 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015397 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15398
Matt Carlson8d519ab2009-04-20 06:58:01 +000015399 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15400 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015401 /* Turn off the debug UART. */
15402 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015403 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015404 /* Keep VMain power. */
15405 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15406 GRC_LCLCTRL_GPIO_OUTPUT0;
15407 }
15408
Matt Carlson16821282011-07-13 09:27:28 +000015409 /* Switch out of Vaux if it is a NIC */
15410 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015411
Linus Torvalds1da177e2005-04-16 15:20:36 -070015412 /* Derive initial jumbo mode from MTU assigned in
15413 * ether_setup() via the alloc_etherdev() call
15414 */
Joe Perches63c3a662011-04-26 08:12:10 +000015415 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15416 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015417
15418 /* Determine WakeOnLan speed to use. */
15419 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15420 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
15421 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
15422 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015423 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015425 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015426 }
15427
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015428 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015429 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015430
Linus Torvalds1da177e2005-04-16 15:20:36 -070015431 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015432 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15433 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070015435 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015436 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15437 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15438 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015439
15440 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
15441 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015442 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015443 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015444 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445
Joe Perches63c3a662011-04-26 08:12:10 +000015446 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015447 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080015448 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015449 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015450 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070015451 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070015452 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070015453 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
15454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015455 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15456 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015457 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015458 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015459 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015460 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015461 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015463
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015464 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15465 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
15466 tp->phy_otp = tg3_read_otp_phycfg(tp);
15467 if (tp->phy_otp == 0)
15468 tp->phy_otp = TG3_OTP_DEFAULT;
15469 }
15470
Joe Perches63c3a662011-04-26 08:12:10 +000015471 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015472 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15473 else
15474 tp->mi_mode = MAC_MI_MODE_BASE;
15475
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015477 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
15478 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
15479 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15480
Matt Carlson4d958472011-04-20 07:57:35 +000015481 /* Set these bits to enable statistics workaround. */
15482 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15483 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
15484 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
15485 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15486 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15487 }
15488
Matt Carlson321d32a2008-11-21 17:22:19 -080015489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015491 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015492
Matt Carlson158d7ab2008-05-29 01:37:54 -070015493 err = tg3_mdio_init(tp);
15494 if (err)
15495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015496
15497 /* Initialize data/descriptor byte/word swapping. */
15498 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000015499 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15500 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15501 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15502 GRC_MODE_B2HRX_ENABLE |
15503 GRC_MODE_HTX2B_ENABLE |
15504 GRC_MODE_HOST_STACKUP);
15505 else
15506 val &= GRC_MODE_HOST_STACKUP;
15507
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 tw32(GRC_MODE, val | tp->grc_mode);
15509
15510 tg3_switch_clocks(tp);
15511
15512 /* Clear this out for sanity. */
15513 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15514
15515 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15516 &pci_state_reg);
15517 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015518 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015519 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
15520
15521 if (chiprevid == CHIPREV_ID_5701_A0 ||
15522 chiprevid == CHIPREV_ID_5701_B0 ||
15523 chiprevid == CHIPREV_ID_5701_B2 ||
15524 chiprevid == CHIPREV_ID_5701_B5) {
15525 void __iomem *sram_base;
15526
15527 /* Write some dummy words into the SRAM status block
15528 * area, see if it reads back correctly. If the return
15529 * value is bad, force enable the PCIX workaround.
15530 */
15531 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15532
15533 writel(0x00000000, sram_base);
15534 writel(0x00000000, sram_base + 4);
15535 writel(0xffffffff, sram_base + 4);
15536 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015537 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538 }
15539 }
15540
15541 udelay(50);
15542 tg3_nvram_init(tp);
15543
15544 grc_misc_cfg = tr32(GRC_MISC_CFG);
15545 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15546
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
15548 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15549 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015550 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551
Joe Perches63c3a662011-04-26 08:12:10 +000015552 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000015553 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015554 tg3_flag_set(tp, TAGGED_STATUS);
15555 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015556 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15557 HOSTCC_MODE_CLRTICK_TXBD);
15558
15559 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15560 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15561 tp->misc_host_ctrl);
15562 }
15563
Matt Carlson3bda1252008-08-15 14:08:22 -070015564 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015565 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015566 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015567 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015568 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015569
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015570 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015571 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015572
15573 err = tg3_phy_probe(tp);
15574 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015575 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015576 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015577 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578 }
15579
Matt Carlson184b8902010-04-05 10:19:25 +000015580 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015581 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015582
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015583 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15584 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 } else {
15586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015587 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015589 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015590 }
15591
15592 /* 5700 {AX,BX} chips have a broken status block link
15593 * change bit implementation, so we must use the
15594 * status register in those cases.
15595 */
15596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015597 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015598 else
Joe Perches63c3a662011-04-26 08:12:10 +000015599 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015600
15601 /* The led_ctrl is set during tg3_phy_probe, here we might
15602 * have to force the link status polling mechanism based
15603 * upon subsystem IDs.
15604 */
15605 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070015606 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015607 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15608 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015609 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015610 }
15611
15612 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015613 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015614 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015615 else
Joe Perches63c3a662011-04-26 08:12:10 +000015616 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015617
Eric Dumazet9205fd92011-11-18 06:47:01 +000015618 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015619 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015621 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015622 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015623#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015624 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015625#endif
15626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015627
Matt Carlson2c49a442010-09-30 10:34:35 +000015628 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15629 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015630 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15631
Matt Carlson2c49a442010-09-30 10:34:35 +000015632 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015633
15634 /* Increment the rx prod index on the rx std ring by at most
15635 * 8 for these chips to workaround hw errata.
15636 */
15637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
15638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
15639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
15640 tp->rx_std_max_post = 8;
15641
Joe Perches63c3a662011-04-26 08:12:10 +000015642 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015643 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15644 PCIE_PWR_MGMT_L1_THRESH_MSK;
15645
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646 return err;
15647}
15648
David S. Miller49b6e95f2007-03-29 01:38:42 -070015649#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015650static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015651{
15652 struct net_device *dev = tp->dev;
15653 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015654 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015655 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015656 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015657
David S. Miller49b6e95f2007-03-29 01:38:42 -070015658 addr = of_get_property(dp, "local-mac-address", &len);
15659 if (addr && len == 6) {
15660 memcpy(dev->dev_addr, addr, 6);
15661 memcpy(dev->perm_addr, dev->dev_addr, 6);
15662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015663 }
15664 return -ENODEV;
15665}
15666
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015667static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015668{
15669 struct net_device *dev = tp->dev;
15670
15671 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070015672 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015673 return 0;
15674}
15675#endif
15676
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015677static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678{
15679 struct net_device *dev = tp->dev;
15680 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015681 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015682
David S. Miller49b6e95f2007-03-29 01:38:42 -070015683#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015684 if (!tg3_get_macaddr_sparc(tp))
15685 return 0;
15686#endif
15687
15688 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015690 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015691 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15692 mac_offset = 0xcc;
15693 if (tg3_nvram_lock(tp))
15694 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15695 else
15696 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015697 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015698 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015699 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015700 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015701 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000015702 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015703 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015704
15705 /* First try to get it from MAC address mailbox. */
15706 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15707 if ((hi >> 16) == 0x484b) {
15708 dev->dev_addr[0] = (hi >> 8) & 0xff;
15709 dev->dev_addr[1] = (hi >> 0) & 0xff;
15710
15711 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15712 dev->dev_addr[2] = (lo >> 24) & 0xff;
15713 dev->dev_addr[3] = (lo >> 16) & 0xff;
15714 dev->dev_addr[4] = (lo >> 8) & 0xff;
15715 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716
Michael Chan008652b2006-03-27 23:14:53 -080015717 /* Some old bootcode may report a 0 MAC address in SRAM */
15718 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15719 }
15720 if (!addr_ok) {
15721 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015722 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015723 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015724 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015725 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15726 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015727 }
15728 /* Finally just fetch it out of the MAC control regs. */
15729 else {
15730 hi = tr32(MAC_ADDR_0_HIGH);
15731 lo = tr32(MAC_ADDR_0_LOW);
15732
15733 dev->dev_addr[5] = lo & 0xff;
15734 dev->dev_addr[4] = (lo >> 8) & 0xff;
15735 dev->dev_addr[3] = (lo >> 16) & 0xff;
15736 dev->dev_addr[2] = (lo >> 24) & 0xff;
15737 dev->dev_addr[1] = hi & 0xff;
15738 dev->dev_addr[0] = (hi >> 8) & 0xff;
15739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015740 }
15741
15742 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015743#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 if (!tg3_get_default_macaddr_sparc(tp))
15745 return 0;
15746#endif
15747 return -EINVAL;
15748 }
John W. Linville2ff43692005-09-12 14:44:20 -070015749 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015750 return 0;
15751}
15752
David S. Miller59e6b432005-05-18 22:50:10 -070015753#define BOUNDARY_SINGLE_CACHELINE 1
15754#define BOUNDARY_MULTI_CACHELINE 2
15755
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015756static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015757{
15758 int cacheline_size;
15759 u8 byte;
15760 int goal;
15761
15762 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
15763 if (byte == 0)
15764 cacheline_size = 1024;
15765 else
15766 cacheline_size = (int) byte * 4;
15767
15768 /* On 5703 and later chips, the boundary bits have no
15769 * effect.
15770 */
15771 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15772 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015773 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070015774 goto out;
15775
15776#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
15777 goal = BOUNDARY_MULTI_CACHELINE;
15778#else
15779#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
15780 goal = BOUNDARY_SINGLE_CACHELINE;
15781#else
15782 goal = 0;
15783#endif
15784#endif
15785
Joe Perches63c3a662011-04-26 08:12:10 +000015786 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015787 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
15788 goto out;
15789 }
15790
David S. Miller59e6b432005-05-18 22:50:10 -070015791 if (!goal)
15792 goto out;
15793
15794 /* PCI controllers on most RISC systems tend to disconnect
15795 * when a device tries to burst across a cache-line boundary.
15796 * Therefore, letting tg3 do so just wastes PCI bandwidth.
15797 *
15798 * Unfortunately, for PCI-E there are only limited
15799 * write-side controls for this, and thus for reads
15800 * we will still get the disconnects. We'll also waste
15801 * these PCI cycles for both read and write for chips
15802 * other than 5700 and 5701 which do not implement the
15803 * boundary bits.
15804 */
Joe Perches63c3a662011-04-26 08:12:10 +000015805 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015806 switch (cacheline_size) {
15807 case 16:
15808 case 32:
15809 case 64:
15810 case 128:
15811 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15812 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15813 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15814 } else {
15815 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15816 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15817 }
15818 break;
15819
15820 case 256:
15821 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15822 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15823 break;
15824
15825 default:
15826 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15827 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15828 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015829 }
Joe Perches63c3a662011-04-26 08:12:10 +000015830 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015831 switch (cacheline_size) {
15832 case 16:
15833 case 32:
15834 case 64:
15835 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15836 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15837 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15838 break;
15839 }
15840 /* fallthrough */
15841 case 128:
15842 default:
15843 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15844 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15845 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015846 }
David S. Miller59e6b432005-05-18 22:50:10 -070015847 } else {
15848 switch (cacheline_size) {
15849 case 16:
15850 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15851 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15852 DMA_RWCTRL_WRITE_BNDRY_16);
15853 break;
15854 }
15855 /* fallthrough */
15856 case 32:
15857 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15858 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15859 DMA_RWCTRL_WRITE_BNDRY_32);
15860 break;
15861 }
15862 /* fallthrough */
15863 case 64:
15864 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15865 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15866 DMA_RWCTRL_WRITE_BNDRY_64);
15867 break;
15868 }
15869 /* fallthrough */
15870 case 128:
15871 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15872 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15873 DMA_RWCTRL_WRITE_BNDRY_128);
15874 break;
15875 }
15876 /* fallthrough */
15877 case 256:
15878 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15879 DMA_RWCTRL_WRITE_BNDRY_256);
15880 break;
15881 case 512:
15882 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15883 DMA_RWCTRL_WRITE_BNDRY_512);
15884 break;
15885 case 1024:
15886 default:
15887 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15888 DMA_RWCTRL_WRITE_BNDRY_1024);
15889 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015890 }
David S. Miller59e6b432005-05-18 22:50:10 -070015891 }
15892
15893out:
15894 return val;
15895}
15896
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015897static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
15898 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015899{
15900 struct tg3_internal_buffer_desc test_desc;
15901 u32 sram_dma_descs;
15902 int i, ret;
15903
15904 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15905
15906 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15907 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15908 tw32(RDMAC_STATUS, 0);
15909 tw32(WDMAC_STATUS, 0);
15910
15911 tw32(BUFMGR_MODE, 0);
15912 tw32(FTQ_RESET, 0);
15913
15914 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15915 test_desc.addr_lo = buf_dma & 0xffffffff;
15916 test_desc.nic_mbuf = 0x00002100;
15917 test_desc.len = size;
15918
15919 /*
15920 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15921 * the *second* time the tg3 driver was getting loaded after an
15922 * initial scan.
15923 *
15924 * Broadcom tells me:
15925 * ...the DMA engine is connected to the GRC block and a DMA
15926 * reset may affect the GRC block in some unpredictable way...
15927 * The behavior of resets to individual blocks has not been tested.
15928 *
15929 * Broadcom noted the GRC reset will also reset all sub-components.
15930 */
15931 if (to_device) {
15932 test_desc.cqid_sqid = (13 << 8) | 2;
15933
15934 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15935 udelay(40);
15936 } else {
15937 test_desc.cqid_sqid = (16 << 8) | 7;
15938
15939 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15940 udelay(40);
15941 }
15942 test_desc.flags = 0x00000005;
15943
15944 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15945 u32 val;
15946
15947 val = *(((u32 *)&test_desc) + i);
15948 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15949 sram_dma_descs + (i * sizeof(u32)));
15950 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15951 }
15952 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15953
Matt Carlson859a588792010-04-05 10:19:28 +000015954 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015955 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015956 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015957 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015958
15959 ret = -ENODEV;
15960 for (i = 0; i < 40; i++) {
15961 u32 val;
15962
15963 if (to_device)
15964 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15965 else
15966 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15967 if ((val & 0xffff) == sram_dma_descs) {
15968 ret = 0;
15969 break;
15970 }
15971
15972 udelay(100);
15973 }
15974
15975 return ret;
15976}
15977
David S. Millerded73402005-05-23 13:59:47 -070015978#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015979
Matt Carlson41434702011-03-09 16:58:22 +000015980static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015981 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15982 { },
15983};
15984
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015985static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015986{
15987 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015988 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015989 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015990
Matt Carlson4bae65c2010-11-24 08:31:52 +000015991 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15992 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015993 if (!buf) {
15994 ret = -ENOMEM;
15995 goto out_nofree;
15996 }
15997
15998 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15999 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
16000
David S. Miller59e6b432005-05-18 22:50:10 -070016001 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016002
Joe Perches63c3a662011-04-26 08:12:10 +000016003 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000016004 goto out;
16005
Joe Perches63c3a662011-04-26 08:12:10 +000016006 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016007 /* DMA read watermark not used on PCIE */
16008 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000016009 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070016010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
16011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016012 tp->dma_rwctrl |= 0x003f0000;
16013 else
16014 tp->dma_rwctrl |= 0x003f000f;
16015 } else {
16016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
16018 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016019 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016020
Michael Chan4a29cc22006-03-19 13:21:12 -080016021 /* If the 5704 is behind the EPB bridge, we can
16022 * do the less restrictive ONE_DMA workaround for
16023 * better performance.
16024 */
Joe Perches63c3a662011-04-26 08:12:10 +000016025 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080016026 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16027 tp->dma_rwctrl |= 0x8000;
16028 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016029 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16030
Michael Chan49afdeb2007-02-13 12:17:03 -080016031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
16032 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016033 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016034 tp->dma_rwctrl |=
16035 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16036 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16037 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070016038 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
16039 /* 5780 always in PCIX mode */
16040 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070016041 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
16042 /* 5714 always in PCIX mode */
16043 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016044 } else {
16045 tp->dma_rwctrl |= 0x001b000f;
16046 }
16047 }
16048
16049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16051 tp->dma_rwctrl &= 0xfffffff0;
16052
16053 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
16054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
16055 /* Remove this if it causes problems for some boards. */
16056 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16057
16058 /* On 5700/5701 chips, we need to set this bit.
16059 * Otherwise the chip will issue cacheline transactions
16060 * to streamable DMA memory with not all the byte
16061 * enables turned on. This is an error on several
16062 * RISC PCI controllers, in particular sparc64.
16063 *
16064 * On 5703/5704 chips, this bit has been reassigned
16065 * a different meaning. In particular, it is used
16066 * on those chips to enable a PCI-X workaround.
16067 */
16068 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16069 }
16070
16071 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16072
16073#if 0
16074 /* Unneeded, already done by tg3_get_invariants. */
16075 tg3_switch_clocks(tp);
16076#endif
16077
Linus Torvalds1da177e2005-04-16 15:20:36 -070016078 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
16079 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
16080 goto out;
16081
David S. Miller59e6b432005-05-18 22:50:10 -070016082 /* It is best to perform DMA test with maximum write burst size
16083 * to expose the 5700/5701 write DMA bug.
16084 */
16085 saved_dma_rwctrl = tp->dma_rwctrl;
16086 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16087 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16088
Linus Torvalds1da177e2005-04-16 15:20:36 -070016089 while (1) {
16090 u32 *p = buf, i;
16091
16092 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16093 p[i] = i;
16094
16095 /* Send the buffer to the chip. */
16096 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16097 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016098 dev_err(&tp->pdev->dev,
16099 "%s: Buffer write failed. err = %d\n",
16100 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016101 break;
16102 }
16103
16104#if 0
16105 /* validate data reached card RAM correctly. */
16106 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16107 u32 val;
16108 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16109 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016110 dev_err(&tp->pdev->dev,
16111 "%s: Buffer corrupted on device! "
16112 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016113 /* ret = -ENODEV here? */
16114 }
16115 p[i] = 0;
16116 }
16117#endif
16118 /* Now read it back. */
16119 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16120 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016121 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16122 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016123 break;
16124 }
16125
16126 /* Verify it. */
16127 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16128 if (p[i] == i)
16129 continue;
16130
David S. Miller59e6b432005-05-18 22:50:10 -070016131 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16132 DMA_RWCTRL_WRITE_BNDRY_16) {
16133 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016134 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16135 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16136 break;
16137 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016138 dev_err(&tp->pdev->dev,
16139 "%s: Buffer corrupted on read back! "
16140 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016141 ret = -ENODEV;
16142 goto out;
16143 }
16144 }
16145
16146 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16147 /* Success. */
16148 ret = 0;
16149 break;
16150 }
16151 }
David S. Miller59e6b432005-05-18 22:50:10 -070016152 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16153 DMA_RWCTRL_WRITE_BNDRY_16) {
16154 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016155 * now look for chipsets that are known to expose the
16156 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016157 */
Matt Carlson41434702011-03-09 16:58:22 +000016158 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016159 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16160 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016161 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016162 /* Safe to use the calculated DMA boundary. */
16163 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016164 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016165
David S. Miller59e6b432005-05-18 22:50:10 -070016166 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016168
16169out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016170 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016171out_nofree:
16172 return ret;
16173}
16174
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016175static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016176{
Joe Perches63c3a662011-04-26 08:12:10 +000016177 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016178 tp->bufmgr_config.mbuf_read_dma_low_water =
16179 DEFAULT_MB_RDMA_LOW_WATER_5705;
16180 tp->bufmgr_config.mbuf_mac_rx_low_water =
16181 DEFAULT_MB_MACRX_LOW_WATER_57765;
16182 tp->bufmgr_config.mbuf_high_water =
16183 DEFAULT_MB_HIGH_WATER_57765;
16184
16185 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16186 DEFAULT_MB_RDMA_LOW_WATER_5705;
16187 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16188 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16189 tp->bufmgr_config.mbuf_high_water_jumbo =
16190 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016191 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016192 tp->bufmgr_config.mbuf_read_dma_low_water =
16193 DEFAULT_MB_RDMA_LOW_WATER_5705;
16194 tp->bufmgr_config.mbuf_mac_rx_low_water =
16195 DEFAULT_MB_MACRX_LOW_WATER_5705;
16196 tp->bufmgr_config.mbuf_high_water =
16197 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070016198 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
16199 tp->bufmgr_config.mbuf_mac_rx_low_water =
16200 DEFAULT_MB_MACRX_LOW_WATER_5906;
16201 tp->bufmgr_config.mbuf_high_water =
16202 DEFAULT_MB_HIGH_WATER_5906;
16203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016204
Michael Chanfdfec1722005-07-25 12:31:48 -070016205 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16206 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16207 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16208 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16209 tp->bufmgr_config.mbuf_high_water_jumbo =
16210 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16211 } else {
16212 tp->bufmgr_config.mbuf_read_dma_low_water =
16213 DEFAULT_MB_RDMA_LOW_WATER;
16214 tp->bufmgr_config.mbuf_mac_rx_low_water =
16215 DEFAULT_MB_MACRX_LOW_WATER;
16216 tp->bufmgr_config.mbuf_high_water =
16217 DEFAULT_MB_HIGH_WATER;
16218
16219 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16220 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16221 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16222 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16223 tp->bufmgr_config.mbuf_high_water_jumbo =
16224 DEFAULT_MB_HIGH_WATER_JUMBO;
16225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016226
16227 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16228 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16229}
16230
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016231static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016232{
Matt Carlson79eb6902010-02-17 15:17:03 +000016233 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16234 case TG3_PHY_ID_BCM5400: return "5400";
16235 case TG3_PHY_ID_BCM5401: return "5401";
16236 case TG3_PHY_ID_BCM5411: return "5411";
16237 case TG3_PHY_ID_BCM5701: return "5701";
16238 case TG3_PHY_ID_BCM5703: return "5703";
16239 case TG3_PHY_ID_BCM5704: return "5704";
16240 case TG3_PHY_ID_BCM5705: return "5705";
16241 case TG3_PHY_ID_BCM5750: return "5750";
16242 case TG3_PHY_ID_BCM5752: return "5752";
16243 case TG3_PHY_ID_BCM5714: return "5714";
16244 case TG3_PHY_ID_BCM5780: return "5780";
16245 case TG3_PHY_ID_BCM5755: return "5755";
16246 case TG3_PHY_ID_BCM5787: return "5787";
16247 case TG3_PHY_ID_BCM5784: return "5784";
16248 case TG3_PHY_ID_BCM5756: return "5722/5756";
16249 case TG3_PHY_ID_BCM5906: return "5906";
16250 case TG3_PHY_ID_BCM5761: return "5761";
16251 case TG3_PHY_ID_BCM5718C: return "5718C";
16252 case TG3_PHY_ID_BCM5718S: return "5718S";
16253 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016254 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016255 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016256 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016257 case 0: return "serdes";
16258 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016260}
16261
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016262static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016263{
Joe Perches63c3a662011-04-26 08:12:10 +000016264 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016265 strcpy(str, "PCI Express");
16266 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016267 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016268 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16269
16270 strcpy(str, "PCIX:");
16271
16272 if ((clock_ctrl == 7) ||
16273 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16274 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16275 strcat(str, "133MHz");
16276 else if (clock_ctrl == 0)
16277 strcat(str, "33MHz");
16278 else if (clock_ctrl == 2)
16279 strcat(str, "50MHz");
16280 else if (clock_ctrl == 4)
16281 strcat(str, "66MHz");
16282 else if (clock_ctrl == 6)
16283 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016284 } else {
16285 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016286 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016287 strcat(str, "66MHz");
16288 else
16289 strcat(str, "33MHz");
16290 }
Joe Perches63c3a662011-04-26 08:12:10 +000016291 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016292 strcat(str, ":32-bit");
16293 else
16294 strcat(str, ":64-bit");
16295 return str;
16296}
16297
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016298static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016299{
16300 struct ethtool_coalesce *ec = &tp->coal;
16301
16302 memset(ec, 0, sizeof(*ec));
16303 ec->cmd = ETHTOOL_GCOALESCE;
16304 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16305 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16306 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16307 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16308 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16309 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16310 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16311 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16312 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16313
16314 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16315 HOSTCC_MODE_CLRTICK_TXBD)) {
16316 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16317 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16318 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16319 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16320 }
Michael Chand244c892005-07-05 14:42:33 -070016321
Joe Perches63c3a662011-04-26 08:12:10 +000016322 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016323 ec->rx_coalesce_usecs_irq = 0;
16324 ec->tx_coalesce_usecs_irq = 0;
16325 ec->stats_block_coalesce_usecs = 0;
16326 }
David S. Miller15f98502005-05-18 22:49:26 -070016327}
16328
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016329static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016330 const struct pci_device_id *ent)
16331{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016332 struct net_device *dev;
16333 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016334 int i, err, pm_cap;
16335 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016336 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016337 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016338 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016339
Joe Perches05dbe002010-02-17 19:44:19 +000016340 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016341
16342 err = pci_enable_device(pdev);
16343 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016344 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016345 return err;
16346 }
16347
Linus Torvalds1da177e2005-04-16 15:20:36 -070016348 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16349 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016350 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016351 goto err_out_disable_pdev;
16352 }
16353
16354 pci_set_master(pdev);
16355
16356 /* Find power-management capability. */
16357 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16358 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016359 dev_err(&pdev->dev,
16360 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016361 err = -EIO;
16362 goto err_out_free_res;
16363 }
16364
Matt Carlson16821282011-07-13 09:27:28 +000016365 err = pci_set_power_state(pdev, PCI_D0);
16366 if (err) {
16367 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16368 goto err_out_free_res;
16369 }
16370
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016371 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016372 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016373 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016374 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016375 }
16376
Linus Torvalds1da177e2005-04-16 15:20:36 -070016377 SET_NETDEV_DEV(dev, &pdev->dev);
16378
Linus Torvalds1da177e2005-04-16 15:20:36 -070016379 tp = netdev_priv(dev);
16380 tp->pdev = pdev;
16381 tp->dev = dev;
16382 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016383 tp->rx_mode = TG3_DEF_RX_MODE;
16384 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016385 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016386
Linus Torvalds1da177e2005-04-16 15:20:36 -070016387 if (tg3_debug > 0)
16388 tp->msg_enable = tg3_debug;
16389 else
16390 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16391
16392 /* The word/byte swap controls here control register access byte
16393 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16394 * setting below.
16395 */
16396 tp->misc_host_ctrl =
16397 MISC_HOST_CTRL_MASK_PCI_INT |
16398 MISC_HOST_CTRL_WORD_SWAP |
16399 MISC_HOST_CTRL_INDIR_ACCESS |
16400 MISC_HOST_CTRL_PCISTATE_RW;
16401
16402 /* The NONFRM (non-frame) byte/word swap controls take effect
16403 * on descriptor entries, anything which isn't packet data.
16404 *
16405 * The StrongARM chips on the board (one for tx, one for rx)
16406 * are running in big-endian mode.
16407 */
16408 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16409 GRC_MODE_WSWAP_NONFRM_DATA);
16410#ifdef __BIG_ENDIAN
16411 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16412#endif
16413 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016414 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016415 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416
Matt Carlsond5fe4882008-11-21 17:20:32 -080016417 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016418 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016419 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016420 err = -ENOMEM;
16421 goto err_out_free_dev;
16422 }
16423
Matt Carlsonc9cab242011-07-13 09:27:27 +000016424 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16425 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16427 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16428 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016429 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016430 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16431 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
16432 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
16433 tg3_flag_set(tp, ENABLE_APE);
16434 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16435 if (!tp->aperegs) {
16436 dev_err(&pdev->dev,
16437 "Cannot map APE registers, aborting\n");
16438 err = -ENOMEM;
16439 goto err_out_iounmap;
16440 }
16441 }
16442
Linus Torvalds1da177e2005-04-16 15:20:36 -070016443 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16444 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016445
Linus Torvalds1da177e2005-04-16 15:20:36 -070016446 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016447 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016448 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016449 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016450
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016451 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016452 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016453 dev_err(&pdev->dev,
16454 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016455 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016456 }
16457
Michael Chan4a29cc22006-03-19 13:21:12 -080016458 /* The EPB bridge inside 5714, 5715, and 5780 and any
16459 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016460 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16461 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16462 * do DMA address check in tg3_start_xmit().
16463 */
Joe Perches63c3a662011-04-26 08:12:10 +000016464 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016465 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016466 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016467 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016468#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016469 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016470#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016471 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016472 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016473
16474 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016475 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016476 err = pci_set_dma_mask(pdev, dma_mask);
16477 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016478 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016479 err = pci_set_consistent_dma_mask(pdev,
16480 persist_dma_mask);
16481 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016482 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16483 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016484 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016485 }
16486 }
16487 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016488 if (err || dma_mask == DMA_BIT_MASK(32)) {
16489 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016490 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016491 dev_err(&pdev->dev,
16492 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016493 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016494 }
16495 }
16496
Michael Chanfdfec1722005-07-25 12:31:48 -070016497 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016498
Matt Carlson0da06062011-05-19 12:12:53 +000016499 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16500
16501 /* 5700 B0 chips do not support checksumming correctly due
16502 * to hardware bugs.
16503 */
16504 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
16505 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16506
16507 if (tg3_flag(tp, 5755_PLUS))
16508 features |= NETIF_F_IPV6_CSUM;
16509 }
16510
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016511 /* TSO is on by default on chips that support hardware TSO.
16512 * Firmware TSO on older chips gives lower performance, so it
16513 * is off by default, but can be enabled using ethtool.
16514 */
Joe Perches63c3a662011-04-26 08:12:10 +000016515 if ((tg3_flag(tp, HW_TSO_1) ||
16516 tg3_flag(tp, HW_TSO_2) ||
16517 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016518 (features & NETIF_F_IP_CSUM))
16519 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016520 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016521 if (features & NETIF_F_IPV6_CSUM)
16522 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016523 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000016524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070016525 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
16526 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000016527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000016528 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016529 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016531
Matt Carlsond542fe22011-05-19 16:02:43 +000016532 dev->features |= features;
16533 dev->vlan_features |= features;
16534
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016535 /*
16536 * Add loopback capability only for a subset of devices that support
16537 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16538 * loopback for the remaining devices.
16539 */
16540 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
16541 !tg3_flag(tp, CPMU_PRESENT))
16542 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016543 features |= NETIF_F_LOOPBACK;
16544
Matt Carlson0da06062011-05-19 12:12:53 +000016545 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016546
Linus Torvalds1da177e2005-04-16 15:20:36 -070016547 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016548 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016549 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016550 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016551 tp->rx_pending = 63;
16552 }
16553
Linus Torvalds1da177e2005-04-16 15:20:36 -070016554 err = tg3_get_device_address(tp);
16555 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016556 dev_err(&pdev->dev,
16557 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016558 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016559 }
16560
Matt Carlsonc88864d2007-11-12 21:07:01 -080016561 /*
16562 * Reset chip in case UNDI or EFI driver did not shutdown
16563 * DMA self test will enable WDMAC and we'll see (spurious)
16564 * pending DMA on the PCI bus at that point.
16565 */
16566 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16567 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16568 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16569 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16570 }
16571
16572 err = tg3_test_dma(tp);
16573 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016574 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016575 goto err_out_apeunmap;
16576 }
16577
Matt Carlson78f90dc2009-11-13 13:03:42 +000016578 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16579 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16580 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016581 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016582 struct tg3_napi *tnapi = &tp->napi[i];
16583
16584 tnapi->tp = tp;
16585 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16586
16587 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016588 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016589 intmbx += 0x8;
16590 else
16591 intmbx += 0x4;
16592
16593 tnapi->consmbox = rcvmbx;
16594 tnapi->prodmbox = sndmbx;
16595
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016596 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016597 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016598 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016599 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016600
Joe Perches63c3a662011-04-26 08:12:10 +000016601 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016602 break;
16603
16604 /*
16605 * If we support MSIX, we'll be using RSS. If we're using
16606 * RSS, the first vector only handles link interrupts and the
16607 * remaining vectors handle rx and tx interrupts. Reuse the
16608 * mailbox values for the next iteration. The values we setup
16609 * above are still useful for the single vectored mode.
16610 */
16611 if (!i)
16612 continue;
16613
16614 rcvmbx += 0x8;
16615
16616 if (sndmbx & 0x4)
16617 sndmbx -= 0x4;
16618 else
16619 sndmbx += 0xc;
16620 }
16621
Matt Carlsonc88864d2007-11-12 21:07:01 -080016622 tg3_init_coal(tp);
16623
Michael Chanc49a1562006-12-17 17:07:29 -080016624 pci_set_drvdata(pdev, dev);
16625
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
16627 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
16628 tg3_flag_set(tp, PTP_CAPABLE);
16629
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016630 if (tg3_flag(tp, 5717_PLUS)) {
16631 /* Resume a low-power mode */
16632 tg3_frob_aux_power(tp, false);
16633 }
16634
Matt Carlson21f76382012-02-22 12:35:21 +000016635 tg3_timer_init(tp);
16636
Linus Torvalds1da177e2005-04-16 15:20:36 -070016637 err = register_netdev(dev);
16638 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016639 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016640 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016641 }
16642
Joe Perches05dbe002010-02-17 19:44:19 +000016643 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16644 tp->board_part_number,
16645 tp->pci_chip_rev_id,
16646 tg3_bus_string(tp, str),
16647 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016648
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016649 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016650 struct phy_device *phydev;
16651 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016652 netdev_info(dev,
16653 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016654 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016655 } else {
16656 char *ethtype;
16657
16658 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16659 ethtype = "10/100Base-TX";
16660 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16661 ethtype = "1000Base-SX";
16662 else
16663 ethtype = "10/100/1000Base-T";
16664
Matt Carlson5129c3a2010-04-05 10:19:23 +000016665 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016666 "(WireSpeed[%d], EEE[%d])\n",
16667 tg3_phy_string(tp), ethtype,
16668 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16669 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016670 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016671
Joe Perches05dbe002010-02-17 19:44:19 +000016672 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016673 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016674 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016675 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016676 tg3_flag(tp, ENABLE_ASF) != 0,
16677 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016678 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16679 tp->dma_rwctrl,
16680 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16681 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016682
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016683 pci_save_state(pdev);
16684
Linus Torvalds1da177e2005-04-16 15:20:36 -070016685 return 0;
16686
Matt Carlson0d3031d2007-10-10 18:02:43 -070016687err_out_apeunmap:
16688 if (tp->aperegs) {
16689 iounmap(tp->aperegs);
16690 tp->aperegs = NULL;
16691 }
16692
Linus Torvalds1da177e2005-04-16 15:20:36 -070016693err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016694 if (tp->regs) {
16695 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016696 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016698
16699err_out_free_dev:
16700 free_netdev(dev);
16701
Matt Carlson16821282011-07-13 09:27:28 +000016702err_out_power_down:
16703 pci_set_power_state(pdev, PCI_D3hot);
16704
Linus Torvalds1da177e2005-04-16 15:20:36 -070016705err_out_free_res:
16706 pci_release_regions(pdev);
16707
16708err_out_disable_pdev:
16709 pci_disable_device(pdev);
16710 pci_set_drvdata(pdev, NULL);
16711 return err;
16712}
16713
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016714static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016715{
16716 struct net_device *dev = pci_get_drvdata(pdev);
16717
16718 if (dev) {
16719 struct tg3 *tp = netdev_priv(dev);
16720
Jesper Juhle3c55302012-04-09 22:50:15 +020016721 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016722
Matt Carlsondb219972011-11-04 09:15:03 +000016723 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016724
David S. Miller1805b2f2011-10-24 18:18:09 -040016725 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016726 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016727 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016728 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016729
Linus Torvalds1da177e2005-04-16 15:20:36 -070016730 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016731 if (tp->aperegs) {
16732 iounmap(tp->aperegs);
16733 tp->aperegs = NULL;
16734 }
Michael Chan68929142005-08-09 20:17:14 -070016735 if (tp->regs) {
16736 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016737 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016739 free_netdev(dev);
16740 pci_release_regions(pdev);
16741 pci_disable_device(pdev);
16742 pci_set_drvdata(pdev, NULL);
16743 }
16744}
16745
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016746#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016747static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016748{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016749 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016750 struct net_device *dev = pci_get_drvdata(pdev);
16751 struct tg3 *tp = netdev_priv(dev);
16752 int err;
16753
16754 if (!netif_running(dev))
16755 return 0;
16756
Matt Carlsondb219972011-11-04 09:15:03 +000016757 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016758 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016759 tg3_netif_stop(tp);
16760
Matt Carlson21f76382012-02-22 12:35:21 +000016761 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016762
David S. Millerf47c11e2005-06-24 20:18:35 -070016763 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016764 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070016765 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016766
16767 netif_device_detach(dev);
16768
David S. Millerf47c11e2005-06-24 20:18:35 -070016769 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070016770 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000016771 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070016772 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016773
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016774 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016775 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016776 int err2;
16777
David S. Millerf47c11e2005-06-24 20:18:35 -070016778 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016779
Joe Perches63c3a662011-04-26 08:12:10 +000016780 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016781 err2 = tg3_restart_hw(tp, 1);
16782 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070016783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016784
Matt Carlson21f76382012-02-22 12:35:21 +000016785 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016786
16787 netif_device_attach(dev);
16788 tg3_netif_start(tp);
16789
Michael Chanb9ec6c12006-07-25 16:37:27 -070016790out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016791 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016792
16793 if (!err2)
16794 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016795 }
16796
16797 return err;
16798}
16799
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016800static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016801{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016802 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016803 struct net_device *dev = pci_get_drvdata(pdev);
16804 struct tg3 *tp = netdev_priv(dev);
16805 int err;
16806
16807 if (!netif_running(dev))
16808 return 0;
16809
Linus Torvalds1da177e2005-04-16 15:20:36 -070016810 netif_device_attach(dev);
16811
David S. Millerf47c11e2005-06-24 20:18:35 -070016812 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016813
Joe Perches63c3a662011-04-26 08:12:10 +000016814 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016815 err = tg3_restart_hw(tp, 1);
16816 if (err)
16817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818
Matt Carlson21f76382012-02-22 12:35:21 +000016819 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016820
Linus Torvalds1da177e2005-04-16 15:20:36 -070016821 tg3_netif_start(tp);
16822
Michael Chanb9ec6c12006-07-25 16:37:27 -070016823out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016824 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016825
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016826 if (!err)
16827 tg3_phy_start(tp);
16828
Michael Chanb9ec6c12006-07-25 16:37:27 -070016829 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016830}
16831
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016832static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016833#define TG3_PM_OPS (&tg3_pm_ops)
16834
16835#else
16836
16837#define TG3_PM_OPS NULL
16838
16839#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016840
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016841/**
16842 * tg3_io_error_detected - called when PCI error is detected
16843 * @pdev: Pointer to PCI device
16844 * @state: The current pci connection state
16845 *
16846 * This function is called after a PCI bus error affecting
16847 * this device has been detected.
16848 */
16849static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16850 pci_channel_state_t state)
16851{
16852 struct net_device *netdev = pci_get_drvdata(pdev);
16853 struct tg3 *tp = netdev_priv(netdev);
16854 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16855
16856 netdev_info(netdev, "PCI I/O error detected\n");
16857
16858 rtnl_lock();
16859
16860 if (!netif_running(netdev))
16861 goto done;
16862
16863 tg3_phy_stop(tp);
16864
16865 tg3_netif_stop(tp);
16866
Matt Carlson21f76382012-02-22 12:35:21 +000016867 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016868
16869 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016870 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016871
16872 netif_device_detach(netdev);
16873
16874 /* Clean up software state, even if MMIO is blocked */
16875 tg3_full_lock(tp, 0);
16876 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16877 tg3_full_unlock(tp);
16878
16879done:
16880 if (state == pci_channel_io_perm_failure)
16881 err = PCI_ERS_RESULT_DISCONNECT;
16882 else
16883 pci_disable_device(pdev);
16884
16885 rtnl_unlock();
16886
16887 return err;
16888}
16889
16890/**
16891 * tg3_io_slot_reset - called after the pci bus has been reset.
16892 * @pdev: Pointer to PCI device
16893 *
16894 * Restart the card from scratch, as if from a cold-boot.
16895 * At this point, the card has exprienced a hard reset,
16896 * followed by fixups by BIOS, and has its config space
16897 * set up identically to what it was at cold boot.
16898 */
16899static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16900{
16901 struct net_device *netdev = pci_get_drvdata(pdev);
16902 struct tg3 *tp = netdev_priv(netdev);
16903 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16904 int err;
16905
16906 rtnl_lock();
16907
16908 if (pci_enable_device(pdev)) {
16909 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16910 goto done;
16911 }
16912
16913 pci_set_master(pdev);
16914 pci_restore_state(pdev);
16915 pci_save_state(pdev);
16916
16917 if (!netif_running(netdev)) {
16918 rc = PCI_ERS_RESULT_RECOVERED;
16919 goto done;
16920 }
16921
16922 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016923 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016924 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016925
16926 rc = PCI_ERS_RESULT_RECOVERED;
16927
16928done:
16929 rtnl_unlock();
16930
16931 return rc;
16932}
16933
16934/**
16935 * tg3_io_resume - called when traffic can start flowing again.
16936 * @pdev: Pointer to PCI device
16937 *
16938 * This callback is called when the error recovery driver tells
16939 * us that its OK to resume normal operation.
16940 */
16941static void tg3_io_resume(struct pci_dev *pdev)
16942{
16943 struct net_device *netdev = pci_get_drvdata(pdev);
16944 struct tg3 *tp = netdev_priv(netdev);
16945 int err;
16946
16947 rtnl_lock();
16948
16949 if (!netif_running(netdev))
16950 goto done;
16951
16952 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016953 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016954 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016955 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016956 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016957 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16958 goto done;
16959 }
16960
16961 netif_device_attach(netdev);
16962
Matt Carlson21f76382012-02-22 12:35:21 +000016963 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016964
16965 tg3_netif_start(tp);
16966
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016967 tg3_full_unlock(tp);
16968
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016969 tg3_phy_start(tp);
16970
16971done:
16972 rtnl_unlock();
16973}
16974
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070016975static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016976 .error_detected = tg3_io_error_detected,
16977 .slot_reset = tg3_io_slot_reset,
16978 .resume = tg3_io_resume
16979};
16980
Linus Torvalds1da177e2005-04-16 15:20:36 -070016981static struct pci_driver tg3_driver = {
16982 .name = DRV_MODULE_NAME,
16983 .id_table = tg3_pci_tbl,
16984 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016985 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016986 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016987 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016988};
16989
16990static int __init tg3_init(void)
16991{
Jeff Garzik29917622006-08-19 17:48:59 -040016992 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016993}
16994
16995static void __exit tg3_cleanup(void)
16996{
16997 pci_unregister_driver(&tg3_driver);
16998}
16999
17000module_init(tg3_init);
17001module_exit(tg3_cleanup);