blob: d326d9219aa898f4de34278ea5d1516c869a0347 [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
Matt Carlson1d36ba42011-04-20 07:57:42 +00001286#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1287 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1288 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1289 MII_TG3_AUXCTL_ACTL_TX_6DB)
1290
1291#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1292 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1293 MII_TG3_AUXCTL_ACTL_TX_6DB);
1294
Matt Carlson95e28692008-05-25 23:44:14 -07001295static int tg3_bmcr_reset(struct tg3 *tp)
1296{
1297 u32 phy_control;
1298 int limit, err;
1299
1300 /* OK, reset it, and poll the BMCR_RESET bit until it
1301 * clears or we time out.
1302 */
1303 phy_control = BMCR_RESET;
1304 err = tg3_writephy(tp, MII_BMCR, phy_control);
1305 if (err != 0)
1306 return -EBUSY;
1307
1308 limit = 5000;
1309 while (limit--) {
1310 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1311 if (err != 0)
1312 return -EBUSY;
1313
1314 if ((phy_control & BMCR_RESET) == 0) {
1315 udelay(40);
1316 break;
1317 }
1318 udelay(10);
1319 }
Roel Kluind4675b52009-02-12 16:33:27 -08001320 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001321 return -EBUSY;
1322
1323 return 0;
1324}
1325
Matt Carlson158d7ab2008-05-29 01:37:54 -07001326static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1327{
Francois Romieu3d165432009-01-19 16:56:50 -08001328 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329 u32 val;
1330
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001331 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332
1333 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001334 val = -EIO;
1335
1336 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001337
1338 return val;
1339}
1340
1341static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1342{
Francois Romieu3d165432009-01-19 16:56:50 -08001343 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001344 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001345
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001346 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001347
1348 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001349 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001350
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001351 spin_unlock_bh(&tp->lock);
1352
1353 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001354}
1355
1356static int tg3_mdio_reset(struct mii_bus *bp)
1357{
1358 return 0;
1359}
1360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001362{
1363 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001364 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001365
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001366 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001367 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001368 case PHY_ID_BCM50610:
1369 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001370 val = MAC_PHYCFG2_50610_LED_MODES;
1371 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001372 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001373 val = MAC_PHYCFG2_AC131_LED_MODES;
1374 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001375 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001376 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1377 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001378 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001379 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1380 break;
1381 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001383 }
1384
1385 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1386 tw32(MAC_PHYCFG2, val);
1387
1388 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001389 val &= ~(MAC_PHYCFG1_RGMII_INT |
1390 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1391 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001392 tw32(MAC_PHYCFG1, val);
1393
1394 return;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1399 MAC_PHYCFG2_FMODE_MASK_MASK |
1400 MAC_PHYCFG2_GMODE_MASK_MASK |
1401 MAC_PHYCFG2_ACT_MASK_MASK |
1402 MAC_PHYCFG2_QUAL_MASK_MASK |
1403 MAC_PHYCFG2_INBAND_ENABLE;
1404
1405 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001406
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001407 val = tr32(MAC_PHYCFG1);
1408 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1409 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001410 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1411 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001412 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001413 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001414 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1415 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001416 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1417 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1418 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001419
Matt Carlsona9daf362008-05-25 23:49:44 -07001420 val = tr32(MAC_EXT_RGMII_MODE);
1421 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1422 MAC_RGMII_MODE_RX_QUALITY |
1423 MAC_RGMII_MODE_RX_ACTIVITY |
1424 MAC_RGMII_MODE_RX_ENG_DET |
1425 MAC_RGMII_MODE_TX_ENABLE |
1426 MAC_RGMII_MODE_TX_LOWPWR |
1427 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001428 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1429 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001430 val |= MAC_RGMII_MODE_RX_INT_B |
1431 MAC_RGMII_MODE_RX_QUALITY |
1432 MAC_RGMII_MODE_RX_ACTIVITY |
1433 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001434 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001435 val |= MAC_RGMII_MODE_TX_ENABLE |
1436 MAC_RGMII_MODE_TX_LOWPWR |
1437 MAC_RGMII_MODE_TX_RESET;
1438 }
1439 tw32(MAC_EXT_RGMII_MODE, val);
1440}
1441
Matt Carlson158d7ab2008-05-29 01:37:54 -07001442static void tg3_mdio_start(struct tg3 *tp)
1443{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001444 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1445 tw32_f(MAC_MI_MODE, tp->mi_mode);
1446 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001447
Joe Perches63c3a662011-04-26 08:12:10 +00001448 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001449 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1450 tg3_mdio_config_5785(tp);
1451}
1452
1453static int tg3_mdio_init(struct tg3 *tp)
1454{
1455 int i;
1456 u32 reg;
1457 struct phy_device *phydev;
1458
Joe Perches63c3a662011-04-26 08:12:10 +00001459 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001460 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001461
Matt Carlson69f11c92011-07-13 09:27:30 +00001462 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001463
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001464 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1465 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1466 else
1467 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1468 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001469 if (is_serdes)
1470 tp->phy_addr += 7;
1471 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001472 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001473
Matt Carlson158d7ab2008-05-29 01:37:54 -07001474 tg3_mdio_start(tp);
1475
Joe Perches63c3a662011-04-26 08:12:10 +00001476 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001477 return 0;
1478
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001479 tp->mdio_bus = mdiobus_alloc();
1480 if (tp->mdio_bus == NULL)
1481 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001482
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001483 tp->mdio_bus->name = "tg3 mdio bus";
1484 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001486 tp->mdio_bus->priv = tp;
1487 tp->mdio_bus->parent = &tp->pdev->dev;
1488 tp->mdio_bus->read = &tg3_mdio_read;
1489 tp->mdio_bus->write = &tg3_mdio_write;
1490 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001491 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001492 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001493
1494 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001495 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001496
1497 /* The bus registration will look for all the PHYs on the mdio bus.
1498 * Unfortunately, it does not ensure the PHY is powered up before
1499 * accessing the PHY ID registers. A chip reset is the
1500 * quickest way to bring the device back to an operational state..
1501 */
1502 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1503 tg3_bmcr_reset(tp);
1504
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001505 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001506 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001507 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001508 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001509 return i;
1510 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001511
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001512 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001513
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001514 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001515 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001516 mdiobus_unregister(tp->mdio_bus);
1517 mdiobus_free(tp->mdio_bus);
1518 return -ENODEV;
1519 }
1520
1521 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001522 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001523 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001524 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001525 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001526 case PHY_ID_BCM50610:
1527 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001528 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001529 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001530 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001531 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001532 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001533 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001534 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001535 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001536 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001537 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001538 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001539 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001540 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001541 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001542 case PHY_ID_RTL8201E:
1543 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001544 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001545 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001546 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001547 break;
1548 }
1549
Joe Perches63c3a662011-04-26 08:12:10 +00001550 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001551
1552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1553 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001554
1555 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001556}
1557
1558static void tg3_mdio_fini(struct tg3 *tp)
1559{
Joe Perches63c3a662011-04-26 08:12:10 +00001560 if (tg3_flag(tp, MDIOBUS_INITED)) {
1561 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001562 mdiobus_unregister(tp->mdio_bus);
1563 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001564 }
1565}
1566
Matt Carlson95e28692008-05-25 23:44:14 -07001567/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001568static inline void tg3_generate_fw_event(struct tg3 *tp)
1569{
1570 u32 val;
1571
1572 val = tr32(GRC_RX_CPU_EVENT);
1573 val |= GRC_RX_CPU_DRIVER_EVENT;
1574 tw32_f(GRC_RX_CPU_EVENT, val);
1575
1576 tp->last_event_jiffies = jiffies;
1577}
1578
1579#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1580
1581/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001582static void tg3_wait_for_event_ack(struct tg3 *tp)
1583{
1584 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001585 unsigned int delay_cnt;
1586 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001587
Matt Carlson4ba526c2008-08-15 14:10:04 -07001588 /* If enough time has passed, no wait is necessary. */
1589 time_remain = (long)(tp->last_event_jiffies + 1 +
1590 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1591 (long)jiffies;
1592 if (time_remain < 0)
1593 return;
1594
1595 /* Check if we can shorten the wait time. */
1596 delay_cnt = jiffies_to_usecs(time_remain);
1597 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1598 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1599 delay_cnt = (delay_cnt >> 3) + 1;
1600
1601 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001602 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1603 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001604 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001605 }
1606}
1607
1608/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001609static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001610{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001611 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001612
1613 val = 0;
1614 if (!tg3_readphy(tp, MII_BMCR, &reg))
1615 val = reg << 16;
1616 if (!tg3_readphy(tp, MII_BMSR, &reg))
1617 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001618 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001619
1620 val = 0;
1621 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1622 val = reg << 16;
1623 if (!tg3_readphy(tp, MII_LPA, &reg))
1624 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001625 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001626
1627 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001628 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001629 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1630 val = reg << 16;
1631 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1632 val |= (reg & 0xffff);
1633 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001634 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001635
1636 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1637 val = reg << 16;
1638 else
1639 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001640 *data++ = val;
1641}
1642
1643/* tp->lock is held. */
1644static void tg3_ump_link_report(struct tg3 *tp)
1645{
1646 u32 data[4];
1647
1648 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1649 return;
1650
1651 tg3_phy_gather_ump_data(tp, data);
1652
1653 tg3_wait_for_event_ack(tp);
1654
1655 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1656 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1657 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1658 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1659 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1660 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001661
Matt Carlson4ba526c2008-08-15 14:10:04 -07001662 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001663}
1664
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001665/* tp->lock is held. */
1666static void tg3_stop_fw(struct tg3 *tp)
1667{
1668 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1669 /* Wait for RX cpu to ACK the previous event. */
1670 tg3_wait_for_event_ack(tp);
1671
1672 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1673
1674 tg3_generate_fw_event(tp);
1675
1676 /* Wait for RX cpu to ACK this event. */
1677 tg3_wait_for_event_ack(tp);
1678 }
1679}
1680
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001681/* tp->lock is held. */
1682static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1683{
1684 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1685 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1686
1687 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1688 switch (kind) {
1689 case RESET_KIND_INIT:
1690 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1691 DRV_STATE_START);
1692 break;
1693
1694 case RESET_KIND_SHUTDOWN:
1695 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1696 DRV_STATE_UNLOAD);
1697 break;
1698
1699 case RESET_KIND_SUSPEND:
1700 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1701 DRV_STATE_SUSPEND);
1702 break;
1703
1704 default:
1705 break;
1706 }
1707 }
1708
1709 if (kind == RESET_KIND_INIT ||
1710 kind == RESET_KIND_SUSPEND)
1711 tg3_ape_driver_state_change(tp, kind);
1712}
1713
1714/* tp->lock is held. */
1715static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1716{
1717 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1718 switch (kind) {
1719 case RESET_KIND_INIT:
1720 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1721 DRV_STATE_START_DONE);
1722 break;
1723
1724 case RESET_KIND_SHUTDOWN:
1725 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1726 DRV_STATE_UNLOAD_DONE);
1727 break;
1728
1729 default:
1730 break;
1731 }
1732 }
1733
1734 if (kind == RESET_KIND_SHUTDOWN)
1735 tg3_ape_driver_state_change(tp, kind);
1736}
1737
1738/* tp->lock is held. */
1739static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1740{
1741 if (tg3_flag(tp, ENABLE_ASF)) {
1742 switch (kind) {
1743 case RESET_KIND_INIT:
1744 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1745 DRV_STATE_START);
1746 break;
1747
1748 case RESET_KIND_SHUTDOWN:
1749 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1750 DRV_STATE_UNLOAD);
1751 break;
1752
1753 case RESET_KIND_SUSPEND:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_SUSPEND);
1756 break;
1757
1758 default:
1759 break;
1760 }
1761 }
1762}
1763
1764static int tg3_poll_fw(struct tg3 *tp)
1765{
1766 int i;
1767 u32 val;
1768
1769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1770 /* Wait up to 20ms for init done. */
1771 for (i = 0; i < 200; i++) {
1772 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1773 return 0;
1774 udelay(100);
1775 }
1776 return -ENODEV;
1777 }
1778
1779 /* Wait for firmware initialization to complete. */
1780 for (i = 0; i < 100000; i++) {
1781 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1782 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1783 break;
1784 udelay(10);
1785 }
1786
1787 /* Chip might not be fitted with firmware. Some Sun onboard
1788 * parts are configured like that. So don't signal the timeout
1789 * of the above loop as an error, but do report the lack of
1790 * running firmware once.
1791 */
1792 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1793 tg3_flag_set(tp, NO_FWARE_REPORTED);
1794
1795 netdev_info(tp->dev, "No firmware running\n");
1796 }
1797
1798 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1799 /* The 57765 A0 needs a little more
1800 * time to do some important work.
1801 */
1802 mdelay(10);
1803 }
1804
1805 return 0;
1806}
1807
Matt Carlson95e28692008-05-25 23:44:14 -07001808static void tg3_link_report(struct tg3 *tp)
1809{
1810 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001811 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001812 tg3_ump_link_report(tp);
1813 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001814 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1815 (tp->link_config.active_speed == SPEED_1000 ?
1816 1000 :
1817 (tp->link_config.active_speed == SPEED_100 ?
1818 100 : 10)),
1819 (tp->link_config.active_duplex == DUPLEX_FULL ?
1820 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001821
Joe Perches05dbe002010-02-17 19:44:19 +00001822 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1823 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1824 "on" : "off",
1825 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1826 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001827
1828 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1829 netdev_info(tp->dev, "EEE is %s\n",
1830 tp->setlpicnt ? "enabled" : "disabled");
1831
Matt Carlson95e28692008-05-25 23:44:14 -07001832 tg3_ump_link_report(tp);
1833 }
1834}
1835
Matt Carlson95e28692008-05-25 23:44:14 -07001836static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1837{
1838 u16 miireg;
1839
Steve Glendinninge18ce342008-12-16 02:00:00 -08001840 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001841 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001842 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001843 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001844 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001845 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1846 else
1847 miireg = 0;
1848
1849 return miireg;
1850}
1851
Matt Carlson95e28692008-05-25 23:44:14 -07001852static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1853{
1854 u8 cap = 0;
1855
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001856 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1857 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1858 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1859 if (lcladv & ADVERTISE_1000XPAUSE)
1860 cap = FLOW_CTRL_RX;
1861 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001862 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001863 }
1864
1865 return cap;
1866}
1867
Matt Carlsonf51f3562008-05-25 23:45:08 -07001868static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001869{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001870 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001871 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001872 u32 old_rx_mode = tp->rx_mode;
1873 u32 old_tx_mode = tp->tx_mode;
1874
Joe Perches63c3a662011-04-26 08:12:10 +00001875 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001876 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001877 else
1878 autoneg = tp->link_config.autoneg;
1879
Joe Perches63c3a662011-04-26 08:12:10 +00001880 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001881 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001882 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001883 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001884 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001885 } else
1886 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001887
Matt Carlsonf51f3562008-05-25 23:45:08 -07001888 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001889
Steve Glendinninge18ce342008-12-16 02:00:00 -08001890 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001891 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1892 else
1893 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1894
Matt Carlsonf51f3562008-05-25 23:45:08 -07001895 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001896 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001897
Steve Glendinninge18ce342008-12-16 02:00:00 -08001898 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001899 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1900 else
1901 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1902
Matt Carlsonf51f3562008-05-25 23:45:08 -07001903 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001904 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001905}
1906
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001907static void tg3_adjust_link(struct net_device *dev)
1908{
1909 u8 oldflowctrl, linkmesg = 0;
1910 u32 mac_mode, lcl_adv, rmt_adv;
1911 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001912 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001913
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001914 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001915
1916 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1917 MAC_MODE_HALF_DUPLEX);
1918
1919 oldflowctrl = tp->link_config.active_flowctrl;
1920
1921 if (phydev->link) {
1922 lcl_adv = 0;
1923 rmt_adv = 0;
1924
1925 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1926 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001927 else if (phydev->speed == SPEED_1000 ||
1928 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001929 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001930 else
1931 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001932
1933 if (phydev->duplex == DUPLEX_HALF)
1934 mac_mode |= MAC_MODE_HALF_DUPLEX;
1935 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001936 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001937 tp->link_config.flowctrl);
1938
1939 if (phydev->pause)
1940 rmt_adv = LPA_PAUSE_CAP;
1941 if (phydev->asym_pause)
1942 rmt_adv |= LPA_PAUSE_ASYM;
1943 }
1944
1945 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1946 } else
1947 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1948
1949 if (mac_mode != tp->mac_mode) {
1950 tp->mac_mode = mac_mode;
1951 tw32_f(MAC_MODE, tp->mac_mode);
1952 udelay(40);
1953 }
1954
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1956 if (phydev->speed == SPEED_10)
1957 tw32(MAC_MI_STAT,
1958 MAC_MI_STAT_10MBPS_MODE |
1959 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1960 else
1961 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1962 }
1963
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001964 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1965 tw32(MAC_TX_LENGTHS,
1966 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1967 (6 << TX_LENGTHS_IPG_SHIFT) |
1968 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1969 else
1970 tw32(MAC_TX_LENGTHS,
1971 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1972 (6 << TX_LENGTHS_IPG_SHIFT) |
1973 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1974
Matt Carlson34655ad2012-02-22 12:35:18 +00001975 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976 phydev->speed != tp->link_config.active_speed ||
1977 phydev->duplex != tp->link_config.active_duplex ||
1978 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001979 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001980
Matt Carlson34655ad2012-02-22 12:35:18 +00001981 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001982 tp->link_config.active_speed = phydev->speed;
1983 tp->link_config.active_duplex = phydev->duplex;
1984
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001985 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001986
1987 if (linkmesg)
1988 tg3_link_report(tp);
1989}
1990
1991static int tg3_phy_init(struct tg3 *tp)
1992{
1993 struct phy_device *phydev;
1994
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001995 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001996 return 0;
1997
1998 /* Bring the PHY back to a known state. */
1999 tg3_bmcr_reset(tp);
2000
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002001 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002002
2003 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08002004 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07002005 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002007 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002008 return PTR_ERR(phydev);
2009 }
2010
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002011 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002012 switch (phydev->interface) {
2013 case PHY_INTERFACE_MODE_GMII:
2014 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002015 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002016 phydev->supported &= (PHY_GBIT_FEATURES |
2017 SUPPORTED_Pause |
2018 SUPPORTED_Asym_Pause);
2019 break;
2020 }
2021 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002022 case PHY_INTERFACE_MODE_MII:
2023 phydev->supported &= (PHY_BASIC_FEATURES |
2024 SUPPORTED_Pause |
2025 SUPPORTED_Asym_Pause);
2026 break;
2027 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002028 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002029 return -EINVAL;
2030 }
2031
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002032 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033
2034 phydev->advertising = phydev->supported;
2035
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002036 return 0;
2037}
2038
2039static void tg3_phy_start(struct tg3 *tp)
2040{
2041 struct phy_device *phydev;
2042
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002043 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002044 return;
2045
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002046 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002047
Matt Carlson80096062010-08-02 11:26:06 +00002048 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2049 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002050 phydev->speed = tp->link_config.speed;
2051 phydev->duplex = tp->link_config.duplex;
2052 phydev->autoneg = tp->link_config.autoneg;
2053 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002054 }
2055
2056 phy_start(phydev);
2057
2058 phy_start_aneg(phydev);
2059}
2060
2061static void tg3_phy_stop(struct tg3 *tp)
2062{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002063 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002064 return;
2065
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002066 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067}
2068
2069static void tg3_phy_fini(struct tg3 *tp)
2070{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002071 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002072 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002073 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002074 }
2075}
2076
Matt Carlson941ec902011-08-19 13:58:23 +00002077static int tg3_phy_set_extloopbk(struct tg3 *tp)
2078{
2079 int err;
2080 u32 val;
2081
2082 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2083 return 0;
2084
2085 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2086 /* Cannot do read-modify-write on 5401 */
2087 err = tg3_phy_auxctl_write(tp,
2088 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2089 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2090 0x4c20);
2091 goto done;
2092 }
2093
2094 err = tg3_phy_auxctl_read(tp,
2095 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2096 if (err)
2097 return err;
2098
2099 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2100 err = tg3_phy_auxctl_write(tp,
2101 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2102
2103done:
2104 return err;
2105}
2106
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002107static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2108{
2109 u32 phytest;
2110
2111 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2112 u32 phy;
2113
2114 tg3_writephy(tp, MII_TG3_FET_TEST,
2115 phytest | MII_TG3_FET_SHADOW_EN);
2116 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2117 if (enable)
2118 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2119 else
2120 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2121 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2122 }
2123 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2124 }
2125}
2126
Matt Carlson6833c042008-11-21 17:18:59 -08002127static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2128{
2129 u32 reg;
2130
Joe Perches63c3a662011-04-26 08:12:10 +00002131 if (!tg3_flag(tp, 5705_PLUS) ||
2132 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002133 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002134 return;
2135
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002136 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002137 tg3_phy_fet_toggle_apd(tp, enable);
2138 return;
2139 }
2140
Matt Carlson6833c042008-11-21 17:18:59 -08002141 reg = MII_TG3_MISC_SHDW_WREN |
2142 MII_TG3_MISC_SHDW_SCR5_SEL |
2143 MII_TG3_MISC_SHDW_SCR5_LPED |
2144 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2145 MII_TG3_MISC_SHDW_SCR5_SDTL |
2146 MII_TG3_MISC_SHDW_SCR5_C125OE;
2147 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2148 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2149
2150 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2151
2152
2153 reg = MII_TG3_MISC_SHDW_WREN |
2154 MII_TG3_MISC_SHDW_APD_SEL |
2155 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2156 if (enable)
2157 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2158
2159 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2160}
2161
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002162static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2163{
2164 u32 phy;
2165
Joe Perches63c3a662011-04-26 08:12:10 +00002166 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002167 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002168 return;
2169
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002170 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002171 u32 ephy;
2172
Matt Carlson535ef6e2009-08-25 10:09:36 +00002173 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2174 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2175
2176 tg3_writephy(tp, MII_TG3_FET_TEST,
2177 ephy | MII_TG3_FET_SHADOW_EN);
2178 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002179 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002180 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002181 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002182 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2183 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002184 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002185 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002186 }
2187 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002188 int ret;
2189
2190 ret = tg3_phy_auxctl_read(tp,
2191 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2192 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002193 if (enable)
2194 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2195 else
2196 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002197 tg3_phy_auxctl_write(tp,
2198 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002199 }
2200 }
2201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203static void tg3_phy_set_wirespeed(struct tg3 *tp)
2204{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002205 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 u32 val;
2207
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002208 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return;
2210
Matt Carlson15ee95c2011-04-20 07:57:40 +00002211 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2212 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002213 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2214 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002217static void tg3_phy_apply_otp(struct tg3 *tp)
2218{
2219 u32 otp, phy;
2220
2221 if (!tp->phy_otp)
2222 return;
2223
2224 otp = tp->phy_otp;
2225
Matt Carlson1d36ba42011-04-20 07:57:42 +00002226 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2227 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002228
2229 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2230 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2231 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2232
2233 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2234 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2235 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2236
2237 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2238 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2239 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2240
2241 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2242 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2243
2244 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2245 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2246
2247 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2248 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2249 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2250
Matt Carlson1d36ba42011-04-20 07:57:42 +00002251 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002252}
2253
Matt Carlson52b02d02010-10-14 10:37:41 +00002254static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2255{
2256 u32 val;
2257
2258 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2259 return;
2260
2261 tp->setlpicnt = 0;
2262
2263 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2264 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002265 tp->link_config.active_duplex == DUPLEX_FULL &&
2266 (tp->link_config.active_speed == SPEED_100 ||
2267 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002268 u32 eeectl;
2269
2270 if (tp->link_config.active_speed == SPEED_1000)
2271 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2272 else
2273 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2274
2275 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2276
Matt Carlson3110f5f52010-12-06 08:28:50 +00002277 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2278 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002279
Matt Carlsonb0c59432011-05-19 12:12:48 +00002280 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2281 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002282 tp->setlpicnt = 2;
2283 }
2284
2285 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002286 if (current_link_up == 1 &&
2287 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2288 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2289 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2290 }
2291
Matt Carlson52b02d02010-10-14 10:37:41 +00002292 val = tr32(TG3_CPMU_EEE_MODE);
2293 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2294 }
2295}
2296
Matt Carlsonb0c59432011-05-19 12:12:48 +00002297static void tg3_phy_eee_enable(struct tg3 *tp)
2298{
2299 u32 val;
2300
2301 if (tp->link_config.active_speed == SPEED_1000 &&
2302 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002304 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002305 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002306 val = MII_TG3_DSP_TAP26_ALNOKO |
2307 MII_TG3_DSP_TAP26_RMRXSTO;
2308 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002309 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2310 }
2311
2312 val = tr32(TG3_CPMU_EEE_MODE);
2313 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static int tg3_wait_macro_done(struct tg3 *tp)
2317{
2318 int limit = 100;
2319
2320 while (limit--) {
2321 u32 tmp32;
2322
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002323 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if ((tmp32 & 0x1000) == 0)
2325 break;
2326 }
2327 }
Roel Kluind4675b52009-02-12 16:33:27 -08002328 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -EBUSY;
2330
2331 return 0;
2332}
2333
2334static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2335{
2336 static const u32 test_pat[4][6] = {
2337 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2338 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2339 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2340 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2341 };
2342 int chan;
2343
2344 for (chan = 0; chan < 4; chan++) {
2345 int i;
2346
2347 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2348 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002349 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 for (i = 0; i < 6; i++)
2352 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2353 test_pat[chan][i]);
2354
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002355 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (tg3_wait_macro_done(tp)) {
2357 *resetp = 1;
2358 return -EBUSY;
2359 }
2360
2361 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2362 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002363 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (tg3_wait_macro_done(tp)) {
2365 *resetp = 1;
2366 return -EBUSY;
2367 }
2368
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002369 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (tg3_wait_macro_done(tp)) {
2371 *resetp = 1;
2372 return -EBUSY;
2373 }
2374
2375 for (i = 0; i < 6; i += 2) {
2376 u32 low, high;
2377
2378 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2379 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2380 tg3_wait_macro_done(tp)) {
2381 *resetp = 1;
2382 return -EBUSY;
2383 }
2384 low &= 0x7fff;
2385 high &= 0x000f;
2386 if (low != test_pat[chan][i] ||
2387 high != test_pat[chan][i+1]) {
2388 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2389 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2390 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2391
2392 return -EBUSY;
2393 }
2394 }
2395 }
2396
2397 return 0;
2398}
2399
2400static int tg3_phy_reset_chanpat(struct tg3 *tp)
2401{
2402 int chan;
2403
2404 for (chan = 0; chan < 4; chan++) {
2405 int i;
2406
2407 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2408 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002409 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 for (i = 0; i < 6; i++)
2411 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002412 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (tg3_wait_macro_done(tp))
2414 return -EBUSY;
2415 }
2416
2417 return 0;
2418}
2419
2420static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2421{
2422 u32 reg32, phy9_orig;
2423 int retries, do_phy_reset, err;
2424
2425 retries = 10;
2426 do_phy_reset = 1;
2427 do {
2428 if (do_phy_reset) {
2429 err = tg3_bmcr_reset(tp);
2430 if (err)
2431 return err;
2432 do_phy_reset = 0;
2433 }
2434
2435 /* Disable transmitter and interrupt. */
2436 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2437 continue;
2438
2439 reg32 |= 0x3000;
2440 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2441
2442 /* Set full-duplex, 1000 mbps. */
2443 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002444 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002447 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 continue;
2449
Matt Carlson221c5632011-06-13 13:39:01 +00002450 tg3_writephy(tp, MII_CTRL1000,
2451 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Matt Carlson1d36ba42011-04-20 07:57:42 +00002453 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2454 if (err)
2455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002458 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2461 if (!err)
2462 break;
2463 } while (--retries);
2464
2465 err = tg3_phy_reset_chanpat(tp);
2466 if (err)
2467 return err;
2468
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002469 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002472 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Matt Carlson1d36ba42011-04-20 07:57:42 +00002474 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Matt Carlson221c5632011-06-13 13:39:01 +00002476 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2479 reg32 &= ~0x3000;
2480 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2481 } else if (!err)
2482 err = -EBUSY;
2483
2484 return err;
2485}
2486
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002487static void tg3_carrier_on(struct tg3 *tp)
2488{
2489 netif_carrier_on(tp->dev);
2490 tp->link_up = true;
2491}
2492
2493static void tg3_carrier_off(struct tg3 *tp)
2494{
2495 netif_carrier_off(tp->dev);
2496 tp->link_up = false;
2497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499/* This will reset the tigon3 PHY if there is no valid
2500 * link unless the FORCE argument is non-zero.
2501 */
2502static int tg3_phy_reset(struct tg3 *tp)
2503{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002504 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int err;
2506
Michael Chan60189dd2006-12-17 17:08:07 -08002507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002508 val = tr32(GRC_MISC_CFG);
2509 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2510 udelay(40);
2511 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002512 err = tg3_readphy(tp, MII_BMSR, &val);
2513 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (err != 0)
2515 return -EBUSY;
2516
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002517 if (netif_running(tp->dev) && tp->link_up) {
2518 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002519 tg3_link_report(tp);
2520 }
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2523 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2525 err = tg3_phy_reset_5703_4_5(tp);
2526 if (err)
2527 return err;
2528 goto out;
2529 }
2530
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002531 cpmuctrl = 0;
2532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2533 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2534 cpmuctrl = tr32(TG3_CPMU_CTRL);
2535 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2536 tw32(TG3_CPMU_CTRL,
2537 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2538 }
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 err = tg3_bmcr_reset(tp);
2541 if (err)
2542 return err;
2543
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002544 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002545 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2546 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002547
2548 tw32(TG3_CPMU_CTRL, cpmuctrl);
2549 }
2550
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002551 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2552 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002553 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2554 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2555 CPMU_LSPD_1000MB_MACCLK_12_5) {
2556 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2557 udelay(40);
2558 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2559 }
2560 }
2561
Joe Perches63c3a662011-04-26 08:12:10 +00002562 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002563 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002564 return 0;
2565
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002566 tg3_phy_apply_otp(tp);
2567
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002568 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002569 tg3_phy_toggle_apd(tp, true);
2570 else
2571 tg3_phy_toggle_apd(tp, false);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002574 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2575 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002576 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2577 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002578 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002580
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002581 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002582 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2583 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002585
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002586 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002587 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2588 tg3_phydsp_write(tp, 0x000a, 0x310b);
2589 tg3_phydsp_write(tp, 0x201f, 0x9506);
2590 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2591 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2592 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002593 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002594 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2595 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2596 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2597 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2598 tg3_writephy(tp, MII_TG3_TEST1,
2599 MII_TG3_TEST1_TRIM_EN | 0x4);
2600 } else
2601 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2602
2603 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2604 }
Michael Chanc424cb22006-04-29 18:56:34 -07002605 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Set Extended packet length bit (bit 14) on all chips that */
2608 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002609 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002611 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002612 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002614 err = tg3_phy_auxctl_read(tp,
2615 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2616 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002617 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2618 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
2621 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2622 * jumbo frames transmission.
2623 */
Joe Perches63c3a662011-04-26 08:12:10 +00002624 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002625 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002626 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002627 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629
Michael Chan715116a2006-09-27 16:09:25 -07002630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002631 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002632 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002633 }
2634
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002635 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 tg3_phy_set_wirespeed(tp);
2637 return 0;
2638}
2639
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002640#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2641#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2642#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2643 TG3_GPIO_MSG_NEED_VAUX)
2644#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2645 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2646 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2647 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2648 (TG3_GPIO_MSG_DRVR_PRES << 12))
2649
2650#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2651 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2652 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2653 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2654 (TG3_GPIO_MSG_NEED_VAUX << 12))
2655
2656static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2657{
2658 u32 status, shift;
2659
2660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2661 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2662 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2663 else
2664 status = tr32(TG3_CPMU_DRV_STATUS);
2665
2666 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2667 status &= ~(TG3_GPIO_MSG_MASK << shift);
2668 status |= (newstat << shift);
2669
2670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2672 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2673 else
2674 tw32(TG3_CPMU_DRV_STATUS, status);
2675
2676 return status >> TG3_APE_GPIO_MSG_SHIFT;
2677}
2678
Matt Carlson520b2752011-06-13 13:39:02 +00002679static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2680{
2681 if (!tg3_flag(tp, IS_NIC))
2682 return 0;
2683
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2687 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2688 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002689
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002690 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2691
2692 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2693 TG3_GRC_LCLCTL_PWRSW_DELAY);
2694
2695 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2696 } else {
2697 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2698 TG3_GRC_LCLCTL_PWRSW_DELAY);
2699 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002700
Matt Carlson520b2752011-06-13 13:39:02 +00002701 return 0;
2702}
2703
2704static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2705{
2706 u32 grc_local_ctrl;
2707
2708 if (!tg3_flag(tp, IS_NIC) ||
2709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2710 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2711 return;
2712
2713 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2714
2715 tw32_wait_f(GRC_LOCAL_CTRL,
2716 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2717 TG3_GRC_LCLCTL_PWRSW_DELAY);
2718
2719 tw32_wait_f(GRC_LOCAL_CTRL,
2720 grc_local_ctrl,
2721 TG3_GRC_LCLCTL_PWRSW_DELAY);
2722
2723 tw32_wait_f(GRC_LOCAL_CTRL,
2724 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2725 TG3_GRC_LCLCTL_PWRSW_DELAY);
2726}
2727
2728static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2729{
2730 if (!tg3_flag(tp, IS_NIC))
2731 return;
2732
2733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2735 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2736 (GRC_LCLCTRL_GPIO_OE0 |
2737 GRC_LCLCTRL_GPIO_OE1 |
2738 GRC_LCLCTRL_GPIO_OE2 |
2739 GRC_LCLCTRL_GPIO_OUTPUT0 |
2740 GRC_LCLCTRL_GPIO_OUTPUT1),
2741 TG3_GRC_LCLCTL_PWRSW_DELAY);
2742 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2743 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2744 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2745 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2746 GRC_LCLCTRL_GPIO_OE1 |
2747 GRC_LCLCTRL_GPIO_OE2 |
2748 GRC_LCLCTRL_GPIO_OUTPUT0 |
2749 GRC_LCLCTRL_GPIO_OUTPUT1 |
2750 tp->grc_local_ctrl;
2751 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2752 TG3_GRC_LCLCTL_PWRSW_DELAY);
2753
2754 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2755 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2756 TG3_GRC_LCLCTL_PWRSW_DELAY);
2757
2758 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2759 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2760 TG3_GRC_LCLCTL_PWRSW_DELAY);
2761 } else {
2762 u32 no_gpio2;
2763 u32 grc_local_ctrl = 0;
2764
2765 /* Workaround to prevent overdrawing Amps. */
2766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2767 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2768 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2769 grc_local_ctrl,
2770 TG3_GRC_LCLCTL_PWRSW_DELAY);
2771 }
2772
2773 /* On 5753 and variants, GPIO2 cannot be used. */
2774 no_gpio2 = tp->nic_sram_data_cfg &
2775 NIC_SRAM_DATA_CFG_NO_GPIO2;
2776
2777 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2778 GRC_LCLCTRL_GPIO_OE1 |
2779 GRC_LCLCTRL_GPIO_OE2 |
2780 GRC_LCLCTRL_GPIO_OUTPUT1 |
2781 GRC_LCLCTRL_GPIO_OUTPUT2;
2782 if (no_gpio2) {
2783 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2784 GRC_LCLCTRL_GPIO_OUTPUT2);
2785 }
2786 tw32_wait_f(GRC_LOCAL_CTRL,
2787 tp->grc_local_ctrl | grc_local_ctrl,
2788 TG3_GRC_LCLCTL_PWRSW_DELAY);
2789
2790 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2791
2792 tw32_wait_f(GRC_LOCAL_CTRL,
2793 tp->grc_local_ctrl | grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 if (!no_gpio2) {
2797 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2798 tw32_wait_f(GRC_LOCAL_CTRL,
2799 tp->grc_local_ctrl | grc_local_ctrl,
2800 TG3_GRC_LCLCTL_PWRSW_DELAY);
2801 }
2802 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002803}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002804
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002805static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002806{
2807 u32 msg = 0;
2808
2809 /* Serialize power state transitions */
2810 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2811 return;
2812
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002813 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002814 msg = TG3_GPIO_MSG_NEED_VAUX;
2815
2816 msg = tg3_set_function_status(tp, msg);
2817
2818 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2819 goto done;
2820
2821 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2822 tg3_pwrsrc_switch_to_vaux(tp);
2823 else
2824 tg3_pwrsrc_die_with_vmain(tp);
2825
2826done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002827 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002828}
2829
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002830static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Matt Carlson683644b2011-03-09 16:58:23 +00002832 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Matt Carlson334355a2010-01-20 16:58:10 +00002834 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002835 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return;
2837
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002841 tg3_frob_aux_power_5717(tp, include_wol ?
2842 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002843 return;
2844 }
2845
2846 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002847 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002849 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002850
Michael Chanbc1c7562006-03-20 17:48:03 -08002851 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002852 if (dev_peer) {
2853 struct tg3 *tp_peer = netdev_priv(dev_peer);
2854
Joe Perches63c3a662011-04-26 08:12:10 +00002855 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002856 return;
2857
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002858 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002859 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002860 need_vaux = true;
2861 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002864 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2865 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002866 need_vaux = true;
2867
Matt Carlson520b2752011-06-13 13:39:02 +00002868 if (need_vaux)
2869 tg3_pwrsrc_switch_to_vaux(tp);
2870 else
2871 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002874static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2875{
2876 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2877 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002878 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002879 if (speed != SPEED_10)
2880 return 1;
2881 } else if (speed == SPEED_10)
2882 return 1;
2883
2884 return 0;
2885}
2886
Matt Carlson0a459aa2008-11-03 16:54:15 -08002887static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002888{
Matt Carlsonce057f02007-11-12 21:08:03 -08002889 u32 val;
2890
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002891 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2893 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2894 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2895
2896 sg_dig_ctrl |=
2897 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2898 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2899 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2900 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002901 return;
Michael Chan51297242007-02-13 12:17:57 -08002902 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002903
Michael Chan60189dd2006-12-17 17:08:07 -08002904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002905 tg3_bmcr_reset(tp);
2906 val = tr32(GRC_MISC_CFG);
2907 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2908 udelay(40);
2909 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002910 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002911 u32 phytest;
2912 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2913 u32 phy;
2914
2915 tg3_writephy(tp, MII_ADVERTISE, 0);
2916 tg3_writephy(tp, MII_BMCR,
2917 BMCR_ANENABLE | BMCR_ANRESTART);
2918
2919 tg3_writephy(tp, MII_TG3_FET_TEST,
2920 phytest | MII_TG3_FET_SHADOW_EN);
2921 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2922 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2923 tg3_writephy(tp,
2924 MII_TG3_FET_SHDW_AUXMODE4,
2925 phy);
2926 }
2927 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2928 }
2929 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002930 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002931 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2932 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002933
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002934 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2935 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2936 MII_TG3_AUXCTL_PCTL_VREG_11V;
2937 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002938 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002939
Michael Chan15c3b692006-03-22 01:06:52 -08002940 /* The PHY should not be powered down on some chips because
2941 * of bugs.
2942 */
2943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2945 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002946 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2947 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2948 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002949 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002950
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002951 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2952 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002953 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2954 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2955 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2956 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2957 }
2958
Michael Chan15c3b692006-03-22 01:06:52 -08002959 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2960}
2961
Matt Carlson3f007892008-11-03 16:51:36 -08002962/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002963static int tg3_nvram_lock(struct tg3 *tp)
2964{
Joe Perches63c3a662011-04-26 08:12:10 +00002965 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002966 int i;
2967
2968 if (tp->nvram_lock_cnt == 0) {
2969 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2970 for (i = 0; i < 8000; i++) {
2971 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2972 break;
2973 udelay(20);
2974 }
2975 if (i == 8000) {
2976 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2977 return -ENODEV;
2978 }
2979 }
2980 tp->nvram_lock_cnt++;
2981 }
2982 return 0;
2983}
2984
2985/* tp->lock is held. */
2986static void tg3_nvram_unlock(struct tg3 *tp)
2987{
Joe Perches63c3a662011-04-26 08:12:10 +00002988 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002989 if (tp->nvram_lock_cnt > 0)
2990 tp->nvram_lock_cnt--;
2991 if (tp->nvram_lock_cnt == 0)
2992 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2993 }
2994}
2995
2996/* tp->lock is held. */
2997static void tg3_enable_nvram_access(struct tg3 *tp)
2998{
Joe Perches63c3a662011-04-26 08:12:10 +00002999 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003000 u32 nvaccess = tr32(NVRAM_ACCESS);
3001
3002 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3003 }
3004}
3005
3006/* tp->lock is held. */
3007static void tg3_disable_nvram_access(struct tg3 *tp)
3008{
Joe Perches63c3a662011-04-26 08:12:10 +00003009 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003010 u32 nvaccess = tr32(NVRAM_ACCESS);
3011
3012 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3013 }
3014}
3015
3016static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3017 u32 offset, u32 *val)
3018{
3019 u32 tmp;
3020 int i;
3021
3022 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3023 return -EINVAL;
3024
3025 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3026 EEPROM_ADDR_DEVID_MASK |
3027 EEPROM_ADDR_READ);
3028 tw32(GRC_EEPROM_ADDR,
3029 tmp |
3030 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3031 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3032 EEPROM_ADDR_ADDR_MASK) |
3033 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3034
3035 for (i = 0; i < 1000; i++) {
3036 tmp = tr32(GRC_EEPROM_ADDR);
3037
3038 if (tmp & EEPROM_ADDR_COMPLETE)
3039 break;
3040 msleep(1);
3041 }
3042 if (!(tmp & EEPROM_ADDR_COMPLETE))
3043 return -EBUSY;
3044
Matt Carlson62cedd12009-04-20 14:52:29 -07003045 tmp = tr32(GRC_EEPROM_DATA);
3046
3047 /*
3048 * The data will always be opposite the native endian
3049 * format. Perform a blind byteswap to compensate.
3050 */
3051 *val = swab32(tmp);
3052
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003053 return 0;
3054}
3055
3056#define NVRAM_CMD_TIMEOUT 10000
3057
3058static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3059{
3060 int i;
3061
3062 tw32(NVRAM_CMD, nvram_cmd);
3063 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3064 udelay(10);
3065 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3066 udelay(10);
3067 break;
3068 }
3069 }
3070
3071 if (i == NVRAM_CMD_TIMEOUT)
3072 return -EBUSY;
3073
3074 return 0;
3075}
3076
3077static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3078{
Joe Perches63c3a662011-04-26 08:12:10 +00003079 if (tg3_flag(tp, NVRAM) &&
3080 tg3_flag(tp, NVRAM_BUFFERED) &&
3081 tg3_flag(tp, FLASH) &&
3082 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003083 (tp->nvram_jedecnum == JEDEC_ATMEL))
3084
3085 addr = ((addr / tp->nvram_pagesize) <<
3086 ATMEL_AT45DB0X1B_PAGE_POS) +
3087 (addr % tp->nvram_pagesize);
3088
3089 return addr;
3090}
3091
3092static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3093{
Joe Perches63c3a662011-04-26 08:12:10 +00003094 if (tg3_flag(tp, NVRAM) &&
3095 tg3_flag(tp, NVRAM_BUFFERED) &&
3096 tg3_flag(tp, FLASH) &&
3097 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003098 (tp->nvram_jedecnum == JEDEC_ATMEL))
3099
3100 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3101 tp->nvram_pagesize) +
3102 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3103
3104 return addr;
3105}
3106
Matt Carlsone4f34112009-02-25 14:25:00 +00003107/* NOTE: Data read in from NVRAM is byteswapped according to
3108 * the byteswapping settings for all other register accesses.
3109 * tg3 devices are BE devices, so on a BE machine, the data
3110 * returned will be exactly as it is seen in NVRAM. On a LE
3111 * machine, the 32-bit value will be byteswapped.
3112 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003113static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3114{
3115 int ret;
3116
Joe Perches63c3a662011-04-26 08:12:10 +00003117 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003118 return tg3_nvram_read_using_eeprom(tp, offset, val);
3119
3120 offset = tg3_nvram_phys_addr(tp, offset);
3121
3122 if (offset > NVRAM_ADDR_MSK)
3123 return -EINVAL;
3124
3125 ret = tg3_nvram_lock(tp);
3126 if (ret)
3127 return ret;
3128
3129 tg3_enable_nvram_access(tp);
3130
3131 tw32(NVRAM_ADDR, offset);
3132 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3133 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3134
3135 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003136 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003137
3138 tg3_disable_nvram_access(tp);
3139
3140 tg3_nvram_unlock(tp);
3141
3142 return ret;
3143}
3144
Matt Carlsona9dc5292009-02-25 14:25:30 +00003145/* Ensures NVRAM data is in bytestream format. */
3146static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003147{
3148 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003149 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003150 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003151 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003152 return res;
3153}
3154
Matt Carlsondbe9b922012-02-13 10:20:09 +00003155static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3156 u32 offset, u32 len, u8 *buf)
3157{
3158 int i, j, rc = 0;
3159 u32 val;
3160
3161 for (i = 0; i < len; i += 4) {
3162 u32 addr;
3163 __be32 data;
3164
3165 addr = offset + i;
3166
3167 memcpy(&data, buf + i, 4);
3168
3169 /*
3170 * The SEEPROM interface expects the data to always be opposite
3171 * the native endian format. We accomplish this by reversing
3172 * all the operations that would have been performed on the
3173 * data from a call to tg3_nvram_read_be32().
3174 */
3175 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3176
3177 val = tr32(GRC_EEPROM_ADDR);
3178 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3179
3180 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3181 EEPROM_ADDR_READ);
3182 tw32(GRC_EEPROM_ADDR, val |
3183 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3184 (addr & EEPROM_ADDR_ADDR_MASK) |
3185 EEPROM_ADDR_START |
3186 EEPROM_ADDR_WRITE);
3187
3188 for (j = 0; j < 1000; j++) {
3189 val = tr32(GRC_EEPROM_ADDR);
3190
3191 if (val & EEPROM_ADDR_COMPLETE)
3192 break;
3193 msleep(1);
3194 }
3195 if (!(val & EEPROM_ADDR_COMPLETE)) {
3196 rc = -EBUSY;
3197 break;
3198 }
3199 }
3200
3201 return rc;
3202}
3203
3204/* offset and length are dword aligned */
3205static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3206 u8 *buf)
3207{
3208 int ret = 0;
3209 u32 pagesize = tp->nvram_pagesize;
3210 u32 pagemask = pagesize - 1;
3211 u32 nvram_cmd;
3212 u8 *tmp;
3213
3214 tmp = kmalloc(pagesize, GFP_KERNEL);
3215 if (tmp == NULL)
3216 return -ENOMEM;
3217
3218 while (len) {
3219 int j;
3220 u32 phy_addr, page_off, size;
3221
3222 phy_addr = offset & ~pagemask;
3223
3224 for (j = 0; j < pagesize; j += 4) {
3225 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3226 (__be32 *) (tmp + j));
3227 if (ret)
3228 break;
3229 }
3230 if (ret)
3231 break;
3232
3233 page_off = offset & pagemask;
3234 size = pagesize;
3235 if (len < size)
3236 size = len;
3237
3238 len -= size;
3239
3240 memcpy(tmp + page_off, buf, size);
3241
3242 offset = offset + (pagesize - page_off);
3243
3244 tg3_enable_nvram_access(tp);
3245
3246 /*
3247 * Before we can erase the flash page, we need
3248 * to issue a special "write enable" command.
3249 */
3250 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3251
3252 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3253 break;
3254
3255 /* Erase the target page */
3256 tw32(NVRAM_ADDR, phy_addr);
3257
3258 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3259 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3260
3261 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3262 break;
3263
3264 /* Issue another write enable to start the write. */
3265 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3266
3267 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3268 break;
3269
3270 for (j = 0; j < pagesize; j += 4) {
3271 __be32 data;
3272
3273 data = *((__be32 *) (tmp + j));
3274
3275 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3276
3277 tw32(NVRAM_ADDR, phy_addr + j);
3278
3279 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3280 NVRAM_CMD_WR;
3281
3282 if (j == 0)
3283 nvram_cmd |= NVRAM_CMD_FIRST;
3284 else if (j == (pagesize - 4))
3285 nvram_cmd |= NVRAM_CMD_LAST;
3286
3287 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3288 if (ret)
3289 break;
3290 }
3291 if (ret)
3292 break;
3293 }
3294
3295 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3296 tg3_nvram_exec_cmd(tp, nvram_cmd);
3297
3298 kfree(tmp);
3299
3300 return ret;
3301}
3302
3303/* offset and length are dword aligned */
3304static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3305 u8 *buf)
3306{
3307 int i, ret = 0;
3308
3309 for (i = 0; i < len; i += 4, offset += 4) {
3310 u32 page_off, phy_addr, nvram_cmd;
3311 __be32 data;
3312
3313 memcpy(&data, buf + i, 4);
3314 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3315
3316 page_off = offset % tp->nvram_pagesize;
3317
3318 phy_addr = tg3_nvram_phys_addr(tp, offset);
3319
Matt Carlsondbe9b922012-02-13 10:20:09 +00003320 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3321
3322 if (page_off == 0 || i == 0)
3323 nvram_cmd |= NVRAM_CMD_FIRST;
3324 if (page_off == (tp->nvram_pagesize - 4))
3325 nvram_cmd |= NVRAM_CMD_LAST;
3326
3327 if (i == (len - 4))
3328 nvram_cmd |= NVRAM_CMD_LAST;
3329
Matt Carlson42278222012-02-13 15:20:11 +00003330 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3331 !tg3_flag(tp, FLASH) ||
3332 !tg3_flag(tp, 57765_PLUS))
3333 tw32(NVRAM_ADDR, phy_addr);
3334
Matt Carlsondbe9b922012-02-13 10:20:09 +00003335 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3336 !tg3_flag(tp, 5755_PLUS) &&
3337 (tp->nvram_jedecnum == JEDEC_ST) &&
3338 (nvram_cmd & NVRAM_CMD_FIRST)) {
3339 u32 cmd;
3340
3341 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3342 ret = tg3_nvram_exec_cmd(tp, cmd);
3343 if (ret)
3344 break;
3345 }
3346 if (!tg3_flag(tp, FLASH)) {
3347 /* We always do complete word writes to eeprom. */
3348 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3349 }
3350
3351 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3352 if (ret)
3353 break;
3354 }
3355 return ret;
3356}
3357
3358/* offset and length are dword aligned */
3359static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3360{
3361 int ret;
3362
3363 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3364 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3365 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3366 udelay(40);
3367 }
3368
3369 if (!tg3_flag(tp, NVRAM)) {
3370 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3371 } else {
3372 u32 grc_mode;
3373
3374 ret = tg3_nvram_lock(tp);
3375 if (ret)
3376 return ret;
3377
3378 tg3_enable_nvram_access(tp);
3379 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3380 tw32(NVRAM_WRITE1, 0x406);
3381
3382 grc_mode = tr32(GRC_MODE);
3383 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3384
3385 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3386 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3387 buf);
3388 } else {
3389 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3390 buf);
3391 }
3392
3393 grc_mode = tr32(GRC_MODE);
3394 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3395
3396 tg3_disable_nvram_access(tp);
3397 tg3_nvram_unlock(tp);
3398 }
3399
3400 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3401 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3402 udelay(40);
3403 }
3404
3405 return ret;
3406}
3407
Matt Carlson997b4f12011-08-31 11:44:53 +00003408#define RX_CPU_SCRATCH_BASE 0x30000
3409#define RX_CPU_SCRATCH_SIZE 0x04000
3410#define TX_CPU_SCRATCH_BASE 0x34000
3411#define TX_CPU_SCRATCH_SIZE 0x04000
3412
3413/* tp->lock is held. */
3414static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3415{
3416 int i;
3417
3418 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3419
3420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3421 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3422
3423 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3424 return 0;
3425 }
3426 if (offset == RX_CPU_BASE) {
3427 for (i = 0; i < 10000; i++) {
3428 tw32(offset + CPU_STATE, 0xffffffff);
3429 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3430 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3431 break;
3432 }
3433
3434 tw32(offset + CPU_STATE, 0xffffffff);
3435 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3436 udelay(10);
3437 } else {
3438 for (i = 0; i < 10000; i++) {
3439 tw32(offset + CPU_STATE, 0xffffffff);
3440 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3441 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3442 break;
3443 }
3444 }
3445
3446 if (i >= 10000) {
3447 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3448 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3449 return -ENODEV;
3450 }
3451
3452 /* Clear firmware's nvram arbitration. */
3453 if (tg3_flag(tp, NVRAM))
3454 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3455 return 0;
3456}
3457
3458struct fw_info {
3459 unsigned int fw_base;
3460 unsigned int fw_len;
3461 const __be32 *fw_data;
3462};
3463
3464/* tp->lock is held. */
3465static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3466 u32 cpu_scratch_base, int cpu_scratch_size,
3467 struct fw_info *info)
3468{
3469 int err, lock_err, i;
3470 void (*write_op)(struct tg3 *, u32, u32);
3471
3472 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3473 netdev_err(tp->dev,
3474 "%s: Trying to load TX cpu firmware which is 5705\n",
3475 __func__);
3476 return -EINVAL;
3477 }
3478
3479 if (tg3_flag(tp, 5705_PLUS))
3480 write_op = tg3_write_mem;
3481 else
3482 write_op = tg3_write_indirect_reg32;
3483
3484 /* It is possible that bootcode is still loading at this point.
3485 * Get the nvram lock first before halting the cpu.
3486 */
3487 lock_err = tg3_nvram_lock(tp);
3488 err = tg3_halt_cpu(tp, cpu_base);
3489 if (!lock_err)
3490 tg3_nvram_unlock(tp);
3491 if (err)
3492 goto out;
3493
3494 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3495 write_op(tp, cpu_scratch_base + i, 0);
3496 tw32(cpu_base + CPU_STATE, 0xffffffff);
3497 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3498 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3499 write_op(tp, (cpu_scratch_base +
3500 (info->fw_base & 0xffff) +
3501 (i * sizeof(u32))),
3502 be32_to_cpu(info->fw_data[i]));
3503
3504 err = 0;
3505
3506out:
3507 return err;
3508}
3509
3510/* tp->lock is held. */
3511static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3512{
3513 struct fw_info info;
3514 const __be32 *fw_data;
3515 int err, i;
3516
3517 fw_data = (void *)tp->fw->data;
3518
3519 /* Firmware blob starts with version numbers, followed by
3520 start address and length. We are setting complete length.
3521 length = end_address_of_bss - start_address_of_text.
3522 Remainder is the blob to be loaded contiguously
3523 from start address. */
3524
3525 info.fw_base = be32_to_cpu(fw_data[1]);
3526 info.fw_len = tp->fw->size - 12;
3527 info.fw_data = &fw_data[3];
3528
3529 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3530 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3531 &info);
3532 if (err)
3533 return err;
3534
3535 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3536 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3537 &info);
3538 if (err)
3539 return err;
3540
3541 /* Now startup only the RX cpu. */
3542 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3543 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3544
3545 for (i = 0; i < 5; i++) {
3546 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3547 break;
3548 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3549 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3550 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3551 udelay(1000);
3552 }
3553 if (i >= 5) {
3554 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3555 "should be %08x\n", __func__,
3556 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3557 return -ENODEV;
3558 }
3559 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3560 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3561
3562 return 0;
3563}
3564
3565/* tp->lock is held. */
3566static int tg3_load_tso_firmware(struct tg3 *tp)
3567{
3568 struct fw_info info;
3569 const __be32 *fw_data;
3570 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3571 int err, i;
3572
3573 if (tg3_flag(tp, HW_TSO_1) ||
3574 tg3_flag(tp, HW_TSO_2) ||
3575 tg3_flag(tp, HW_TSO_3))
3576 return 0;
3577
3578 fw_data = (void *)tp->fw->data;
3579
3580 /* Firmware blob starts with version numbers, followed by
3581 start address and length. We are setting complete length.
3582 length = end_address_of_bss - start_address_of_text.
3583 Remainder is the blob to be loaded contiguously
3584 from start address. */
3585
3586 info.fw_base = be32_to_cpu(fw_data[1]);
3587 cpu_scratch_size = tp->fw_len;
3588 info.fw_len = tp->fw->size - 12;
3589 info.fw_data = &fw_data[3];
3590
3591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3592 cpu_base = RX_CPU_BASE;
3593 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3594 } else {
3595 cpu_base = TX_CPU_BASE;
3596 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3597 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3598 }
3599
3600 err = tg3_load_firmware_cpu(tp, cpu_base,
3601 cpu_scratch_base, cpu_scratch_size,
3602 &info);
3603 if (err)
3604 return err;
3605
3606 /* Now startup the cpu. */
3607 tw32(cpu_base + CPU_STATE, 0xffffffff);
3608 tw32_f(cpu_base + CPU_PC, info.fw_base);
3609
3610 for (i = 0; i < 5; i++) {
3611 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3612 break;
3613 tw32(cpu_base + CPU_STATE, 0xffffffff);
3614 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3615 tw32_f(cpu_base + CPU_PC, info.fw_base);
3616 udelay(1000);
3617 }
3618 if (i >= 5) {
3619 netdev_err(tp->dev,
3620 "%s fails to set CPU PC, is %08x should be %08x\n",
3621 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3622 return -ENODEV;
3623 }
3624 tw32(cpu_base + CPU_STATE, 0xffffffff);
3625 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3626 return 0;
3627}
3628
3629
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003630/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003631static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3632{
3633 u32 addr_high, addr_low;
3634 int i;
3635
3636 addr_high = ((tp->dev->dev_addr[0] << 8) |
3637 tp->dev->dev_addr[1]);
3638 addr_low = ((tp->dev->dev_addr[2] << 24) |
3639 (tp->dev->dev_addr[3] << 16) |
3640 (tp->dev->dev_addr[4] << 8) |
3641 (tp->dev->dev_addr[5] << 0));
3642 for (i = 0; i < 4; i++) {
3643 if (i == 1 && skip_mac_1)
3644 continue;
3645 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3646 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3647 }
3648
3649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3651 for (i = 0; i < 12; i++) {
3652 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3653 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3654 }
3655 }
3656
3657 addr_high = (tp->dev->dev_addr[0] +
3658 tp->dev->dev_addr[1] +
3659 tp->dev->dev_addr[2] +
3660 tp->dev->dev_addr[3] +
3661 tp->dev->dev_addr[4] +
3662 tp->dev->dev_addr[5]) &
3663 TX_BACKOFF_SEED_MASK;
3664 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3665}
3666
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003667static void tg3_enable_register_access(struct tg3 *tp)
3668{
3669 /*
3670 * Make sure register accesses (indirect or otherwise) will function
3671 * correctly.
3672 */
3673 pci_write_config_dword(tp->pdev,
3674 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3675}
3676
3677static int tg3_power_up(struct tg3 *tp)
3678{
Matt Carlsonbed98292011-07-13 09:27:29 +00003679 int err;
3680
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003681 tg3_enable_register_access(tp);
3682
Matt Carlsonbed98292011-07-13 09:27:29 +00003683 err = pci_set_power_state(tp->pdev, PCI_D0);
3684 if (!err) {
3685 /* Switch out of Vaux if it is a NIC */
3686 tg3_pwrsrc_switch_to_vmain(tp);
3687 } else {
3688 netdev_err(tp->dev, "Transition to D0 failed\n");
3689 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003690
Matt Carlsonbed98292011-07-13 09:27:29 +00003691 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003692}
3693
Matt Carlson4b409522012-02-13 10:20:11 +00003694static int tg3_setup_phy(struct tg3 *, int);
3695
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003696static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003699 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003701 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003702
3703 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003704 if (tg3_flag(tp, CLKREQ_BUG))
3705 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3706 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3709 tw32(TG3PCI_MISC_HOST_CTRL,
3710 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3711
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003712 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003713 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003714
Joe Perches63c3a662011-04-26 08:12:10 +00003715 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003716 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003717 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003718 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003719 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003720 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003721
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003722 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003723
Matt Carlson80096062010-08-02 11:26:06 +00003724 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003725
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003726 tp->link_config.speed = phydev->speed;
3727 tp->link_config.duplex = phydev->duplex;
3728 tp->link_config.autoneg = phydev->autoneg;
3729 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003730
3731 advertising = ADVERTISED_TP |
3732 ADVERTISED_Pause |
3733 ADVERTISED_Autoneg |
3734 ADVERTISED_10baseT_Half;
3735
Joe Perches63c3a662011-04-26 08:12:10 +00003736 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3737 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003738 advertising |=
3739 ADVERTISED_100baseT_Half |
3740 ADVERTISED_100baseT_Full |
3741 ADVERTISED_10baseT_Full;
3742 else
3743 advertising |= ADVERTISED_10baseT_Full;
3744 }
3745
3746 phydev->advertising = advertising;
3747
3748 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003749
3750 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003751 if (phyid != PHY_ID_BCMAC131) {
3752 phyid &= PHY_BCM_OUI_MASK;
3753 if (phyid == PHY_BCM_OUI_1 ||
3754 phyid == PHY_BCM_OUI_2 ||
3755 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003756 do_low_power = true;
3757 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003758 }
Matt Carlsondd477002008-05-25 23:45:58 -07003759 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003760 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003761
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003762 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003763 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Matt Carlson2855b9f2012-02-13 15:20:14 +00003765 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003766 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
Michael Chanb5d37722006-09-27 16:06:21 -07003769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3770 u32 val;
3771
3772 val = tr32(GRC_VCPU_EXT_CTRL);
3773 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003774 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003775 int i;
3776 u32 val;
3777
3778 for (i = 0; i < 200; i++) {
3779 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3780 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3781 break;
3782 msleep(1);
3783 }
3784 }
Joe Perches63c3a662011-04-26 08:12:10 +00003785 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003786 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3787 WOL_DRV_STATE_SHUTDOWN |
3788 WOL_DRV_WOL |
3789 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003790
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003791 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 u32 mac_mode;
3793
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003794 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003795 if (do_low_power &&
3796 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3797 tg3_phy_auxctl_write(tp,
3798 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3799 MII_TG3_AUXCTL_PCTL_WOL_EN |
3800 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3801 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003802 udelay(40);
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003805 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003806 mac_mode = MAC_MODE_PORT_MODE_GMII;
3807 else
3808 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003810 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3811 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3812 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003813 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003814 SPEED_100 : SPEED_10;
3815 if (tg3_5700_link_polarity(tp, speed))
3816 mac_mode |= MAC_MODE_LINK_POLARITY;
3817 else
3818 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 } else {
3821 mac_mode = MAC_MODE_PORT_MODE_TBI;
3822 }
3823
Joe Perches63c3a662011-04-26 08:12:10 +00003824 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 tw32(MAC_LED_CTRL, tp->led_ctrl);
3826
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003827 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003828 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3829 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003830 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Joe Perches63c3a662011-04-26 08:12:10 +00003832 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003833 mac_mode |= MAC_MODE_APE_TX_EN |
3834 MAC_MODE_APE_RX_EN |
3835 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 tw32_f(MAC_MODE, mac_mode);
3838 udelay(100);
3839
3840 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3841 udelay(10);
3842 }
3843
Joe Perches63c3a662011-04-26 08:12:10 +00003844 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3847 u32 base_val;
3848
3849 base_val = tp->pci_clock_ctrl;
3850 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3851 CLOCK_CTRL_TXCLK_DISABLE);
3852
Michael Chanb401e9e2005-12-19 16:27:04 -08003853 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3854 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003855 } else if (tg3_flag(tp, 5780_CLASS) ||
3856 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003858 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003859 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 u32 newbits1, newbits2;
3861
3862 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3864 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3865 CLOCK_CTRL_TXCLK_DISABLE |
3866 CLOCK_CTRL_ALTCLK);
3867 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003868 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 newbits1 = CLOCK_CTRL_625_CORE;
3870 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3871 } else {
3872 newbits1 = CLOCK_CTRL_ALTCLK;
3873 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3874 }
3875
Michael Chanb401e9e2005-12-19 16:27:04 -08003876 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3877 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Michael Chanb401e9e2005-12-19 16:27:04 -08003879 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3880 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Joe Perches63c3a662011-04-26 08:12:10 +00003882 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 u32 newbits3;
3884
3885 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3886 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3887 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3888 CLOCK_CTRL_TXCLK_DISABLE |
3889 CLOCK_CTRL_44MHZ_CORE);
3890 } else {
3891 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3892 }
3893
Michael Chanb401e9e2005-12-19 16:27:04 -08003894 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3895 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 }
3898
Joe Perches63c3a662011-04-26 08:12:10 +00003899 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003900 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003901
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003902 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 /* Workaround for unstable PLL clock */
3905 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3906 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3907 u32 val = tr32(0x7d00);
3908
3909 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3910 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003911 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003912 int err;
3913
3914 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003916 if (!err)
3917 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
3920
Michael Chanbbadf502006-04-06 21:46:34 -07003921 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return 0;
3924}
3925
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003926static void tg3_power_down(struct tg3 *tp)
3927{
3928 tg3_power_down_prepare(tp);
3929
Joe Perches63c3a662011-04-26 08:12:10 +00003930 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003931 pci_set_power_state(tp->pdev, PCI_D3hot);
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3935{
3936 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3937 case MII_TG3_AUX_STAT_10HALF:
3938 *speed = SPEED_10;
3939 *duplex = DUPLEX_HALF;
3940 break;
3941
3942 case MII_TG3_AUX_STAT_10FULL:
3943 *speed = SPEED_10;
3944 *duplex = DUPLEX_FULL;
3945 break;
3946
3947 case MII_TG3_AUX_STAT_100HALF:
3948 *speed = SPEED_100;
3949 *duplex = DUPLEX_HALF;
3950 break;
3951
3952 case MII_TG3_AUX_STAT_100FULL:
3953 *speed = SPEED_100;
3954 *duplex = DUPLEX_FULL;
3955 break;
3956
3957 case MII_TG3_AUX_STAT_1000HALF:
3958 *speed = SPEED_1000;
3959 *duplex = DUPLEX_HALF;
3960 break;
3961
3962 case MII_TG3_AUX_STAT_1000FULL:
3963 *speed = SPEED_1000;
3964 *duplex = DUPLEX_FULL;
3965 break;
3966
3967 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003968 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003969 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3970 SPEED_10;
3971 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3972 DUPLEX_HALF;
3973 break;
3974 }
Matt Carlsone7405222012-02-13 15:20:16 +00003975 *speed = SPEED_UNKNOWN;
3976 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
Matt Carlson42b64a42011-05-19 12:12:49 +00003981static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Matt Carlson42b64a42011-05-19 12:12:49 +00003983 int err = 0;
3984 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Matt Carlson42b64a42011-05-19 12:12:49 +00003986 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003987 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003988 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Matt Carlson42b64a42011-05-19 12:12:49 +00003990 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3991 if (err)
3992 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Matt Carlson4f272092011-12-14 11:09:57 +00003994 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3995 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003996
Matt Carlson4f272092011-12-14 11:09:57 +00003997 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3998 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3999 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004000
Matt Carlson4f272092011-12-14 11:09:57 +00004001 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4002 if (err)
4003 goto done;
4004 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004005
Matt Carlson42b64a42011-05-19 12:12:49 +00004006 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4007 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Matt Carlson42b64a42011-05-19 12:12:49 +00004009 tw32(TG3_CPMU_EEE_MODE,
4010 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004011
Matt Carlson42b64a42011-05-19 12:12:49 +00004012 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
4013 if (!err) {
4014 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004015
Matt Carlsona6b68da2010-12-06 08:28:52 +00004016 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004017 /* Advertise 100-BaseTX EEE ability */
4018 if (advertise & ADVERTISED_100baseT_Full)
4019 val |= MDIO_AN_EEE_ADV_100TX;
4020 /* Advertise 1000-BaseT EEE ability */
4021 if (advertise & ADVERTISED_1000baseT_Full)
4022 val |= MDIO_AN_EEE_ADV_1000T;
4023 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004024 if (err)
4025 val = 0;
4026
4027 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
4028 case ASIC_REV_5717:
4029 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004030 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004031 case ASIC_REV_5719:
4032 /* If we advertised any eee advertisements above... */
4033 if (val)
4034 val = MII_TG3_DSP_TAP26_ALNOKO |
4035 MII_TG3_DSP_TAP26_RMRXSTO |
4036 MII_TG3_DSP_TAP26_OPCSINPT;
4037 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4038 /* Fall through */
4039 case ASIC_REV_5720:
4040 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4041 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4042 MII_TG3_DSP_CH34TP2_HIBW01);
4043 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004044
Matt Carlson42b64a42011-05-19 12:12:49 +00004045 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
4046 if (!err)
4047 err = err2;
4048 }
4049
4050done:
4051 return err;
4052}
4053
4054static void tg3_phy_copper_begin(struct tg3 *tp)
4055{
Matt Carlsond13ba512012-02-22 12:35:19 +00004056 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4057 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4058 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004059
Matt Carlsond13ba512012-02-22 12:35:19 +00004060 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4061 adv = ADVERTISED_10baseT_Half |
4062 ADVERTISED_10baseT_Full;
4063 if (tg3_flag(tp, WOL_SPEED_100MB))
4064 adv |= ADVERTISED_100baseT_Half |
4065 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004066
Matt Carlsond13ba512012-02-22 12:35:19 +00004067 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004068 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004069 adv = tp->link_config.advertising;
4070 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4071 adv &= ~(ADVERTISED_1000baseT_Half |
4072 ADVERTISED_1000baseT_Full);
4073
4074 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004075 }
4076
Matt Carlsond13ba512012-02-22 12:35:19 +00004077 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004078
Matt Carlsond13ba512012-02-22 12:35:19 +00004079 tg3_writephy(tp, MII_BMCR,
4080 BMCR_ANENABLE | BMCR_ANRESTART);
4081 } else {
4082 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 u32 bmcr, orig_bmcr;
4084
4085 tp->link_config.active_speed = tp->link_config.speed;
4086 tp->link_config.active_duplex = tp->link_config.duplex;
4087
4088 bmcr = 0;
4089 switch (tp->link_config.speed) {
4090 default:
4091 case SPEED_10:
4092 break;
4093
4094 case SPEED_100:
4095 bmcr |= BMCR_SPEED100;
4096 break;
4097
4098 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004099 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
4103 if (tp->link_config.duplex == DUPLEX_FULL)
4104 bmcr |= BMCR_FULLDPLX;
4105
4106 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4107 (bmcr != orig_bmcr)) {
4108 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4109 for (i = 0; i < 1500; i++) {
4110 u32 tmp;
4111
4112 udelay(10);
4113 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4114 tg3_readphy(tp, MII_BMSR, &tmp))
4115 continue;
4116 if (!(tmp & BMSR_LSTATUS)) {
4117 udelay(40);
4118 break;
4119 }
4120 }
4121 tg3_writephy(tp, MII_BMCR, bmcr);
4122 udelay(40);
4123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125}
4126
4127static int tg3_init_5401phy_dsp(struct tg3 *tp)
4128{
4129 int err;
4130
4131 /* Turn off tap power management. */
4132 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004133 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004135 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4136 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4137 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4138 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4139 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141 udelay(40);
4142
4143 return err;
4144}
4145
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004146static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004148 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004149
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004150 advertising = tp->link_config.advertising;
4151 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004153 advmsk = ADVERTISE_ALL;
4154 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004155 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004156 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004159 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4160 return false;
4161
4162 if ((*lcladv & advmsk) != tgtadv)
4163 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004164
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004165 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 u32 tg3_ctrl;
4167
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004168 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004169
Matt Carlson221c5632011-06-13 13:39:01 +00004170 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004171 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Matt Carlson3198e072012-02-13 15:20:10 +00004173 if (tgtadv &&
4174 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4175 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4176 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4177 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4178 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4179 } else {
4180 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4181 }
4182
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004183 if (tg3_ctrl != tgtadv)
4184 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004186
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004187 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004188}
4189
Matt Carlson859edb22011-12-08 14:40:16 +00004190static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4191{
4192 u32 lpeth = 0;
4193
4194 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4195 u32 val;
4196
4197 if (tg3_readphy(tp, MII_STAT1000, &val))
4198 return false;
4199
4200 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4201 }
4202
4203 if (tg3_readphy(tp, MII_LPA, rmtadv))
4204 return false;
4205
4206 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4207 tp->link_config.rmt_adv = lpeth;
4208
4209 return true;
4210}
4211
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004212static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4213{
4214 if (curr_link_up != tp->link_up) {
4215 if (curr_link_up) {
4216 tg3_carrier_on(tp);
4217 } else {
4218 tg3_carrier_off(tp);
4219 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4220 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4221 }
4222
4223 tg3_link_report(tp);
4224 return true;
4225 }
4226
4227 return false;
4228}
4229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4231{
4232 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004233 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004234 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 u16 current_speed;
4236 u8 current_duplex;
4237 int i, err;
4238
4239 tw32(MAC_EVENT, 0);
4240
4241 tw32_f(MAC_STATUS,
4242 (MAC_STATUS_SYNC_CHANGED |
4243 MAC_STATUS_CFG_CHANGED |
4244 MAC_STATUS_MI_COMPLETION |
4245 MAC_STATUS_LNKSTATE_CHANGED));
4246 udelay(40);
4247
Matt Carlson8ef21422008-05-02 16:47:53 -07004248 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4249 tw32_f(MAC_MI_MODE,
4250 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4251 udelay(80);
4252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004254 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
4256 /* Some third-party PHYs need to be reset on link going
4257 * down.
4258 */
4259 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4260 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004262 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 tg3_readphy(tp, MII_BMSR, &bmsr);
4264 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4265 !(bmsr & BMSR_LSTATUS))
4266 force_reset = 1;
4267 }
4268 if (force_reset)
4269 tg3_phy_reset(tp);
4270
Matt Carlson79eb6902010-02-17 15:17:03 +00004271 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 tg3_readphy(tp, MII_BMSR, &bmsr);
4273 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004274 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 bmsr = 0;
4276
4277 if (!(bmsr & BMSR_LSTATUS)) {
4278 err = tg3_init_5401phy_dsp(tp);
4279 if (err)
4280 return err;
4281
4282 tg3_readphy(tp, MII_BMSR, &bmsr);
4283 for (i = 0; i < 1000; i++) {
4284 udelay(10);
4285 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4286 (bmsr & BMSR_LSTATUS)) {
4287 udelay(40);
4288 break;
4289 }
4290 }
4291
Matt Carlson79eb6902010-02-17 15:17:03 +00004292 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4293 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 !(bmsr & BMSR_LSTATUS) &&
4295 tp->link_config.active_speed == SPEED_1000) {
4296 err = tg3_phy_reset(tp);
4297 if (!err)
4298 err = tg3_init_5401phy_dsp(tp);
4299 if (err)
4300 return err;
4301 }
4302 }
4303 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4304 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4305 /* 5701 {A0,B0} CRC bug workaround */
4306 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004307 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4308 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4309 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
4311
4312 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004313 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4314 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004316 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004318 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4320
4321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4323 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4324 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4325 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4326 else
4327 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4328 }
4329
4330 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004331 current_speed = SPEED_UNKNOWN;
4332 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004333 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004334 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004336 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004337 err = tg3_phy_auxctl_read(tp,
4338 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4339 &val);
4340 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004341 tg3_phy_auxctl_write(tp,
4342 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4343 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 goto relink;
4345 }
4346 }
4347
4348 bmsr = 0;
4349 for (i = 0; i < 100; i++) {
4350 tg3_readphy(tp, MII_BMSR, &bmsr);
4351 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4352 (bmsr & BMSR_LSTATUS))
4353 break;
4354 udelay(40);
4355 }
4356
4357 if (bmsr & BMSR_LSTATUS) {
4358 u32 aux_stat, bmcr;
4359
4360 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4361 for (i = 0; i < 2000; i++) {
4362 udelay(10);
4363 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4364 aux_stat)
4365 break;
4366 }
4367
4368 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4369 &current_speed,
4370 &current_duplex);
4371
4372 bmcr = 0;
4373 for (i = 0; i < 200; i++) {
4374 tg3_readphy(tp, MII_BMCR, &bmcr);
4375 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4376 continue;
4377 if (bmcr && bmcr != 0x7fff)
4378 break;
4379 udelay(10);
4380 }
4381
Matt Carlsonef167e22007-12-20 20:10:01 -08004382 lcl_adv = 0;
4383 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Matt Carlsonef167e22007-12-20 20:10:01 -08004385 tp->link_config.active_speed = current_speed;
4386 tp->link_config.active_duplex = current_duplex;
4387
4388 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4389 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004390 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004391 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004392 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 } else {
4394 if (!(bmcr & BMCR_ANENABLE) &&
4395 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004396 tp->link_config.duplex == current_duplex &&
4397 tp->link_config.flowctrl ==
4398 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 }
4401 }
4402
Matt Carlsonef167e22007-12-20 20:10:01 -08004403 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004404 tp->link_config.active_duplex == DUPLEX_FULL) {
4405 u32 reg, bit;
4406
4407 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4408 reg = MII_TG3_FET_GEN_STAT;
4409 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4410 } else {
4411 reg = MII_TG3_EXT_STAT;
4412 bit = MII_TG3_EXT_STAT_MDIX;
4413 }
4414
4415 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4416 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4417
Matt Carlsonef167e22007-12-20 20:10:01 -08004418 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 }
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422relink:
Matt Carlson80096062010-08-02 11:26:06 +00004423 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 tg3_phy_copper_begin(tp);
4425
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004426 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004427 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4428 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 current_link_up = 1;
4430 }
4431
4432 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4433 if (current_link_up == 1) {
4434 if (tp->link_config.active_speed == SPEED_100 ||
4435 tp->link_config.active_speed == SPEED_10)
4436 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4437 else
4438 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004439 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004440 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4441 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4443
4444 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4445 if (tp->link_config.active_duplex == DUPLEX_HALF)
4446 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004449 if (current_link_up == 1 &&
4450 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004452 else
4453 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
4456 /* ??? Without this setting Netgear GA302T PHY does not
4457 * ??? send/receive packets...
4458 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004459 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4461 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4462 tw32_f(MAC_MI_MODE, tp->mi_mode);
4463 udelay(80);
4464 }
4465
4466 tw32_f(MAC_MODE, tp->mac_mode);
4467 udelay(40);
4468
Matt Carlson52b02d02010-10-14 10:37:41 +00004469 tg3_phy_eee_adjust(tp, current_link_up);
4470
Joe Perches63c3a662011-04-26 08:12:10 +00004471 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 /* Polled via timer. */
4473 tw32_f(MAC_EVENT, 0);
4474 } else {
4475 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4476 }
4477 udelay(40);
4478
4479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4480 current_link_up == 1 &&
4481 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004482 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 udelay(120);
4484 tw32_f(MAC_STATUS,
4485 (MAC_STATUS_SYNC_CHANGED |
4486 MAC_STATUS_CFG_CHANGED));
4487 udelay(40);
4488 tg3_write_mem(tp,
4489 NIC_SRAM_FIRMWARE_MBOX,
4490 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4491 }
4492
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004493 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004494 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004495 if (tp->link_config.active_speed == SPEED_100 ||
4496 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004497 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4498 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004499 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004500 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4501 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004502 }
4503
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004504 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 return 0;
4507}
4508
4509struct tg3_fiber_aneginfo {
4510 int state;
4511#define ANEG_STATE_UNKNOWN 0
4512#define ANEG_STATE_AN_ENABLE 1
4513#define ANEG_STATE_RESTART_INIT 2
4514#define ANEG_STATE_RESTART 3
4515#define ANEG_STATE_DISABLE_LINK_OK 4
4516#define ANEG_STATE_ABILITY_DETECT_INIT 5
4517#define ANEG_STATE_ABILITY_DETECT 6
4518#define ANEG_STATE_ACK_DETECT_INIT 7
4519#define ANEG_STATE_ACK_DETECT 8
4520#define ANEG_STATE_COMPLETE_ACK_INIT 9
4521#define ANEG_STATE_COMPLETE_ACK 10
4522#define ANEG_STATE_IDLE_DETECT_INIT 11
4523#define ANEG_STATE_IDLE_DETECT 12
4524#define ANEG_STATE_LINK_OK 13
4525#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4526#define ANEG_STATE_NEXT_PAGE_WAIT 15
4527
4528 u32 flags;
4529#define MR_AN_ENABLE 0x00000001
4530#define MR_RESTART_AN 0x00000002
4531#define MR_AN_COMPLETE 0x00000004
4532#define MR_PAGE_RX 0x00000008
4533#define MR_NP_LOADED 0x00000010
4534#define MR_TOGGLE_TX 0x00000020
4535#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4536#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4537#define MR_LP_ADV_SYM_PAUSE 0x00000100
4538#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4539#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4540#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4541#define MR_LP_ADV_NEXT_PAGE 0x00001000
4542#define MR_TOGGLE_RX 0x00002000
4543#define MR_NP_RX 0x00004000
4544
4545#define MR_LINK_OK 0x80000000
4546
4547 unsigned long link_time, cur_time;
4548
4549 u32 ability_match_cfg;
4550 int ability_match_count;
4551
4552 char ability_match, idle_match, ack_match;
4553
4554 u32 txconfig, rxconfig;
4555#define ANEG_CFG_NP 0x00000080
4556#define ANEG_CFG_ACK 0x00000040
4557#define ANEG_CFG_RF2 0x00000020
4558#define ANEG_CFG_RF1 0x00000010
4559#define ANEG_CFG_PS2 0x00000001
4560#define ANEG_CFG_PS1 0x00008000
4561#define ANEG_CFG_HD 0x00004000
4562#define ANEG_CFG_FD 0x00002000
4563#define ANEG_CFG_INVAL 0x00001f06
4564
4565};
4566#define ANEG_OK 0
4567#define ANEG_DONE 1
4568#define ANEG_TIMER_ENAB 2
4569#define ANEG_FAILED -1
4570
4571#define ANEG_STATE_SETTLE_TIME 10000
4572
4573static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4574 struct tg3_fiber_aneginfo *ap)
4575{
Matt Carlson5be73b42007-12-20 20:09:29 -08004576 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 unsigned long delta;
4578 u32 rx_cfg_reg;
4579 int ret;
4580
4581 if (ap->state == ANEG_STATE_UNKNOWN) {
4582 ap->rxconfig = 0;
4583 ap->link_time = 0;
4584 ap->cur_time = 0;
4585 ap->ability_match_cfg = 0;
4586 ap->ability_match_count = 0;
4587 ap->ability_match = 0;
4588 ap->idle_match = 0;
4589 ap->ack_match = 0;
4590 }
4591 ap->cur_time++;
4592
4593 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4594 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4595
4596 if (rx_cfg_reg != ap->ability_match_cfg) {
4597 ap->ability_match_cfg = rx_cfg_reg;
4598 ap->ability_match = 0;
4599 ap->ability_match_count = 0;
4600 } else {
4601 if (++ap->ability_match_count > 1) {
4602 ap->ability_match = 1;
4603 ap->ability_match_cfg = rx_cfg_reg;
4604 }
4605 }
4606 if (rx_cfg_reg & ANEG_CFG_ACK)
4607 ap->ack_match = 1;
4608 else
4609 ap->ack_match = 0;
4610
4611 ap->idle_match = 0;
4612 } else {
4613 ap->idle_match = 1;
4614 ap->ability_match_cfg = 0;
4615 ap->ability_match_count = 0;
4616 ap->ability_match = 0;
4617 ap->ack_match = 0;
4618
4619 rx_cfg_reg = 0;
4620 }
4621
4622 ap->rxconfig = rx_cfg_reg;
4623 ret = ANEG_OK;
4624
Matt Carlson33f401a2010-04-05 10:19:27 +00004625 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 case ANEG_STATE_UNKNOWN:
4627 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4628 ap->state = ANEG_STATE_AN_ENABLE;
4629
4630 /* fallthru */
4631 case ANEG_STATE_AN_ENABLE:
4632 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4633 if (ap->flags & MR_AN_ENABLE) {
4634 ap->link_time = 0;
4635 ap->cur_time = 0;
4636 ap->ability_match_cfg = 0;
4637 ap->ability_match_count = 0;
4638 ap->ability_match = 0;
4639 ap->idle_match = 0;
4640 ap->ack_match = 0;
4641
4642 ap->state = ANEG_STATE_RESTART_INIT;
4643 } else {
4644 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4645 }
4646 break;
4647
4648 case ANEG_STATE_RESTART_INIT:
4649 ap->link_time = ap->cur_time;
4650 ap->flags &= ~(MR_NP_LOADED);
4651 ap->txconfig = 0;
4652 tw32(MAC_TX_AUTO_NEG, 0);
4653 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4654 tw32_f(MAC_MODE, tp->mac_mode);
4655 udelay(40);
4656
4657 ret = ANEG_TIMER_ENAB;
4658 ap->state = ANEG_STATE_RESTART;
4659
4660 /* fallthru */
4661 case ANEG_STATE_RESTART:
4662 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004663 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004665 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 break;
4668
4669 case ANEG_STATE_DISABLE_LINK_OK:
4670 ret = ANEG_DONE;
4671 break;
4672
4673 case ANEG_STATE_ABILITY_DETECT_INIT:
4674 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004675 ap->txconfig = ANEG_CFG_FD;
4676 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4677 if (flowctrl & ADVERTISE_1000XPAUSE)
4678 ap->txconfig |= ANEG_CFG_PS1;
4679 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4680 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4682 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4683 tw32_f(MAC_MODE, tp->mac_mode);
4684 udelay(40);
4685
4686 ap->state = ANEG_STATE_ABILITY_DETECT;
4687 break;
4688
4689 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004690 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 break;
4693
4694 case ANEG_STATE_ACK_DETECT_INIT:
4695 ap->txconfig |= ANEG_CFG_ACK;
4696 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4697 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4698 tw32_f(MAC_MODE, tp->mac_mode);
4699 udelay(40);
4700
4701 ap->state = ANEG_STATE_ACK_DETECT;
4702
4703 /* fallthru */
4704 case ANEG_STATE_ACK_DETECT:
4705 if (ap->ack_match != 0) {
4706 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4707 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4708 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4709 } else {
4710 ap->state = ANEG_STATE_AN_ENABLE;
4711 }
4712 } else if (ap->ability_match != 0 &&
4713 ap->rxconfig == 0) {
4714 ap->state = ANEG_STATE_AN_ENABLE;
4715 }
4716 break;
4717
4718 case ANEG_STATE_COMPLETE_ACK_INIT:
4719 if (ap->rxconfig & ANEG_CFG_INVAL) {
4720 ret = ANEG_FAILED;
4721 break;
4722 }
4723 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4724 MR_LP_ADV_HALF_DUPLEX |
4725 MR_LP_ADV_SYM_PAUSE |
4726 MR_LP_ADV_ASYM_PAUSE |
4727 MR_LP_ADV_REMOTE_FAULT1 |
4728 MR_LP_ADV_REMOTE_FAULT2 |
4729 MR_LP_ADV_NEXT_PAGE |
4730 MR_TOGGLE_RX |
4731 MR_NP_RX);
4732 if (ap->rxconfig & ANEG_CFG_FD)
4733 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4734 if (ap->rxconfig & ANEG_CFG_HD)
4735 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4736 if (ap->rxconfig & ANEG_CFG_PS1)
4737 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4738 if (ap->rxconfig & ANEG_CFG_PS2)
4739 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4740 if (ap->rxconfig & ANEG_CFG_RF1)
4741 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4742 if (ap->rxconfig & ANEG_CFG_RF2)
4743 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4744 if (ap->rxconfig & ANEG_CFG_NP)
4745 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4746
4747 ap->link_time = ap->cur_time;
4748
4749 ap->flags ^= (MR_TOGGLE_TX);
4750 if (ap->rxconfig & 0x0008)
4751 ap->flags |= MR_TOGGLE_RX;
4752 if (ap->rxconfig & ANEG_CFG_NP)
4753 ap->flags |= MR_NP_RX;
4754 ap->flags |= MR_PAGE_RX;
4755
4756 ap->state = ANEG_STATE_COMPLETE_ACK;
4757 ret = ANEG_TIMER_ENAB;
4758 break;
4759
4760 case ANEG_STATE_COMPLETE_ACK:
4761 if (ap->ability_match != 0 &&
4762 ap->rxconfig == 0) {
4763 ap->state = ANEG_STATE_AN_ENABLE;
4764 break;
4765 }
4766 delta = ap->cur_time - ap->link_time;
4767 if (delta > ANEG_STATE_SETTLE_TIME) {
4768 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4769 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4770 } else {
4771 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4772 !(ap->flags & MR_NP_RX)) {
4773 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4774 } else {
4775 ret = ANEG_FAILED;
4776 }
4777 }
4778 }
4779 break;
4780
4781 case ANEG_STATE_IDLE_DETECT_INIT:
4782 ap->link_time = ap->cur_time;
4783 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4784 tw32_f(MAC_MODE, tp->mac_mode);
4785 udelay(40);
4786
4787 ap->state = ANEG_STATE_IDLE_DETECT;
4788 ret = ANEG_TIMER_ENAB;
4789 break;
4790
4791 case ANEG_STATE_IDLE_DETECT:
4792 if (ap->ability_match != 0 &&
4793 ap->rxconfig == 0) {
4794 ap->state = ANEG_STATE_AN_ENABLE;
4795 break;
4796 }
4797 delta = ap->cur_time - ap->link_time;
4798 if (delta > ANEG_STATE_SETTLE_TIME) {
4799 /* XXX another gem from the Broadcom driver :( */
4800 ap->state = ANEG_STATE_LINK_OK;
4801 }
4802 break;
4803
4804 case ANEG_STATE_LINK_OK:
4805 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4806 ret = ANEG_DONE;
4807 break;
4808
4809 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4810 /* ??? unimplemented */
4811 break;
4812
4813 case ANEG_STATE_NEXT_PAGE_WAIT:
4814 /* ??? unimplemented */
4815 break;
4816
4817 default:
4818 ret = ANEG_FAILED;
4819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 return ret;
4823}
4824
Matt Carlson5be73b42007-12-20 20:09:29 -08004825static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 int res = 0;
4828 struct tg3_fiber_aneginfo aninfo;
4829 int status = ANEG_FAILED;
4830 unsigned int tick;
4831 u32 tmp;
4832
4833 tw32_f(MAC_TX_AUTO_NEG, 0);
4834
4835 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4836 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4837 udelay(40);
4838
4839 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4840 udelay(40);
4841
4842 memset(&aninfo, 0, sizeof(aninfo));
4843 aninfo.flags |= MR_AN_ENABLE;
4844 aninfo.state = ANEG_STATE_UNKNOWN;
4845 aninfo.cur_time = 0;
4846 tick = 0;
4847 while (++tick < 195000) {
4848 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4849 if (status == ANEG_DONE || status == ANEG_FAILED)
4850 break;
4851
4852 udelay(1);
4853 }
4854
4855 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4856 tw32_f(MAC_MODE, tp->mac_mode);
4857 udelay(40);
4858
Matt Carlson5be73b42007-12-20 20:09:29 -08004859 *txflags = aninfo.txconfig;
4860 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 if (status == ANEG_DONE &&
4863 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4864 MR_LP_ADV_FULL_DUPLEX)))
4865 res = 1;
4866
4867 return res;
4868}
4869
4870static void tg3_init_bcm8002(struct tg3 *tp)
4871{
4872 u32 mac_status = tr32(MAC_STATUS);
4873 int i;
4874
4875 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004876 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 !(mac_status & MAC_STATUS_PCS_SYNCED))
4878 return;
4879
4880 /* Set PLL lock range. */
4881 tg3_writephy(tp, 0x16, 0x8007);
4882
4883 /* SW reset */
4884 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4885
4886 /* Wait for reset to complete. */
4887 /* XXX schedule_timeout() ... */
4888 for (i = 0; i < 500; i++)
4889 udelay(10);
4890
4891 /* Config mode; select PMA/Ch 1 regs. */
4892 tg3_writephy(tp, 0x10, 0x8411);
4893
4894 /* Enable auto-lock and comdet, select txclk for tx. */
4895 tg3_writephy(tp, 0x11, 0x0a10);
4896
4897 tg3_writephy(tp, 0x18, 0x00a0);
4898 tg3_writephy(tp, 0x16, 0x41ff);
4899
4900 /* Assert and deassert POR. */
4901 tg3_writephy(tp, 0x13, 0x0400);
4902 udelay(40);
4903 tg3_writephy(tp, 0x13, 0x0000);
4904
4905 tg3_writephy(tp, 0x11, 0x0a50);
4906 udelay(40);
4907 tg3_writephy(tp, 0x11, 0x0a10);
4908
4909 /* Wait for signal to stabilize */
4910 /* XXX schedule_timeout() ... */
4911 for (i = 0; i < 15000; i++)
4912 udelay(10);
4913
4914 /* Deselect the channel register so we can read the PHYID
4915 * later.
4916 */
4917 tg3_writephy(tp, 0x10, 0x8011);
4918}
4919
4920static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4921{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004922 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 u32 sg_dig_ctrl, sg_dig_status;
4924 u32 serdes_cfg, expected_sg_dig_ctrl;
4925 int workaround, port_a;
4926 int current_link_up;
4927
4928 serdes_cfg = 0;
4929 expected_sg_dig_ctrl = 0;
4930 workaround = 0;
4931 port_a = 1;
4932 current_link_up = 0;
4933
4934 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4935 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4936 workaround = 1;
4937 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4938 port_a = 0;
4939
4940 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4941 /* preserve bits 20-23 for voltage regulator */
4942 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4943 }
4944
4945 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4946
4947 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004948 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (workaround) {
4950 u32 val = serdes_cfg;
4951
4952 if (port_a)
4953 val |= 0xc010000;
4954 else
4955 val |= 0x4010000;
4956 tw32_f(MAC_SERDES_CFG, val);
4957 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004958
4959 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4962 tg3_setup_flow_control(tp, 0, 0);
4963 current_link_up = 1;
4964 }
4965 goto out;
4966 }
4967
4968 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004969 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Matt Carlson82cd3d12007-12-20 20:09:00 -08004971 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4972 if (flowctrl & ADVERTISE_1000XPAUSE)
4973 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4974 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4975 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004978 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004979 tp->serdes_counter &&
4980 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4981 MAC_STATUS_RCVD_CFG)) ==
4982 MAC_STATUS_PCS_SYNCED)) {
4983 tp->serdes_counter--;
4984 current_link_up = 1;
4985 goto out;
4986 }
4987restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 if (workaround)
4989 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004990 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 udelay(5);
4992 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4993
Michael Chan3d3ebe72006-09-27 15:59:15 -07004994 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004995 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4997 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004998 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 mac_status = tr32(MAC_STATUS);
5000
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005001 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005003 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Matt Carlson82cd3d12007-12-20 20:09:00 -08005005 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5006 local_adv |= ADVERTISE_1000XPAUSE;
5007 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5008 local_adv |= ADVERTISE_1000XPSE_ASYM;
5009
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005010 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005011 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005012 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005013 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Matt Carlson859edb22011-12-08 14:40:16 +00005015 tp->link_config.rmt_adv =
5016 mii_adv_to_ethtool_adv_x(remote_adv);
5017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 tg3_setup_flow_control(tp, local_adv, remote_adv);
5019 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005020 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005021 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005022 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005023 if (tp->serdes_counter)
5024 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 else {
5026 if (workaround) {
5027 u32 val = serdes_cfg;
5028
5029 if (port_a)
5030 val |= 0xc010000;
5031 else
5032 val |= 0x4010000;
5033
5034 tw32_f(MAC_SERDES_CFG, val);
5035 }
5036
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005037 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 udelay(40);
5039
5040 /* Link parallel detection - link is up */
5041 /* only if we have PCS_SYNC and not */
5042 /* receiving config code words */
5043 mac_status = tr32(MAC_STATUS);
5044 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5045 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5046 tg3_setup_flow_control(tp, 0, 0);
5047 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005048 tp->phy_flags |=
5049 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005050 tp->serdes_counter =
5051 SERDES_PARALLEL_DET_TIMEOUT;
5052 } else
5053 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
5055 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005056 } else {
5057 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005058 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
5060
5061out:
5062 return current_link_up;
5063}
5064
5065static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5066{
5067 int current_link_up = 0;
5068
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005069 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
5072 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005073 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005075
Matt Carlson5be73b42007-12-20 20:09:29 -08005076 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5077 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Matt Carlson5be73b42007-12-20 20:09:29 -08005079 if (txflags & ANEG_CFG_PS1)
5080 local_adv |= ADVERTISE_1000XPAUSE;
5081 if (txflags & ANEG_CFG_PS2)
5082 local_adv |= ADVERTISE_1000XPSE_ASYM;
5083
5084 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5085 remote_adv |= LPA_1000XPAUSE;
5086 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5087 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Matt Carlson859edb22011-12-08 14:40:16 +00005089 tp->link_config.rmt_adv =
5090 mii_adv_to_ethtool_adv_x(remote_adv);
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 tg3_setup_flow_control(tp, local_adv, remote_adv);
5093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 current_link_up = 1;
5095 }
5096 for (i = 0; i < 30; i++) {
5097 udelay(20);
5098 tw32_f(MAC_STATUS,
5099 (MAC_STATUS_SYNC_CHANGED |
5100 MAC_STATUS_CFG_CHANGED));
5101 udelay(40);
5102 if ((tr32(MAC_STATUS) &
5103 (MAC_STATUS_SYNC_CHANGED |
5104 MAC_STATUS_CFG_CHANGED)) == 0)
5105 break;
5106 }
5107
5108 mac_status = tr32(MAC_STATUS);
5109 if (current_link_up == 0 &&
5110 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5111 !(mac_status & MAC_STATUS_RCVD_CFG))
5112 current_link_up = 1;
5113 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005114 tg3_setup_flow_control(tp, 0, 0);
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* Forcing 1000FD link up. */
5117 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5120 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005121
5122 tw32_f(MAC_MODE, tp->mac_mode);
5123 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125
5126out:
5127 return current_link_up;
5128}
5129
5130static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5131{
5132 u32 orig_pause_cfg;
5133 u16 orig_active_speed;
5134 u8 orig_active_duplex;
5135 u32 mac_status;
5136 int current_link_up;
5137 int i;
5138
Matt Carlson8d018622007-12-20 20:05:44 -08005139 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 orig_active_speed = tp->link_config.active_speed;
5141 orig_active_duplex = tp->link_config.active_duplex;
5142
Joe Perches63c3a662011-04-26 08:12:10 +00005143 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005144 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005145 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 mac_status = tr32(MAC_STATUS);
5147 mac_status &= (MAC_STATUS_PCS_SYNCED |
5148 MAC_STATUS_SIGNAL_DET |
5149 MAC_STATUS_CFG_CHANGED |
5150 MAC_STATUS_RCVD_CFG);
5151 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5152 MAC_STATUS_SIGNAL_DET)) {
5153 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5154 MAC_STATUS_CFG_CHANGED));
5155 return 0;
5156 }
5157 }
5158
5159 tw32_f(MAC_TX_AUTO_NEG, 0);
5160
5161 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5162 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5163 tw32_f(MAC_MODE, tp->mac_mode);
5164 udelay(40);
5165
Matt Carlson79eb6902010-02-17 15:17:03 +00005166 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 tg3_init_bcm8002(tp);
5168
5169 /* Enable link change event even when serdes polling. */
5170 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5171 udelay(40);
5172
5173 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005174 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 mac_status = tr32(MAC_STATUS);
5176
Joe Perches63c3a662011-04-26 08:12:10 +00005177 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5179 else
5180 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5181
Matt Carlson898a56f2009-08-28 14:02:40 +00005182 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005184 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
5186 for (i = 0; i < 100; i++) {
5187 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5188 MAC_STATUS_CFG_CHANGED));
5189 udelay(5);
5190 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005191 MAC_STATUS_CFG_CHANGED |
5192 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 break;
5194 }
5195
5196 mac_status = tr32(MAC_STATUS);
5197 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5198 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005199 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5200 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 tw32_f(MAC_MODE, (tp->mac_mode |
5202 MAC_MODE_SEND_CONFIGS));
5203 udelay(1);
5204 tw32_f(MAC_MODE, tp->mac_mode);
5205 }
5206 }
5207
5208 if (current_link_up == 1) {
5209 tp->link_config.active_speed = SPEED_1000;
5210 tp->link_config.active_duplex = DUPLEX_FULL;
5211 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5212 LED_CTRL_LNKLED_OVERRIDE |
5213 LED_CTRL_1000MBPS_ON));
5214 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005215 tp->link_config.active_speed = SPEED_UNKNOWN;
5216 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5218 LED_CTRL_LNKLED_OVERRIDE |
5219 LED_CTRL_TRAFFIC_OVERRIDE));
5220 }
5221
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005222 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005223 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (orig_pause_cfg != now_pause_cfg ||
5225 orig_active_speed != tp->link_config.active_speed ||
5226 orig_active_duplex != tp->link_config.active_duplex)
5227 tg3_link_report(tp);
5228 }
5229
5230 return 0;
5231}
5232
Michael Chan747e8f82005-07-25 12:33:22 -07005233static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5234{
5235 int current_link_up, err = 0;
5236 u32 bmsr, bmcr;
5237 u16 current_speed;
5238 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005239 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005240
5241 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5242 tw32_f(MAC_MODE, tp->mac_mode);
5243 udelay(40);
5244
5245 tw32(MAC_EVENT, 0);
5246
5247 tw32_f(MAC_STATUS,
5248 (MAC_STATUS_SYNC_CHANGED |
5249 MAC_STATUS_CFG_CHANGED |
5250 MAC_STATUS_MI_COMPLETION |
5251 MAC_STATUS_LNKSTATE_CHANGED));
5252 udelay(40);
5253
5254 if (force_reset)
5255 tg3_phy_reset(tp);
5256
5257 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005258 current_speed = SPEED_UNKNOWN;
5259 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005260 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005261
5262 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5263 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5265 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5266 bmsr |= BMSR_LSTATUS;
5267 else
5268 bmsr &= ~BMSR_LSTATUS;
5269 }
Michael Chan747e8f82005-07-25 12:33:22 -07005270
5271 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5272
5273 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005274 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005275 /* do nothing, just check for link up at the end */
5276 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005277 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005278
5279 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005280 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5281 ADVERTISE_1000XPAUSE |
5282 ADVERTISE_1000XPSE_ASYM |
5283 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005284
Matt Carlson28011cf2011-11-16 18:36:59 -05005285 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005286 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005287
Matt Carlson28011cf2011-11-16 18:36:59 -05005288 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5289 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005290 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5291 tg3_writephy(tp, MII_BMCR, bmcr);
5292
5293 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005294 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005295 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005296
5297 return err;
5298 }
5299 } else {
5300 u32 new_bmcr;
5301
5302 bmcr &= ~BMCR_SPEED1000;
5303 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5304
5305 if (tp->link_config.duplex == DUPLEX_FULL)
5306 new_bmcr |= BMCR_FULLDPLX;
5307
5308 if (new_bmcr != bmcr) {
5309 /* BMCR_SPEED1000 is a reserved bit that needs
5310 * to be set on write.
5311 */
5312 new_bmcr |= BMCR_SPEED1000;
5313
5314 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005315 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005316 u32 adv;
5317
5318 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5319 adv &= ~(ADVERTISE_1000XFULL |
5320 ADVERTISE_1000XHALF |
5321 ADVERTISE_SLCT);
5322 tg3_writephy(tp, MII_ADVERTISE, adv);
5323 tg3_writephy(tp, MII_BMCR, bmcr |
5324 BMCR_ANRESTART |
5325 BMCR_ANENABLE);
5326 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005327 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005328 }
5329 tg3_writephy(tp, MII_BMCR, new_bmcr);
5330 bmcr = new_bmcr;
5331 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5332 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005333 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5334 ASIC_REV_5714) {
5335 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5336 bmsr |= BMSR_LSTATUS;
5337 else
5338 bmsr &= ~BMSR_LSTATUS;
5339 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005340 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005341 }
5342 }
5343
5344 if (bmsr & BMSR_LSTATUS) {
5345 current_speed = SPEED_1000;
5346 current_link_up = 1;
5347 if (bmcr & BMCR_FULLDPLX)
5348 current_duplex = DUPLEX_FULL;
5349 else
5350 current_duplex = DUPLEX_HALF;
5351
Matt Carlsonef167e22007-12-20 20:10:01 -08005352 local_adv = 0;
5353 remote_adv = 0;
5354
Michael Chan747e8f82005-07-25 12:33:22 -07005355 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005356 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005357
5358 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5359 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5360 common = local_adv & remote_adv;
5361 if (common & (ADVERTISE_1000XHALF |
5362 ADVERTISE_1000XFULL)) {
5363 if (common & ADVERTISE_1000XFULL)
5364 current_duplex = DUPLEX_FULL;
5365 else
5366 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005367
5368 tp->link_config.rmt_adv =
5369 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005370 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005371 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005372 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005373 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005374 }
Michael Chan747e8f82005-07-25 12:33:22 -07005375 }
5376 }
5377
Matt Carlsonef167e22007-12-20 20:10:01 -08005378 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5379 tg3_setup_flow_control(tp, local_adv, remote_adv);
5380
Michael Chan747e8f82005-07-25 12:33:22 -07005381 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5382 if (tp->link_config.active_duplex == DUPLEX_HALF)
5383 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5384
5385 tw32_f(MAC_MODE, tp->mac_mode);
5386 udelay(40);
5387
5388 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5389
5390 tp->link_config.active_speed = current_speed;
5391 tp->link_config.active_duplex = current_duplex;
5392
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005393 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005394 return err;
5395}
5396
5397static void tg3_serdes_parallel_detect(struct tg3 *tp)
5398{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005399 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005400 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005401 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005402 return;
5403 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005404
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005405 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005406 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5407 u32 bmcr;
5408
5409 tg3_readphy(tp, MII_BMCR, &bmcr);
5410 if (bmcr & BMCR_ANENABLE) {
5411 u32 phy1, phy2;
5412
5413 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005414 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5415 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005416
5417 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005418 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5419 MII_TG3_DSP_EXP1_INT_STAT);
5420 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5421 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005422
5423 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5424 /* We have signal detect and not receiving
5425 * config code words, link is up by parallel
5426 * detection.
5427 */
5428
5429 bmcr &= ~BMCR_ANENABLE;
5430 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5431 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005432 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005433 }
5434 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005435 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005436 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005437 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005438 u32 phy2;
5439
5440 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005441 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5442 MII_TG3_DSP_EXP1_INT_STAT);
5443 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005444 if (phy2 & 0x20) {
5445 u32 bmcr;
5446
5447 /* Config code words received, turn on autoneg. */
5448 tg3_readphy(tp, MII_BMCR, &bmcr);
5449 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5450
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005451 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005452
5453 }
5454 }
5455}
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5458{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005459 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 int err;
5461
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005462 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005464 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005465 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005466 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005469 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005470 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005471
5472 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5473 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5474 scale = 65;
5475 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5476 scale = 6;
5477 else
5478 scale = 12;
5479
5480 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5481 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5482 tw32(GRC_MISC_CFG, val);
5483 }
5484
Matt Carlsonf2096f92011-04-05 14:22:48 +00005485 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5486 (6 << TX_LENGTHS_IPG_SHIFT);
5487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5488 val |= tr32(MAC_TX_LENGTHS) &
5489 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5490 TX_LENGTHS_CNT_DWN_VAL_MSK);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (tp->link_config.active_speed == SPEED_1000 &&
5493 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005494 tw32(MAC_TX_LENGTHS, val |
5495 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005497 tw32(MAC_TX_LENGTHS, val |
5498 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Joe Perches63c3a662011-04-26 08:12:10 +00005500 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005501 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005503 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 } else {
5505 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5506 }
5507 }
5508
Joe Perches63c3a662011-04-26 08:12:10 +00005509 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005510 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005511 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005512 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5513 tp->pwrmgmt_thresh;
5514 else
5515 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5516 tw32(PCIE_PWR_MGMT_THRESH, val);
5517 }
5518
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return err;
5520}
5521
Matt Carlsonbe947302012-12-03 19:36:57 +00005522/* tp->lock must be held */
Matt Carlson7d41e492012-12-03 19:36:58 +00005523static u64 tg3_refclk_read(struct tg3 *tp)
5524{
5525 u64 stamp = tr32(TG3_EAV_REF_CLCK_LSB);
5526 return stamp | (u64)tr32(TG3_EAV_REF_CLCK_MSB) << 32;
5527}
5528
5529/* tp->lock must be held */
Matt Carlsonbe947302012-12-03 19:36:57 +00005530static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5531{
5532 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5533 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5534 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5535 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5536}
5537
Matt Carlson7d41e492012-12-03 19:36:58 +00005538static inline void tg3_full_lock(struct tg3 *tp, int irq_sync);
5539static inline void tg3_full_unlock(struct tg3 *tp);
5540static int tg3_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
5541{
5542 struct tg3 *tp = netdev_priv(dev);
5543
5544 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
5545 SOF_TIMESTAMPING_RX_SOFTWARE |
5546 SOF_TIMESTAMPING_SOFTWARE |
5547 SOF_TIMESTAMPING_TX_HARDWARE |
5548 SOF_TIMESTAMPING_RX_HARDWARE |
5549 SOF_TIMESTAMPING_RAW_HARDWARE;
5550
5551 if (tp->ptp_clock)
5552 info->phc_index = ptp_clock_index(tp->ptp_clock);
5553 else
5554 info->phc_index = -1;
5555
5556 info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
5557
5558 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
5559 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
5560 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
5561 (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
5562 return 0;
5563}
5564
5565static int tg3_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
5566{
5567 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5568 bool neg_adj = false;
5569 u32 correction = 0;
5570
5571 if (ppb < 0) {
5572 neg_adj = true;
5573 ppb = -ppb;
5574 }
5575
5576 /* Frequency adjustment is performed using hardware with a 24 bit
5577 * accumulator and a programmable correction value. On each clk, the
5578 * correction value gets added to the accumulator and when it
5579 * overflows, the time counter is incremented/decremented.
5580 *
5581 * So conversion from ppb to correction value is
5582 * ppb * (1 << 24) / 1000000000
5583 */
5584 correction = div_u64((u64)ppb * (1 << 24), 1000000000ULL) &
5585 TG3_EAV_REF_CLK_CORRECT_MASK;
5586
5587 tg3_full_lock(tp, 0);
5588
5589 if (correction)
5590 tw32(TG3_EAV_REF_CLK_CORRECT_CTL,
5591 TG3_EAV_REF_CLK_CORRECT_EN |
5592 (neg_adj ? TG3_EAV_REF_CLK_CORRECT_NEG : 0) | correction);
5593 else
5594 tw32(TG3_EAV_REF_CLK_CORRECT_CTL, 0);
5595
5596 tg3_full_unlock(tp);
5597
5598 return 0;
5599}
5600
5601static int tg3_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
5602{
5603 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5604
5605 tg3_full_lock(tp, 0);
5606 tp->ptp_adjust += delta;
5607 tg3_full_unlock(tp);
5608
5609 return 0;
5610}
5611
5612static int tg3_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
5613{
5614 u64 ns;
5615 u32 remainder;
5616 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5617
5618 tg3_full_lock(tp, 0);
5619 ns = tg3_refclk_read(tp);
5620 ns += tp->ptp_adjust;
5621 tg3_full_unlock(tp);
5622
5623 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
5624 ts->tv_nsec = remainder;
5625
5626 return 0;
5627}
5628
5629static int tg3_ptp_settime(struct ptp_clock_info *ptp,
5630 const struct timespec *ts)
5631{
5632 u64 ns;
5633 struct tg3 *tp = container_of(ptp, struct tg3, ptp_info);
5634
5635 ns = timespec_to_ns(ts);
5636
5637 tg3_full_lock(tp, 0);
5638 tg3_refclk_write(tp, ns);
5639 tp->ptp_adjust = 0;
5640 tg3_full_unlock(tp);
5641
5642 return 0;
5643}
5644
5645static int tg3_ptp_enable(struct ptp_clock_info *ptp,
5646 struct ptp_clock_request *rq, int on)
5647{
5648 return -EOPNOTSUPP;
5649}
5650
5651static const struct ptp_clock_info tg3_ptp_caps = {
5652 .owner = THIS_MODULE,
5653 .name = "tg3 clock",
5654 .max_adj = 250000000,
5655 .n_alarm = 0,
5656 .n_ext_ts = 0,
5657 .n_per_out = 0,
5658 .pps = 0,
5659 .adjfreq = tg3_ptp_adjfreq,
5660 .adjtime = tg3_ptp_adjtime,
5661 .gettime = tg3_ptp_gettime,
5662 .settime = tg3_ptp_settime,
5663 .enable = tg3_ptp_enable,
5664};
5665
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005666static void tg3_hwclock_to_timestamp(struct tg3 *tp, u64 hwclock,
5667 struct skb_shared_hwtstamps *timestamp)
5668{
5669 memset(timestamp, 0, sizeof(struct skb_shared_hwtstamps));
5670 timestamp->hwtstamp = ns_to_ktime((hwclock & TG3_TSTAMP_MASK) +
5671 tp->ptp_adjust);
5672}
5673
Matt Carlsonbe947302012-12-03 19:36:57 +00005674/* tp->lock must be held */
5675static void tg3_ptp_init(struct tg3 *tp)
5676{
5677 if (!tg3_flag(tp, PTP_CAPABLE))
5678 return;
5679
5680 /* Initialize the hardware clock to the system time. */
5681 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5682 tp->ptp_adjust = 0;
Matt Carlson7d41e492012-12-03 19:36:58 +00005683 tp->ptp_info = tg3_ptp_caps;
Matt Carlsonbe947302012-12-03 19:36:57 +00005684}
5685
5686/* tp->lock must be held */
5687static void tg3_ptp_resume(struct tg3 *tp)
5688{
5689 if (!tg3_flag(tp, PTP_CAPABLE))
5690 return;
5691
5692 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5693 tp->ptp_adjust = 0;
5694}
5695
5696static void tg3_ptp_fini(struct tg3 *tp)
5697{
5698 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5699 return;
5700
Matt Carlson7d41e492012-12-03 19:36:58 +00005701 ptp_clock_unregister(tp->ptp_clock);
Matt Carlsonbe947302012-12-03 19:36:57 +00005702 tp->ptp_clock = NULL;
5703 tp->ptp_adjust = 0;
5704}
5705
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005706static inline int tg3_irq_sync(struct tg3 *tp)
5707{
5708 return tp->irq_sync;
5709}
5710
Matt Carlson97bd8e42011-04-13 11:05:04 +00005711static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5712{
5713 int i;
5714
5715 dst = (u32 *)((u8 *)dst + off);
5716 for (i = 0; i < len; i += sizeof(u32))
5717 *dst++ = tr32(off + i);
5718}
5719
5720static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5721{
5722 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5723 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5724 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5725 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5726 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5727 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5728 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5729 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5730 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5731 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5732 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5733 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5734 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5735 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5736 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5737 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5738 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5739 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5740 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5741
Joe Perches63c3a662011-04-26 08:12:10 +00005742 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005743 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5744
5745 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5746 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5747 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5748 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5749 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5750 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5751 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5752 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5753
Joe Perches63c3a662011-04-26 08:12:10 +00005754 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005755 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5756 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5757 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5758 }
5759
5760 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5761 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5762 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5763 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5764 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5765
Joe Perches63c3a662011-04-26 08:12:10 +00005766 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005767 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5768}
5769
5770static void tg3_dump_state(struct tg3 *tp)
5771{
5772 int i;
5773 u32 *regs;
5774
5775 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5776 if (!regs) {
5777 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5778 return;
5779 }
5780
Joe Perches63c3a662011-04-26 08:12:10 +00005781 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005782 /* Read up to but not including private PCI registers */
5783 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5784 regs[i / sizeof(u32)] = tr32(i);
5785 } else
5786 tg3_dump_legacy_regs(tp, regs);
5787
5788 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5789 if (!regs[i + 0] && !regs[i + 1] &&
5790 !regs[i + 2] && !regs[i + 3])
5791 continue;
5792
5793 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5794 i * 4,
5795 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5796 }
5797
5798 kfree(regs);
5799
5800 for (i = 0; i < tp->irq_cnt; i++) {
5801 struct tg3_napi *tnapi = &tp->napi[i];
5802
5803 /* SW status block */
5804 netdev_err(tp->dev,
5805 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5806 i,
5807 tnapi->hw_status->status,
5808 tnapi->hw_status->status_tag,
5809 tnapi->hw_status->rx_jumbo_consumer,
5810 tnapi->hw_status->rx_consumer,
5811 tnapi->hw_status->rx_mini_consumer,
5812 tnapi->hw_status->idx[0].rx_producer,
5813 tnapi->hw_status->idx[0].tx_consumer);
5814
5815 netdev_err(tp->dev,
5816 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5817 i,
5818 tnapi->last_tag, tnapi->last_irq_tag,
5819 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5820 tnapi->rx_rcb_ptr,
5821 tnapi->prodring.rx_std_prod_idx,
5822 tnapi->prodring.rx_std_cons_idx,
5823 tnapi->prodring.rx_jmb_prod_idx,
5824 tnapi->prodring.rx_jmb_cons_idx);
5825 }
5826}
5827
Michael Chandf3e6542006-05-26 17:48:07 -07005828/* This is called whenever we suspect that the system chipset is re-
5829 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5830 * is bogus tx completions. We try to recover by setting the
5831 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5832 * in the workqueue.
5833 */
5834static void tg3_tx_recover(struct tg3 *tp)
5835{
Joe Perches63c3a662011-04-26 08:12:10 +00005836 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005837 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5838
Matt Carlson5129c3a2010-04-05 10:19:23 +00005839 netdev_warn(tp->dev,
5840 "The system may be re-ordering memory-mapped I/O "
5841 "cycles to the network device, attempting to recover. "
5842 "Please report the problem to the driver maintainer "
5843 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005844
5845 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005846 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005847 spin_unlock(&tp->lock);
5848}
5849
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005850static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005851{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005852 /* Tell compiler to fetch tx indices from memory. */
5853 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005854 return tnapi->tx_pending -
5855 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005856}
5857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858/* Tigon3 never reports partial packet sends. So we do not
5859 * need special logic to handle SKBs that have not had all
5860 * of their frags sent yet, like SunGEM does.
5861 */
Matt Carlson17375d22009-08-28 14:02:18 +00005862static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
Matt Carlson17375d22009-08-28 14:02:18 +00005864 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005865 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005866 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005867 struct netdev_queue *txq;
5868 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005869 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005870
Joe Perches63c3a662011-04-26 08:12:10 +00005871 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005872 index--;
5873
5874 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
5876 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005877 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005879 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Michael Chandf3e6542006-05-26 17:48:07 -07005881 if (unlikely(skb == NULL)) {
5882 tg3_tx_recover(tp);
5883 return;
5884 }
5885
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00005886 if (tnapi->tx_ring[sw_idx].len_flags & TXD_FLAG_HWTSTAMP) {
5887 struct skb_shared_hwtstamps timestamp;
5888 u64 hwclock = tr32(TG3_TX_TSTAMP_LSB);
5889 hwclock |= (u64)tr32(TG3_TX_TSTAMP_MSB) << 32;
5890
5891 tg3_hwclock_to_timestamp(tp, hwclock, &timestamp);
5892
5893 skb_tstamp_tx(skb, &timestamp);
5894 }
5895
Alexander Duyckf4188d82009-12-02 16:48:38 +00005896 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005897 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005898 skb_headlen(skb),
5899 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
5901 ri->skb = NULL;
5902
Matt Carlsone01ee142011-07-27 14:20:50 +00005903 while (ri->fragmented) {
5904 ri->fragmented = false;
5905 sw_idx = NEXT_TX(sw_idx);
5906 ri = &tnapi->tx_buffers[sw_idx];
5907 }
5908
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 sw_idx = NEXT_TX(sw_idx);
5910
5911 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005912 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005913 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5914 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005915
5916 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005917 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005918 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005919 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005920
5921 while (ri->fragmented) {
5922 ri->fragmented = false;
5923 sw_idx = NEXT_TX(sw_idx);
5924 ri = &tnapi->tx_buffers[sw_idx];
5925 }
5926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 sw_idx = NEXT_TX(sw_idx);
5928 }
5929
Tom Herbert298376d2011-11-28 16:33:30 +00005930 pkts_compl++;
5931 bytes_compl += skb->len;
5932
David S. Millerf47c11e2005-06-24 20:18:35 -07005933 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005934
5935 if (unlikely(tx_bug)) {
5936 tg3_tx_recover(tp);
5937 return;
5938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 }
5940
Tom Herbert5cb917b2012-03-05 19:53:50 +00005941 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005942
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005943 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
Michael Chan1b2a7202006-08-07 21:46:02 -07005945 /* Need to make the tx_cons update visible to tg3_start_xmit()
5946 * before checking for netif_queue_stopped(). Without the
5947 * memory barrier, there is a small possibility that tg3_start_xmit()
5948 * will miss it and cause the queue to be stopped forever.
5949 */
5950 smp_mb();
5951
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005952 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005953 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005954 __netif_tx_lock(txq, smp_processor_id());
5955 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005956 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005957 netif_tx_wake_queue(txq);
5958 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960}
5961
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005962static void tg3_frag_free(bool is_frag, void *data)
5963{
5964 if (is_frag)
5965 put_page(virt_to_head_page(data));
5966 else
5967 kfree(data);
5968}
5969
Eric Dumazet9205fd92011-11-18 06:47:01 +00005970static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005971{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005972 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5973 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5974
Eric Dumazet9205fd92011-11-18 06:47:01 +00005975 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005976 return;
5977
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005978 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005979 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005980 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005981 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005982}
5983
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005984
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985/* Returns size of skb allocated or < 0 on error.
5986 *
5987 * We only need to fill in the address because the other members
5988 * of the RX descriptor are invariant, see tg3_init_rings.
5989 *
5990 * Note the purposeful assymetry of cpu vs. chip accesses. For
5991 * posting buffers we only dirty the first cache line of the RX
5992 * descriptor (containing the address). Whereas for the RX status
5993 * buffers the cpu only reads the last cacheline of the RX descriptor
5994 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5995 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005996static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005997 u32 opaque_key, u32 dest_idx_unmasked,
5998 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
6000 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00006001 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006002 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006004 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 switch (opaque_key) {
6007 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006008 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00006009 desc = &tpr->rx_std[dest_idx];
6010 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006011 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 break;
6013
6014 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006015 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00006016 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00006017 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00006018 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 break;
6020
6021 default:
6022 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 /* Do not overwrite any of the map or rp information
6026 * until we are sure we can commit to a new buffer.
6027 *
6028 * Callers depend upon this behavior and assume that
6029 * we leave everything unchanged if we fail.
6030 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00006031 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
6032 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006033 if (skb_size <= PAGE_SIZE) {
6034 data = netdev_alloc_frag(skb_size);
6035 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006036 } else {
6037 data = kmalloc(skb_size, GFP_ATOMIC);
6038 *frag_size = 0;
6039 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00006040 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 return -ENOMEM;
6042
Eric Dumazet9205fd92011-11-18 06:47:01 +00006043 mapping = pci_map_single(tp->pdev,
6044 data + TG3_RX_OFFSET(tp),
6045 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006047 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00006048 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00006049 return -EIO;
6050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Eric Dumazet9205fd92011-11-18 06:47:01 +00006052 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006053 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 desc->addr_hi = ((u64)mapping >> 32);
6056 desc->addr_lo = ((u64)mapping & 0xffffffff);
6057
Eric Dumazet9205fd92011-11-18 06:47:01 +00006058 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059}
6060
6061/* We only need to move over in the address because the other
6062 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00006063 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 */
Matt Carlsona3896162009-11-13 13:03:44 +00006065static void tg3_recycle_rx(struct tg3_napi *tnapi,
6066 struct tg3_rx_prodring_set *dpr,
6067 u32 opaque_key, int src_idx,
6068 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
Matt Carlson17375d22009-08-28 14:02:18 +00006070 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
6072 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006073 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006074 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076 switch (opaque_key) {
6077 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00006078 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006079 dest_desc = &dpr->rx_std[dest_idx];
6080 dest_map = &dpr->rx_std_buffers[dest_idx];
6081 src_desc = &spr->rx_std[src_idx];
6082 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 break;
6084
6085 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00006086 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00006087 dest_desc = &dpr->rx_jmb[dest_idx].std;
6088 dest_map = &dpr->rx_jmb_buffers[dest_idx];
6089 src_desc = &spr->rx_jmb[src_idx].std;
6090 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 break;
6092
6093 default:
6094 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
Eric Dumazet9205fd92011-11-18 06:47:01 +00006097 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006098 dma_unmap_addr_set(dest_map, mapping,
6099 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 dest_desc->addr_hi = src_desc->addr_hi;
6101 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00006102
6103 /* Ensure that the update to the skb happens after the physical
6104 * addresses have been transferred to the new BD location.
6105 */
6106 smp_wmb();
6107
Eric Dumazet9205fd92011-11-18 06:47:01 +00006108 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109}
6110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111/* The RX ring scheme is composed of multiple rings which post fresh
6112 * buffers to the chip, and one special ring the chip uses to report
6113 * status back to the host.
6114 *
6115 * The special ring reports the status of received packets to the
6116 * host. The chip does not write into the original descriptor the
6117 * RX buffer was obtained from. The chip simply takes the original
6118 * descriptor as provided by the host, updates the status and length
6119 * field, then writes this into the next status ring entry.
6120 *
6121 * Each ring the host uses to post buffers to the chip is described
6122 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
6123 * it is first placed into the on-chip ram. When the packet's length
6124 * is known, it walks down the TG3_BDINFO entries to select the ring.
6125 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
6126 * which is within the range of the new packet's length is chosen.
6127 *
6128 * The "separate ring for rx status" scheme may sound queer, but it makes
6129 * sense from a cache coherency perspective. If only the host writes
6130 * to the buffer post rings, and only the chip writes to the rx status
6131 * rings, then cache lines never move beyond shared-modified state.
6132 * If both the host and chip were to write into the same ring, cache line
6133 * eviction could occur since both entities want it in an exclusive state.
6134 */
Matt Carlson17375d22009-08-28 14:02:18 +00006135static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136{
Matt Carlson17375d22009-08-28 14:02:18 +00006137 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07006138 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006139 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00006140 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07006141 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00006143 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006145 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 /*
6147 * We need to order the read of hw_idx and the read of
6148 * the opaque cookie.
6149 */
6150 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 work_mask = 0;
6152 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00006153 std_prod_idx = tpr->rx_std_prod_idx;
6154 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006156 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006157 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 unsigned int len;
6159 struct sk_buff *skb;
6160 dma_addr_t dma_addr;
6161 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006162 u8 *data;
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006163 u64 tstamp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
6165 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6166 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6167 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006168 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006169 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006170 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006171 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006172 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006174 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006175 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006176 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006177 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006178 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
6181 work_mask |= opaque_key;
6182
6183 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6184 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6185 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006186 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 desc_idx, *post_ptr);
6188 drop_it_no_recycle:
6189 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006190 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 goto next_pkt;
6192 }
6193
Eric Dumazet9205fd92011-11-18 06:47:01 +00006194 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006195 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6196 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006198 if ((desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6199 RXD_FLAG_PTPSTAT_PTPV1 ||
6200 (desc->type_flags & RXD_FLAG_PTPSTAT_MASK) ==
6201 RXD_FLAG_PTPSTAT_PTPV2) {
6202 tstamp = tr32(TG3_RX_TSTAMP_LSB);
6203 tstamp |= (u64)tr32(TG3_RX_TSTAMP_MSB) << 32;
6204 }
6205
Matt Carlsond2757fc2010-04-12 06:58:27 +00006206 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006208 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Eric Dumazet9205fd92011-11-18 06:47:01 +00006210 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006211 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 if (skb_size < 0)
6213 goto drop_it;
6214
Matt Carlson287be122009-08-28 13:58:46 +00006215 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 PCI_DMA_FROMDEVICE);
6217
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006218 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006219 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006220 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006221 goto drop_it_no_recycle;
6222 }
6223 skb_reserve(skb, TG3_RX_OFFSET(tp));
6224 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006225 * after the usage of the old DMA mapping.
6226 */
6227 smp_wmb();
6228
Eric Dumazet9205fd92011-11-18 06:47:01 +00006229 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006232 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 desc_idx, *post_ptr);
6234
Eric Dumazet9205fd92011-11-18 06:47:01 +00006235 skb = netdev_alloc_skb(tp->dev,
6236 len + TG3_RAW_IP_ALIGN);
6237 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 goto drop_it_no_recycle;
6239
Eric Dumazet9205fd92011-11-18 06:47:01 +00006240 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006242 memcpy(skb->data,
6243 data + TG3_RX_OFFSET(tp),
6244 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 }
6247
Eric Dumazet9205fd92011-11-18 06:47:01 +00006248 skb_put(skb, len);
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00006249 if (tstamp)
6250 tg3_hwclock_to_timestamp(tp, tstamp,
6251 skb_hwtstamps(skb));
6252
Michał Mirosławdc668912011-04-07 03:35:07 +00006253 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6255 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6256 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6257 skb->ip_summed = CHECKSUM_UNNECESSARY;
6258 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006259 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006262
6263 if (len > (tp->dev->mtu + ETH_HLEN) &&
6264 skb->protocol != htons(ETH_P_8021Q)) {
6265 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006266 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006267 }
6268
Matt Carlson9dc7a112010-04-12 06:58:28 +00006269 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006270 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6271 __vlan_hwaccel_put_tag(skb,
6272 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006273
Matt Carlsonbf933c82011-01-25 15:58:49 +00006274 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 received++;
6277 budget--;
6278
6279next_pkt:
6280 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006281
6282 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006283 tpr->rx_std_prod_idx = std_prod_idx &
6284 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006285 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6286 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006287 work_mask &= ~RXD_OPAQUE_RING_STD;
6288 rx_std_posted = 0;
6289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006291 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006292 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006293
6294 /* Refresh hw_idx to see if there is new work */
6295 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006296 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006297 rmb();
6298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 }
6300
6301 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006302 tnapi->rx_rcb_ptr = sw_idx;
6303 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304
6305 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006306 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006307 /* Sync BD data before updating mailbox */
6308 wmb();
6309
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006310 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006311 tpr->rx_std_prod_idx = std_prod_idx &
6312 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006313 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6314 tpr->rx_std_prod_idx);
6315 }
6316 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006317 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6318 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006319 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6320 tpr->rx_jmb_prod_idx);
6321 }
6322 mmiowb();
6323 } else if (work_mask) {
6324 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6325 * updated before the producer indices can be updated.
6326 */
6327 smp_wmb();
6328
Matt Carlson2c49a442010-09-30 10:34:35 +00006329 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6330 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006331
Michael Chan7ae52892012-03-21 15:38:33 +00006332 if (tnapi != &tp->napi[1]) {
6333 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006334 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
6338 return received;
6339}
6340
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006341static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006344 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006345 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6346
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 if (sblk->status & SD_STATUS_LINK_CHG) {
6348 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006349 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006350 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006351 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006352 tw32_f(MAC_STATUS,
6353 (MAC_STATUS_SYNC_CHANGED |
6354 MAC_STATUS_CFG_CHANGED |
6355 MAC_STATUS_MI_COMPLETION |
6356 MAC_STATUS_LNKSTATE_CHANGED));
6357 udelay(40);
6358 } else
6359 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006360 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 }
6362 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006363}
6364
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006365static int tg3_rx_prodring_xfer(struct tg3 *tp,
6366 struct tg3_rx_prodring_set *dpr,
6367 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006368{
6369 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006370 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006371
6372 while (1) {
6373 src_prod_idx = spr->rx_std_prod_idx;
6374
6375 /* Make sure updates to the rx_std_buffers[] entries and the
6376 * standard producer index are seen in the correct order.
6377 */
6378 smp_rmb();
6379
6380 if (spr->rx_std_cons_idx == src_prod_idx)
6381 break;
6382
6383 if (spr->rx_std_cons_idx < src_prod_idx)
6384 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6385 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006386 cpycnt = tp->rx_std_ring_mask + 1 -
6387 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006388
Matt Carlson2c49a442010-09-30 10:34:35 +00006389 cpycnt = min(cpycnt,
6390 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006391
6392 si = spr->rx_std_cons_idx;
6393 di = dpr->rx_std_prod_idx;
6394
Matt Carlsone92967b2010-02-12 14:47:06 +00006395 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006396 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006397 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006398 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006399 break;
6400 }
6401 }
6402
6403 if (!cpycnt)
6404 break;
6405
6406 /* Ensure that updates to the rx_std_buffers ring and the
6407 * shadowed hardware producer ring from tg3_recycle_skb() are
6408 * ordered correctly WRT the skb check above.
6409 */
6410 smp_rmb();
6411
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006412 memcpy(&dpr->rx_std_buffers[di],
6413 &spr->rx_std_buffers[si],
6414 cpycnt * sizeof(struct ring_info));
6415
6416 for (i = 0; i < cpycnt; i++, di++, si++) {
6417 struct tg3_rx_buffer_desc *sbd, *dbd;
6418 sbd = &spr->rx_std[si];
6419 dbd = &dpr->rx_std[di];
6420 dbd->addr_hi = sbd->addr_hi;
6421 dbd->addr_lo = sbd->addr_lo;
6422 }
6423
Matt Carlson2c49a442010-09-30 10:34:35 +00006424 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6425 tp->rx_std_ring_mask;
6426 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6427 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006428 }
6429
6430 while (1) {
6431 src_prod_idx = spr->rx_jmb_prod_idx;
6432
6433 /* Make sure updates to the rx_jmb_buffers[] entries and
6434 * the jumbo producer index are seen in the correct order.
6435 */
6436 smp_rmb();
6437
6438 if (spr->rx_jmb_cons_idx == src_prod_idx)
6439 break;
6440
6441 if (spr->rx_jmb_cons_idx < src_prod_idx)
6442 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6443 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006444 cpycnt = tp->rx_jmb_ring_mask + 1 -
6445 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006446
6447 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006448 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006449
6450 si = spr->rx_jmb_cons_idx;
6451 di = dpr->rx_jmb_prod_idx;
6452
Matt Carlsone92967b2010-02-12 14:47:06 +00006453 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006454 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006455 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006456 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006457 break;
6458 }
6459 }
6460
6461 if (!cpycnt)
6462 break;
6463
6464 /* Ensure that updates to the rx_jmb_buffers ring and the
6465 * shadowed hardware producer ring from tg3_recycle_skb() are
6466 * ordered correctly WRT the skb check above.
6467 */
6468 smp_rmb();
6469
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006470 memcpy(&dpr->rx_jmb_buffers[di],
6471 &spr->rx_jmb_buffers[si],
6472 cpycnt * sizeof(struct ring_info));
6473
6474 for (i = 0; i < cpycnt; i++, di++, si++) {
6475 struct tg3_rx_buffer_desc *sbd, *dbd;
6476 sbd = &spr->rx_jmb[si].std;
6477 dbd = &dpr->rx_jmb[di].std;
6478 dbd->addr_hi = sbd->addr_hi;
6479 dbd->addr_lo = sbd->addr_lo;
6480 }
6481
Matt Carlson2c49a442010-09-30 10:34:35 +00006482 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6483 tp->rx_jmb_ring_mask;
6484 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6485 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006486 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006487
6488 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006489}
6490
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006491static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6492{
6493 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
6495 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006496 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006497 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006498 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006499 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 }
6501
Matt Carlsonf891ea12012-04-24 13:37:01 +00006502 if (!tnapi->rx_rcb_prod_idx)
6503 return work_done;
6504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 /* run RX thread, within the bounds set by NAPI.
6506 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006507 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006509 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006510 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
Joe Perches63c3a662011-04-26 08:12:10 +00006512 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006513 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006514 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006515 u32 std_prod_idx = dpr->rx_std_prod_idx;
6516 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006517
Michael Chan7ae52892012-03-21 15:38:33 +00006518 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006519 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006520 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006521 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006522
6523 wmb();
6524
Matt Carlsone4af1af2010-02-12 14:47:05 +00006525 if (std_prod_idx != dpr->rx_std_prod_idx)
6526 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6527 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006528
Matt Carlsone4af1af2010-02-12 14:47:05 +00006529 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6530 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6531 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006532
6533 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006534
6535 if (err)
6536 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006537 }
6538
David S. Miller6f535762007-10-11 18:08:29 -07006539 return work_done;
6540}
David S. Millerf7383c222005-05-18 22:50:53 -07006541
Matt Carlsondb219972011-11-04 09:15:03 +00006542static inline void tg3_reset_task_schedule(struct tg3 *tp)
6543{
6544 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6545 schedule_work(&tp->reset_task);
6546}
6547
6548static inline void tg3_reset_task_cancel(struct tg3 *tp)
6549{
6550 cancel_work_sync(&tp->reset_task);
6551 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006552 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006553}
6554
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006555static int tg3_poll_msix(struct napi_struct *napi, int budget)
6556{
6557 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6558 struct tg3 *tp = tnapi->tp;
6559 int work_done = 0;
6560 struct tg3_hw_status *sblk = tnapi->hw_status;
6561
6562 while (1) {
6563 work_done = tg3_poll_work(tnapi, work_done, budget);
6564
Joe Perches63c3a662011-04-26 08:12:10 +00006565 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006566 goto tx_recovery;
6567
6568 if (unlikely(work_done >= budget))
6569 break;
6570
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006571 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006572 * to tell the hw how much work has been processed,
6573 * so we must read it before checking for more work.
6574 */
6575 tnapi->last_tag = sblk->status_tag;
6576 tnapi->last_irq_tag = tnapi->last_tag;
6577 rmb();
6578
6579 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006580 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6581 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006582
6583 /* This test here is not race free, but will reduce
6584 * the number of interrupts by looping again.
6585 */
6586 if (tnapi == &tp->napi[1] && tp->rx_refill)
6587 continue;
6588
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006589 napi_complete(napi);
6590 /* Reenable interrupts. */
6591 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006592
6593 /* This test here is synchronized by napi_schedule()
6594 * and napi_complete() to close the race condition.
6595 */
6596 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6597 tw32(HOSTCC_MODE, tp->coalesce_mode |
6598 HOSTCC_MODE_ENABLE |
6599 tnapi->coal_now);
6600 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006601 mmiowb();
6602 break;
6603 }
6604 }
6605
6606 return work_done;
6607
6608tx_recovery:
6609 /* work_done is guaranteed to be less than budget. */
6610 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006611 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006612 return work_done;
6613}
6614
Matt Carlsone64de4e2011-04-13 11:05:05 +00006615static void tg3_process_error(struct tg3 *tp)
6616{
6617 u32 val;
6618 bool real_error = false;
6619
Joe Perches63c3a662011-04-26 08:12:10 +00006620 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006621 return;
6622
6623 /* Check Flow Attention register */
6624 val = tr32(HOSTCC_FLOW_ATTN);
6625 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6626 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6627 real_error = true;
6628 }
6629
6630 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6631 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6632 real_error = true;
6633 }
6634
6635 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6636 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6637 real_error = true;
6638 }
6639
6640 if (!real_error)
6641 return;
6642
6643 tg3_dump_state(tp);
6644
Joe Perches63c3a662011-04-26 08:12:10 +00006645 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006646 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006647}
6648
David S. Miller6f535762007-10-11 18:08:29 -07006649static int tg3_poll(struct napi_struct *napi, int budget)
6650{
Matt Carlson8ef04422009-08-28 14:01:37 +00006651 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6652 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006653 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006654 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006655
6656 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006657 if (sblk->status & SD_STATUS_ERROR)
6658 tg3_process_error(tp);
6659
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006660 tg3_poll_link(tp);
6661
Matt Carlson17375d22009-08-28 14:02:18 +00006662 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006663
Joe Perches63c3a662011-04-26 08:12:10 +00006664 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006665 goto tx_recovery;
6666
6667 if (unlikely(work_done >= budget))
6668 break;
6669
Joe Perches63c3a662011-04-26 08:12:10 +00006670 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006671 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006672 * to tell the hw how much work has been processed,
6673 * so we must read it before checking for more work.
6674 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006675 tnapi->last_tag = sblk->status_tag;
6676 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006677 rmb();
6678 } else
6679 sblk->status &= ~SD_STATUS_UPDATED;
6680
Matt Carlson17375d22009-08-28 14:02:18 +00006681 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006682 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006683 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006684 break;
6685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 }
6687
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006688 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006689
6690tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006691 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006692 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006693 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006694 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695}
6696
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006697static void tg3_napi_disable(struct tg3 *tp)
6698{
6699 int i;
6700
6701 for (i = tp->irq_cnt - 1; i >= 0; i--)
6702 napi_disable(&tp->napi[i].napi);
6703}
6704
6705static void tg3_napi_enable(struct tg3 *tp)
6706{
6707 int i;
6708
6709 for (i = 0; i < tp->irq_cnt; i++)
6710 napi_enable(&tp->napi[i].napi);
6711}
6712
6713static void tg3_napi_init(struct tg3 *tp)
6714{
6715 int i;
6716
6717 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6718 for (i = 1; i < tp->irq_cnt; i++)
6719 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6720}
6721
6722static void tg3_napi_fini(struct tg3 *tp)
6723{
6724 int i;
6725
6726 for (i = 0; i < tp->irq_cnt; i++)
6727 netif_napi_del(&tp->napi[i].napi);
6728}
6729
6730static inline void tg3_netif_stop(struct tg3 *tp)
6731{
6732 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6733 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006734 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006735 netif_tx_disable(tp->dev);
6736}
6737
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006738/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006739static inline void tg3_netif_start(struct tg3 *tp)
6740{
Matt Carlsonbe947302012-12-03 19:36:57 +00006741 tg3_ptp_resume(tp);
6742
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006743 /* NOTE: unconditional netif_tx_wake_all_queues is only
6744 * appropriate so long as all callers are assured to
6745 * have free tx slots (such as after tg3_init_hw)
6746 */
6747 netif_tx_wake_all_queues(tp->dev);
6748
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006749 if (tp->link_up)
6750 netif_carrier_on(tp->dev);
6751
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006752 tg3_napi_enable(tp);
6753 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6754 tg3_enable_ints(tp);
6755}
6756
David S. Millerf47c11e2005-06-24 20:18:35 -07006757static void tg3_irq_quiesce(struct tg3 *tp)
6758{
Matt Carlson4f125f42009-09-01 12:55:02 +00006759 int i;
6760
David S. Millerf47c11e2005-06-24 20:18:35 -07006761 BUG_ON(tp->irq_sync);
6762
6763 tp->irq_sync = 1;
6764 smp_mb();
6765
Matt Carlson4f125f42009-09-01 12:55:02 +00006766 for (i = 0; i < tp->irq_cnt; i++)
6767 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006768}
6769
David S. Millerf47c11e2005-06-24 20:18:35 -07006770/* Fully shutdown all tg3 driver activity elsewhere in the system.
6771 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6772 * with as well. Most of the time, this is not necessary except when
6773 * shutting down the device.
6774 */
6775static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6776{
Michael Chan46966542007-07-11 19:47:19 -07006777 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006778 if (irq_sync)
6779 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006780}
6781
6782static inline void tg3_full_unlock(struct tg3 *tp)
6783{
David S. Millerf47c11e2005-06-24 20:18:35 -07006784 spin_unlock_bh(&tp->lock);
6785}
6786
Michael Chanfcfa0a32006-03-20 22:28:41 -08006787/* One-shot MSI handler - Chip automatically disables interrupt
6788 * after sending MSI so driver doesn't have to do it.
6789 */
David Howells7d12e782006-10-05 14:55:46 +01006790static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006791{
Matt Carlson09943a12009-08-28 14:01:57 +00006792 struct tg3_napi *tnapi = dev_id;
6793 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006794
Matt Carlson898a56f2009-08-28 14:02:40 +00006795 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006796 if (tnapi->rx_rcb)
6797 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006798
6799 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006800 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006801
6802 return IRQ_HANDLED;
6803}
6804
Michael Chan88b06bc22005-04-21 17:13:25 -07006805/* MSI ISR - No need to check for interrupt sharing and no need to
6806 * flush status block and interrupt mailbox. PCI ordering rules
6807 * guarantee that MSI will arrive after the status block.
6808 */
David Howells7d12e782006-10-05 14:55:46 +01006809static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006810{
Matt Carlson09943a12009-08-28 14:01:57 +00006811 struct tg3_napi *tnapi = dev_id;
6812 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006813
Matt Carlson898a56f2009-08-28 14:02:40 +00006814 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006815 if (tnapi->rx_rcb)
6816 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006817 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006818 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006819 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006820 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006821 * NIC to stop sending us irqs, engaging "in-intr-handler"
6822 * event coalescing.
6823 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006824 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006825 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006826 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006827
Michael Chan88b06bc22005-04-21 17:13:25 -07006828 return IRQ_RETVAL(1);
6829}
6830
David Howells7d12e782006-10-05 14:55:46 +01006831static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832{
Matt Carlson09943a12009-08-28 14:01:57 +00006833 struct tg3_napi *tnapi = dev_id;
6834 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006835 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 unsigned int handled = 1;
6837
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 /* In INTx mode, it is possible for the interrupt to arrive at
6839 * the CPU before the status block posted prior to the interrupt.
6840 * Reading the PCI State register will confirm whether the
6841 * interrupt is ours and will flush the status block.
6842 */
Michael Chand18edcb2007-03-24 20:57:11 -07006843 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006844 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006845 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6846 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006847 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006848 }
Michael Chand18edcb2007-03-24 20:57:11 -07006849 }
6850
6851 /*
6852 * Writing any value to intr-mbox-0 clears PCI INTA# and
6853 * chip-internal interrupt pending events.
6854 * Writing non-zero to intr-mbox-0 additional tells the
6855 * NIC to stop sending us irqs, engaging "in-intr-handler"
6856 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006857 *
6858 * Flush the mailbox to de-assert the IRQ immediately to prevent
6859 * spurious interrupts. The flush impacts performance but
6860 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006861 */
Michael Chanc04cb342007-05-07 00:26:15 -07006862 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006863 if (tg3_irq_sync(tp))
6864 goto out;
6865 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006866 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006867 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006868 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006869 } else {
6870 /* No work, shared interrupt perhaps? re-enable
6871 * interrupts, and flush that PCI write
6872 */
6873 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6874 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006875 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006876out:
David S. Millerfac9b832005-05-18 22:46:34 -07006877 return IRQ_RETVAL(handled);
6878}
6879
David Howells7d12e782006-10-05 14:55:46 +01006880static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006881{
Matt Carlson09943a12009-08-28 14:01:57 +00006882 struct tg3_napi *tnapi = dev_id;
6883 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006884 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006885 unsigned int handled = 1;
6886
David S. Millerfac9b832005-05-18 22:46:34 -07006887 /* In INTx mode, it is possible for the interrupt to arrive at
6888 * the CPU before the status block posted prior to the interrupt.
6889 * Reading the PCI State register will confirm whether the
6890 * interrupt is ours and will flush the status block.
6891 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006892 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006893 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006894 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6895 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 }
Michael Chand18edcb2007-03-24 20:57:11 -07006898 }
6899
6900 /*
6901 * writing any value to intr-mbox-0 clears PCI INTA# and
6902 * chip-internal interrupt pending events.
6903 * writing non-zero to intr-mbox-0 additional tells the
6904 * NIC to stop sending us irqs, engaging "in-intr-handler"
6905 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006906 *
6907 * Flush the mailbox to de-assert the IRQ immediately to prevent
6908 * spurious interrupts. The flush impacts performance but
6909 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006910 */
Michael Chanc04cb342007-05-07 00:26:15 -07006911 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006912
6913 /*
6914 * In a shared interrupt configuration, sometimes other devices'
6915 * interrupts will scream. We record the current status tag here
6916 * so that the above check can report that the screaming interrupts
6917 * are unhandled. Eventually they will be silenced.
6918 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006919 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006920
Michael Chand18edcb2007-03-24 20:57:11 -07006921 if (tg3_irq_sync(tp))
6922 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006923
Matt Carlson72334482009-08-28 14:03:01 +00006924 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006925
Matt Carlson09943a12009-08-28 14:01:57 +00006926 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006927
David S. Millerf47c11e2005-06-24 20:18:35 -07006928out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 return IRQ_RETVAL(handled);
6930}
6931
Michael Chan79381092005-04-21 17:13:59 -07006932/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006933static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006934{
Matt Carlson09943a12009-08-28 14:01:57 +00006935 struct tg3_napi *tnapi = dev_id;
6936 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006937 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006938
Michael Chanf9804dd2005-09-27 12:13:10 -07006939 if ((sblk->status & SD_STATUS_UPDATED) ||
6940 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006941 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006942 return IRQ_RETVAL(1);
6943 }
6944 return IRQ_RETVAL(0);
6945}
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947#ifdef CONFIG_NET_POLL_CONTROLLER
6948static void tg3_poll_controller(struct net_device *dev)
6949{
Matt Carlson4f125f42009-09-01 12:55:02 +00006950 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006951 struct tg3 *tp = netdev_priv(dev);
6952
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +00006953 if (tg3_irq_sync(tp))
6954 return;
6955
Matt Carlson4f125f42009-09-01 12:55:02 +00006956 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006957 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958}
6959#endif
6960
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961static void tg3_tx_timeout(struct net_device *dev)
6962{
6963 struct tg3 *tp = netdev_priv(dev);
6964
Michael Chanb0408752007-02-13 12:18:30 -08006965 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006966 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006967 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969
Matt Carlsondb219972011-11-04 09:15:03 +00006970 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971}
6972
Michael Chanc58ec932005-09-17 00:46:27 -07006973/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6974static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6975{
6976 u32 base = (u32) mapping & 0xffffffff;
6977
Eric Dumazet807540b2010-09-23 05:40:09 +00006978 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006979}
6980
Michael Chan72f2afb2006-03-06 19:28:35 -08006981/* Test for DMA addresses > 40-bit */
6982static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6983 int len)
6984{
6985#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006986 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006987 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006988 return 0;
6989#else
6990 return 0;
6991#endif
6992}
6993
Matt Carlsond1a3b732011-07-27 14:20:51 +00006994static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006995 dma_addr_t mapping, u32 len, u32 flags,
6996 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006997{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006998 txbd->addr_hi = ((u64) mapping >> 32);
6999 txbd->addr_lo = ((u64) mapping & 0xffffffff);
7000 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
7001 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00007002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
Matt Carlson84b67b22011-07-27 14:20:52 +00007004static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007005 dma_addr_t map, u32 len, u32 flags,
7006 u32 mss, u32 vlan)
7007{
7008 struct tg3 *tp = tnapi->tp;
7009 bool hwbug = false;
7010
7011 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00007012 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007013
7014 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007015 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007016
7017 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00007018 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00007019
Matt Carlsona4cb4282011-12-14 11:09:58 +00007020 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007021 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00007022 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00007023 while (len > tp->dma_limit && *budget) {
7024 u32 frag_len = tp->dma_limit;
7025 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00007026
Matt Carlsonb9e45482011-11-04 09:14:59 +00007027 /* Avoid the 8byte DMA problem */
7028 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00007029 len += tp->dma_limit / 2;
7030 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00007031 }
7032
Matt Carlsonb9e45482011-11-04 09:14:59 +00007033 tnapi->tx_buffers[*entry].fragmented = true;
7034
7035 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7036 frag_len, tmp_flag, mss, vlan);
7037 *budget -= 1;
7038 prvidx = *entry;
7039 *entry = NEXT_TX(*entry);
7040
Matt Carlsone31aa982011-07-27 14:20:53 +00007041 map += frag_len;
7042 }
7043
7044 if (len) {
7045 if (*budget) {
7046 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7047 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00007048 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00007049 *entry = NEXT_TX(*entry);
7050 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00007051 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007052 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00007053 }
7054 }
7055 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00007056 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
7057 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00007058 *entry = NEXT_TX(*entry);
7059 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00007060
7061 return hwbug;
7062}
7063
Matt Carlson0d681b22011-07-27 14:20:49 +00007064static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00007065{
7066 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00007067 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00007068 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007069
Matt Carlson0d681b22011-07-27 14:20:49 +00007070 skb = txb->skb;
7071 txb->skb = NULL;
7072
Matt Carlson432aa7e2011-05-19 12:12:45 +00007073 pci_unmap_single(tnapi->tp->pdev,
7074 dma_unmap_addr(txb, mapping),
7075 skb_headlen(skb),
7076 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007077
7078 while (txb->fragmented) {
7079 txb->fragmented = false;
7080 entry = NEXT_TX(entry);
7081 txb = &tnapi->tx_buffers[entry];
7082 }
7083
Matt Carlsonba1142e2011-11-04 09:15:00 +00007084 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00007085 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00007086
7087 entry = NEXT_TX(entry);
7088 txb = &tnapi->tx_buffers[entry];
7089
7090 pci_unmap_page(tnapi->tp->pdev,
7091 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00007092 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00007093
7094 while (txb->fragmented) {
7095 txb->fragmented = false;
7096 entry = NEXT_TX(entry);
7097 txb = &tnapi->tx_buffers[entry];
7098 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00007099 }
7100}
7101
Michael Chan72f2afb2006-03-06 19:28:35 -08007102/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007103static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04007104 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00007105 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00007106 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107{
Matt Carlson24f4efd2009-11-13 13:03:35 +00007108 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04007109 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07007110 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007111 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Matt Carlson41588ba12008-04-19 18:12:33 -07007113 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
7114 new_skb = skb_copy(skb, GFP_ATOMIC);
7115 else {
7116 int more_headroom = 4 - ((unsigned long)skb->data & 3);
7117
7118 new_skb = skb_copy_expand(skb,
7119 skb_headroom(skb) + more_headroom,
7120 skb_tailroom(skb), GFP_ATOMIC);
7121 }
7122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07007124 ret = -1;
7125 } else {
7126 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00007127 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
7128 PCI_DMA_TODEVICE);
7129 /* Make sure the mapping succeeded */
7130 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00007131 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07007132 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07007133 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00007134 u32 save_entry = *entry;
7135
Matt Carlson92cd3a12011-07-27 14:20:47 +00007136 base_flags |= TXD_FLAG_END;
7137
Matt Carlson84b67b22011-07-27 14:20:52 +00007138 tnapi->tx_buffers[*entry].skb = new_skb;
7139 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00007140 mapping, new_addr);
7141
Matt Carlson84b67b22011-07-27 14:20:52 +00007142 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00007143 new_skb->len, base_flags,
7144 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00007145 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00007146 dev_kfree_skb(new_skb);
7147 ret = -1;
7148 }
Michael Chanc58ec932005-09-17 00:46:27 -07007149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 }
7151
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04007153 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07007154 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155}
7156
Matt Carlson2ffcc982011-05-19 12:12:44 +00007157static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07007158
7159/* Use GSO to workaround a rare TSO bug that may be triggered when the
7160 * TSO header is greater than 80 bytes.
7161 */
7162static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
7163{
7164 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007165 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07007166
7167 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007168 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07007169 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007170
7171 /* netif_tx_stop_queue() must be done before checking
7172 * checking tx index in tg3_tx_avail() below, because in
7173 * tg3_tx(), we update tx index before checking for
7174 * netif_tx_queue_stopped().
7175 */
7176 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007177 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007178 return NETDEV_TX_BUSY;
7179
7180 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007181 }
7182
7183 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007184 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007185 goto tg3_tso_bug_end;
7186
7187 do {
7188 nskb = segs;
7189 segs = segs->next;
7190 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007191 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007192 } while (segs);
7193
7194tg3_tso_bug_end:
7195 dev_kfree_skb(skb);
7196
7197 return NETDEV_TX_OK;
7198}
Michael Chan52c0fd82006-06-29 20:15:54 -07007199
Michael Chan5a6f3072006-03-20 22:28:05 -08007200/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007201 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007202 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007203static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007204{
7205 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007206 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007207 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007208 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007209 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007210 struct tg3_napi *tnapi;
7211 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007212 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007213
Matt Carlson24f4efd2009-11-13 13:03:35 +00007214 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7215 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007216 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007217 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
Matt Carlson84b67b22011-07-27 14:20:52 +00007219 budget = tg3_tx_avail(tnapi);
7220
Michael Chan00b70502006-06-17 21:58:45 -07007221 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007222 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007223 * interrupt. Furthermore, IRQ processing runs lockless so we have
7224 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007226 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007227 if (!netif_tx_queue_stopped(txq)) {
7228 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007229
7230 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007231 netdev_err(dev,
7232 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 return NETDEV_TX_BUSY;
7235 }
7236
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007237 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007239 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007241
Matt Carlsonbe98da62010-07-11 09:31:46 +00007242 mss = skb_shinfo(skb)->gso_size;
7243 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007244 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007245 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
7247 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007248 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7249 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Matt Carlson34195c32010-07-11 09:31:42 +00007251 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007252 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
Eric Dumazeta5a11952012-01-23 01:22:09 +00007254 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007255
Eric Dumazeta5a11952012-01-23 01:22:09 +00007256 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007257 iph->check = 0;
7258 iph->tot_len = htons(mss + hdr_len);
7259 }
7260
Michael Chan52c0fd82006-06-29 20:15:54 -07007261 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007262 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007263 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007264
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7266 TXD_FLAG_CPU_POST_DMA);
7267
Joe Perches63c3a662011-04-26 08:12:10 +00007268 if (tg3_flag(tp, HW_TSO_1) ||
7269 tg3_flag(tp, HW_TSO_2) ||
7270 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007271 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007273 } else
7274 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7275 iph->daddr, 0,
7276 IPPROTO_TCP,
7277 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Joe Perches63c3a662011-04-26 08:12:10 +00007279 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007280 mss |= (hdr_len & 0xc) << 12;
7281 if (hdr_len & 0x10)
7282 base_flags |= 0x00000010;
7283 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007284 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007285 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007286 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007288 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 int tsflags;
7290
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007291 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 mss |= (tsflags << 11);
7293 }
7294 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007295 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 int tsflags;
7297
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007298 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 base_flags |= tsflags << 12;
7300 }
7301 }
7302 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007303
Matt Carlson93a700a2011-08-31 11:44:54 +00007304 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7305 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7306 base_flags |= TXD_FLAG_JMB_PKT;
7307
Matt Carlson92cd3a12011-07-27 14:20:47 +00007308 if (vlan_tx_tag_present(skb)) {
7309 base_flags |= TXD_FLAG_VLAN;
7310 vlan = vlan_tx_tag_get(skb);
7311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00007313 if ((unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) &&
7314 tg3_flag(tp, TX_TSTAMP_EN)) {
7315 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
7316 base_flags |= TXD_FLAG_HWTSTAMP;
7317 }
7318
Alexander Duyckf4188d82009-12-02 16:48:38 +00007319 len = skb_headlen(skb);
7320
7321 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007322 if (pci_dma_mapping_error(tp->pdev, mapping))
7323 goto drop;
7324
David S. Miller90079ce2008-09-11 04:52:51 -07007325
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007326 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007327 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328
7329 would_hit_hwbug = 0;
7330
Joe Perches63c3a662011-04-26 08:12:10 +00007331 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007332 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
Matt Carlson84b67b22011-07-27 14:20:52 +00007334 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007335 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007336 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007337 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007338 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007339 u32 tmp_mss = mss;
7340
7341 if (!tg3_flag(tp, HW_TSO_1) &&
7342 !tg3_flag(tp, HW_TSO_2) &&
7343 !tg3_flag(tp, HW_TSO_3))
7344 tmp_mss = 0;
7345
Matt Carlsonc5665a52012-02-13 10:20:12 +00007346 /* Now loop through additional data
7347 * fragments, and queue them.
7348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 last = skb_shinfo(skb)->nr_frags - 1;
7350 for (i = 0; i <= last; i++) {
7351 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7352
Eric Dumazet9e903e02011-10-18 21:00:24 +00007353 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007354 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007355 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007357 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007358 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007359 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007360 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007361 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
Matt Carlsonb9e45482011-11-04 09:14:59 +00007363 if (!budget ||
7364 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007365 len, base_flags |
7366 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007367 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007368 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007369 break;
7370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 }
7372 }
7373
7374 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007375 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
7377 /* If the workaround fails due to memory/mapping
7378 * failure, silently drop this packet.
7379 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007380 entry = tnapi->tx_prod;
7381 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007382 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007383 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007384 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 }
7386
Richard Cochrand515b452011-06-19 03:31:41 +00007387 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007388 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007389
Michael Chan6541b802012-03-04 14:48:14 +00007390 /* Sync BD data before updating mailbox */
7391 wmb();
7392
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007394 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007396 tnapi->tx_prod = entry;
7397 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007398 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007399
7400 /* netif_tx_stop_queue() must be done before checking
7401 * checking tx index in tg3_tx_avail() below, because in
7402 * tg3_tx(), we update tx index before checking for
7403 * netif_tx_queue_stopped().
7404 */
7405 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007406 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007407 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007410 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007412
7413dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007414 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007415 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007416drop:
7417 dev_kfree_skb(skb);
7418drop_nofree:
7419 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007420 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421}
7422
Matt Carlson6e01b202011-08-19 13:58:20 +00007423static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7424{
7425 if (enable) {
7426 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7427 MAC_MODE_PORT_MODE_MASK);
7428
7429 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7430
7431 if (!tg3_flag(tp, 5705_PLUS))
7432 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7433
7434 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7435 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7436 else
7437 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7438 } else {
7439 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7440
7441 if (tg3_flag(tp, 5705_PLUS) ||
7442 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7443 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7444 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7445 }
7446
7447 tw32(MAC_MODE, tp->mac_mode);
7448 udelay(40);
7449}
7450
Matt Carlson941ec902011-08-19 13:58:23 +00007451static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007452{
Matt Carlson941ec902011-08-19 13:58:23 +00007453 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007454
7455 tg3_phy_toggle_apd(tp, false);
7456 tg3_phy_toggle_automdix(tp, 0);
7457
Matt Carlson941ec902011-08-19 13:58:23 +00007458 if (extlpbk && tg3_phy_set_extloopbk(tp))
7459 return -EIO;
7460
7461 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007462 switch (speed) {
7463 case SPEED_10:
7464 break;
7465 case SPEED_100:
7466 bmcr |= BMCR_SPEED100;
7467 break;
7468 case SPEED_1000:
7469 default:
7470 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7471 speed = SPEED_100;
7472 bmcr |= BMCR_SPEED100;
7473 } else {
7474 speed = SPEED_1000;
7475 bmcr |= BMCR_SPEED1000;
7476 }
7477 }
7478
Matt Carlson941ec902011-08-19 13:58:23 +00007479 if (extlpbk) {
7480 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7481 tg3_readphy(tp, MII_CTRL1000, &val);
7482 val |= CTL1000_AS_MASTER |
7483 CTL1000_ENABLE_MASTER;
7484 tg3_writephy(tp, MII_CTRL1000, val);
7485 } else {
7486 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7487 MII_TG3_FET_PTEST_TRIM_2;
7488 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7489 }
7490 } else
7491 bmcr |= BMCR_LOOPBACK;
7492
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007493 tg3_writephy(tp, MII_BMCR, bmcr);
7494
7495 /* The write needs to be flushed for the FETs */
7496 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7497 tg3_readphy(tp, MII_BMCR, &bmcr);
7498
7499 udelay(40);
7500
7501 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007503 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007504 MII_TG3_FET_PTEST_FRC_TX_LINK |
7505 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7506
7507 /* The write needs to be flushed for the AC131 */
7508 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7509 }
7510
7511 /* Reset to prevent losing 1st rx packet intermittently */
7512 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7513 tg3_flag(tp, 5780_CLASS)) {
7514 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7515 udelay(10);
7516 tw32_f(MAC_RX_MODE, tp->rx_mode);
7517 }
7518
7519 mac_mode = tp->mac_mode &
7520 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7521 if (speed == SPEED_1000)
7522 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7523 else
7524 mac_mode |= MAC_MODE_PORT_MODE_MII;
7525
7526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7527 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7528
7529 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7530 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7531 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7532 mac_mode |= MAC_MODE_LINK_POLARITY;
7533
7534 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7535 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7536 }
7537
7538 tw32(MAC_MODE, mac_mode);
7539 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007540
7541 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007542}
7543
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007544static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007545{
7546 struct tg3 *tp = netdev_priv(dev);
7547
7548 if (features & NETIF_F_LOOPBACK) {
7549 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7550 return;
7551
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007552 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007553 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007554 netif_carrier_on(tp->dev);
7555 spin_unlock_bh(&tp->lock);
7556 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7557 } else {
7558 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7559 return;
7560
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007561 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007562 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007563 /* Force link status check */
7564 tg3_setup_phy(tp, 1);
7565 spin_unlock_bh(&tp->lock);
7566 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7567 }
7568}
7569
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007570static netdev_features_t tg3_fix_features(struct net_device *dev,
7571 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007572{
7573 struct tg3 *tp = netdev_priv(dev);
7574
Joe Perches63c3a662011-04-26 08:12:10 +00007575 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007576 features &= ~NETIF_F_ALL_TSO;
7577
7578 return features;
7579}
7580
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007581static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007582{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007583 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007584
7585 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7586 tg3_set_loopback(dev, features);
7587
7588 return 0;
7589}
7590
Matt Carlson21f581a2009-08-28 14:00:25 +00007591static void tg3_rx_prodring_free(struct tg3 *tp,
7592 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 int i;
7595
Matt Carlson8fea32b2010-09-15 08:59:58 +00007596 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007597 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007598 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007599 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007600 tp->rx_pkt_map_sz);
7601
Joe Perches63c3a662011-04-26 08:12:10 +00007602 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007603 for (i = tpr->rx_jmb_cons_idx;
7604 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007605 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007606 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007607 TG3_RX_JMB_MAP_SZ);
7608 }
7609 }
7610
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007611 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613
Matt Carlson2c49a442010-09-30 10:34:35 +00007614 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007615 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007616 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617
Joe Perches63c3a662011-04-26 08:12:10 +00007618 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007619 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007620 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007621 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 }
7623}
7624
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007625/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626 *
7627 * The chip has been shut down and the driver detached from
7628 * the networking, so no interrupts or new tx packets will
7629 * end up in the driver. tp->{tx,}lock are held and thus
7630 * we may not sleep.
7631 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007632static int tg3_rx_prodring_alloc(struct tg3 *tp,
7633 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634{
Matt Carlson287be122009-08-28 13:58:46 +00007635 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007637 tpr->rx_std_cons_idx = 0;
7638 tpr->rx_std_prod_idx = 0;
7639 tpr->rx_jmb_cons_idx = 0;
7640 tpr->rx_jmb_prod_idx = 0;
7641
Matt Carlson8fea32b2010-09-15 08:59:58 +00007642 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007643 memset(&tpr->rx_std_buffers[0], 0,
7644 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007645 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007646 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007647 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007648 goto done;
7649 }
7650
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007652 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
Matt Carlson287be122009-08-28 13:58:46 +00007654 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007655 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007656 tp->dev->mtu > ETH_DATA_LEN)
7657 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7658 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad42005-07-25 12:31:17 -07007659
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 /* Initialize invariants of the rings, we only set this
7661 * stuff once. This works because the card does not
7662 * write into the rx buffer posting rings.
7663 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007664 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 struct tg3_rx_buffer_desc *rxd;
7666
Matt Carlson21f581a2009-08-28 14:00:25 +00007667 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007668 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7670 rxd->opaque = (RXD_OPAQUE_RING_STD |
7671 (i << RXD_OPAQUE_INDEX_SHIFT));
7672 }
7673
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007674 /* Now allocate fresh SKBs for each rx ring. */
7675 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007676 unsigned int frag_size;
7677
7678 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7679 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007680 netdev_warn(tp->dev,
7681 "Using a smaller RX standard ring. Only "
7682 "%d out of %d buffers were allocated "
7683 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007684 if (i == 0)
7685 goto initfail;
7686 tp->rx_pending = i;
7687 break;
7688 }
7689 }
7690
Joe Perches63c3a662011-04-26 08:12:10 +00007691 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007692 goto done;
7693
Matt Carlson2c49a442010-09-30 10:34:35 +00007694 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007695
Joe Perches63c3a662011-04-26 08:12:10 +00007696 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007697 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698
Matt Carlson2c49a442010-09-30 10:34:35 +00007699 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007700 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701
Matt Carlson0d86df82010-02-17 15:17:00 +00007702 rxd = &tpr->rx_jmb[i].std;
7703 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7704 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7705 RXD_FLAG_JUMBO;
7706 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7707 (i << RXD_OPAQUE_INDEX_SHIFT));
7708 }
7709
7710 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007711 unsigned int frag_size;
7712
7713 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7714 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007715 netdev_warn(tp->dev,
7716 "Using a smaller RX jumbo ring. Only %d "
7717 "out of %d buffers were allocated "
7718 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007719 if (i == 0)
7720 goto initfail;
7721 tp->rx_jumbo_pending = i;
7722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 }
7724 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007725
7726done:
Michael Chan32d8c572006-07-25 16:38:29 -07007727 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007728
7729initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007730 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007731 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732}
7733
Matt Carlson21f581a2009-08-28 14:00:25 +00007734static void tg3_rx_prodring_fini(struct tg3 *tp,
7735 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736{
Matt Carlson21f581a2009-08-28 14:00:25 +00007737 kfree(tpr->rx_std_buffers);
7738 tpr->rx_std_buffers = NULL;
7739 kfree(tpr->rx_jmb_buffers);
7740 tpr->rx_jmb_buffers = NULL;
7741 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007742 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7743 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007744 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007746 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007747 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7748 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007749 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007751}
7752
Matt Carlson21f581a2009-08-28 14:00:25 +00007753static int tg3_rx_prodring_init(struct tg3 *tp,
7754 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007755{
Matt Carlson2c49a442010-09-30 10:34:35 +00007756 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7757 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007758 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007759 return -ENOMEM;
7760
Matt Carlson4bae65c2010-11-24 08:31:52 +00007761 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7762 TG3_RX_STD_RING_BYTES(tp),
7763 &tpr->rx_std_mapping,
7764 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007765 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007766 goto err_out;
7767
Joe Perches63c3a662011-04-26 08:12:10 +00007768 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007769 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007770 GFP_KERNEL);
7771 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007772 goto err_out;
7773
Matt Carlson4bae65c2010-11-24 08:31:52 +00007774 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7775 TG3_RX_JMB_RING_BYTES(tp),
7776 &tpr->rx_jmb_mapping,
7777 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007778 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007779 goto err_out;
7780 }
7781
7782 return 0;
7783
7784err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007785 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007786 return -ENOMEM;
7787}
7788
7789/* Free up pending packets in all rx/tx rings.
7790 *
7791 * The chip has been shut down and the driver detached from
7792 * the networking, so no interrupts or new tx packets will
7793 * end up in the driver. tp->{tx,}lock is not held and we are not
7794 * in an interrupt context and thus may sleep.
7795 */
7796static void tg3_free_rings(struct tg3 *tp)
7797{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007798 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007799
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007800 for (j = 0; j < tp->irq_cnt; j++) {
7801 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007802
Matt Carlson8fea32b2010-09-15 08:59:58 +00007803 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007804
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007805 if (!tnapi->tx_buffers)
7806 continue;
7807
Matt Carlson0d681b22011-07-27 14:20:49 +00007808 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7809 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007810
Matt Carlson0d681b22011-07-27 14:20:49 +00007811 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007812 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007813
Matt Carlsonba1142e2011-11-04 09:15:00 +00007814 tg3_tx_skb_unmap(tnapi, i,
7815 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007816
7817 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007818 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007819 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007820 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007821}
7822
7823/* Initialize tx/rx rings for packet processing.
7824 *
7825 * The chip has been shut down and the driver detached from
7826 * the networking, so no interrupts or new tx packets will
7827 * end up in the driver. tp->{tx,}lock are held and thus
7828 * we may not sleep.
7829 */
7830static int tg3_init_rings(struct tg3 *tp)
7831{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007832 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007833
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007834 /* Free up all the SKBs. */
7835 tg3_free_rings(tp);
7836
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007837 for (i = 0; i < tp->irq_cnt; i++) {
7838 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007839
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007840 tnapi->last_tag = 0;
7841 tnapi->last_irq_tag = 0;
7842 tnapi->hw_status->status = 0;
7843 tnapi->hw_status->status_tag = 0;
7844 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7845
7846 tnapi->tx_prod = 0;
7847 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007848 if (tnapi->tx_ring)
7849 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007850
7851 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007852 if (tnapi->rx_rcb)
7853 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007854
Matt Carlson8fea32b2010-09-15 08:59:58 +00007855 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007856 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007857 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007858 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007859 }
Matt Carlson72334482009-08-28 14:03:01 +00007860
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007861 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007862}
7863
Michael Chan49a359e2012-09-28 07:12:37 +00007864static void tg3_mem_tx_release(struct tg3 *tp)
7865{
7866 int i;
7867
7868 for (i = 0; i < tp->irq_max; i++) {
7869 struct tg3_napi *tnapi = &tp->napi[i];
7870
7871 if (tnapi->tx_ring) {
7872 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7873 tnapi->tx_ring, tnapi->tx_desc_mapping);
7874 tnapi->tx_ring = NULL;
7875 }
7876
7877 kfree(tnapi->tx_buffers);
7878 tnapi->tx_buffers = NULL;
7879 }
7880}
7881
7882static int tg3_mem_tx_acquire(struct tg3 *tp)
7883{
7884 int i;
7885 struct tg3_napi *tnapi = &tp->napi[0];
7886
7887 /* If multivector TSS is enabled, vector 0 does not handle
7888 * tx interrupts. Don't allocate any resources for it.
7889 */
7890 if (tg3_flag(tp, ENABLE_TSS))
7891 tnapi++;
7892
7893 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7894 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7895 TG3_TX_RING_SIZE, GFP_KERNEL);
7896 if (!tnapi->tx_buffers)
7897 goto err_out;
7898
7899 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7900 TG3_TX_RING_BYTES,
7901 &tnapi->tx_desc_mapping,
7902 GFP_KERNEL);
7903 if (!tnapi->tx_ring)
7904 goto err_out;
7905 }
7906
7907 return 0;
7908
7909err_out:
7910 tg3_mem_tx_release(tp);
7911 return -ENOMEM;
7912}
7913
7914static void tg3_mem_rx_release(struct tg3 *tp)
7915{
7916 int i;
7917
7918 for (i = 0; i < tp->irq_max; i++) {
7919 struct tg3_napi *tnapi = &tp->napi[i];
7920
7921 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7922
7923 if (!tnapi->rx_rcb)
7924 continue;
7925
7926 dma_free_coherent(&tp->pdev->dev,
7927 TG3_RX_RCB_RING_BYTES(tp),
7928 tnapi->rx_rcb,
7929 tnapi->rx_rcb_mapping);
7930 tnapi->rx_rcb = NULL;
7931 }
7932}
7933
7934static int tg3_mem_rx_acquire(struct tg3 *tp)
7935{
7936 unsigned int i, limit;
7937
7938 limit = tp->rxq_cnt;
7939
7940 /* If RSS is enabled, we need a (dummy) producer ring
7941 * set on vector zero. This is the true hw prodring.
7942 */
7943 if (tg3_flag(tp, ENABLE_RSS))
7944 limit++;
7945
7946 for (i = 0; i < limit; i++) {
7947 struct tg3_napi *tnapi = &tp->napi[i];
7948
7949 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7950 goto err_out;
7951
7952 /* If multivector RSS is enabled, vector 0
7953 * does not handle rx or tx interrupts.
7954 * Don't allocate any resources for it.
7955 */
7956 if (!i && tg3_flag(tp, ENABLE_RSS))
7957 continue;
7958
7959 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7960 TG3_RX_RCB_RING_BYTES(tp),
7961 &tnapi->rx_rcb_mapping,
7962 GFP_KERNEL);
7963 if (!tnapi->rx_rcb)
7964 goto err_out;
7965
7966 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
7967 }
7968
7969 return 0;
7970
7971err_out:
7972 tg3_mem_rx_release(tp);
7973 return -ENOMEM;
7974}
7975
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007976/*
7977 * Must not be invoked with interrupt sources disabled and
7978 * the hardware shutdown down.
7979 */
7980static void tg3_free_consistent(struct tg3 *tp)
7981{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007982 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007983
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007984 for (i = 0; i < tp->irq_cnt; i++) {
7985 struct tg3_napi *tnapi = &tp->napi[i];
7986
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007987 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007988 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7989 tnapi->hw_status,
7990 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007991 tnapi->hw_status = NULL;
7992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007994
Michael Chan49a359e2012-09-28 07:12:37 +00007995 tg3_mem_rx_release(tp);
7996 tg3_mem_tx_release(tp);
7997
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007999 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
8000 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 tp->hw_stats = NULL;
8002 }
8003}
8004
8005/*
8006 * Must not be invoked with interrupt sources disabled and
8007 * the hardware shutdown down. Can sleep.
8008 */
8009static int tg3_alloc_consistent(struct tg3 *tp)
8010{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008011 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00008012
Matt Carlson4bae65c2010-11-24 08:31:52 +00008013 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
8014 sizeof(struct tg3_hw_stats),
8015 &tp->stats_mapping,
8016 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017 if (!tp->hw_stats)
8018 goto err_out;
8019
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8021
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008022 for (i = 0; i < tp->irq_cnt; i++) {
8023 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008024 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008025
Matt Carlson4bae65c2010-11-24 08:31:52 +00008026 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
8027 TG3_HW_STATUS_SIZE,
8028 &tnapi->status_mapping,
8029 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008030 if (!tnapi->hw_status)
8031 goto err_out;
8032
8033 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008034 sblk = tnapi->hw_status;
8035
Michael Chan49a359e2012-09-28 07:12:37 +00008036 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07008037 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008038
Michael Chan49a359e2012-09-28 07:12:37 +00008039 /*
8040 * When RSS is enabled, the status block format changes
8041 * slightly. The "rx_jumbo_consumer", "reserved",
8042 * and "rx_mini_consumer" members get mapped to the
8043 * other three rx return ring producer indexes.
8044 */
8045 switch (i) {
8046 case 1:
8047 prodptr = &sblk->idx[0].rx_producer;
8048 break;
8049 case 2:
8050 prodptr = &sblk->rx_jumbo_consumer;
8051 break;
8052 case 3:
8053 prodptr = &sblk->reserved;
8054 break;
8055 case 4:
8056 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00008057 break;
8058 }
Michael Chan49a359e2012-09-28 07:12:37 +00008059 tnapi->rx_rcb_prod_idx = prodptr;
8060 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008061 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00008062 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008063 }
8064
Michael Chan49a359e2012-09-28 07:12:37 +00008065 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
8066 goto err_out;
8067
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 return 0;
8069
8070err_out:
8071 tg3_free_consistent(tp);
8072 return -ENOMEM;
8073}
8074
8075#define MAX_WAIT_CNT 1000
8076
8077/* To stop a block, clear the enable bit and poll till it
8078 * clears. tp->lock is held.
8079 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008080static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081{
8082 unsigned int i;
8083 u32 val;
8084
Joe Perches63c3a662011-04-26 08:12:10 +00008085 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008086 switch (ofs) {
8087 case RCVLSC_MODE:
8088 case DMAC_MODE:
8089 case MBFREE_MODE:
8090 case BUFMGR_MODE:
8091 case MEMARB_MODE:
8092 /* We can't enable/disable these bits of the
8093 * 5705/5750, just say success.
8094 */
8095 return 0;
8096
8097 default:
8098 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 }
8101
8102 val = tr32(ofs);
8103 val &= ~enable_bit;
8104 tw32_f(ofs, val);
8105
8106 for (i = 0; i < MAX_WAIT_CNT; i++) {
8107 udelay(100);
8108 val = tr32(ofs);
8109 if ((val & enable_bit) == 0)
8110 break;
8111 }
8112
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008113 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00008114 dev_err(&tp->pdev->dev,
8115 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
8116 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117 return -ENODEV;
8118 }
8119
8120 return 0;
8121}
8122
8123/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008124static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125{
8126 int i, err;
8127
8128 tg3_disable_ints(tp);
8129
8130 tp->rx_mode &= ~RX_MODE_ENABLE;
8131 tw32_f(MAC_RX_MODE, tp->rx_mode);
8132 udelay(10);
8133
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008134 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
8135 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
8136 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
8137 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
8138 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
8139 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008141 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
8142 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
8143 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
8144 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
8145 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
8146 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
8147 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148
8149 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
8150 tw32_f(MAC_MODE, tp->mac_mode);
8151 udelay(40);
8152
8153 tp->tx_mode &= ~TX_MODE_ENABLE;
8154 tw32_f(MAC_TX_MODE, tp->tx_mode);
8155
8156 for (i = 0; i < MAX_WAIT_CNT; i++) {
8157 udelay(100);
8158 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
8159 break;
8160 }
8161 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00008162 dev_err(&tp->pdev->dev,
8163 "%s timed out, TX_MODE_ENABLE will not clear "
8164 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07008165 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 }
8167
Michael Chane6de8ad2005-05-05 14:42:41 -07008168 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008169 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
8170 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171
8172 tw32(FTQ_RESET, 0xffffffff);
8173 tw32(FTQ_RESET, 0x00000000);
8174
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008175 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
8176 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008178 for (i = 0; i < tp->irq_cnt; i++) {
8179 struct tg3_napi *tnapi = &tp->napi[i];
8180 if (tnapi->hw_status)
8181 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 return err;
8185}
8186
Michael Chanee6a99b2007-07-18 21:49:10 -07008187/* Save PCI command register before chip reset */
8188static void tg3_save_pci_state(struct tg3 *tp)
8189{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008190 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008191}
8192
8193/* Restore PCI state after chip reset */
8194static void tg3_restore_pci_state(struct tg3 *tp)
8195{
8196 u32 val;
8197
8198 /* Re-enable indirect register accesses. */
8199 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8200 tp->misc_host_ctrl);
8201
8202 /* Set MAX PCI retry to zero. */
8203 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
8204 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008205 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008206 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008207 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008208 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008209 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008210 PCISTATE_ALLOW_APE_SHMEM_WR |
8211 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008212 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8213
Matt Carlson8a6eac92007-10-21 16:17:55 -07008214 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008215
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008216 if (!tg3_flag(tp, PCI_EXPRESS)) {
8217 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8218 tp->pci_cacheline_sz);
8219 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8220 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008221 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008222
Michael Chanee6a99b2007-07-18 21:49:10 -07008223 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008224 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008225 u16 pcix_cmd;
8226
8227 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8228 &pcix_cmd);
8229 pcix_cmd &= ~PCI_X_CMD_ERO;
8230 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8231 pcix_cmd);
8232 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008233
Joe Perches63c3a662011-04-26 08:12:10 +00008234 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008235
8236 /* Chip reset on 5780 will reset MSI enable bit,
8237 * so need to restore it.
8238 */
Joe Perches63c3a662011-04-26 08:12:10 +00008239 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008240 u16 ctrl;
8241
8242 pci_read_config_word(tp->pdev,
8243 tp->msi_cap + PCI_MSI_FLAGS,
8244 &ctrl);
8245 pci_write_config_word(tp->pdev,
8246 tp->msi_cap + PCI_MSI_FLAGS,
8247 ctrl | PCI_MSI_FLAGS_ENABLE);
8248 val = tr32(MSGINT_MODE);
8249 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8250 }
8251 }
8252}
8253
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254/* tp->lock is held. */
8255static int tg3_chip_reset(struct tg3 *tp)
8256{
8257 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008258 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008259 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260
David S. Millerf49639e2006-06-09 11:58:36 -07008261 tg3_nvram_lock(tp);
8262
Matt Carlson77b483f2008-08-15 14:07:24 -07008263 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8264
David S. Millerf49639e2006-06-09 11:58:36 -07008265 /* No matching tg3_nvram_unlock() after this because
8266 * chip reset below will undo the nvram lock.
8267 */
8268 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269
Michael Chanee6a99b2007-07-18 21:49:10 -07008270 /* GRC_MISC_CFG core clock reset will clear the memory
8271 * enable bit in PCI register 4 and the MSI enable bit
8272 * on some chips, so we save relevant registers here.
8273 */
8274 tg3_save_pci_state(tp);
8275
Michael Chand9ab5ad12006-03-20 22:27:35 -08008276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008277 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08008278 tw32(GRC_FASTBOOT_PC, 0);
8279
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280 /*
8281 * We must avoid the readl() that normally takes place.
8282 * It locks machines, causes machine checks, and other
8283 * fun things. So, temporarily disable the 5701
8284 * hardware workaround, while we do the reset.
8285 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008286 write_op = tp->write32;
8287 if (write_op == tg3_write_flush_reg32)
8288 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289
Michael Chand18edcb2007-03-24 20:57:11 -07008290 /* Prevent the irq handler from reading or writing PCI registers
8291 * during chip reset when the memory enable bit in the PCI command
8292 * register may be cleared. The chip does not generate interrupt
8293 * at this time, but the irq handler may still be called due to irq
8294 * sharing or irqpoll.
8295 */
Joe Perches63c3a662011-04-26 08:12:10 +00008296 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008297 for (i = 0; i < tp->irq_cnt; i++) {
8298 struct tg3_napi *tnapi = &tp->napi[i];
8299 if (tnapi->hw_status) {
8300 tnapi->hw_status->status = 0;
8301 tnapi->hw_status->status_tag = 0;
8302 }
8303 tnapi->last_tag = 0;
8304 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008305 }
Michael Chand18edcb2007-03-24 20:57:11 -07008306 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008307
8308 for (i = 0; i < tp->irq_cnt; i++)
8309 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008310
Matt Carlson255ca312009-08-25 10:07:27 +00008311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8312 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8313 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8314 }
8315
Linus Torvalds1da177e2005-04-16 15:20:36 -07008316 /* do the reset */
8317 val = GRC_MISC_CFG_CORECLK_RESET;
8318
Joe Perches63c3a662011-04-26 08:12:10 +00008319 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008320 /* Force PCIe 1.0a mode */
8321 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008322 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008323 tr32(TG3_PCIE_PHY_TSTCTL) ==
8324 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8325 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8326
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
8328 tw32(GRC_MISC_CFG, (1 << 29));
8329 val |= (1 << 29);
8330 }
8331 }
8332
Michael Chanb5d37722006-09-27 16:06:21 -07008333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8334 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8335 tw32(GRC_VCPU_EXT_CTRL,
8336 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8337 }
8338
Matt Carlsonf37500d2010-08-02 11:25:59 +00008339 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008340 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008341 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008342
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 tw32(GRC_MISC_CFG, val);
8344
Michael Chan1ee582d2005-08-09 20:16:46 -07008345 /* restore 5701 hardware bug workaround write method */
8346 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
8348 /* Unfortunately, we have to delay before the PCI read back.
8349 * Some 575X chips even will not respond to a PCI cfg access
8350 * when the reset command is given to the chip.
8351 *
8352 * How do these hardware designers expect things to work
8353 * properly if the PCI write is posted for a long period
8354 * of time? It is always necessary to have some method by
8355 * which a register read back can occur to push the write
8356 * out which does the reset.
8357 *
8358 * For most tg3 variants the trick below was working.
8359 * Ho hum...
8360 */
8361 udelay(120);
8362
8363 /* Flush PCI posted writes. The normal MMIO registers
8364 * are inaccessible at this time so this is the only
8365 * way to make this reliably (actually, this is no longer
8366 * the case, see above). I tried to use indirect
8367 * register read/write but this upset some 5701 variants.
8368 */
8369 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8370
8371 udelay(120);
8372
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008373 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008374 u16 val16;
8375
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008377 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 u32 cfg_val;
8379
8380 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008381 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 udelay(100);
8383
8384 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8385 pci_write_config_dword(tp->pdev, 0xc4,
8386 cfg_val | (1 << 15));
8387 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008388
Matt Carlsone7126992009-08-25 10:08:16 +00008389 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008390 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008391 /*
8392 * Older PCIe devices only support the 128 byte
8393 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008394 */
Joe Perches63c3a662011-04-26 08:12:10 +00008395 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008396 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8397 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008398
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008399 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008400 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008401 PCI_EXP_DEVSTA_CED |
8402 PCI_EXP_DEVSTA_NFED |
8403 PCI_EXP_DEVSTA_FED |
8404 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008405 }
8406
Michael Chanee6a99b2007-07-18 21:49:10 -07008407 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408
Joe Perches63c3a662011-04-26 08:12:10 +00008409 tg3_flag_clear(tp, CHIP_RESETTING);
8410 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008411
Michael Chanee6a99b2007-07-18 21:49:10 -07008412 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008413 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008414 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008415 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416
8417 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8418 tg3_stop_fw(tp);
8419 tw32(0x5000, 0x400);
8420 }
8421
8422 tw32(GRC_MODE, tp->grc_mode);
8423
8424 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008425 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426
8427 tw32(0xc4, val | (1 << 15));
8428 }
8429
8430 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8432 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8433 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8434 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8435 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8436 }
8437
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008438 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008439 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008440 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008441 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008442 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008443 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008445 val = 0;
8446
8447 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 udelay(40);
8449
Matt Carlson77b483f2008-08-15 14:07:24 -07008450 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8451
Michael Chan7a6f4362006-09-27 16:03:31 -07008452 err = tg3_poll_fw(tp);
8453 if (err)
8454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455
Matt Carlson0a9140c2009-08-28 12:27:50 +00008456 tg3_mdio_start(tp);
8457
Joe Perches63c3a662011-04-26 08:12:10 +00008458 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008459 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8460 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008461 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008462 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463
8464 tw32(0x7c00, val | (1 << 25));
8465 }
8466
Matt Carlsond78b59f2011-04-05 14:22:46 +00008467 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8468 val = tr32(TG3_CPMU_CLCK_ORIDE);
8469 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8470 }
8471
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008473 tg3_flag_clear(tp, ENABLE_ASF);
8474 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8476 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8477 u32 nic_cfg;
8478
8479 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8480 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008481 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008482 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008483 if (tg3_flag(tp, 5750_PLUS))
8484 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485 }
8486 }
8487
8488 return 0;
8489}
8490
Matt Carlson65ec6982012-02-28 23:33:37 +00008491static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8492static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008493
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008495static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496{
8497 int err;
8498
8499 tg3_stop_fw(tp);
8500
Michael Chan944d9802005-05-29 14:57:48 -07008501 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008503 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 err = tg3_chip_reset(tp);
8505
Matt Carlsondaba2a62009-04-20 06:58:52 +00008506 __tg3_set_mac_addr(tp, 0);
8507
Michael Chan944d9802005-05-29 14:57:48 -07008508 tg3_write_sig_legacy(tp, kind);
8509 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
Matt Carlson92feeab2011-12-08 14:40:14 +00008511 if (tp->hw_stats) {
8512 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008513 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008514 tg3_get_estats(tp, &tp->estats_prev);
8515
8516 /* And make sure the next sample is new data */
8517 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8518 }
8519
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 if (err)
8521 return err;
8522
8523 return 0;
8524}
8525
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526static int tg3_set_mac_addr(struct net_device *dev, void *p)
8527{
8528 struct tg3 *tp = netdev_priv(dev);
8529 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008530 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531
Michael Chanf9804dd2005-09-27 12:13:10 -07008532 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008533 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008534
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8536
Michael Chane75f7c92006-03-20 21:33:26 -08008537 if (!netif_running(dev))
8538 return 0;
8539
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008541 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008542
Michael Chan986e0ae2007-05-05 12:10:20 -07008543 addr0_high = tr32(MAC_ADDR_0_HIGH);
8544 addr0_low = tr32(MAC_ADDR_0_LOW);
8545 addr1_high = tr32(MAC_ADDR_1_HIGH);
8546 addr1_low = tr32(MAC_ADDR_1_LOW);
8547
8548 /* Skip MAC addr 1 if ASF is using it. */
8549 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8550 !(addr1_high == 0 && addr1_low == 0))
8551 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008552 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008553 spin_lock_bh(&tp->lock);
8554 __tg3_set_mac_addr(tp, skip_mac_1);
8555 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008556
Michael Chanb9ec6c12006-07-25 16:37:27 -07008557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558}
8559
8560/* tp->lock is held. */
8561static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8562 dma_addr_t mapping, u32 maxlen_flags,
8563 u32 nic_addr)
8564{
8565 tg3_write_mem(tp,
8566 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8567 ((u64) mapping >> 32));
8568 tg3_write_mem(tp,
8569 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8570 ((u64) mapping & 0xffffffff));
8571 tg3_write_mem(tp,
8572 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8573 maxlen_flags);
8574
Joe Perches63c3a662011-04-26 08:12:10 +00008575 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 tg3_write_mem(tp,
8577 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8578 nic_addr);
8579}
8580
Michael Chana489b6d2012-09-28 07:12:39 +00008581
8582static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008583{
Michael Chana489b6d2012-09-28 07:12:39 +00008584 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008585
Joe Perches63c3a662011-04-26 08:12:10 +00008586 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008587 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8588 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8589 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008590 } else {
8591 tw32(HOSTCC_TXCOL_TICKS, 0);
8592 tw32(HOSTCC_TXMAX_FRAMES, 0);
8593 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008594
8595 for (; i < tp->txq_cnt; i++) {
8596 u32 reg;
8597
8598 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8599 tw32(reg, ec->tx_coalesce_usecs);
8600 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8601 tw32(reg, ec->tx_max_coalesced_frames);
8602 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8603 tw32(reg, ec->tx_max_coalesced_frames_irq);
8604 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008605 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008606
Michael Chana489b6d2012-09-28 07:12:39 +00008607 for (; i < tp->irq_max - 1; i++) {
8608 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8609 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8610 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8611 }
8612}
8613
8614static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8615{
8616 int i = 0;
8617 u32 limit = tp->rxq_cnt;
8618
Joe Perches63c3a662011-04-26 08:12:10 +00008619 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008620 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8621 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8622 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008623 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008624 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008625 tw32(HOSTCC_RXCOL_TICKS, 0);
8626 tw32(HOSTCC_RXMAX_FRAMES, 0);
8627 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008628 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008629
Michael Chana489b6d2012-09-28 07:12:39 +00008630 for (; i < limit; i++) {
8631 u32 reg;
8632
8633 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8634 tw32(reg, ec->rx_coalesce_usecs);
8635 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8636 tw32(reg, ec->rx_max_coalesced_frames);
8637 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8638 tw32(reg, ec->rx_max_coalesced_frames_irq);
8639 }
8640
8641 for (; i < tp->irq_max - 1; i++) {
8642 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8643 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8644 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8645 }
8646}
8647
8648static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8649{
8650 tg3_coal_tx_init(tp, ec);
8651 tg3_coal_rx_init(tp, ec);
8652
Joe Perches63c3a662011-04-26 08:12:10 +00008653 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008654 u32 val = ec->stats_block_coalesce_usecs;
8655
Matt Carlsonb6080e12009-09-01 13:12:00 +00008656 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8657 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8658
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008659 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008660 val = 0;
8661
8662 tw32(HOSTCC_STAT_COAL_TICKS, val);
8663 }
8664}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665
8666/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008667static void tg3_rings_reset(struct tg3 *tp)
8668{
8669 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008670 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008671 struct tg3_napi *tnapi = &tp->napi[0];
8672
8673 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008674 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008675 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008676 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008677 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008678 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008679 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008680 else
8681 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8682
8683 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8684 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8685 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8686 BDINFO_FLAGS_DISABLED);
8687
8688
8689 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008690 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008691 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008692 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008693 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008694 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008695 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008696 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8697 else
8698 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8699
8700 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8701 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8702 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8703 BDINFO_FLAGS_DISABLED);
8704
8705 /* Disable interrupts */
8706 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008707 tp->napi[0].chk_msi_cnt = 0;
8708 tp->napi[0].last_rx_cons = 0;
8709 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008710
8711 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008712 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008713 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008714 tp->napi[i].tx_prod = 0;
8715 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008716 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008717 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008718 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8719 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008720 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008721 tp->napi[i].last_rx_cons = 0;
8722 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008723 }
Joe Perches63c3a662011-04-26 08:12:10 +00008724 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008725 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008726 } else {
8727 tp->napi[0].tx_prod = 0;
8728 tp->napi[0].tx_cons = 0;
8729 tw32_mailbox(tp->napi[0].prodmbox, 0);
8730 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8731 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008732
8733 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008734 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008735 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8736 for (i = 0; i < 16; i++)
8737 tw32_tx_mbox(mbox + i * 8, 0);
8738 }
8739
8740 txrcb = NIC_SRAM_SEND_RCB;
8741 rxrcb = NIC_SRAM_RCV_RET_RCB;
8742
8743 /* Clear status block in ram. */
8744 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8745
8746 /* Set status block DMA address */
8747 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8748 ((u64) tnapi->status_mapping >> 32));
8749 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8750 ((u64) tnapi->status_mapping & 0xffffffff));
8751
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008752 if (tnapi->tx_ring) {
8753 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8754 (TG3_TX_RING_SIZE <<
8755 BDINFO_FLAGS_MAXLEN_SHIFT),
8756 NIC_SRAM_TX_BUFFER_DESC);
8757 txrcb += TG3_BDINFO_SIZE;
8758 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008759
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008760 if (tnapi->rx_rcb) {
8761 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008762 (tp->rx_ret_ring_mask + 1) <<
8763 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008764 rxrcb += TG3_BDINFO_SIZE;
8765 }
8766
8767 stblk = HOSTCC_STATBLCK_RING1;
8768
8769 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8770 u64 mapping = (u64)tnapi->status_mapping;
8771 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8772 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8773
8774 /* Clear status block in ram. */
8775 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8776
Matt Carlson19cfaec2009-12-03 08:36:20 +00008777 if (tnapi->tx_ring) {
8778 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8779 (TG3_TX_RING_SIZE <<
8780 BDINFO_FLAGS_MAXLEN_SHIFT),
8781 NIC_SRAM_TX_BUFFER_DESC);
8782 txrcb += TG3_BDINFO_SIZE;
8783 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008784
8785 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008786 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008787 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8788
8789 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008790 rxrcb += TG3_BDINFO_SIZE;
8791 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008792}
8793
Matt Carlsoneb07a942011-04-20 07:57:36 +00008794static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8795{
8796 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8797
Joe Perches63c3a662011-04-26 08:12:10 +00008798 if (!tg3_flag(tp, 5750_PLUS) ||
8799 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008800 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008801 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8802 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008803 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8804 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8806 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8807 else
8808 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8809
8810 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8811 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8812
8813 val = min(nic_rep_thresh, host_rep_thresh);
8814 tw32(RCVBDI_STD_THRESH, val);
8815
Joe Perches63c3a662011-04-26 08:12:10 +00008816 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008817 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8818
Joe Perches63c3a662011-04-26 08:12:10 +00008819 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008820 return;
8821
Matt Carlson513aa6e2011-11-21 15:01:18 +00008822 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008823
8824 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8825
8826 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8827 tw32(RCVBDI_JUMBO_THRESH, val);
8828
Joe Perches63c3a662011-04-26 08:12:10 +00008829 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008830 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8831}
8832
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008833static inline u32 calc_crc(unsigned char *buf, int len)
8834{
8835 u32 reg;
8836 u32 tmp;
8837 int j, k;
8838
8839 reg = 0xffffffff;
8840
8841 for (j = 0; j < len; j++) {
8842 reg ^= buf[j];
8843
8844 for (k = 0; k < 8; k++) {
8845 tmp = reg & 0x01;
8846
8847 reg >>= 1;
8848
8849 if (tmp)
8850 reg ^= 0xedb88320;
8851 }
8852 }
8853
8854 return ~reg;
8855}
8856
8857static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8858{
8859 /* accept or reject all multicast frames */
8860 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8861 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8862 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8863 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8864}
8865
8866static void __tg3_set_rx_mode(struct net_device *dev)
8867{
8868 struct tg3 *tp = netdev_priv(dev);
8869 u32 rx_mode;
8870
8871 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8872 RX_MODE_KEEP_VLAN_TAG);
8873
8874#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8875 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8876 * flag clear.
8877 */
8878 if (!tg3_flag(tp, ENABLE_ASF))
8879 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8880#endif
8881
8882 if (dev->flags & IFF_PROMISC) {
8883 /* Promiscuous mode. */
8884 rx_mode |= RX_MODE_PROMISC;
8885 } else if (dev->flags & IFF_ALLMULTI) {
8886 /* Accept all multicast. */
8887 tg3_set_multi(tp, 1);
8888 } else if (netdev_mc_empty(dev)) {
8889 /* Reject all multicast. */
8890 tg3_set_multi(tp, 0);
8891 } else {
8892 /* Accept one or more multicast(s). */
8893 struct netdev_hw_addr *ha;
8894 u32 mc_filter[4] = { 0, };
8895 u32 regidx;
8896 u32 bit;
8897 u32 crc;
8898
8899 netdev_for_each_mc_addr(ha, dev) {
8900 crc = calc_crc(ha->addr, ETH_ALEN);
8901 bit = ~crc & 0x7f;
8902 regidx = (bit & 0x60) >> 5;
8903 bit &= 0x1f;
8904 mc_filter[regidx] |= (1 << bit);
8905 }
8906
8907 tw32(MAC_HASH_REG_0, mc_filter[0]);
8908 tw32(MAC_HASH_REG_1, mc_filter[1]);
8909 tw32(MAC_HASH_REG_2, mc_filter[2]);
8910 tw32(MAC_HASH_REG_3, mc_filter[3]);
8911 }
8912
8913 if (rx_mode != tp->rx_mode) {
8914 tp->rx_mode = rx_mode;
8915 tw32_f(MAC_RX_MODE, rx_mode);
8916 udelay(10);
8917 }
8918}
8919
Michael Chan91024262012-09-28 07:12:38 +00008920static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00008921{
8922 int i;
8923
8924 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00008925 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00008926}
8927
8928static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008929{
8930 int i;
8931
8932 if (!tg3_flag(tp, SUPPORT_MSIX))
8933 return;
8934
Michael Chan0b3ba052012-11-14 14:44:29 +00008935 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008936 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008937 return;
8938 }
8939
8940 /* Validate table against current IRQ count */
8941 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00008942 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00008943 break;
8944 }
8945
8946 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00008947 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008948}
8949
Matt Carlson90415472011-12-16 13:33:23 +00008950static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008951{
8952 int i = 0;
8953 u32 reg = MAC_RSS_INDIR_TBL_0;
8954
8955 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8956 u32 val = tp->rss_ind_tbl[i];
8957 i++;
8958 for (; i % 8; i++) {
8959 val <<= 4;
8960 val |= tp->rss_ind_tbl[i];
8961 }
8962 tw32(reg, val);
8963 reg += 4;
8964 }
8965}
8966
Matt Carlson2d31eca2009-09-01 12:53:31 +00008967/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008968static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969{
8970 u32 val, rdmac_mode;
8971 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008972 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008973
8974 tg3_disable_ints(tp);
8975
8976 tg3_stop_fw(tp);
8977
8978 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8979
Joe Perches63c3a662011-04-26 08:12:10 +00008980 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008981 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982
Matt Carlson699c0192010-12-06 08:28:51 +00008983 /* Enable MAC control of LPI */
8984 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8985 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8986 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8987 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8988
8989 tw32_f(TG3_CPMU_EEE_CTRL,
8990 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8991
Matt Carlsona386b902010-12-06 08:28:53 +00008992 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8993 TG3_CPMU_EEEMD_LPI_IN_TX |
8994 TG3_CPMU_EEEMD_LPI_IN_RX |
8995 TG3_CPMU_EEEMD_EEE_ENABLE;
8996
8997 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8998 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8999
Joe Perches63c3a662011-04-26 08:12:10 +00009000 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00009001 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
9002
9003 tw32_f(TG3_CPMU_EEE_MODE, val);
9004
9005 tw32_f(TG3_CPMU_EEE_DBTMR1,
9006 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
9007 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
9008
9009 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00009010 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00009011 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00009012 }
9013
Matt Carlson603f1172010-02-12 14:47:10 +00009014 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08009015 tg3_phy_reset(tp);
9016
Linus Torvalds1da177e2005-04-16 15:20:36 -07009017 err = tg3_chip_reset(tp);
9018 if (err)
9019 return err;
9020
9021 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
9022
Matt Carlsonbcb37f62008-11-03 16:52:09 -08009023 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009024 val = tr32(TG3_CPMU_CTRL);
9025 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
9026 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08009027
9028 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9029 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9030 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9031 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
9032
9033 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
9034 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
9035 val |= CPMU_LNK_AWARE_MACCLK_6_25;
9036 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
9037
9038 val = tr32(TG3_CPMU_HST_ACC);
9039 val &= ~CPMU_HST_ACC_MACCLK_MASK;
9040 val |= CPMU_HST_ACC_MACCLK_6_25;
9041 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07009042 }
9043
Matt Carlson33466d932009-04-20 06:57:41 +00009044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
9045 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
9046 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
9047 PCIE_PWR_MGMT_L1_THRESH_4MS;
9048 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00009049
9050 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
9051 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
9052
9053 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00009054
Matt Carlsonf40386c2009-11-02 14:24:02 +00009055 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
9056 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00009057 }
9058
Joe Perches63c3a662011-04-26 08:12:10 +00009059 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00009060 u32 grc_mode = tr32(GRC_MODE);
9061
9062 /* Access the lower 1K of PL PCIE block registers. */
9063 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9064 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
9065
9066 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
9067 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
9068 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
9069
9070 tw32(GRC_MODE, grc_mode);
9071 }
9072
Matt Carlson55086ad2011-12-14 11:09:59 +00009073 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00009074 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
9075 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00009076
Matt Carlson5093eed2010-11-24 08:31:45 +00009077 /* Access the lower 1K of PL PCIE block registers. */
9078 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9079 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00009080
Matt Carlson5093eed2010-11-24 08:31:45 +00009081 val = tr32(TG3_PCIE_TLDLPL_PORT +
9082 TG3_PCIE_PL_LO_PHYCTL5);
9083 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
9084 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00009085
Matt Carlson5093eed2010-11-24 08:31:45 +00009086 tw32(GRC_MODE, grc_mode);
9087 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00009088
Matt Carlson1ff30a52011-05-19 12:12:46 +00009089 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
9090 u32 grc_mode = tr32(GRC_MODE);
9091
9092 /* Access the lower 1K of DL PCIE block registers. */
9093 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
9094 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
9095
9096 val = tr32(TG3_PCIE_TLDLPL_PORT +
9097 TG3_PCIE_DL_LO_FTSMAX);
9098 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
9099 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
9100 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
9101
9102 tw32(GRC_MODE, grc_mode);
9103 }
9104
Matt Carlsona977dbe2010-04-12 06:58:26 +00009105 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
9106 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
9107 val |= CPMU_LSPD_10MB_MACCLK_6_25;
9108 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00009109 }
9110
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111 /* This works around an issue with Athlon chipsets on
9112 * B3 tigon3 silicon. This bit has no effect on any
9113 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07009114 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009115 */
Joe Perches63c3a662011-04-26 08:12:10 +00009116 if (!tg3_flag(tp, CPMU_PRESENT)) {
9117 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07009118 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
9119 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
9120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121
9122 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00009123 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 val = tr32(TG3PCI_PCISTATE);
9125 val |= PCISTATE_RETRY_SAME_DMA;
9126 tw32(TG3PCI_PCISTATE, val);
9127 }
9128
Joe Perches63c3a662011-04-26 08:12:10 +00009129 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07009130 /* Allow reads and writes to the
9131 * APE register and memory space.
9132 */
9133 val = tr32(TG3PCI_PCISTATE);
9134 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00009135 PCISTATE_ALLOW_APE_SHMEM_WR |
9136 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07009137 tw32(TG3PCI_PCISTATE, val);
9138 }
9139
Linus Torvalds1da177e2005-04-16 15:20:36 -07009140 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
9141 /* Enable some hw fixes. */
9142 val = tr32(TG3PCI_MSI_DATA);
9143 val |= (1 << 26) | (1 << 28) | (1 << 29);
9144 tw32(TG3PCI_MSI_DATA, val);
9145 }
9146
9147 /* Descriptor ring init may make accesses to the
9148 * NIC SRAM area to setup the TX descriptors, so we
9149 * can only do this after the hardware has been
9150 * successfully reset.
9151 */
Michael Chan32d8c572006-07-25 16:38:29 -07009152 err = tg3_init_rings(tp);
9153 if (err)
9154 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155
Joe Perches63c3a662011-04-26 08:12:10 +00009156 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009157 val = tr32(TG3PCI_DMA_RW_CTRL) &
9158 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00009159 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
9160 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00009161 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00009162 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
9163 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00009164 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
9165 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
9166 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07009167 /* This value is determined during the probe time DMA
9168 * engine test, tg3_test_dma.
9169 */
9170 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
9171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172
9173 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
9174 GRC_MODE_4X_NIC_SEND_RINGS |
9175 GRC_MODE_NO_TX_PHDR_CSUM |
9176 GRC_MODE_NO_RX_PHDR_CSUM);
9177 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009178
9179 /* Pseudo-header checksum is done by hardware logic and not
9180 * the offload processers, so make the chip do the pseudo-
9181 * header checksums on receive. For transmit it is more
9182 * convenient to do the pseudo-header checksum in software
9183 * as Linux does that on transmit for us in all cases.
9184 */
9185 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009186
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +00009187 val = GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP;
9188 if (tp->rxptpctl)
9189 tw32(TG3_RX_PTP_CTL,
9190 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
9191
9192 if (tg3_flag(tp, PTP_CAPABLE))
9193 val |= GRC_MODE_TIME_SYNC_ENABLE;
9194
9195 tw32(GRC_MODE, tp->grc_mode | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196
9197 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9198 val = tr32(GRC_MISC_CFG);
9199 val &= ~0xff;
9200 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9201 tw32(GRC_MISC_CFG, val);
9202
9203 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009204 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205 /* Do nothing. */
9206 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
9207 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
9208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
9209 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9210 else
9211 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9212 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9213 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009214 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215 int fw_len;
9216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009217 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9219 tw32(BUFMGR_MB_POOL_ADDR,
9220 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9221 tw32(BUFMGR_MB_POOL_SIZE,
9222 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009224
Michael Chan0f893dc2005-07-25 12:30:38 -07009225 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9227 tp->bufmgr_config.mbuf_read_dma_low_water);
9228 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9229 tp->bufmgr_config.mbuf_mac_rx_low_water);
9230 tw32(BUFMGR_MB_HIGH_WATER,
9231 tp->bufmgr_config.mbuf_high_water);
9232 } else {
9233 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9234 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9235 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9236 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9237 tw32(BUFMGR_MB_HIGH_WATER,
9238 tp->bufmgr_config.mbuf_high_water_jumbo);
9239 }
9240 tw32(BUFMGR_DMA_LOW_WATER,
9241 tp->bufmgr_config.dma_low_water);
9242 tw32(BUFMGR_DMA_HIGH_WATER,
9243 tp->bufmgr_config.dma_high_water);
9244
Matt Carlsond309a462010-09-30 10:34:31 +00009245 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
9246 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
9247 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00009248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9249 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9250 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
9251 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009252 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 for (i = 0; i < 2000; i++) {
9254 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9255 break;
9256 udelay(10);
9257 }
9258 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009259 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009260 return -ENODEV;
9261 }
9262
Matt Carlsoneb07a942011-04-20 07:57:36 +00009263 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
9264 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009265
Matt Carlsoneb07a942011-04-20 07:57:36 +00009266 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
9268 /* Initialize TG3_BDINFO's at:
9269 * RCVDBDI_STD_BD: standard eth size rx ring
9270 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9271 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9272 *
9273 * like so:
9274 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9275 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9276 * ring attribute flags
9277 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9278 *
9279 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9280 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9281 *
9282 * The size of each ring is fixed in the firmware, but the location is
9283 * configurable.
9284 */
9285 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009286 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009287 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009288 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009289 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009290 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9291 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009293 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009294 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9296 BDINFO_FLAGS_DISABLED);
9297
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009298 /* Program the jumbo buffer descriptor ring control
9299 * blocks on those devices that have them.
9300 */
Matt Carlsona0512942011-07-27 14:20:54 +00009301 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009302 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303
Joe Perches63c3a662011-04-26 08:12:10 +00009304 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009306 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009308 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009309 val = TG3_RX_JMB_RING_SIZE(tp) <<
9310 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009312 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009313 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009314 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00009315 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9316 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009317 } else {
9318 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9319 BDINFO_FLAGS_DISABLED);
9320 }
9321
Joe Perches63c3a662011-04-26 08:12:10 +00009322 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009323 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009324 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9325 val |= (TG3_RX_STD_DMA_SZ << 2);
9326 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009327 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009328 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009329 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009330
9331 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332
Matt Carlson411da642009-11-13 13:03:46 +00009333 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00009334 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335
Joe Perches63c3a662011-04-26 08:12:10 +00009336 tpr->rx_jmb_prod_idx =
9337 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00009338 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339
Matt Carlson2d31eca2009-09-01 12:53:31 +00009340 tg3_rings_reset(tp);
9341
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009343 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344
9345 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009346 tw32(MAC_RX_MTU_SIZE,
9347 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009348
9349 /* The slot time is changed by tg3_setup_phy if we
9350 * run at gigabit with half duplex.
9351 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009352 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9353 (6 << TX_LENGTHS_IPG_SHIFT) |
9354 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9355
9356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9357 val |= tr32(MAC_TX_LENGTHS) &
9358 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9359 TX_LENGTHS_CNT_DWN_VAL_MSK);
9360
9361 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009362
9363 /* Receive rules. */
9364 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9365 tw32(RCVLPC_CONFIG, 0x0181);
9366
9367 /* Calculate RDMAC_MODE setting early, we need it to determine
9368 * the RCVLPC_STATE_ENABLE mask.
9369 */
9370 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9371 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9372 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9373 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9374 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009375
Matt Carlsondeabaac2010-11-24 08:31:50 +00009376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009377 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9378
Matt Carlson57e69832008-05-25 23:48:31 -07009379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009380 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9381 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009382 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9383 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9384 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9385
Matt Carlsonc5908932011-03-09 16:58:25 +00009386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9387 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009388 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07009389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9391 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009392 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9394 }
9395 }
9396
Joe Perches63c3a662011-04-26 08:12:10 +00009397 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009398 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9399
Joe Perches63c3a662011-04-26 08:12:10 +00009400 if (tg3_flag(tp, HW_TSO_1) ||
9401 tg3_flag(tp, HW_TSO_2) ||
9402 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009403 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9404
Matt Carlson108a6c12011-05-19 12:12:47 +00009405 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009406 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08009407 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9408 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409
Matt Carlsonf2096f92011-04-05 14:22:48 +00009410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9411 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9412
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009413 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9414 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9415 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9416 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009417 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009418 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Michael Chan10ce95d2012-07-29 19:15:42 +00009419 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009420 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9421 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9422 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9423 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9424 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9425 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009426 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009427 tw32(TG3_RDMA_RSRVCTRL_REG,
9428 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9429 }
9430
Matt Carlsond78b59f2011-04-05 14:22:46 +00009431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9432 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009433 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9434 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9435 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9436 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9437 }
9438
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009440 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009441 val = tr32(RCVLPC_STATS_ENABLE);
9442 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9443 tw32(RCVLPC_STATS_ENABLE, val);
9444 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009445 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 val = tr32(RCVLPC_STATS_ENABLE);
9447 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9448 tw32(RCVLPC_STATS_ENABLE, val);
9449 } else {
9450 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9451 }
9452 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9453 tw32(SNDDATAI_STATSENAB, 0xffffff);
9454 tw32(SNDDATAI_STATSCTRL,
9455 (SNDDATAI_SCTRL_ENABLE |
9456 SNDDATAI_SCTRL_FASTUPD));
9457
9458 /* Setup host coalescing engine. */
9459 tw32(HOSTCC_MODE, 0);
9460 for (i = 0; i < 2000; i++) {
9461 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9462 break;
9463 udelay(10);
9464 }
9465
Michael Chand244c892005-07-05 14:42:33 -07009466 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467
Joe Perches63c3a662011-04-26 08:12:10 +00009468 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469 /* Status/statistics block address. See tg3_timer,
9470 * the tg3_periodic_fetch_stats call there, and
9471 * tg3_get_stats to see how this works for 5705/5750 chips.
9472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9474 ((u64) tp->stats_mapping >> 32));
9475 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9476 ((u64) tp->stats_mapping & 0xffffffff));
9477 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009478
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009480
9481 /* Clear statistics and status block memory areas */
9482 for (i = NIC_SRAM_STATS_BLK;
9483 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9484 i += sizeof(u32)) {
9485 tg3_write_mem(tp, i, 0);
9486 udelay(40);
9487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009488 }
9489
9490 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9491
9492 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9493 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009494 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9496
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009497 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9498 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009499 /* reset to prevent losing 1st rx packet intermittently */
9500 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9501 udelay(10);
9502 }
9503
Matt Carlson3bda1252008-08-15 14:08:22 -07009504 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009505 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9506 MAC_MODE_FHDE_ENABLE;
9507 if (tg3_flag(tp, ENABLE_APE))
9508 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009509 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009510 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009511 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9512 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9514 udelay(40);
9515
Michael Chan314fba32005-04-21 17:07:04 -07009516 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009517 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009518 * register to preserve the GPIO settings for LOMs. The GPIOs,
9519 * whether used as inputs or outputs, are set by boot code after
9520 * reset.
9521 */
Joe Perches63c3a662011-04-26 08:12:10 +00009522 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009523 u32 gpio_mask;
9524
Michael Chan9d26e212006-12-07 00:21:14 -08009525 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9526 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9527 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009528
9529 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9530 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9531 GRC_LCLCTRL_GPIO_OUTPUT3;
9532
Michael Chanaf36e6b2006-03-23 01:28:06 -08009533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9534 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9535
Gary Zambranoaaf84462007-05-05 11:51:45 -07009536 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009537 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9538
9539 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009540 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009541 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9542 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9545 udelay(100);
9546
Matt Carlsonc3b50032012-01-17 15:27:23 +00009547 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009548 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009549 val |= MSGINT_MODE_ENABLE;
9550 if (tp->irq_cnt > 1)
9551 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009552 if (!tg3_flag(tp, 1SHOT_MSI))
9553 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009554 tw32(MSGINT_MODE, val);
9555 }
9556
Joe Perches63c3a662011-04-26 08:12:10 +00009557 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9559 udelay(40);
9560 }
9561
9562 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9563 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9564 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9565 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9566 WDMAC_MODE_LNGREAD_ENAB);
9567
Matt Carlsonc5908932011-03-09 16:58:25 +00009568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9569 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009570 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9572 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9573 /* nothing */
9574 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009575 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009576 val |= WDMAC_MODE_RX_ACCEL;
9577 }
9578 }
9579
Michael Chand9ab5ad12006-03-20 22:27:35 -08009580 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009581 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009582 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009583
Matt Carlson788a0352009-11-02 14:26:03 +00009584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9585 val |= WDMAC_MODE_BURST_ALL_DATA;
9586
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 tw32_f(WDMAC_MODE, val);
9588 udelay(40);
9589
Joe Perches63c3a662011-04-26 08:12:10 +00009590 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009591 u16 pcix_cmd;
9592
9593 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9594 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009596 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9597 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009598 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009599 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9600 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 }
Matt Carlson9974a352007-10-07 23:27:28 -07009602 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9603 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 }
9605
9606 tw32_f(RDMAC_MODE, rdmac_mode);
9607 udelay(40);
9608
Michael Chan091f0ea2012-07-29 19:15:43 +00009609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9610 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9611 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9612 break;
9613 }
9614 if (i < TG3_NUM_RDMA_CHANNELS) {
9615 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9616 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9617 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9618 tg3_flag_set(tp, 5719_RDMA_BUG);
9619 }
9620 }
9621
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009623 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009625
9626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9627 tw32(SNDDATAC_MODE,
9628 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9629 else
9630 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9631
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9633 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009634 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009635 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009636 val |= RCVDBDI_MODE_LRG_RING_SZ;
9637 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009639 if (tg3_flag(tp, HW_TSO_1) ||
9640 tg3_flag(tp, HW_TSO_2) ||
9641 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009643 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009644 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009645 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9646 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9648
9649 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9650 err = tg3_load_5701_a0_firmware_fix(tp);
9651 if (err)
9652 return err;
9653 }
9654
Joe Perches63c3a662011-04-26 08:12:10 +00009655 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 err = tg3_load_tso_firmware(tp);
9657 if (err)
9658 return err;
9659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660
9661 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009662
Joe Perches63c3a662011-04-26 08:12:10 +00009663 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9665 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009666
9667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9668 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9669 tp->tx_mode &= ~val;
9670 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9671 }
9672
Linus Torvalds1da177e2005-04-16 15:20:36 -07009673 tw32_f(MAC_TX_MODE, tp->tx_mode);
9674 udelay(100);
9675
Joe Perches63c3a662011-04-26 08:12:10 +00009676 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009677 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009678
9679 /* Setup the "secret" hash key. */
9680 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9681 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9682 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9683 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9684 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9685 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9686 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9687 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9688 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9689 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9690 }
9691
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009693 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009694 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9695
Joe Perches63c3a662011-04-26 08:12:10 +00009696 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009697 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9698 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9699 RX_MODE_RSS_IPV6_HASH_EN |
9700 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9701 RX_MODE_RSS_IPV4_HASH_EN |
9702 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9703
Linus Torvalds1da177e2005-04-16 15:20:36 -07009704 tw32_f(MAC_RX_MODE, tp->rx_mode);
9705 udelay(10);
9706
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 tw32(MAC_LED_CTRL, tp->led_ctrl);
9708
9709 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009710 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9712 udelay(10);
9713 }
9714 tw32_f(MAC_RX_MODE, tp->rx_mode);
9715 udelay(10);
9716
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009717 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009719 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720 /* Set drive transmission level to 1.2V */
9721 /* only if the signal pre-emphasis bit is not set */
9722 val = tr32(MAC_SERDES_CFG);
9723 val &= 0xfffff000;
9724 val |= 0x880;
9725 tw32(MAC_SERDES_CFG, val);
9726 }
9727 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9728 tw32(MAC_SERDES_CFG, 0x616000);
9729 }
9730
9731 /* Prevent chip from dropping frames when flow control
9732 * is enabled.
9733 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009734 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009735 val = 1;
9736 else
9737 val = 2;
9738 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739
9740 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009741 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009743 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744 }
9745
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009746 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009747 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009748 u32 tmp;
9749
9750 tmp = tr32(SERDES_RX_CTRL);
9751 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9752 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9753 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9754 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9755 }
9756
Joe Perches63c3a662011-04-26 08:12:10 +00009757 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009758 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009759 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760
Matt Carlsondd477002008-05-25 23:45:58 -07009761 err = tg3_setup_phy(tp, 0);
9762 if (err)
9763 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009765 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9766 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009767 u32 tmp;
9768
9769 /* Clear CRC stats. */
9770 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9771 tg3_writephy(tp, MII_TG3_TEST1,
9772 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009773 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775 }
9776 }
9777
9778 __tg3_set_rx_mode(tp->dev);
9779
9780 /* Initialize receive rules. */
9781 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9782 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9783 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9784 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9785
Joe Perches63c3a662011-04-26 08:12:10 +00009786 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787 limit = 8;
9788 else
9789 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009790 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 limit -= 4;
9792 switch (limit) {
9793 case 16:
9794 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9795 case 15:
9796 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9797 case 14:
9798 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9799 case 13:
9800 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9801 case 12:
9802 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9803 case 11:
9804 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9805 case 10:
9806 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9807 case 9:
9808 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9809 case 8:
9810 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9811 case 7:
9812 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9813 case 6:
9814 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9815 case 5:
9816 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9817 case 4:
9818 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9819 case 3:
9820 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9821 case 2:
9822 case 1:
9823
9824 default:
9825 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827
Joe Perches63c3a662011-04-26 08:12:10 +00009828 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009829 /* Write our heartbeat update interval to APE. */
9830 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9831 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009832
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9834
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835 return 0;
9836}
9837
9838/* Called at device open time to get the chip ready for
9839 * packet processing. Invoked with tp->lock held.
9840 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009841static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 tg3_switch_clocks(tp);
9844
9845 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9846
Matt Carlson2f751b62008-08-04 23:17:34 -07009847 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848}
9849
Michael Chanaed93e02012-07-16 16:24:02 +00009850static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9851{
9852 int i;
9853
9854 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9855 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9856
9857 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9858 off += len;
9859
9860 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9861 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9862 memset(ocir, 0, TG3_OCIR_LEN);
9863 }
9864}
9865
9866/* sysfs attributes for hwmon */
9867static ssize_t tg3_show_temp(struct device *dev,
9868 struct device_attribute *devattr, char *buf)
9869{
9870 struct pci_dev *pdev = to_pci_dev(dev);
9871 struct net_device *netdev = pci_get_drvdata(pdev);
9872 struct tg3 *tp = netdev_priv(netdev);
9873 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9874 u32 temperature;
9875
9876 spin_lock_bh(&tp->lock);
9877 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
9878 sizeof(temperature));
9879 spin_unlock_bh(&tp->lock);
9880 return sprintf(buf, "%u\n", temperature);
9881}
9882
9883
9884static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
9885 TG3_TEMP_SENSOR_OFFSET);
9886static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
9887 TG3_TEMP_CAUTION_OFFSET);
9888static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
9889 TG3_TEMP_MAX_OFFSET);
9890
9891static struct attribute *tg3_attributes[] = {
9892 &sensor_dev_attr_temp1_input.dev_attr.attr,
9893 &sensor_dev_attr_temp1_crit.dev_attr.attr,
9894 &sensor_dev_attr_temp1_max.dev_attr.attr,
9895 NULL
9896};
9897
9898static const struct attribute_group tg3_group = {
9899 .attrs = tg3_attributes,
9900};
9901
Michael Chanaed93e02012-07-16 16:24:02 +00009902static void tg3_hwmon_close(struct tg3 *tp)
9903{
Michael Chanaed93e02012-07-16 16:24:02 +00009904 if (tp->hwmon_dev) {
9905 hwmon_device_unregister(tp->hwmon_dev);
9906 tp->hwmon_dev = NULL;
9907 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
9908 }
Michael Chanaed93e02012-07-16 16:24:02 +00009909}
9910
9911static void tg3_hwmon_open(struct tg3 *tp)
9912{
Michael Chanaed93e02012-07-16 16:24:02 +00009913 int i, err;
9914 u32 size = 0;
9915 struct pci_dev *pdev = tp->pdev;
9916 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
9917
9918 tg3_sd_scan_scratchpad(tp, ocirs);
9919
9920 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
9921 if (!ocirs[i].src_data_length)
9922 continue;
9923
9924 size += ocirs[i].src_hdr_length;
9925 size += ocirs[i].src_data_length;
9926 }
9927
9928 if (!size)
9929 return;
9930
9931 /* Register hwmon sysfs hooks */
9932 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
9933 if (err) {
9934 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
9935 return;
9936 }
9937
9938 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
9939 if (IS_ERR(tp->hwmon_dev)) {
9940 tp->hwmon_dev = NULL;
9941 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
9942 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
9943 }
Michael Chanaed93e02012-07-16 16:24:02 +00009944}
9945
9946
Linus Torvalds1da177e2005-04-16 15:20:36 -07009947#define TG3_STAT_ADD32(PSTAT, REG) \
9948do { u32 __val = tr32(REG); \
9949 (PSTAT)->low += __val; \
9950 if ((PSTAT)->low < __val) \
9951 (PSTAT)->high += 1; \
9952} while (0)
9953
9954static void tg3_periodic_fetch_stats(struct tg3 *tp)
9955{
9956 struct tg3_hw_stats *sp = tp->hw_stats;
9957
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009958 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 return;
9960
9961 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9962 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9963 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9964 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9965 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9966 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9967 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9968 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9969 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9970 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9971 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9972 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9973 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +00009974 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
9975 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
9976 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
9977 u32 val;
9978
9979 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9980 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
9981 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9982 tg3_flag_clear(tp, 5719_RDMA_BUG);
9983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984
9985 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9986 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9987 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9988 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9989 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9990 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9991 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9992 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9993 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9994 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9995 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9996 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9997 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9998 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009999
10000 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +000010001 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10002 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
10003 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +000010004 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
10005 } else {
10006 u32 val = tr32(HOSTCC_FLOW_ATTN);
10007 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
10008 if (val) {
10009 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
10010 sp->rx_discards.low += val;
10011 if (sp->rx_discards.low < val)
10012 sp->rx_discards.high += 1;
10013 }
10014 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
10015 }
Michael Chan463d3052006-05-22 16:36:27 -070010016 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017}
10018
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010019static void tg3_chk_missed_msi(struct tg3 *tp)
10020{
10021 u32 i;
10022
10023 for (i = 0; i < tp->irq_cnt; i++) {
10024 struct tg3_napi *tnapi = &tp->napi[i];
10025
10026 if (tg3_has_work(tnapi)) {
10027 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
10028 tnapi->last_tx_cons == tnapi->tx_cons) {
10029 if (tnapi->chk_msi_cnt < 1) {
10030 tnapi->chk_msi_cnt++;
10031 return;
10032 }
Matt Carlson7f230732011-08-31 11:44:48 +000010033 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010034 }
10035 }
10036 tnapi->chk_msi_cnt = 0;
10037 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
10038 tnapi->last_tx_cons = tnapi->tx_cons;
10039 }
10040}
10041
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042static void tg3_timer(unsigned long __opaque)
10043{
10044 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045
Matt Carlson5b190622011-11-04 09:15:04 +000010046 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -080010047 goto restart_timer;
10048
David S. Millerf47c11e2005-06-24 20:18:35 -070010049 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010051 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000010052 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +000010053 tg3_chk_missed_msi(tp);
10054
Joe Perches63c3a662011-04-26 08:12:10 +000010055 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070010056 /* All of this garbage is because when using non-tagged
10057 * IRQ status the mailbox/status_block protocol the chip
10058 * uses with the cpu is race prone.
10059 */
Matt Carlson898a56f2009-08-28 14:02:40 +000010060 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -070010061 tw32(GRC_LOCAL_CTRL,
10062 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
10063 } else {
10064 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010065 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -070010066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067
David S. Millerfac9b832005-05-18 22:46:34 -070010068 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010069 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +000010070 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +000010071 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -070010072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 }
10074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 /* This part only runs once per second. */
10076 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010077 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -070010078 tg3_periodic_fetch_stats(tp);
10079
Matt Carlsonb0c59432011-05-19 12:12:48 +000010080 if (tp->setlpicnt && !--tp->setlpicnt)
10081 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +000010082
Joe Perches63c3a662011-04-26 08:12:10 +000010083 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 u32 mac_stat;
10085 int phy_event;
10086
10087 mac_stat = tr32(MAC_STATUS);
10088
10089 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010090 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
10092 phy_event = 1;
10093 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
10094 phy_event = 1;
10095
10096 if (phy_event)
10097 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000010098 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 u32 mac_stat = tr32(MAC_STATUS);
10100 int need_setup = 0;
10101
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010102 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
10104 need_setup = 1;
10105 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010106 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107 (mac_stat & (MAC_STATUS_PCS_SYNCED |
10108 MAC_STATUS_SIGNAL_DET))) {
10109 need_setup = 1;
10110 }
10111 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -070010112 if (!tp->serdes_counter) {
10113 tw32_f(MAC_MODE,
10114 (tp->mac_mode &
10115 ~MAC_MODE_PORT_MODE_MASK));
10116 udelay(40);
10117 tw32_f(MAC_MODE, tp->mac_mode);
10118 udelay(40);
10119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 tg3_setup_phy(tp, 0);
10121 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010122 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010123 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -070010124 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +000010125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126
10127 tp->timer_counter = tp->timer_multiplier;
10128 }
10129
Michael Chan130b8e42006-09-27 16:00:40 -070010130 /* Heartbeat is only sent once every 2 seconds.
10131 *
10132 * The heartbeat is to tell the ASF firmware that the host
10133 * driver is still alive. In the event that the OS crashes,
10134 * ASF needs to reset the hardware to free up the FIFO space
10135 * that may be filled with rx packets destined for the host.
10136 * If the FIFO is full, ASF will no longer function properly.
10137 *
10138 * Unintended resets have been reported on real time kernels
10139 * where the timer doesn't run on time. Netpoll will also have
10140 * same problem.
10141 *
10142 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
10143 * to check the ring condition when the heartbeat is expiring
10144 * before doing the reset. This will prevent most unintended
10145 * resets.
10146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +000010148 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -070010149 tg3_wait_for_event_ack(tp);
10150
Michael Chanbbadf502006-04-06 21:46:34 -070010151 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -070010152 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -070010153 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010154 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
10155 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -070010156
10157 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 }
10159 tp->asf_counter = tp->asf_multiplier;
10160 }
10161
David S. Millerf47c11e2005-06-24 20:18:35 -070010162 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163
Michael Chanf475f162006-03-27 23:20:14 -080010164restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165 tp->timer.expires = jiffies + tp->timer_offset;
10166 add_timer(&tp->timer);
10167}
10168
Bill Pemberton229b1ad2012-12-03 09:22:59 -050010169static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +000010170{
10171 if (tg3_flag(tp, TAGGED_STATUS) &&
10172 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10173 !tg3_flag(tp, 57765_CLASS))
10174 tp->timer_offset = HZ;
10175 else
10176 tp->timer_offset = HZ / 10;
10177
10178 BUG_ON(tp->timer_offset > HZ);
10179
10180 tp->timer_multiplier = (HZ / tp->timer_offset);
10181 tp->asf_multiplier = (HZ / tp->timer_offset) *
10182 TG3_FW_UPDATE_FREQ_SEC;
10183
10184 init_timer(&tp->timer);
10185 tp->timer.data = (unsigned long) tp;
10186 tp->timer.function = tg3_timer;
10187}
10188
10189static void tg3_timer_start(struct tg3 *tp)
10190{
10191 tp->asf_counter = tp->asf_multiplier;
10192 tp->timer_counter = tp->timer_multiplier;
10193
10194 tp->timer.expires = jiffies + tp->timer_offset;
10195 add_timer(&tp->timer);
10196}
10197
10198static void tg3_timer_stop(struct tg3 *tp)
10199{
10200 del_timer_sync(&tp->timer);
10201}
10202
10203/* Restart hardware after configuration changes, self-test, etc.
10204 * Invoked with tp->lock held.
10205 */
10206static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10207 __releases(tp->lock)
10208 __acquires(tp->lock)
10209{
10210 int err;
10211
10212 err = tg3_init_hw(tp, reset_phy);
10213 if (err) {
10214 netdev_err(tp->dev,
10215 "Failed to re-initialize device, aborting\n");
10216 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10217 tg3_full_unlock(tp);
10218 tg3_timer_stop(tp);
10219 tp->irq_sync = 0;
10220 tg3_napi_enable(tp);
10221 dev_close(tp->dev);
10222 tg3_full_lock(tp, 0);
10223 }
10224 return err;
10225}
10226
10227static void tg3_reset_task(struct work_struct *work)
10228{
10229 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10230 int err;
10231
10232 tg3_full_lock(tp, 0);
10233
10234 if (!netif_running(tp->dev)) {
10235 tg3_flag_clear(tp, RESET_TASK_PENDING);
10236 tg3_full_unlock(tp);
10237 return;
10238 }
10239
10240 tg3_full_unlock(tp);
10241
10242 tg3_phy_stop(tp);
10243
10244 tg3_netif_stop(tp);
10245
10246 tg3_full_lock(tp, 1);
10247
10248 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10249 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10250 tp->write32_rx_mbox = tg3_write_flush_reg32;
10251 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10252 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10253 }
10254
10255 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10256 err = tg3_init_hw(tp, 1);
10257 if (err)
10258 goto out;
10259
10260 tg3_netif_start(tp);
10261
10262out:
10263 tg3_full_unlock(tp);
10264
10265 if (!err)
10266 tg3_phy_start(tp);
10267
10268 tg3_flag_clear(tp, RESET_TASK_PENDING);
10269}
10270
Matt Carlson4f125f42009-09-01 12:55:02 +000010271static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010272{
David Howells7d12e782006-10-05 14:55:46 +010010273 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010274 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010275 char *name;
10276 struct tg3_napi *tnapi = &tp->napi[irq_num];
10277
10278 if (tp->irq_cnt == 1)
10279 name = tp->dev->name;
10280 else {
10281 name = &tnapi->irq_lbl[0];
10282 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10283 name[IFNAMSIZ-1] = 0;
10284 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010285
Joe Perches63c3a662011-04-26 08:12:10 +000010286 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010287 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010288 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010289 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010290 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010291 } else {
10292 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010293 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010294 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010295 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010296 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010297
10298 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010299}
10300
Michael Chan79381092005-04-21 17:13:59 -070010301static int tg3_test_interrupt(struct tg3 *tp)
10302{
Matt Carlson09943a12009-08-28 14:01:57 +000010303 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010304 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010305 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010306 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010307
Michael Chand4bc3922005-05-29 14:59:20 -070010308 if (!netif_running(dev))
10309 return -ENODEV;
10310
Michael Chan79381092005-04-21 17:13:59 -070010311 tg3_disable_ints(tp);
10312
Matt Carlson4f125f42009-09-01 12:55:02 +000010313 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010314
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010315 /*
10316 * Turn off MSI one shot mode. Otherwise this test has no
10317 * observable way to know whether the interrupt was delivered.
10318 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010319 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010320 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10321 tw32(MSGINT_MODE, val);
10322 }
10323
Matt Carlson4f125f42009-09-01 12:55:02 +000010324 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010325 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010326 if (err)
10327 return err;
10328
Matt Carlson898a56f2009-08-28 14:02:40 +000010329 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010330 tg3_enable_ints(tp);
10331
10332 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010333 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010334
10335 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010336 u32 int_mbox, misc_host_ctrl;
10337
Matt Carlson898a56f2009-08-28 14:02:40 +000010338 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010339 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10340
10341 if ((int_mbox != 0) ||
10342 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10343 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010344 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010345 }
10346
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010347 if (tg3_flag(tp, 57765_PLUS) &&
10348 tnapi->hw_status->status_tag != tnapi->last_tag)
10349 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10350
Michael Chan79381092005-04-21 17:13:59 -070010351 msleep(10);
10352 }
10353
10354 tg3_disable_ints(tp);
10355
Matt Carlson4f125f42009-09-01 12:55:02 +000010356 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010357
Matt Carlson4f125f42009-09-01 12:55:02 +000010358 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010359
10360 if (err)
10361 return err;
10362
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010363 if (intr_ok) {
10364 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010365 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010366 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10367 tw32(MSGINT_MODE, val);
10368 }
Michael Chan79381092005-04-21 17:13:59 -070010369 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010370 }
Michael Chan79381092005-04-21 17:13:59 -070010371
10372 return -EIO;
10373}
10374
10375/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10376 * successfully restored
10377 */
10378static int tg3_test_msi(struct tg3 *tp)
10379{
Michael Chan79381092005-04-21 17:13:59 -070010380 int err;
10381 u16 pci_cmd;
10382
Joe Perches63c3a662011-04-26 08:12:10 +000010383 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010384 return 0;
10385
10386 /* Turn off SERR reporting in case MSI terminates with Master
10387 * Abort.
10388 */
10389 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10390 pci_write_config_word(tp->pdev, PCI_COMMAND,
10391 pci_cmd & ~PCI_COMMAND_SERR);
10392
10393 err = tg3_test_interrupt(tp);
10394
10395 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10396
10397 if (!err)
10398 return 0;
10399
10400 /* other failures */
10401 if (err != -EIO)
10402 return err;
10403
10404 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010405 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10406 "to INTx mode. Please report this failure to the PCI "
10407 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010408
Matt Carlson4f125f42009-09-01 12:55:02 +000010409 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010410
Michael Chan79381092005-04-21 17:13:59 -070010411 pci_disable_msi(tp->pdev);
10412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010414 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010415
Matt Carlson4f125f42009-09-01 12:55:02 +000010416 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010417 if (err)
10418 return err;
10419
10420 /* Need to reset the chip because the MSI cycle may have terminated
10421 * with Master Abort.
10422 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010423 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010424
Michael Chan944d9802005-05-29 14:57:48 -070010425 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010426 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010427
David S. Millerf47c11e2005-06-24 20:18:35 -070010428 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010429
10430 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010431 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010432
10433 return err;
10434}
10435
Matt Carlson9e9fd122009-01-19 16:57:45 -080010436static int tg3_request_firmware(struct tg3 *tp)
10437{
10438 const __be32 *fw_data;
10439
10440 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010441 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10442 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010443 return -ENOENT;
10444 }
10445
10446 fw_data = (void *)tp->fw->data;
10447
10448 /* Firmware blob starts with version numbers, followed by
10449 * start address and _full_ length including BSS sections
10450 * (which must be longer than the actual data, of course
10451 */
10452
10453 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10454 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010455 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10456 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010457 release_firmware(tp->fw);
10458 tp->fw = NULL;
10459 return -EINVAL;
10460 }
10461
10462 /* We no longer need firmware; we have it. */
10463 tp->fw_needed = NULL;
10464 return 0;
10465}
10466
Michael Chan91024262012-09-28 07:12:38 +000010467static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010468{
Michael Chan91024262012-09-28 07:12:38 +000010469 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010470
Michael Chan91024262012-09-28 07:12:38 +000010471 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010472 /* We want as many rx rings enabled as there are cpus.
10473 * In multiqueue MSI-X mode, the first MSI-X vector
10474 * only deals with link interrupts, etc, so we add
10475 * one to the number of vectors we are requesting.
10476 */
Michael Chan91024262012-09-28 07:12:38 +000010477 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010478 }
Matt Carlson679563f2009-09-01 12:55:46 +000010479
Michael Chan91024262012-09-28 07:12:38 +000010480 return irq_cnt;
10481}
10482
10483static bool tg3_enable_msix(struct tg3 *tp)
10484{
10485 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010486 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010487
Michael Chan09681692012-09-28 07:12:42 +000010488 tp->txq_cnt = tp->txq_req;
10489 tp->rxq_cnt = tp->rxq_req;
10490 if (!tp->rxq_cnt)
10491 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010492 if (tp->rxq_cnt > tp->rxq_max)
10493 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010494
10495 /* Disable multiple TX rings by default. Simple round-robin hardware
10496 * scheduling of the TX rings can cause starvation of rings with
10497 * small packets when other rings have TSO or jumbo packets.
10498 */
10499 if (!tp->txq_req)
10500 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010501
10502 tp->irq_cnt = tg3_irq_count(tp);
10503
Matt Carlson679563f2009-09-01 12:55:46 +000010504 for (i = 0; i < tp->irq_max; i++) {
10505 msix_ent[i].entry = i;
10506 msix_ent[i].vector = 0;
10507 }
10508
10509 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010510 if (rc < 0) {
10511 return false;
10512 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010513 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10514 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010515 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10516 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010517 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010518 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010519 if (tp->txq_cnt)
10520 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010521 }
10522
10523 for (i = 0; i < tp->irq_max; i++)
10524 tp->napi[i].irq_vec = msix_ent[i].vector;
10525
Michael Chan49a359e2012-09-28 07:12:37 +000010526 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010527 pci_disable_msix(tp->pdev);
10528 return false;
10529 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010530
Michael Chan91024262012-09-28 07:12:38 +000010531 if (tp->irq_cnt == 1)
10532 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010533
Michael Chan91024262012-09-28 07:12:38 +000010534 tg3_flag_set(tp, ENABLE_RSS);
10535
10536 if (tp->txq_cnt > 1)
10537 tg3_flag_set(tp, ENABLE_TSS);
10538
10539 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010540
Matt Carlson679563f2009-09-01 12:55:46 +000010541 return true;
10542}
10543
Matt Carlson07b01732009-08-28 14:01:15 +000010544static void tg3_ints_init(struct tg3 *tp)
10545{
Joe Perches63c3a662011-04-26 08:12:10 +000010546 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10547 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010548 /* All MSI supporting chips should support tagged
10549 * status. Assert that this is the case.
10550 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010551 netdev_warn(tp->dev,
10552 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010553 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010554 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010555
Joe Perches63c3a662011-04-26 08:12:10 +000010556 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10557 tg3_flag_set(tp, USING_MSIX);
10558 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10559 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010560
Joe Perches63c3a662011-04-26 08:12:10 +000010561 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010562 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010563 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010564 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010565 if (!tg3_flag(tp, 1SHOT_MSI))
10566 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010567 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10568 }
10569defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010570 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010571 tp->irq_cnt = 1;
10572 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010573 }
10574
10575 if (tp->irq_cnt == 1) {
10576 tp->txq_cnt = 1;
10577 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010578 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010579 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010580 }
Matt Carlson07b01732009-08-28 14:01:15 +000010581}
10582
10583static void tg3_ints_fini(struct tg3 *tp)
10584{
Joe Perches63c3a662011-04-26 08:12:10 +000010585 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010586 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010587 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010588 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010589 tg3_flag_clear(tp, USING_MSI);
10590 tg3_flag_clear(tp, USING_MSIX);
10591 tg3_flag_clear(tp, ENABLE_RSS);
10592 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010593}
10594
Matt Carlsonbe947302012-12-03 19:36:57 +000010595static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10596 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010597{
Michael Chand8f4cd32012-09-28 07:12:40 +000010598 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010599 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600
Matt Carlson679563f2009-09-01 12:55:46 +000010601 /*
10602 * Setup interrupts first so we know how
10603 * many NAPI resources to allocate
10604 */
10605 tg3_ints_init(tp);
10606
Matt Carlson90415472011-12-16 13:33:23 +000010607 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010608
Linus Torvalds1da177e2005-04-16 15:20:36 -070010609 /* The placement of this call is tied
10610 * to the setup and use of Host TX descriptors.
10611 */
10612 err = tg3_alloc_consistent(tp);
10613 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010614 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010616 tg3_napi_init(tp);
10617
Matt Carlsonfed97812009-09-01 13:10:19 +000010618 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010619
Matt Carlson4f125f42009-09-01 12:55:02 +000010620 for (i = 0; i < tp->irq_cnt; i++) {
10621 struct tg3_napi *tnapi = &tp->napi[i];
10622 err = tg3_request_irq(tp, i);
10623 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010624 for (i--; i >= 0; i--) {
10625 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010626 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010627 }
10628 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010629 }
10630 }
Matt Carlson07b01732009-08-28 14:01:15 +000010631
David S. Millerf47c11e2005-06-24 20:18:35 -070010632 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633
Michael Chand8f4cd32012-09-28 07:12:40 +000010634 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010635 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010636 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010637 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010638 }
10639
David S. Millerf47c11e2005-06-24 20:18:35 -070010640 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010641
Matt Carlson07b01732009-08-28 14:01:15 +000010642 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010643 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010644
Michael Chand8f4cd32012-09-28 07:12:40 +000010645 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010646 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010647
Michael Chan79381092005-04-21 17:13:59 -070010648 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010649 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010650 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010651 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010652 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010653
Matt Carlson679563f2009-09-01 12:55:46 +000010654 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010655 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010656
Joe Perches63c3a662011-04-26 08:12:10 +000010657 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010658 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010659
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010660 tw32(PCIE_TRANSACTION_CFG,
10661 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010662 }
Michael Chan79381092005-04-21 17:13:59 -070010663 }
10664
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010665 tg3_phy_start(tp);
10666
Michael Chanaed93e02012-07-16 16:24:02 +000010667 tg3_hwmon_open(tp);
10668
David S. Millerf47c11e2005-06-24 20:18:35 -070010669 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010670
Matt Carlson21f76382012-02-22 12:35:21 +000010671 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010672 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673 tg3_enable_ints(tp);
10674
Matt Carlsonbe947302012-12-03 19:36:57 +000010675 if (init)
10676 tg3_ptp_init(tp);
10677 else
10678 tg3_ptp_resume(tp);
10679
10680
David S. Millerf47c11e2005-06-24 20:18:35 -070010681 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010683 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010684
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010685 /*
10686 * Reset loopback feature if it was turned on while the device was down
10687 * make sure that it's installed properly now.
10688 */
10689 if (dev->features & NETIF_F_LOOPBACK)
10690 tg3_set_loopback(dev, dev->features);
10691
Linus Torvalds1da177e2005-04-16 15:20:36 -070010692 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010693
Matt Carlson679563f2009-09-01 12:55:46 +000010694err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010695 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10696 struct tg3_napi *tnapi = &tp->napi[i];
10697 free_irq(tnapi->irq_vec, tnapi);
10698 }
Matt Carlson07b01732009-08-28 14:01:15 +000010699
Matt Carlson679563f2009-09-01 12:55:46 +000010700err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010701 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010702 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010703 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010704
10705err_out1:
10706 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010707
Matt Carlson07b01732009-08-28 14:01:15 +000010708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709}
10710
Michael Chan65138592012-09-28 07:12:41 +000010711static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712{
Matt Carlson4f125f42009-09-01 12:55:02 +000010713 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714
Matt Carlsondb219972011-11-04 09:15:03 +000010715 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010716 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010717
Matt Carlson21f76382012-02-22 12:35:21 +000010718 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010719
Michael Chanaed93e02012-07-16 16:24:02 +000010720 tg3_hwmon_close(tp);
10721
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010722 tg3_phy_stop(tp);
10723
David S. Millerf47c11e2005-06-24 20:18:35 -070010724 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725
10726 tg3_disable_ints(tp);
10727
Michael Chan944d9802005-05-29 14:57:48 -070010728 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010729 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010730 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010731
David S. Millerf47c11e2005-06-24 20:18:35 -070010732 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010733
Matt Carlson4f125f42009-09-01 12:55:02 +000010734 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10735 struct tg3_napi *tnapi = &tp->napi[i];
10736 free_irq(tnapi->irq_vec, tnapi);
10737 }
Matt Carlson07b01732009-08-28 14:01:15 +000010738
10739 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010740
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010741 tg3_napi_fini(tp);
10742
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010744}
10745
Michael Chand8f4cd32012-09-28 07:12:40 +000010746static int tg3_open(struct net_device *dev)
10747{
10748 struct tg3 *tp = netdev_priv(dev);
10749 int err;
10750
10751 if (tp->fw_needed) {
10752 err = tg3_request_firmware(tp);
10753 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10754 if (err)
10755 return err;
10756 } else if (err) {
10757 netdev_warn(tp->dev, "TSO capability disabled\n");
10758 tg3_flag_clear(tp, TSO_CAPABLE);
10759 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10760 netdev_notice(tp->dev, "TSO capability restored\n");
10761 tg3_flag_set(tp, TSO_CAPABLE);
10762 }
10763 }
10764
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010765 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010766
10767 err = tg3_power_up(tp);
10768 if (err)
10769 return err;
10770
10771 tg3_full_lock(tp, 0);
10772
10773 tg3_disable_ints(tp);
10774 tg3_flag_clear(tp, INIT_COMPLETE);
10775
10776 tg3_full_unlock(tp);
10777
Matt Carlsonbe947302012-12-03 19:36:57 +000010778 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010779 if (err) {
10780 tg3_frob_aux_power(tp, false);
10781 pci_set_power_state(tp->pdev, PCI_D3hot);
10782 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010783
Matt Carlson7d41e492012-12-03 19:36:58 +000010784 if (tg3_flag(tp, PTP_CAPABLE)) {
10785 tp->ptp_clock = ptp_clock_register(&tp->ptp_info,
10786 &tp->pdev->dev);
10787 if (IS_ERR(tp->ptp_clock))
10788 tp->ptp_clock = NULL;
10789 }
10790
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791 return err;
10792}
10793
10794static int tg3_close(struct net_device *dev)
10795{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010796 struct tg3 *tp = netdev_priv(dev);
10797
Matt Carlsonbe947302012-12-03 19:36:57 +000010798 tg3_ptp_fini(tp);
10799
Michael Chan65138592012-09-28 07:12:41 +000010800 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010801
10802 /* Clear stats across close / open calls */
10803 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10804 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805
10806 tg3_power_down(tp);
10807
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010808 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809
10810 return 0;
10811}
10812
10813static inline u64 get_stat64(tg3_stat64_t *val)
10814{
10815 return ((u64)val->high << 32) | ((u64)val->low);
10816}
10817
10818static u64 tg3_calc_crc_errors(struct tg3 *tp)
10819{
10820 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10821
10822 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10823 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10824 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
10825 u32 val;
10826
10827 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10828 tg3_writephy(tp, MII_TG3_TEST1,
10829 val | MII_TG3_TEST1_CRC_EN);
10830 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10831 } else
10832 val = 0;
10833
10834 tp->phy_crc_errors += val;
10835
10836 return tp->phy_crc_errors;
10837 }
10838
10839 return get_stat64(&hw_stats->rx_fcs_errors);
10840}
10841
10842#define ESTAT_ADD(member) \
10843 estats->member = old_estats->member + \
10844 get_stat64(&hw_stats->member)
10845
10846static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10847{
10848 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10849 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10850
10851 ESTAT_ADD(rx_octets);
10852 ESTAT_ADD(rx_fragments);
10853 ESTAT_ADD(rx_ucast_packets);
10854 ESTAT_ADD(rx_mcast_packets);
10855 ESTAT_ADD(rx_bcast_packets);
10856 ESTAT_ADD(rx_fcs_errors);
10857 ESTAT_ADD(rx_align_errors);
10858 ESTAT_ADD(rx_xon_pause_rcvd);
10859 ESTAT_ADD(rx_xoff_pause_rcvd);
10860 ESTAT_ADD(rx_mac_ctrl_rcvd);
10861 ESTAT_ADD(rx_xoff_entered);
10862 ESTAT_ADD(rx_frame_too_long_errors);
10863 ESTAT_ADD(rx_jabbers);
10864 ESTAT_ADD(rx_undersize_packets);
10865 ESTAT_ADD(rx_in_length_errors);
10866 ESTAT_ADD(rx_out_length_errors);
10867 ESTAT_ADD(rx_64_or_less_octet_packets);
10868 ESTAT_ADD(rx_65_to_127_octet_packets);
10869 ESTAT_ADD(rx_128_to_255_octet_packets);
10870 ESTAT_ADD(rx_256_to_511_octet_packets);
10871 ESTAT_ADD(rx_512_to_1023_octet_packets);
10872 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10873 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10874 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10875 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10876 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10877
10878 ESTAT_ADD(tx_octets);
10879 ESTAT_ADD(tx_collisions);
10880 ESTAT_ADD(tx_xon_sent);
10881 ESTAT_ADD(tx_xoff_sent);
10882 ESTAT_ADD(tx_flow_control);
10883 ESTAT_ADD(tx_mac_errors);
10884 ESTAT_ADD(tx_single_collisions);
10885 ESTAT_ADD(tx_mult_collisions);
10886 ESTAT_ADD(tx_deferred);
10887 ESTAT_ADD(tx_excessive_collisions);
10888 ESTAT_ADD(tx_late_collisions);
10889 ESTAT_ADD(tx_collide_2times);
10890 ESTAT_ADD(tx_collide_3times);
10891 ESTAT_ADD(tx_collide_4times);
10892 ESTAT_ADD(tx_collide_5times);
10893 ESTAT_ADD(tx_collide_6times);
10894 ESTAT_ADD(tx_collide_7times);
10895 ESTAT_ADD(tx_collide_8times);
10896 ESTAT_ADD(tx_collide_9times);
10897 ESTAT_ADD(tx_collide_10times);
10898 ESTAT_ADD(tx_collide_11times);
10899 ESTAT_ADD(tx_collide_12times);
10900 ESTAT_ADD(tx_collide_13times);
10901 ESTAT_ADD(tx_collide_14times);
10902 ESTAT_ADD(tx_collide_15times);
10903 ESTAT_ADD(tx_ucast_packets);
10904 ESTAT_ADD(tx_mcast_packets);
10905 ESTAT_ADD(tx_bcast_packets);
10906 ESTAT_ADD(tx_carrier_sense_errors);
10907 ESTAT_ADD(tx_discards);
10908 ESTAT_ADD(tx_errors);
10909
10910 ESTAT_ADD(dma_writeq_full);
10911 ESTAT_ADD(dma_write_prioq_full);
10912 ESTAT_ADD(rxbds_empty);
10913 ESTAT_ADD(rx_discards);
10914 ESTAT_ADD(rx_errors);
10915 ESTAT_ADD(rx_threshold_hit);
10916
10917 ESTAT_ADD(dma_readq_full);
10918 ESTAT_ADD(dma_read_prioq_full);
10919 ESTAT_ADD(tx_comp_queue_full);
10920
10921 ESTAT_ADD(ring_set_send_prod_index);
10922 ESTAT_ADD(ring_status_update);
10923 ESTAT_ADD(nic_irqs);
10924 ESTAT_ADD(nic_avoided_irqs);
10925 ESTAT_ADD(nic_tx_threshold_hit);
10926
Matt Carlson4452d092011-05-19 12:12:51 +000010927 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010928}
10929
Matt Carlson65ec6982012-02-28 23:33:37 +000010930static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010931{
Eric Dumazet511d2222010-07-07 20:44:24 +000010932 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010933 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10934
Linus Torvalds1da177e2005-04-16 15:20:36 -070010935 stats->rx_packets = old_stats->rx_packets +
10936 get_stat64(&hw_stats->rx_ucast_packets) +
10937 get_stat64(&hw_stats->rx_mcast_packets) +
10938 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010939
Linus Torvalds1da177e2005-04-16 15:20:36 -070010940 stats->tx_packets = old_stats->tx_packets +
10941 get_stat64(&hw_stats->tx_ucast_packets) +
10942 get_stat64(&hw_stats->tx_mcast_packets) +
10943 get_stat64(&hw_stats->tx_bcast_packets);
10944
10945 stats->rx_bytes = old_stats->rx_bytes +
10946 get_stat64(&hw_stats->rx_octets);
10947 stats->tx_bytes = old_stats->tx_bytes +
10948 get_stat64(&hw_stats->tx_octets);
10949
10950 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010951 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010952 stats->tx_errors = old_stats->tx_errors +
10953 get_stat64(&hw_stats->tx_errors) +
10954 get_stat64(&hw_stats->tx_mac_errors) +
10955 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10956 get_stat64(&hw_stats->tx_discards);
10957
10958 stats->multicast = old_stats->multicast +
10959 get_stat64(&hw_stats->rx_mcast_packets);
10960 stats->collisions = old_stats->collisions +
10961 get_stat64(&hw_stats->tx_collisions);
10962
10963 stats->rx_length_errors = old_stats->rx_length_errors +
10964 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10965 get_stat64(&hw_stats->rx_undersize_packets);
10966
10967 stats->rx_over_errors = old_stats->rx_over_errors +
10968 get_stat64(&hw_stats->rxbds_empty);
10969 stats->rx_frame_errors = old_stats->rx_frame_errors +
10970 get_stat64(&hw_stats->rx_align_errors);
10971 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10972 get_stat64(&hw_stats->tx_discards);
10973 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10974 get_stat64(&hw_stats->tx_carrier_sense_errors);
10975
10976 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010977 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010978
John W. Linville4f63b872005-09-12 14:43:18 -070010979 stats->rx_missed_errors = old_stats->rx_missed_errors +
10980 get_stat64(&hw_stats->rx_discards);
10981
Eric Dumazetb0057c52010-10-10 19:55:52 +000010982 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010983 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010984}
10985
Linus Torvalds1da177e2005-04-16 15:20:36 -070010986static int tg3_get_regs_len(struct net_device *dev)
10987{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010988 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010989}
10990
10991static void tg3_get_regs(struct net_device *dev,
10992 struct ethtool_regs *regs, void *_p)
10993{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010994 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995
10996 regs->version = 0;
10997
Matt Carlson97bd8e42011-04-13 11:05:04 +000010998 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010999
Matt Carlson80096062010-08-02 11:26:06 +000011000 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011001 return;
11002
David S. Millerf47c11e2005-06-24 20:18:35 -070011003 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011004
Matt Carlson97bd8e42011-04-13 11:05:04 +000011005 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011006
David S. Millerf47c11e2005-06-24 20:18:35 -070011007 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011008}
11009
11010static int tg3_get_eeprom_len(struct net_device *dev)
11011{
11012 struct tg3 *tp = netdev_priv(dev);
11013
11014 return tp->nvram_size;
11015}
11016
Linus Torvalds1da177e2005-04-16 15:20:36 -070011017static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11018{
11019 struct tg3 *tp = netdev_priv(dev);
11020 int ret;
11021 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080011022 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011023 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011024
Joe Perches63c3a662011-04-26 08:12:10 +000011025 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011026 return -EINVAL;
11027
Matt Carlson80096062010-08-02 11:26:06 +000011028 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011029 return -EAGAIN;
11030
Linus Torvalds1da177e2005-04-16 15:20:36 -070011031 offset = eeprom->offset;
11032 len = eeprom->len;
11033 eeprom->len = 0;
11034
11035 eeprom->magic = TG3_EEPROM_MAGIC;
11036
11037 if (offset & 3) {
11038 /* adjustments to start on required 4 byte boundary */
11039 b_offset = offset & 3;
11040 b_count = 4 - b_offset;
11041 if (b_count > len) {
11042 /* i.e. offset=1 len=2 */
11043 b_count = len;
11044 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000011045 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011046 if (ret)
11047 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000011048 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011049 len -= b_count;
11050 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011051 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011052 }
11053
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011054 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011055 pd = &data[eeprom->len];
11056 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011057 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058 if (ret) {
11059 eeprom->len += i;
11060 return ret;
11061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011062 memcpy(pd + i, &val, 4);
11063 }
11064 eeprom->len += i;
11065
11066 if (len & 3) {
11067 /* read last bytes not ending on 4 byte boundary */
11068 pd = &data[eeprom->len];
11069 b_count = len & 3;
11070 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011071 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011072 if (ret)
11073 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011074 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011075 eeprom->len += b_count;
11076 }
11077 return 0;
11078}
11079
Linus Torvalds1da177e2005-04-16 15:20:36 -070011080static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
11081{
11082 struct tg3 *tp = netdev_priv(dev);
11083 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080011084 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011085 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011086 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011087
Matt Carlson80096062010-08-02 11:26:06 +000011088 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080011089 return -EAGAIN;
11090
Joe Perches63c3a662011-04-26 08:12:10 +000011091 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000011092 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011093 return -EINVAL;
11094
11095 offset = eeprom->offset;
11096 len = eeprom->len;
11097
11098 if ((b_offset = (offset & 3))) {
11099 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011100 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011101 if (ret)
11102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011103 len += b_offset;
11104 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070011105 if (len < 4)
11106 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011107 }
11108
11109 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070011110 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011111 /* adjustments to end on required 4 byte boundary */
11112 odd_len = 1;
11113 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011114 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011115 if (ret)
11116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011117 }
11118
11119 buf = data;
11120 if (b_offset || odd_len) {
11121 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011122 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011123 return -ENOMEM;
11124 if (b_offset)
11125 memcpy(buf, &start, 4);
11126 if (odd_len)
11127 memcpy(buf+len-4, &end, 4);
11128 memcpy(buf + b_offset, data, eeprom->len);
11129 }
11130
11131 ret = tg3_nvram_write_block(tp, offset, len, buf);
11132
11133 if (buf != data)
11134 kfree(buf);
11135
11136 return ret;
11137}
11138
11139static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11140{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011141 struct tg3 *tp = netdev_priv(dev);
11142
Joe Perches63c3a662011-04-26 08:12:10 +000011143 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011144 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011145 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011146 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011147 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11148 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011149 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011150
Linus Torvalds1da177e2005-04-16 15:20:36 -070011151 cmd->supported = (SUPPORTED_Autoneg);
11152
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011153 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011154 cmd->supported |= (SUPPORTED_1000baseT_Half |
11155 SUPPORTED_1000baseT_Full);
11156
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011157 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011158 cmd->supported |= (SUPPORTED_100baseT_Half |
11159 SUPPORTED_100baseT_Full |
11160 SUPPORTED_10baseT_Half |
11161 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080011162 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070011163 cmd->port = PORT_TP;
11164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011165 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070011166 cmd->port = PORT_FIBRE;
11167 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011168
Linus Torvalds1da177e2005-04-16 15:20:36 -070011169 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000011170 if (tg3_flag(tp, PAUSE_AUTONEG)) {
11171 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
11172 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11173 cmd->advertising |= ADVERTISED_Pause;
11174 } else {
11175 cmd->advertising |= ADVERTISED_Pause |
11176 ADVERTISED_Asym_Pause;
11177 }
11178 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
11179 cmd->advertising |= ADVERTISED_Asym_Pause;
11180 }
11181 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011182 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000011183 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011184 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000011185 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011186 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
11187 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
11188 cmd->eth_tp_mdix = ETH_TP_MDI_X;
11189 else
11190 cmd->eth_tp_mdix = ETH_TP_MDI;
11191 }
Matt Carlson64c22182010-10-14 10:37:44 +000011192 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011193 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11194 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011195 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011196 }
Matt Carlson882e9792009-09-01 13:21:36 +000011197 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011198 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011199 cmd->autoneg = tp->link_config.autoneg;
11200 cmd->maxtxpkt = 0;
11201 cmd->maxrxpkt = 0;
11202 return 0;
11203}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011204
Linus Torvalds1da177e2005-04-16 15:20:36 -070011205static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11206{
11207 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011208 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011209
Joe Perches63c3a662011-04-26 08:12:10 +000011210 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011211 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011212 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011213 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011214 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11215 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011216 }
11217
Matt Carlson7e5856b2009-02-25 14:23:01 +000011218 if (cmd->autoneg != AUTONEG_ENABLE &&
11219 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011220 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011221
11222 if (cmd->autoneg == AUTONEG_DISABLE &&
11223 cmd->duplex != DUPLEX_FULL &&
11224 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011225 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011226
Matt Carlson7e5856b2009-02-25 14:23:01 +000011227 if (cmd->autoneg == AUTONEG_ENABLE) {
11228 u32 mask = ADVERTISED_Autoneg |
11229 ADVERTISED_Pause |
11230 ADVERTISED_Asym_Pause;
11231
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011232 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011233 mask |= ADVERTISED_1000baseT_Half |
11234 ADVERTISED_1000baseT_Full;
11235
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011236 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011237 mask |= ADVERTISED_100baseT_Half |
11238 ADVERTISED_100baseT_Full |
11239 ADVERTISED_10baseT_Half |
11240 ADVERTISED_10baseT_Full |
11241 ADVERTISED_TP;
11242 else
11243 mask |= ADVERTISED_FIBRE;
11244
11245 if (cmd->advertising & ~mask)
11246 return -EINVAL;
11247
11248 mask &= (ADVERTISED_1000baseT_Half |
11249 ADVERTISED_1000baseT_Full |
11250 ADVERTISED_100baseT_Half |
11251 ADVERTISED_100baseT_Full |
11252 ADVERTISED_10baseT_Half |
11253 ADVERTISED_10baseT_Full);
11254
11255 cmd->advertising &= mask;
11256 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011257 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011258 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011259 return -EINVAL;
11260
11261 if (cmd->duplex != DUPLEX_FULL)
11262 return -EINVAL;
11263 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011264 if (speed != SPEED_100 &&
11265 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011266 return -EINVAL;
11267 }
11268 }
11269
David S. Millerf47c11e2005-06-24 20:18:35 -070011270 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011271
11272 tp->link_config.autoneg = cmd->autoneg;
11273 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011274 tp->link_config.advertising = (cmd->advertising |
11275 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011276 tp->link_config.speed = SPEED_UNKNOWN;
11277 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011278 } else {
11279 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011280 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011281 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011282 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011283
Linus Torvalds1da177e2005-04-16 15:20:36 -070011284 if (netif_running(dev))
11285 tg3_setup_phy(tp, 1);
11286
David S. Millerf47c11e2005-06-24 20:18:35 -070011287 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011288
Linus Torvalds1da177e2005-04-16 15:20:36 -070011289 return 0;
11290}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011291
Linus Torvalds1da177e2005-04-16 15:20:36 -070011292static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11293{
11294 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011295
Rick Jones68aad782011-11-07 13:29:27 +000011296 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11297 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11298 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11299 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011300}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011301
Linus Torvalds1da177e2005-04-16 15:20:36 -070011302static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11303{
11304 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011305
Joe Perches63c3a662011-04-26 08:12:10 +000011306 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011307 wol->supported = WAKE_MAGIC;
11308 else
11309 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011310 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011311 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011312 wol->wolopts = WAKE_MAGIC;
11313 memset(&wol->sopass, 0, sizeof(wol->sopass));
11314}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011315
Linus Torvalds1da177e2005-04-16 15:20:36 -070011316static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11317{
11318 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011319 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011320
Linus Torvalds1da177e2005-04-16 15:20:36 -070011321 if (wol->wolopts & ~WAKE_MAGIC)
11322 return -EINVAL;
11323 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011324 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011325 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011326
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011327 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11328
David S. Millerf47c11e2005-06-24 20:18:35 -070011329 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011330 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011331 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011332 else
Joe Perches63c3a662011-04-26 08:12:10 +000011333 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011334 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011335
Linus Torvalds1da177e2005-04-16 15:20:36 -070011336 return 0;
11337}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011338
Linus Torvalds1da177e2005-04-16 15:20:36 -070011339static u32 tg3_get_msglevel(struct net_device *dev)
11340{
11341 struct tg3 *tp = netdev_priv(dev);
11342 return tp->msg_enable;
11343}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011344
Linus Torvalds1da177e2005-04-16 15:20:36 -070011345static void tg3_set_msglevel(struct net_device *dev, u32 value)
11346{
11347 struct tg3 *tp = netdev_priv(dev);
11348 tp->msg_enable = value;
11349}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011350
Linus Torvalds1da177e2005-04-16 15:20:36 -070011351static int tg3_nway_reset(struct net_device *dev)
11352{
11353 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011354 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011355
Linus Torvalds1da177e2005-04-16 15:20:36 -070011356 if (!netif_running(dev))
11357 return -EAGAIN;
11358
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011359 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011360 return -EINVAL;
11361
Joe Perches63c3a662011-04-26 08:12:10 +000011362 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011363 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011364 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011365 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011366 } else {
11367 u32 bmcr;
11368
11369 spin_lock_bh(&tp->lock);
11370 r = -EINVAL;
11371 tg3_readphy(tp, MII_BMCR, &bmcr);
11372 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11373 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011374 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011375 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11376 BMCR_ANENABLE);
11377 r = 0;
11378 }
11379 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011380 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011381
Linus Torvalds1da177e2005-04-16 15:20:36 -070011382 return r;
11383}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011384
Linus Torvalds1da177e2005-04-16 15:20:36 -070011385static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11386{
11387 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011388
Matt Carlson2c49a442010-09-30 10:34:35 +000011389 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011390 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011391 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011392 else
11393 ering->rx_jumbo_max_pending = 0;
11394
11395 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011396
11397 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011398 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011399 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11400 else
11401 ering->rx_jumbo_pending = 0;
11402
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011403 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011404}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011405
Linus Torvalds1da177e2005-04-16 15:20:36 -070011406static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11407{
11408 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011409 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011410
Matt Carlson2c49a442010-09-30 10:34:35 +000011411 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11412 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011413 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11414 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011415 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011416 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011417 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011418
Michael Chanbbe832c2005-06-24 20:20:04 -070011419 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011420 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011421 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011422 irq_sync = 1;
11423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011424
Michael Chanbbe832c2005-06-24 20:20:04 -070011425 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011426
Linus Torvalds1da177e2005-04-16 15:20:36 -070011427 tp->rx_pending = ering->rx_pending;
11428
Joe Perches63c3a662011-04-26 08:12:10 +000011429 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011430 tp->rx_pending > 63)
11431 tp->rx_pending = 63;
11432 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011433
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011434 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011435 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011436
11437 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011438 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011439 err = tg3_restart_hw(tp, 1);
11440 if (!err)
11441 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011442 }
11443
David S. Millerf47c11e2005-06-24 20:18:35 -070011444 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011445
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011446 if (irq_sync && !err)
11447 tg3_phy_start(tp);
11448
Michael Chanb9ec6c12006-07-25 16:37:27 -070011449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011450}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011451
Linus Torvalds1da177e2005-04-16 15:20:36 -070011452static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11453{
11454 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011455
Joe Perches63c3a662011-04-26 08:12:10 +000011456 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011457
Matt Carlson4a2db502011-12-08 14:40:17 +000011458 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011459 epause->rx_pause = 1;
11460 else
11461 epause->rx_pause = 0;
11462
Matt Carlson4a2db502011-12-08 14:40:17 +000011463 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011464 epause->tx_pause = 1;
11465 else
11466 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011467}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011468
Linus Torvalds1da177e2005-04-16 15:20:36 -070011469static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11470{
11471 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011472 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011473
Joe Perches63c3a662011-04-26 08:12:10 +000011474 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011475 u32 newadv;
11476 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011477
Matt Carlson27121682010-02-17 15:16:57 +000011478 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011479
Matt Carlson27121682010-02-17 15:16:57 +000011480 if (!(phydev->supported & SUPPORTED_Pause) ||
11481 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011482 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011483 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011484
Matt Carlson27121682010-02-17 15:16:57 +000011485 tp->link_config.flowctrl = 0;
11486 if (epause->rx_pause) {
11487 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011488
Matt Carlson27121682010-02-17 15:16:57 +000011489 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011490 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011491 newadv = ADVERTISED_Pause;
11492 } else
11493 newadv = ADVERTISED_Pause |
11494 ADVERTISED_Asym_Pause;
11495 } else if (epause->tx_pause) {
11496 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11497 newadv = ADVERTISED_Asym_Pause;
11498 } else
11499 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011500
Matt Carlson27121682010-02-17 15:16:57 +000011501 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011502 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011503 else
Joe Perches63c3a662011-04-26 08:12:10 +000011504 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011505
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011506 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011507 u32 oldadv = phydev->advertising &
11508 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11509 if (oldadv != newadv) {
11510 phydev->advertising &=
11511 ~(ADVERTISED_Pause |
11512 ADVERTISED_Asym_Pause);
11513 phydev->advertising |= newadv;
11514 if (phydev->autoneg) {
11515 /*
11516 * Always renegotiate the link to
11517 * inform our link partner of our
11518 * flow control settings, even if the
11519 * flow control is forced. Let
11520 * tg3_adjust_link() do the final
11521 * flow control setup.
11522 */
11523 return phy_start_aneg(phydev);
11524 }
11525 }
11526
11527 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011528 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011529 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011530 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011531 ~(ADVERTISED_Pause |
11532 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011533 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011534 }
11535 } else {
11536 int irq_sync = 0;
11537
11538 if (netif_running(dev)) {
11539 tg3_netif_stop(tp);
11540 irq_sync = 1;
11541 }
11542
11543 tg3_full_lock(tp, irq_sync);
11544
11545 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011546 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011547 else
Joe Perches63c3a662011-04-26 08:12:10 +000011548 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011549 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011550 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011551 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011552 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011553 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011554 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011555 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011556 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011557
11558 if (netif_running(dev)) {
11559 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11560 err = tg3_restart_hw(tp, 1);
11561 if (!err)
11562 tg3_netif_start(tp);
11563 }
11564
11565 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011567
Michael Chanb9ec6c12006-07-25 16:37:27 -070011568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011569}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011570
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011571static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011572{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011573 switch (sset) {
11574 case ETH_SS_TEST:
11575 return TG3_NUM_TEST;
11576 case ETH_SS_STATS:
11577 return TG3_NUM_STATS;
11578 default:
11579 return -EOPNOTSUPP;
11580 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011581}
11582
Matt Carlson90415472011-12-16 13:33:23 +000011583static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11584 u32 *rules __always_unused)
11585{
11586 struct tg3 *tp = netdev_priv(dev);
11587
11588 if (!tg3_flag(tp, SUPPORT_MSIX))
11589 return -EOPNOTSUPP;
11590
11591 switch (info->cmd) {
11592 case ETHTOOL_GRXRINGS:
11593 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011594 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011595 else {
11596 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011597 if (info->data > TG3_RSS_MAX_NUM_QS)
11598 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011599 }
11600
11601 /* The first interrupt vector only
11602 * handles link interrupts.
11603 */
11604 info->data -= 1;
11605 return 0;
11606
11607 default:
11608 return -EOPNOTSUPP;
11609 }
11610}
11611
11612static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11613{
11614 u32 size = 0;
11615 struct tg3 *tp = netdev_priv(dev);
11616
11617 if (tg3_flag(tp, SUPPORT_MSIX))
11618 size = TG3_RSS_INDIR_TBL_SIZE;
11619
11620 return size;
11621}
11622
11623static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11624{
11625 struct tg3 *tp = netdev_priv(dev);
11626 int i;
11627
11628 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11629 indir[i] = tp->rss_ind_tbl[i];
11630
11631 return 0;
11632}
11633
11634static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11635{
11636 struct tg3 *tp = netdev_priv(dev);
11637 size_t i;
11638
11639 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11640 tp->rss_ind_tbl[i] = indir[i];
11641
11642 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11643 return 0;
11644
11645 /* It is legal to write the indirection
11646 * table while the device is running.
11647 */
11648 tg3_full_lock(tp, 0);
11649 tg3_rss_write_indir_tbl(tp);
11650 tg3_full_unlock(tp);
11651
11652 return 0;
11653}
11654
Michael Chan09681692012-09-28 07:12:42 +000011655static void tg3_get_channels(struct net_device *dev,
11656 struct ethtool_channels *channel)
11657{
11658 struct tg3 *tp = netdev_priv(dev);
11659 u32 deflt_qs = netif_get_num_default_rss_queues();
11660
11661 channel->max_rx = tp->rxq_max;
11662 channel->max_tx = tp->txq_max;
11663
11664 if (netif_running(dev)) {
11665 channel->rx_count = tp->rxq_cnt;
11666 channel->tx_count = tp->txq_cnt;
11667 } else {
11668 if (tp->rxq_req)
11669 channel->rx_count = tp->rxq_req;
11670 else
11671 channel->rx_count = min(deflt_qs, tp->rxq_max);
11672
11673 if (tp->txq_req)
11674 channel->tx_count = tp->txq_req;
11675 else
11676 channel->tx_count = min(deflt_qs, tp->txq_max);
11677 }
11678}
11679
11680static int tg3_set_channels(struct net_device *dev,
11681 struct ethtool_channels *channel)
11682{
11683 struct tg3 *tp = netdev_priv(dev);
11684
11685 if (!tg3_flag(tp, SUPPORT_MSIX))
11686 return -EOPNOTSUPP;
11687
11688 if (channel->rx_count > tp->rxq_max ||
11689 channel->tx_count > tp->txq_max)
11690 return -EINVAL;
11691
11692 tp->rxq_req = channel->rx_count;
11693 tp->txq_req = channel->tx_count;
11694
11695 if (!netif_running(dev))
11696 return 0;
11697
11698 tg3_stop(tp);
11699
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011700 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011701
Matt Carlsonbe947302012-12-03 19:36:57 +000011702 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011703
11704 return 0;
11705}
11706
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011707static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011708{
11709 switch (stringset) {
11710 case ETH_SS_STATS:
11711 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11712 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011713 case ETH_SS_TEST:
11714 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011716 default:
11717 WARN_ON(1); /* we need a WARN() */
11718 break;
11719 }
11720}
11721
stephen hemminger81b87092011-04-04 08:43:50 +000011722static int tg3_set_phys_id(struct net_device *dev,
11723 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011724{
11725 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011726
11727 if (!netif_running(tp->dev))
11728 return -EAGAIN;
11729
stephen hemminger81b87092011-04-04 08:43:50 +000011730 switch (state) {
11731 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011732 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011733
stephen hemminger81b87092011-04-04 08:43:50 +000011734 case ETHTOOL_ID_ON:
11735 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11736 LED_CTRL_1000MBPS_ON |
11737 LED_CTRL_100MBPS_ON |
11738 LED_CTRL_10MBPS_ON |
11739 LED_CTRL_TRAFFIC_OVERRIDE |
11740 LED_CTRL_TRAFFIC_BLINK |
11741 LED_CTRL_TRAFFIC_LED);
11742 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011743
stephen hemminger81b87092011-04-04 08:43:50 +000011744 case ETHTOOL_ID_OFF:
11745 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11746 LED_CTRL_TRAFFIC_OVERRIDE);
11747 break;
Michael Chan4009a932005-09-05 17:52:54 -070011748
stephen hemminger81b87092011-04-04 08:43:50 +000011749 case ETHTOOL_ID_INACTIVE:
11750 tw32(MAC_LED_CTRL, tp->led_ctrl);
11751 break;
Michael Chan4009a932005-09-05 17:52:54 -070011752 }
stephen hemminger81b87092011-04-04 08:43:50 +000011753
Michael Chan4009a932005-09-05 17:52:54 -070011754 return 0;
11755}
11756
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011757static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011758 struct ethtool_stats *estats, u64 *tmp_stats)
11759{
11760 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011761
Matt Carlsonb546e462012-02-13 15:20:09 +000011762 if (tp->hw_stats)
11763 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11764 else
11765 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011766}
11767
Matt Carlson535a4902011-07-20 10:20:56 +000011768static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011769{
11770 int i;
11771 __be32 *buf;
11772 u32 offset = 0, len = 0;
11773 u32 magic, val;
11774
Joe Perches63c3a662011-04-26 08:12:10 +000011775 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011776 return NULL;
11777
11778 if (magic == TG3_EEPROM_MAGIC) {
11779 for (offset = TG3_NVM_DIR_START;
11780 offset < TG3_NVM_DIR_END;
11781 offset += TG3_NVM_DIRENT_SIZE) {
11782 if (tg3_nvram_read(tp, offset, &val))
11783 return NULL;
11784
11785 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11786 TG3_NVM_DIRTYPE_EXTVPD)
11787 break;
11788 }
11789
11790 if (offset != TG3_NVM_DIR_END) {
11791 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11792 if (tg3_nvram_read(tp, offset + 4, &offset))
11793 return NULL;
11794
11795 offset = tg3_nvram_logical_addr(tp, offset);
11796 }
11797 }
11798
11799 if (!offset || !len) {
11800 offset = TG3_NVM_VPD_OFF;
11801 len = TG3_NVM_VPD_LEN;
11802 }
11803
11804 buf = kmalloc(len, GFP_KERNEL);
11805 if (buf == NULL)
11806 return NULL;
11807
11808 if (magic == TG3_EEPROM_MAGIC) {
11809 for (i = 0; i < len; i += 4) {
11810 /* The data is in little-endian format in NVRAM.
11811 * Use the big-endian read routines to preserve
11812 * the byte order as it exists in NVRAM.
11813 */
11814 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11815 goto error;
11816 }
11817 } else {
11818 u8 *ptr;
11819 ssize_t cnt;
11820 unsigned int pos = 0;
11821
11822 ptr = (u8 *)&buf[0];
11823 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11824 cnt = pci_read_vpd(tp->pdev, pos,
11825 len - pos, ptr);
11826 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11827 cnt = 0;
11828 else if (cnt < 0)
11829 goto error;
11830 }
11831 if (pos != len)
11832 goto error;
11833 }
11834
Matt Carlson535a4902011-07-20 10:20:56 +000011835 *vpdlen = len;
11836
Matt Carlsonc3e94502011-04-13 11:05:08 +000011837 return buf;
11838
11839error:
11840 kfree(buf);
11841 return NULL;
11842}
11843
Michael Chan566f86a2005-05-29 14:56:58 -070011844#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011845#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11846#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11847#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011848#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11849#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011850#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011851#define NVRAM_SELFBOOT_HW_SIZE 0x20
11852#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011853
11854static int tg3_test_nvram(struct tg3 *tp)
11855{
Matt Carlson535a4902011-07-20 10:20:56 +000011856 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011857 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011858 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011859
Joe Perches63c3a662011-04-26 08:12:10 +000011860 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011861 return 0;
11862
Matt Carlsone4f34112009-02-25 14:25:00 +000011863 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011864 return -EIO;
11865
Michael Chan1b277772006-03-20 22:27:48 -080011866 if (magic == TG3_EEPROM_MAGIC)
11867 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011868 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011869 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11870 TG3_EEPROM_SB_FORMAT_1) {
11871 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11872 case TG3_EEPROM_SB_REVISION_0:
11873 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11874 break;
11875 case TG3_EEPROM_SB_REVISION_2:
11876 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11877 break;
11878 case TG3_EEPROM_SB_REVISION_3:
11879 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11880 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011881 case TG3_EEPROM_SB_REVISION_4:
11882 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11883 break;
11884 case TG3_EEPROM_SB_REVISION_5:
11885 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11886 break;
11887 case TG3_EEPROM_SB_REVISION_6:
11888 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11889 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011890 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011891 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011892 }
11893 } else
Michael Chan1b277772006-03-20 22:27:48 -080011894 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011895 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11896 size = NVRAM_SELFBOOT_HW_SIZE;
11897 else
Michael Chan1b277772006-03-20 22:27:48 -080011898 return -EIO;
11899
11900 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011901 if (buf == NULL)
11902 return -ENOMEM;
11903
Michael Chan1b277772006-03-20 22:27:48 -080011904 err = -EIO;
11905 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011906 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11907 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011908 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011909 }
Michael Chan1b277772006-03-20 22:27:48 -080011910 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011911 goto out;
11912
Michael Chan1b277772006-03-20 22:27:48 -080011913 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011914 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011915 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011916 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011917 u8 *buf8 = (u8 *) buf, csum8 = 0;
11918
Al Virob9fc7dc2007-12-17 22:59:57 -080011919 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011920 TG3_EEPROM_SB_REVISION_2) {
11921 /* For rev 2, the csum doesn't include the MBA. */
11922 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11923 csum8 += buf8[i];
11924 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11925 csum8 += buf8[i];
11926 } else {
11927 for (i = 0; i < size; i++)
11928 csum8 += buf8[i];
11929 }
Michael Chan1b277772006-03-20 22:27:48 -080011930
Adrian Bunkad96b482006-04-05 22:21:04 -070011931 if (csum8 == 0) {
11932 err = 0;
11933 goto out;
11934 }
11935
11936 err = -EIO;
11937 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011938 }
Michael Chan566f86a2005-05-29 14:56:58 -070011939
Al Virob9fc7dc2007-12-17 22:59:57 -080011940 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011941 TG3_EEPROM_MAGIC_HW) {
11942 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011943 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011944 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011945
11946 /* Separate the parity bits and the data bytes. */
11947 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11948 if ((i == 0) || (i == 8)) {
11949 int l;
11950 u8 msk;
11951
11952 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11953 parity[k++] = buf8[i] & msk;
11954 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011955 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011956 int l;
11957 u8 msk;
11958
11959 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11960 parity[k++] = buf8[i] & msk;
11961 i++;
11962
11963 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11964 parity[k++] = buf8[i] & msk;
11965 i++;
11966 }
11967 data[j++] = buf8[i];
11968 }
11969
11970 err = -EIO;
11971 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11972 u8 hw8 = hweight8(data[i]);
11973
11974 if ((hw8 & 0x1) && parity[i])
11975 goto out;
11976 else if (!(hw8 & 0x1) && !parity[i])
11977 goto out;
11978 }
11979 err = 0;
11980 goto out;
11981 }
11982
Matt Carlson01c3a392011-03-09 16:58:20 +000011983 err = -EIO;
11984
Michael Chan566f86a2005-05-29 14:56:58 -070011985 /* Bootstrap checksum at offset 0x10 */
11986 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011987 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011988 goto out;
11989
11990 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11991 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011992 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011993 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011994
Matt Carlsonc3e94502011-04-13 11:05:08 +000011995 kfree(buf);
11996
Matt Carlson535a4902011-07-20 10:20:56 +000011997 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011998 if (!buf)
11999 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000012000
Matt Carlson535a4902011-07-20 10:20:56 +000012001 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000012002 if (i > 0) {
12003 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
12004 if (j < 0)
12005 goto out;
12006
Matt Carlson535a4902011-07-20 10:20:56 +000012007 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000012008 goto out;
12009
12010 i += PCI_VPD_LRDT_TAG_SIZE;
12011 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
12012 PCI_VPD_RO_KEYWORD_CHKSUM);
12013 if (j > 0) {
12014 u8 csum8 = 0;
12015
12016 j += PCI_VPD_INFO_FLD_HDR_SIZE;
12017
12018 for (i = 0; i <= j; i++)
12019 csum8 += ((u8 *)buf)[i];
12020
12021 if (csum8)
12022 goto out;
12023 }
12024 }
12025
Michael Chan566f86a2005-05-29 14:56:58 -070012026 err = 0;
12027
12028out:
12029 kfree(buf);
12030 return err;
12031}
12032
Michael Chanca430072005-05-29 14:57:23 -070012033#define TG3_SERDES_TIMEOUT_SEC 2
12034#define TG3_COPPER_TIMEOUT_SEC 6
12035
12036static int tg3_test_link(struct tg3 *tp)
12037{
12038 int i, max;
12039
12040 if (!netif_running(tp->dev))
12041 return -ENODEV;
12042
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012043 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070012044 max = TG3_SERDES_TIMEOUT_SEC;
12045 else
12046 max = TG3_COPPER_TIMEOUT_SEC;
12047
12048 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000012049 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070012050 return 0;
12051
12052 if (msleep_interruptible(1000))
12053 break;
12054 }
12055
12056 return -EIO;
12057}
12058
Michael Chana71116d2005-05-29 14:58:11 -070012059/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080012060static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070012061{
Michael Chanb16250e2006-09-27 16:10:14 -070012062 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070012063 u32 offset, read_mask, write_mask, val, save_val, read_val;
12064 static struct {
12065 u16 offset;
12066 u16 flags;
12067#define TG3_FL_5705 0x1
12068#define TG3_FL_NOT_5705 0x2
12069#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070012070#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070012071 u32 read_mask;
12072 u32 write_mask;
12073 } reg_tbl[] = {
12074 /* MAC Control Registers */
12075 { MAC_MODE, TG3_FL_NOT_5705,
12076 0x00000000, 0x00ef6f8c },
12077 { MAC_MODE, TG3_FL_5705,
12078 0x00000000, 0x01ef6b8c },
12079 { MAC_STATUS, TG3_FL_NOT_5705,
12080 0x03800107, 0x00000000 },
12081 { MAC_STATUS, TG3_FL_5705,
12082 0x03800100, 0x00000000 },
12083 { MAC_ADDR_0_HIGH, 0x0000,
12084 0x00000000, 0x0000ffff },
12085 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012086 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070012087 { MAC_RX_MTU_SIZE, 0x0000,
12088 0x00000000, 0x0000ffff },
12089 { MAC_TX_MODE, 0x0000,
12090 0x00000000, 0x00000070 },
12091 { MAC_TX_LENGTHS, 0x0000,
12092 0x00000000, 0x00003fff },
12093 { MAC_RX_MODE, TG3_FL_NOT_5705,
12094 0x00000000, 0x000007fc },
12095 { MAC_RX_MODE, TG3_FL_5705,
12096 0x00000000, 0x000007dc },
12097 { MAC_HASH_REG_0, 0x0000,
12098 0x00000000, 0xffffffff },
12099 { MAC_HASH_REG_1, 0x0000,
12100 0x00000000, 0xffffffff },
12101 { MAC_HASH_REG_2, 0x0000,
12102 0x00000000, 0xffffffff },
12103 { MAC_HASH_REG_3, 0x0000,
12104 0x00000000, 0xffffffff },
12105
12106 /* Receive Data and Receive BD Initiator Control Registers. */
12107 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
12108 0x00000000, 0xffffffff },
12109 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
12110 0x00000000, 0xffffffff },
12111 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
12112 0x00000000, 0x00000003 },
12113 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
12114 0x00000000, 0xffffffff },
12115 { RCVDBDI_STD_BD+0, 0x0000,
12116 0x00000000, 0xffffffff },
12117 { RCVDBDI_STD_BD+4, 0x0000,
12118 0x00000000, 0xffffffff },
12119 { RCVDBDI_STD_BD+8, 0x0000,
12120 0x00000000, 0xffff0002 },
12121 { RCVDBDI_STD_BD+0xc, 0x0000,
12122 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012123
Michael Chana71116d2005-05-29 14:58:11 -070012124 /* Receive BD Initiator Control Registers. */
12125 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
12126 0x00000000, 0xffffffff },
12127 { RCVBDI_STD_THRESH, TG3_FL_5705,
12128 0x00000000, 0x000003ff },
12129 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
12130 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012131
Michael Chana71116d2005-05-29 14:58:11 -070012132 /* Host Coalescing Control Registers. */
12133 { HOSTCC_MODE, TG3_FL_NOT_5705,
12134 0x00000000, 0x00000004 },
12135 { HOSTCC_MODE, TG3_FL_5705,
12136 0x00000000, 0x000000f6 },
12137 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
12138 0x00000000, 0xffffffff },
12139 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
12140 0x00000000, 0x000003ff },
12141 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
12142 0x00000000, 0xffffffff },
12143 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
12144 0x00000000, 0x000003ff },
12145 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
12146 0x00000000, 0xffffffff },
12147 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12148 0x00000000, 0x000000ff },
12149 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
12150 0x00000000, 0xffffffff },
12151 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
12152 0x00000000, 0x000000ff },
12153 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
12154 0x00000000, 0xffffffff },
12155 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
12156 0x00000000, 0xffffffff },
12157 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12158 0x00000000, 0xffffffff },
12159 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12160 0x00000000, 0x000000ff },
12161 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
12162 0x00000000, 0xffffffff },
12163 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
12164 0x00000000, 0x000000ff },
12165 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
12166 0x00000000, 0xffffffff },
12167 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
12168 0x00000000, 0xffffffff },
12169 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
12170 0x00000000, 0xffffffff },
12171 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
12172 0x00000000, 0xffffffff },
12173 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
12174 0x00000000, 0xffffffff },
12175 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
12176 0xffffffff, 0x00000000 },
12177 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
12178 0xffffffff, 0x00000000 },
12179
12180 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070012181 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012182 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070012183 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070012184 0x00000000, 0x007fffff },
12185 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
12186 0x00000000, 0x0000003f },
12187 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
12188 0x00000000, 0x000001ff },
12189 { BUFMGR_MB_HIGH_WATER, 0x0000,
12190 0x00000000, 0x000001ff },
12191 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12192 0xffffffff, 0x00000000 },
12193 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12194 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012195
Michael Chana71116d2005-05-29 14:58:11 -070012196 /* Mailbox Registers */
12197 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12198 0x00000000, 0x000001ff },
12199 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12200 0x00000000, 0x000001ff },
12201 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12202 0x00000000, 0x000007ff },
12203 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12204 0x00000000, 0x000001ff },
12205
12206 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12207 };
12208
Michael Chanb16250e2006-09-27 16:10:14 -070012209 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012210 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012211 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012212 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012213 is_5750 = 1;
12214 }
Michael Chana71116d2005-05-29 14:58:11 -070012215
12216 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12217 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12218 continue;
12219
12220 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12221 continue;
12222
Joe Perches63c3a662011-04-26 08:12:10 +000012223 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012224 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12225 continue;
12226
Michael Chanb16250e2006-09-27 16:10:14 -070012227 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12228 continue;
12229
Michael Chana71116d2005-05-29 14:58:11 -070012230 offset = (u32) reg_tbl[i].offset;
12231 read_mask = reg_tbl[i].read_mask;
12232 write_mask = reg_tbl[i].write_mask;
12233
12234 /* Save the original register content */
12235 save_val = tr32(offset);
12236
12237 /* Determine the read-only value. */
12238 read_val = save_val & read_mask;
12239
12240 /* Write zero to the register, then make sure the read-only bits
12241 * are not changed and the read/write bits are all zeros.
12242 */
12243 tw32(offset, 0);
12244
12245 val = tr32(offset);
12246
12247 /* Test the read-only and read/write bits. */
12248 if (((val & read_mask) != read_val) || (val & write_mask))
12249 goto out;
12250
12251 /* Write ones to all the bits defined by RdMask and WrMask, then
12252 * make sure the read-only bits are not changed and the
12253 * read/write bits are all ones.
12254 */
12255 tw32(offset, read_mask | write_mask);
12256
12257 val = tr32(offset);
12258
12259 /* Test the read-only bits. */
12260 if ((val & read_mask) != read_val)
12261 goto out;
12262
12263 /* Test the read/write bits. */
12264 if ((val & write_mask) != write_mask)
12265 goto out;
12266
12267 tw32(offset, save_val);
12268 }
12269
12270 return 0;
12271
12272out:
Michael Chan9f88f292006-12-07 00:22:54 -080012273 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012274 netdev_err(tp->dev,
12275 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012276 tw32(offset, save_val);
12277 return -EIO;
12278}
12279
Michael Chan7942e1d2005-05-29 14:58:36 -070012280static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12281{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012282 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012283 int i;
12284 u32 j;
12285
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012286 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012287 for (j = 0; j < len; j += 4) {
12288 u32 val;
12289
12290 tg3_write_mem(tp, offset + j, test_pattern[i]);
12291 tg3_read_mem(tp, offset + j, &val);
12292 if (val != test_pattern[i])
12293 return -EIO;
12294 }
12295 }
12296 return 0;
12297}
12298
12299static int tg3_test_memory(struct tg3 *tp)
12300{
12301 static struct mem_entry {
12302 u32 offset;
12303 u32 len;
12304 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012305 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012306 { 0x00002000, 0x1c000},
12307 { 0xffffffff, 0x00000}
12308 }, mem_tbl_5705[] = {
12309 { 0x00000100, 0x0000c},
12310 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012311 { 0x00004000, 0x00800},
12312 { 0x00006000, 0x01000},
12313 { 0x00008000, 0x02000},
12314 { 0x00010000, 0x0e000},
12315 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012316 }, mem_tbl_5755[] = {
12317 { 0x00000200, 0x00008},
12318 { 0x00004000, 0x00800},
12319 { 0x00006000, 0x00800},
12320 { 0x00008000, 0x02000},
12321 { 0x00010000, 0x0c000},
12322 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012323 }, mem_tbl_5906[] = {
12324 { 0x00000200, 0x00008},
12325 { 0x00004000, 0x00400},
12326 { 0x00006000, 0x00400},
12327 { 0x00008000, 0x01000},
12328 { 0x00010000, 0x01000},
12329 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012330 }, mem_tbl_5717[] = {
12331 { 0x00000200, 0x00008},
12332 { 0x00010000, 0x0a000},
12333 { 0x00020000, 0x13c00},
12334 { 0xffffffff, 0x00000}
12335 }, mem_tbl_57765[] = {
12336 { 0x00000200, 0x00008},
12337 { 0x00004000, 0x00800},
12338 { 0x00006000, 0x09800},
12339 { 0x00010000, 0x0a000},
12340 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012341 };
12342 struct mem_entry *mem_tbl;
12343 int err = 0;
12344 int i;
12345
Joe Perches63c3a662011-04-26 08:12:10 +000012346 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012347 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000012348 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012349 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012350 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012351 mem_tbl = mem_tbl_5755;
12352 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12353 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012354 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012355 mem_tbl = mem_tbl_5705;
12356 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012357 mem_tbl = mem_tbl_570x;
12358
12359 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012360 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12361 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012362 break;
12363 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012364
Michael Chan7942e1d2005-05-29 14:58:36 -070012365 return err;
12366}
12367
Matt Carlsonbb158d62011-04-25 12:42:47 +000012368#define TG3_TSO_MSS 500
12369
12370#define TG3_TSO_IP_HDR_LEN 20
12371#define TG3_TSO_TCP_HDR_LEN 20
12372#define TG3_TSO_TCP_OPT_LEN 12
12373
12374static const u8 tg3_tso_header[] = {
123750x08, 0x00,
123760x45, 0x00, 0x00, 0x00,
123770x00, 0x00, 0x40, 0x00,
123780x40, 0x06, 0x00, 0x00,
123790x0a, 0x00, 0x00, 0x01,
123800x0a, 0x00, 0x00, 0x02,
123810x0d, 0x00, 0xe0, 0x00,
123820x00, 0x00, 0x01, 0x00,
123830x00, 0x00, 0x02, 0x00,
123840x80, 0x10, 0x10, 0x00,
123850x14, 0x09, 0x00, 0x00,
123860x01, 0x01, 0x08, 0x0a,
123870x11, 0x11, 0x11, 0x11,
123880x11, 0x11, 0x11, 0x11,
12389};
Michael Chan9f40dea2005-09-05 17:53:06 -070012390
Matt Carlson28a45952011-08-19 13:58:22 +000012391static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012392{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012393 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012394 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012395 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012396 struct sk_buff *skb;
12397 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012398 dma_addr_t map;
12399 int num_pkts, tx_len, rx_len, i, err;
12400 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012401 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012402 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012403
Matt Carlsonc8873402010-02-12 14:47:11 +000012404 tnapi = &tp->napi[0];
12405 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012406 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012407 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012408 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012409 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012410 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012411 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012412 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012413
Michael Chanc76949a2005-05-29 14:58:59 -070012414 err = -EIO;
12415
Matt Carlson4852a862011-04-13 11:05:07 +000012416 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012417 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012418 if (!skb)
12419 return -ENOMEM;
12420
Michael Chanc76949a2005-05-29 14:58:59 -070012421 tx_data = skb_put(skb, tx_len);
12422 memcpy(tx_data, tp->dev->dev_addr, 6);
12423 memset(tx_data + 6, 0x0, 8);
12424
Matt Carlson4852a862011-04-13 11:05:07 +000012425 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012426
Matt Carlson28a45952011-08-19 13:58:22 +000012427 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012428 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12429
12430 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12431 TG3_TSO_TCP_OPT_LEN;
12432
12433 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12434 sizeof(tg3_tso_header));
12435 mss = TG3_TSO_MSS;
12436
12437 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12438 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12439
12440 /* Set the total length field in the IP header */
12441 iph->tot_len = htons((u16)(mss + hdr_len));
12442
12443 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12444 TXD_FLAG_CPU_POST_DMA);
12445
Joe Perches63c3a662011-04-26 08:12:10 +000012446 if (tg3_flag(tp, HW_TSO_1) ||
12447 tg3_flag(tp, HW_TSO_2) ||
12448 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012449 struct tcphdr *th;
12450 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12451 th = (struct tcphdr *)&tx_data[val];
12452 th->check = 0;
12453 } else
12454 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12455
Joe Perches63c3a662011-04-26 08:12:10 +000012456 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012457 mss |= (hdr_len & 0xc) << 12;
12458 if (hdr_len & 0x10)
12459 base_flags |= 0x00000010;
12460 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012461 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012462 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012463 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000012464 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
12465 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12466 } else {
12467 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12468 }
12469
12470 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12471 } else {
12472 num_pkts = 1;
12473 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012474
12475 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12476 tx_len > VLAN_ETH_FRAME_LEN)
12477 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012478 }
12479
12480 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012481 tx_data[i] = (u8) (i & 0xff);
12482
Alexander Duyckf4188d82009-12-02 16:48:38 +000012483 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12484 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012485 dev_kfree_skb(skb);
12486 return -EIO;
12487 }
Michael Chanc76949a2005-05-29 14:58:59 -070012488
Matt Carlson0d681b22011-07-27 14:20:49 +000012489 val = tnapi->tx_prod;
12490 tnapi->tx_buffers[val].skb = skb;
12491 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12492
Michael Chanc76949a2005-05-29 14:58:59 -070012493 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012494 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012495
12496 udelay(10);
12497
Matt Carlson898a56f2009-08-28 14:02:40 +000012498 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012499
Matt Carlson84b67b22011-07-27 14:20:52 +000012500 budget = tg3_tx_avail(tnapi);
12501 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012502 base_flags | TXD_FLAG_END, mss, 0)) {
12503 tnapi->tx_buffers[val].skb = NULL;
12504 dev_kfree_skb(skb);
12505 return -EIO;
12506 }
Michael Chanc76949a2005-05-29 14:58:59 -070012507
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012508 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012509
Michael Chan6541b802012-03-04 14:48:14 +000012510 /* Sync BD data before updating mailbox */
12511 wmb();
12512
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012513 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12514 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012515
12516 udelay(10);
12517
Matt Carlson303fc922009-11-02 14:27:34 +000012518 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12519 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012520 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012521 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012522
12523 udelay(10);
12524
Matt Carlson898a56f2009-08-28 14:02:40 +000012525 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12526 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012527 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012528 (rx_idx == (rx_start_idx + num_pkts)))
12529 break;
12530 }
12531
Matt Carlsonba1142e2011-11-04 09:15:00 +000012532 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012533 dev_kfree_skb(skb);
12534
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012535 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012536 goto out;
12537
12538 if (rx_idx != rx_start_idx + num_pkts)
12539 goto out;
12540
Matt Carlsonbb158d62011-04-25 12:42:47 +000012541 val = data_off;
12542 while (rx_idx != rx_start_idx) {
12543 desc = &rnapi->rx_rcb[rx_start_idx++];
12544 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12545 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012546
Matt Carlsonbb158d62011-04-25 12:42:47 +000012547 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12548 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012549 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012550
Matt Carlsonbb158d62011-04-25 12:42:47 +000012551 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12552 - ETH_FCS_LEN;
12553
Matt Carlson28a45952011-08-19 13:58:22 +000012554 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012555 if (rx_len != tx_len)
12556 goto out;
12557
12558 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12559 if (opaque_key != RXD_OPAQUE_RING_STD)
12560 goto out;
12561 } else {
12562 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12563 goto out;
12564 }
12565 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12566 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012567 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012568 goto out;
12569 }
12570
12571 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012572 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012573 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12574 mapping);
12575 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012576 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012577 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12578 mapping);
12579 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012580 goto out;
12581
Matt Carlsonbb158d62011-04-25 12:42:47 +000012582 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12583 PCI_DMA_FROMDEVICE);
12584
Eric Dumazet9205fd92011-11-18 06:47:01 +000012585 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012586 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012587 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012588 goto out;
12589 }
Matt Carlson4852a862011-04-13 11:05:07 +000012590 }
12591
Michael Chanc76949a2005-05-29 14:58:59 -070012592 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012593
Eric Dumazet9205fd92011-11-18 06:47:01 +000012594 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012595out:
12596 return err;
12597}
12598
Matt Carlson00c266b2011-04-25 12:42:46 +000012599#define TG3_STD_LOOPBACK_FAILED 1
12600#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012601#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012602#define TG3_LOOPBACK_FAILED \
12603 (TG3_STD_LOOPBACK_FAILED | \
12604 TG3_JMB_LOOPBACK_FAILED | \
12605 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012606
Matt Carlson941ec902011-08-19 13:58:23 +000012607static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012608{
Matt Carlson28a45952011-08-19 13:58:22 +000012609 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012610 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012611 u32 jmb_pkt_sz = 9000;
12612
12613 if (tp->dma_limit)
12614 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012615
Matt Carlsonab789042011-01-25 15:58:54 +000012616 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12617 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12618
Matt Carlson28a45952011-08-19 13:58:22 +000012619 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012620 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12621 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012622 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012623 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012624 goto done;
12625 }
12626
Michael Chanb9ec6c12006-07-25 16:37:27 -070012627 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012628 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012629 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12630 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012631 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012632 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012633 goto done;
12634 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012635
Joe Perches63c3a662011-04-26 08:12:10 +000012636 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012637 int i;
12638
12639 /* Reroute all rx packets to the 1st queue */
12640 for (i = MAC_RSS_INDIR_TBL_0;
12641 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12642 tw32(i, 0x0);
12643 }
12644
Matt Carlson6e01b202011-08-19 13:58:20 +000012645 /* HW errata - mac loopback fails in some cases on 5780.
12646 * Normal traffic and PHY loopback are not affected by
12647 * errata. Also, the MAC loopback test is deprecated for
12648 * all newer ASIC revisions.
12649 */
12650 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12651 !tg3_flag(tp, CPMU_PRESENT)) {
12652 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012653
Matt Carlson28a45952011-08-19 13:58:22 +000012654 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012655 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012656
12657 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012658 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012659 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012660
12661 tg3_mac_loopback(tp, false);
12662 }
Matt Carlson4852a862011-04-13 11:05:07 +000012663
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012664 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012665 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012666 int i;
12667
Matt Carlson941ec902011-08-19 13:58:23 +000012668 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012669
12670 /* Wait for link */
12671 for (i = 0; i < 100; i++) {
12672 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12673 break;
12674 mdelay(1);
12675 }
12676
Matt Carlson28a45952011-08-19 13:58:22 +000012677 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012678 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012679 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012680 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012681 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012682 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012683 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012684 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012685
Matt Carlson941ec902011-08-19 13:58:23 +000012686 if (do_extlpbk) {
12687 tg3_phy_lpbk_set(tp, 0, true);
12688
12689 /* All link indications report up, but the hardware
12690 * isn't really ready for about 20 msec. Double it
12691 * to be sure.
12692 */
12693 mdelay(40);
12694
12695 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012696 data[TG3_EXT_LOOPB_TEST] |=
12697 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012698 if (tg3_flag(tp, TSO_CAPABLE) &&
12699 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012700 data[TG3_EXT_LOOPB_TEST] |=
12701 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012702 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012703 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012704 data[TG3_EXT_LOOPB_TEST] |=
12705 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012706 }
12707
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012708 /* Re-enable gphy autopowerdown. */
12709 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12710 tg3_phy_toggle_apd(tp, true);
12711 }
Matt Carlson6833c042008-11-21 17:18:59 -080012712
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012713 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12714 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012715
Matt Carlsonab789042011-01-25 15:58:54 +000012716done:
12717 tp->phy_flags |= eee_cap;
12718
Michael Chan9f40dea2005-09-05 17:53:06 -070012719 return err;
12720}
12721
Michael Chan4cafd3f2005-05-29 14:56:34 -070012722static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12723 u64 *data)
12724{
Michael Chan566f86a2005-05-29 14:56:58 -070012725 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012726 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012727
Matt Carlsonbed98292011-07-13 09:27:29 +000012728 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12729 tg3_power_up(tp)) {
12730 etest->flags |= ETH_TEST_FL_FAILED;
12731 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12732 return;
12733 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012734
Michael Chan566f86a2005-05-29 14:56:58 -070012735 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12736
12737 if (tg3_test_nvram(tp) != 0) {
12738 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012739 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012740 }
Matt Carlson941ec902011-08-19 13:58:23 +000012741 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012742 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012743 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012744 }
Michael Chana71116d2005-05-29 14:58:11 -070012745 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012746 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012747
Michael Chanbbe832c2005-06-24 20:20:04 -070012748 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012749 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012750 tg3_netif_stop(tp);
12751 irq_sync = 1;
12752 }
12753
12754 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012755 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012756 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012757 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012758 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012759 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012760 if (!err)
12761 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012762
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012763 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012764 tg3_phy_reset(tp);
12765
Michael Chana71116d2005-05-29 14:58:11 -070012766 if (tg3_test_registers(tp) != 0) {
12767 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012768 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012769 }
Matt Carlson28a45952011-08-19 13:58:22 +000012770
Michael Chan7942e1d2005-05-29 14:58:36 -070012771 if (tg3_test_memory(tp) != 0) {
12772 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012773 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012774 }
Matt Carlson28a45952011-08-19 13:58:22 +000012775
Matt Carlson941ec902011-08-19 13:58:23 +000012776 if (doextlpbk)
12777 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12778
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012779 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012780 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012781
David S. Millerf47c11e2005-06-24 20:18:35 -070012782 tg3_full_unlock(tp);
12783
Michael Chand4bc3922005-05-29 14:59:20 -070012784 if (tg3_test_interrupt(tp) != 0) {
12785 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012786 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012787 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012788
12789 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012790
Michael Chana71116d2005-05-29 14:58:11 -070012791 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12792 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012793 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012794 err2 = tg3_restart_hw(tp, 1);
12795 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012796 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012797 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012798
12799 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012800
12801 if (irq_sync && !err2)
12802 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012803 }
Matt Carlson80096062010-08-02 11:26:06 +000012804 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012805 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012806
Michael Chan4cafd3f2005-05-29 14:56:34 -070012807}
12808
Matt Carlson0a633ac2012-12-03 19:36:59 +000012809static int tg3_hwtstamp_ioctl(struct net_device *dev,
12810 struct ifreq *ifr, int cmd)
12811{
12812 struct tg3 *tp = netdev_priv(dev);
12813 struct hwtstamp_config stmpconf;
12814
12815 if (!tg3_flag(tp, PTP_CAPABLE))
12816 return -EINVAL;
12817
12818 if (copy_from_user(&stmpconf, ifr->ifr_data, sizeof(stmpconf)))
12819 return -EFAULT;
12820
12821 if (stmpconf.flags)
12822 return -EINVAL;
12823
12824 switch (stmpconf.tx_type) {
12825 case HWTSTAMP_TX_ON:
12826 tg3_flag_set(tp, TX_TSTAMP_EN);
12827 break;
12828 case HWTSTAMP_TX_OFF:
12829 tg3_flag_clear(tp, TX_TSTAMP_EN);
12830 break;
12831 default:
12832 return -ERANGE;
12833 }
12834
12835 switch (stmpconf.rx_filter) {
12836 case HWTSTAMP_FILTER_NONE:
12837 tp->rxptpctl = 0;
12838 break;
12839 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
12840 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12841 TG3_RX_PTP_CTL_ALL_V1_EVENTS;
12842 break;
12843 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
12844 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12845 TG3_RX_PTP_CTL_SYNC_EVNT;
12846 break;
12847 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
12848 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V1_EN |
12849 TG3_RX_PTP_CTL_DELAY_REQ;
12850 break;
12851 case HWTSTAMP_FILTER_PTP_V2_EVENT:
12852 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12853 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12854 break;
12855 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
12856 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12857 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12858 break;
12859 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
12860 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12861 TG3_RX_PTP_CTL_ALL_V2_EVENTS;
12862 break;
12863 case HWTSTAMP_FILTER_PTP_V2_SYNC:
12864 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12865 TG3_RX_PTP_CTL_SYNC_EVNT;
12866 break;
12867 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
12868 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12869 TG3_RX_PTP_CTL_SYNC_EVNT;
12870 break;
12871 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
12872 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12873 TG3_RX_PTP_CTL_SYNC_EVNT;
12874 break;
12875 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
12876 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_EN |
12877 TG3_RX_PTP_CTL_DELAY_REQ;
12878 break;
12879 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
12880 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L2_EN |
12881 TG3_RX_PTP_CTL_DELAY_REQ;
12882 break;
12883 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
12884 tp->rxptpctl = TG3_RX_PTP_CTL_RX_PTP_V2_L4_EN |
12885 TG3_RX_PTP_CTL_DELAY_REQ;
12886 break;
12887 default:
12888 return -ERANGE;
12889 }
12890
12891 if (netif_running(dev) && tp->rxptpctl)
12892 tw32(TG3_RX_PTP_CTL,
12893 tp->rxptpctl | TG3_RX_PTP_CTL_HWTS_INTERLOCK);
12894
12895 return copy_to_user(ifr->ifr_data, &stmpconf, sizeof(stmpconf)) ?
12896 -EFAULT : 0;
12897}
12898
Linus Torvalds1da177e2005-04-16 15:20:36 -070012899static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12900{
12901 struct mii_ioctl_data *data = if_mii(ifr);
12902 struct tg3 *tp = netdev_priv(dev);
12903 int err;
12904
Joe Perches63c3a662011-04-26 08:12:10 +000012905 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012906 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012907 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012908 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012909 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012910 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012911 }
12912
Matt Carlson33f401a2010-04-05 10:19:27 +000012913 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012914 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012915 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012916
12917 /* fallthru */
12918 case SIOCGMIIREG: {
12919 u32 mii_regval;
12920
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012921 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922 break; /* We have no PHY */
12923
Matt Carlson34eea5a2011-04-20 07:57:38 +000012924 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012925 return -EAGAIN;
12926
David S. Millerf47c11e2005-06-24 20:18:35 -070012927 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012928 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012929 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930
12931 data->val_out = mii_regval;
12932
12933 return err;
12934 }
12935
12936 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012937 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012938 break; /* We have no PHY */
12939
Matt Carlson34eea5a2011-04-20 07:57:38 +000012940 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012941 return -EAGAIN;
12942
David S. Millerf47c11e2005-06-24 20:18:35 -070012943 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012945 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946
12947 return err;
12948
Matt Carlson0a633ac2012-12-03 19:36:59 +000012949 case SIOCSHWTSTAMP:
12950 return tg3_hwtstamp_ioctl(dev, ifr, cmd);
12951
Linus Torvalds1da177e2005-04-16 15:20:36 -070012952 default:
12953 /* do nothing */
12954 break;
12955 }
12956 return -EOPNOTSUPP;
12957}
12958
David S. Miller15f98502005-05-18 22:49:26 -070012959static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12960{
12961 struct tg3 *tp = netdev_priv(dev);
12962
12963 memcpy(ec, &tp->coal, sizeof(*ec));
12964 return 0;
12965}
12966
Michael Chand244c892005-07-05 14:42:33 -070012967static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12968{
12969 struct tg3 *tp = netdev_priv(dev);
12970 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12971 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12972
Joe Perches63c3a662011-04-26 08:12:10 +000012973 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012974 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12975 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12976 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12977 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12978 }
12979
12980 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12981 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12982 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12983 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12984 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12985 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12986 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12987 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12988 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12989 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12990 return -EINVAL;
12991
12992 /* No rx interrupts will be generated if both are zero */
12993 if ((ec->rx_coalesce_usecs == 0) &&
12994 (ec->rx_max_coalesced_frames == 0))
12995 return -EINVAL;
12996
12997 /* No tx interrupts will be generated if both are zero */
12998 if ((ec->tx_coalesce_usecs == 0) &&
12999 (ec->tx_max_coalesced_frames == 0))
13000 return -EINVAL;
13001
13002 /* Only copy relevant parameters, ignore all others. */
13003 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
13004 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
13005 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
13006 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
13007 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
13008 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
13009 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
13010 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
13011 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
13012
13013 if (netif_running(dev)) {
13014 tg3_full_lock(tp, 0);
13015 __tg3_set_coalesce(tp, &tp->coal);
13016 tg3_full_unlock(tp);
13017 }
13018 return 0;
13019}
13020
Jeff Garzik7282d492006-09-13 14:30:00 -040013021static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013022 .get_settings = tg3_get_settings,
13023 .set_settings = tg3_set_settings,
13024 .get_drvinfo = tg3_get_drvinfo,
13025 .get_regs_len = tg3_get_regs_len,
13026 .get_regs = tg3_get_regs,
13027 .get_wol = tg3_get_wol,
13028 .set_wol = tg3_set_wol,
13029 .get_msglevel = tg3_get_msglevel,
13030 .set_msglevel = tg3_set_msglevel,
13031 .nway_reset = tg3_nway_reset,
13032 .get_link = ethtool_op_get_link,
13033 .get_eeprom_len = tg3_get_eeprom_len,
13034 .get_eeprom = tg3_get_eeprom,
13035 .set_eeprom = tg3_set_eeprom,
13036 .get_ringparam = tg3_get_ringparam,
13037 .set_ringparam = tg3_set_ringparam,
13038 .get_pauseparam = tg3_get_pauseparam,
13039 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070013040 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013041 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000013042 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013043 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070013044 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070013045 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070013046 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000013047 .get_rxnfc = tg3_get_rxnfc,
13048 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
13049 .get_rxfh_indir = tg3_get_rxfh_indir,
13050 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000013051 .get_channels = tg3_get_channels,
13052 .set_channels = tg3_set_channels,
Matt Carlson7d41e492012-12-03 19:36:58 +000013053 .get_ts_info = tg3_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054};
13055
David S. Millerb4017c52012-03-01 17:57:40 -050013056static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
13057 struct rtnl_link_stats64 *stats)
13058{
13059 struct tg3 *tp = netdev_priv(dev);
13060
David S. Millerb4017c52012-03-01 17:57:40 -050013061 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000013062 if (!tp->hw_stats) {
13063 spin_unlock_bh(&tp->lock);
13064 return &tp->net_stats_prev;
13065 }
13066
David S. Millerb4017c52012-03-01 17:57:40 -050013067 tg3_get_nstats(tp, stats);
13068 spin_unlock_bh(&tp->lock);
13069
13070 return stats;
13071}
13072
Matt Carlsonccd5ba92012-02-13 10:20:08 +000013073static void tg3_set_rx_mode(struct net_device *dev)
13074{
13075 struct tg3 *tp = netdev_priv(dev);
13076
13077 if (!netif_running(dev))
13078 return;
13079
13080 tg3_full_lock(tp, 0);
13081 __tg3_set_rx_mode(dev);
13082 tg3_full_unlock(tp);
13083}
13084
Matt Carlsonfaf16272012-02-13 10:20:07 +000013085static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
13086 int new_mtu)
13087{
13088 dev->mtu = new_mtu;
13089
13090 if (new_mtu > ETH_DATA_LEN) {
13091 if (tg3_flag(tp, 5780_CLASS)) {
13092 netdev_update_features(dev);
13093 tg3_flag_clear(tp, TSO_CAPABLE);
13094 } else {
13095 tg3_flag_set(tp, JUMBO_RING_ENABLE);
13096 }
13097 } else {
13098 if (tg3_flag(tp, 5780_CLASS)) {
13099 tg3_flag_set(tp, TSO_CAPABLE);
13100 netdev_update_features(dev);
13101 }
13102 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
13103 }
13104}
13105
13106static int tg3_change_mtu(struct net_device *dev, int new_mtu)
13107{
13108 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000013109 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000013110
13111 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
13112 return -EINVAL;
13113
13114 if (!netif_running(dev)) {
13115 /* We'll just catch it later when the
13116 * device is up'd.
13117 */
13118 tg3_set_mtu(dev, tp, new_mtu);
13119 return 0;
13120 }
13121
13122 tg3_phy_stop(tp);
13123
13124 tg3_netif_stop(tp);
13125
13126 tg3_full_lock(tp, 1);
13127
13128 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
13129
13130 tg3_set_mtu(dev, tp, new_mtu);
13131
Michael Chan2fae5e32012-03-04 14:48:15 +000013132 /* Reset PHY, otherwise the read DMA engine will be in a mode that
13133 * breaks all requests to 256 bytes.
13134 */
13135 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
13136 reset_phy = 1;
13137
13138 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000013139
13140 if (!err)
13141 tg3_netif_start(tp);
13142
13143 tg3_full_unlock(tp);
13144
13145 if (!err)
13146 tg3_phy_start(tp);
13147
13148 return err;
13149}
13150
13151static const struct net_device_ops tg3_netdev_ops = {
13152 .ndo_open = tg3_open,
13153 .ndo_stop = tg3_close,
13154 .ndo_start_xmit = tg3_start_xmit,
13155 .ndo_get_stats64 = tg3_get_stats64,
13156 .ndo_validate_addr = eth_validate_addr,
13157 .ndo_set_rx_mode = tg3_set_rx_mode,
13158 .ndo_set_mac_address = tg3_set_mac_addr,
13159 .ndo_do_ioctl = tg3_ioctl,
13160 .ndo_tx_timeout = tg3_tx_timeout,
13161 .ndo_change_mtu = tg3_change_mtu,
13162 .ndo_fix_features = tg3_fix_features,
13163 .ndo_set_features = tg3_set_features,
13164#ifdef CONFIG_NET_POLL_CONTROLLER
13165 .ndo_poll_controller = tg3_poll_controller,
13166#endif
13167};
13168
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013169static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013170{
Michael Chan1b277772006-03-20 22:27:48 -080013171 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013172
13173 tp->nvram_size = EEPROM_CHIP_SIZE;
13174
Matt Carlsone4f34112009-02-25 14:25:00 +000013175 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013176 return;
13177
Michael Chanb16250e2006-09-27 16:10:14 -070013178 if ((magic != TG3_EEPROM_MAGIC) &&
13179 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
13180 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013181 return;
13182
13183 /*
13184 * Size the chip by reading offsets at increasing powers of two.
13185 * When we encounter our validation signature, we know the addressing
13186 * has wrapped around, and thus have our chip size.
13187 */
Michael Chan1b277772006-03-20 22:27:48 -080013188 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013189
13190 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013191 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013192 return;
13193
Michael Chan18201802006-03-20 22:29:15 -080013194 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013195 break;
13196
13197 cursize <<= 1;
13198 }
13199
13200 tp->nvram_size = cursize;
13201}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013202
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013203static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013204{
13205 u32 val;
13206
Joe Perches63c3a662011-04-26 08:12:10 +000013207 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080013208 return;
13209
13210 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080013211 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080013212 tg3_get_eeprom_size(tp);
13213 return;
13214 }
13215
Matt Carlson6d348f22009-02-25 14:25:52 +000013216 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013217 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000013218 /* This is confusing. We want to operate on the
13219 * 16-bit value at offset 0xf2. The tg3_nvram_read()
13220 * call will read from NVRAM and byteswap the data
13221 * according to the byteswapping settings for all
13222 * other register accesses. This ensures the data we
13223 * want will always reside in the lower 16-bits.
13224 * However, the data in NVRAM is in LE format, which
13225 * means the data from the NVRAM read will always be
13226 * opposite the endianness of the CPU. The 16-bit
13227 * byteswap then brings the data to CPU endianness.
13228 */
13229 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013230 return;
13231 }
13232 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070013233 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234}
13235
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013236static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013237{
13238 u32 nvcfg1;
13239
13240 nvcfg1 = tr32(NVRAM_CFG1);
13241 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000013242 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013243 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013244 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13245 tw32(NVRAM_CFG1, nvcfg1);
13246 }
13247
Matt Carlson6ff6f812011-05-19 12:12:54 +000013248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013249 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013250 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013251 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
13252 tp->nvram_jedecnum = JEDEC_ATMEL;
13253 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013254 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013255 break;
13256 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
13257 tp->nvram_jedecnum = JEDEC_ATMEL;
13258 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
13259 break;
13260 case FLASH_VENDOR_ATMEL_EEPROM:
13261 tp->nvram_jedecnum = JEDEC_ATMEL;
13262 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013263 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013264 break;
13265 case FLASH_VENDOR_ST:
13266 tp->nvram_jedecnum = JEDEC_ST;
13267 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013268 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013269 break;
13270 case FLASH_VENDOR_SAIFUN:
13271 tp->nvram_jedecnum = JEDEC_SAIFUN;
13272 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
13273 break;
13274 case FLASH_VENDOR_SST_SMALL:
13275 case FLASH_VENDOR_SST_LARGE:
13276 tp->nvram_jedecnum = JEDEC_SST;
13277 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
13278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013279 }
Matt Carlson8590a602009-08-28 12:29:16 +000013280 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013281 tp->nvram_jedecnum = JEDEC_ATMEL;
13282 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013283 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013284 }
13285}
13286
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013287static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013288{
13289 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13290 case FLASH_5752PAGE_SIZE_256:
13291 tp->nvram_pagesize = 256;
13292 break;
13293 case FLASH_5752PAGE_SIZE_512:
13294 tp->nvram_pagesize = 512;
13295 break;
13296 case FLASH_5752PAGE_SIZE_1K:
13297 tp->nvram_pagesize = 1024;
13298 break;
13299 case FLASH_5752PAGE_SIZE_2K:
13300 tp->nvram_pagesize = 2048;
13301 break;
13302 case FLASH_5752PAGE_SIZE_4K:
13303 tp->nvram_pagesize = 4096;
13304 break;
13305 case FLASH_5752PAGE_SIZE_264:
13306 tp->nvram_pagesize = 264;
13307 break;
13308 case FLASH_5752PAGE_SIZE_528:
13309 tp->nvram_pagesize = 528;
13310 break;
13311 }
13312}
13313
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013314static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013315{
13316 u32 nvcfg1;
13317
13318 nvcfg1 = tr32(NVRAM_CFG1);
13319
Michael Chane6af3012005-04-21 17:12:05 -070013320 /* NVRAM protection for TPM */
13321 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013322 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013323
Michael Chan361b4ac2005-04-21 17:11:21 -070013324 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013325 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13326 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13327 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013328 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013329 break;
13330 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13331 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013332 tg3_flag_set(tp, NVRAM_BUFFERED);
13333 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013334 break;
13335 case FLASH_5752VENDOR_ST_M45PE10:
13336 case FLASH_5752VENDOR_ST_M45PE20:
13337 case FLASH_5752VENDOR_ST_M45PE40:
13338 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013339 tg3_flag_set(tp, NVRAM_BUFFERED);
13340 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013341 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013342 }
13343
Joe Perches63c3a662011-04-26 08:12:10 +000013344 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013345 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013346 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013347 /* For eeprom, set pagesize to maximum eeprom size */
13348 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13349
13350 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13351 tw32(NVRAM_CFG1, nvcfg1);
13352 }
13353}
13354
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013355static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013356{
Matt Carlson989a9d22007-05-05 11:51:05 -070013357 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013358
13359 nvcfg1 = tr32(NVRAM_CFG1);
13360
13361 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013362 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013363 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013364 protect = 1;
13365 }
Michael Chand3c7b882006-03-23 01:28:25 -080013366
Matt Carlson989a9d22007-05-05 11:51:05 -070013367 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13368 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013369 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13370 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13371 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13372 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13373 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013374 tg3_flag_set(tp, NVRAM_BUFFERED);
13375 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013376 tp->nvram_pagesize = 264;
13377 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13378 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13379 tp->nvram_size = (protect ? 0x3e200 :
13380 TG3_NVRAM_SIZE_512KB);
13381 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13382 tp->nvram_size = (protect ? 0x1f200 :
13383 TG3_NVRAM_SIZE_256KB);
13384 else
13385 tp->nvram_size = (protect ? 0x1f200 :
13386 TG3_NVRAM_SIZE_128KB);
13387 break;
13388 case FLASH_5752VENDOR_ST_M45PE10:
13389 case FLASH_5752VENDOR_ST_M45PE20:
13390 case FLASH_5752VENDOR_ST_M45PE40:
13391 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013392 tg3_flag_set(tp, NVRAM_BUFFERED);
13393 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013394 tp->nvram_pagesize = 256;
13395 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13396 tp->nvram_size = (protect ?
13397 TG3_NVRAM_SIZE_64KB :
13398 TG3_NVRAM_SIZE_128KB);
13399 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13400 tp->nvram_size = (protect ?
13401 TG3_NVRAM_SIZE_64KB :
13402 TG3_NVRAM_SIZE_256KB);
13403 else
13404 tp->nvram_size = (protect ?
13405 TG3_NVRAM_SIZE_128KB :
13406 TG3_NVRAM_SIZE_512KB);
13407 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013408 }
13409}
13410
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013411static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013412{
13413 u32 nvcfg1;
13414
13415 nvcfg1 = tr32(NVRAM_CFG1);
13416
13417 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013418 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13419 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13420 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13421 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13422 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013423 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013424 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013425
Matt Carlson8590a602009-08-28 12:29:16 +000013426 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13427 tw32(NVRAM_CFG1, nvcfg1);
13428 break;
13429 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13430 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13431 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13432 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13433 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013434 tg3_flag_set(tp, NVRAM_BUFFERED);
13435 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013436 tp->nvram_pagesize = 264;
13437 break;
13438 case FLASH_5752VENDOR_ST_M45PE10:
13439 case FLASH_5752VENDOR_ST_M45PE20:
13440 case FLASH_5752VENDOR_ST_M45PE40:
13441 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013442 tg3_flag_set(tp, NVRAM_BUFFERED);
13443 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013444 tp->nvram_pagesize = 256;
13445 break;
Michael Chan1b277772006-03-20 22:27:48 -080013446 }
13447}
13448
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013449static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013450{
13451 u32 nvcfg1, protect = 0;
13452
13453 nvcfg1 = tr32(NVRAM_CFG1);
13454
13455 /* NVRAM protection for TPM */
13456 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013457 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013458 protect = 1;
13459 }
13460
13461 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13462 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013463 case FLASH_5761VENDOR_ATMEL_ADB021D:
13464 case FLASH_5761VENDOR_ATMEL_ADB041D:
13465 case FLASH_5761VENDOR_ATMEL_ADB081D:
13466 case FLASH_5761VENDOR_ATMEL_ADB161D:
13467 case FLASH_5761VENDOR_ATMEL_MDB021D:
13468 case FLASH_5761VENDOR_ATMEL_MDB041D:
13469 case FLASH_5761VENDOR_ATMEL_MDB081D:
13470 case FLASH_5761VENDOR_ATMEL_MDB161D:
13471 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013472 tg3_flag_set(tp, NVRAM_BUFFERED);
13473 tg3_flag_set(tp, FLASH);
13474 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013475 tp->nvram_pagesize = 256;
13476 break;
13477 case FLASH_5761VENDOR_ST_A_M45PE20:
13478 case FLASH_5761VENDOR_ST_A_M45PE40:
13479 case FLASH_5761VENDOR_ST_A_M45PE80:
13480 case FLASH_5761VENDOR_ST_A_M45PE16:
13481 case FLASH_5761VENDOR_ST_M_M45PE20:
13482 case FLASH_5761VENDOR_ST_M_M45PE40:
13483 case FLASH_5761VENDOR_ST_M_M45PE80:
13484 case FLASH_5761VENDOR_ST_M_M45PE16:
13485 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013486 tg3_flag_set(tp, NVRAM_BUFFERED);
13487 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013488 tp->nvram_pagesize = 256;
13489 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013490 }
13491
13492 if (protect) {
13493 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13494 } else {
13495 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013496 case FLASH_5761VENDOR_ATMEL_ADB161D:
13497 case FLASH_5761VENDOR_ATMEL_MDB161D:
13498 case FLASH_5761VENDOR_ST_A_M45PE16:
13499 case FLASH_5761VENDOR_ST_M_M45PE16:
13500 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13501 break;
13502 case FLASH_5761VENDOR_ATMEL_ADB081D:
13503 case FLASH_5761VENDOR_ATMEL_MDB081D:
13504 case FLASH_5761VENDOR_ST_A_M45PE80:
13505 case FLASH_5761VENDOR_ST_M_M45PE80:
13506 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13507 break;
13508 case FLASH_5761VENDOR_ATMEL_ADB041D:
13509 case FLASH_5761VENDOR_ATMEL_MDB041D:
13510 case FLASH_5761VENDOR_ST_A_M45PE40:
13511 case FLASH_5761VENDOR_ST_M_M45PE40:
13512 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13513 break;
13514 case FLASH_5761VENDOR_ATMEL_ADB021D:
13515 case FLASH_5761VENDOR_ATMEL_MDB021D:
13516 case FLASH_5761VENDOR_ST_A_M45PE20:
13517 case FLASH_5761VENDOR_ST_M_M45PE20:
13518 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13519 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013520 }
13521 }
13522}
13523
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013524static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013525{
13526 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013527 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013528 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13529}
13530
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013531static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013532{
13533 u32 nvcfg1;
13534
13535 nvcfg1 = tr32(NVRAM_CFG1);
13536
13537 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13538 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13539 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13540 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013541 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013542 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13543
13544 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13545 tw32(NVRAM_CFG1, nvcfg1);
13546 return;
13547 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13548 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13549 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13550 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13551 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13552 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13553 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13554 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013555 tg3_flag_set(tp, NVRAM_BUFFERED);
13556 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013557
13558 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13559 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13560 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13561 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13562 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13563 break;
13564 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13565 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13566 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13567 break;
13568 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13569 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13570 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13571 break;
13572 }
13573 break;
13574 case FLASH_5752VENDOR_ST_M45PE10:
13575 case FLASH_5752VENDOR_ST_M45PE20:
13576 case FLASH_5752VENDOR_ST_M45PE40:
13577 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013578 tg3_flag_set(tp, NVRAM_BUFFERED);
13579 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013580
13581 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13582 case FLASH_5752VENDOR_ST_M45PE10:
13583 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13584 break;
13585 case FLASH_5752VENDOR_ST_M45PE20:
13586 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13587 break;
13588 case FLASH_5752VENDOR_ST_M45PE40:
13589 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13590 break;
13591 }
13592 break;
13593 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013594 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013595 return;
13596 }
13597
Matt Carlsona1b950d2009-09-01 13:20:17 +000013598 tg3_nvram_get_pagesize(tp, nvcfg1);
13599 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013600 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013601}
13602
13603
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013604static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013605{
13606 u32 nvcfg1;
13607
13608 nvcfg1 = tr32(NVRAM_CFG1);
13609
13610 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13611 case FLASH_5717VENDOR_ATMEL_EEPROM:
13612 case FLASH_5717VENDOR_MICRO_EEPROM:
13613 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013614 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013615 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13616
13617 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13618 tw32(NVRAM_CFG1, nvcfg1);
13619 return;
13620 case FLASH_5717VENDOR_ATMEL_MDB011D:
13621 case FLASH_5717VENDOR_ATMEL_ADB011B:
13622 case FLASH_5717VENDOR_ATMEL_ADB011D:
13623 case FLASH_5717VENDOR_ATMEL_MDB021D:
13624 case FLASH_5717VENDOR_ATMEL_ADB021B:
13625 case FLASH_5717VENDOR_ATMEL_ADB021D:
13626 case FLASH_5717VENDOR_ATMEL_45USPT:
13627 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013628 tg3_flag_set(tp, NVRAM_BUFFERED);
13629 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013630
13631 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13632 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013633 /* Detect size with tg3_nvram_get_size() */
13634 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013635 case FLASH_5717VENDOR_ATMEL_ADB021B:
13636 case FLASH_5717VENDOR_ATMEL_ADB021D:
13637 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13638 break;
13639 default:
13640 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13641 break;
13642 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013643 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013644 case FLASH_5717VENDOR_ST_M_M25PE10:
13645 case FLASH_5717VENDOR_ST_A_M25PE10:
13646 case FLASH_5717VENDOR_ST_M_M45PE10:
13647 case FLASH_5717VENDOR_ST_A_M45PE10:
13648 case FLASH_5717VENDOR_ST_M_M25PE20:
13649 case FLASH_5717VENDOR_ST_A_M25PE20:
13650 case FLASH_5717VENDOR_ST_M_M45PE20:
13651 case FLASH_5717VENDOR_ST_A_M45PE20:
13652 case FLASH_5717VENDOR_ST_25USPT:
13653 case FLASH_5717VENDOR_ST_45USPT:
13654 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013655 tg3_flag_set(tp, NVRAM_BUFFERED);
13656 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013657
13658 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13659 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013660 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013661 /* Detect size with tg3_nvram_get_size() */
13662 break;
13663 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013664 case FLASH_5717VENDOR_ST_A_M45PE20:
13665 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13666 break;
13667 default:
13668 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13669 break;
13670 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013671 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013672 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013673 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013674 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013675 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013676
13677 tg3_nvram_get_pagesize(tp, nvcfg1);
13678 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013679 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013680}
13681
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013682static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013683{
13684 u32 nvcfg1, nvmpinstrp;
13685
13686 nvcfg1 = tr32(NVRAM_CFG1);
13687 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13688
13689 switch (nvmpinstrp) {
13690 case FLASH_5720_EEPROM_HD:
13691 case FLASH_5720_EEPROM_LD:
13692 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013693 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013694
13695 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13696 tw32(NVRAM_CFG1, nvcfg1);
13697 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13698 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13699 else
13700 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13701 return;
13702 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13703 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13704 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13705 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13706 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13707 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13708 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13709 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13710 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13711 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13712 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13713 case FLASH_5720VENDOR_ATMEL_45USPT:
13714 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013715 tg3_flag_set(tp, NVRAM_BUFFERED);
13716 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013717
13718 switch (nvmpinstrp) {
13719 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13720 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13721 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13722 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13723 break;
13724 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13725 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13726 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13727 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13728 break;
13729 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13730 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13731 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13732 break;
13733 default:
13734 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13735 break;
13736 }
13737 break;
13738 case FLASH_5720VENDOR_M_ST_M25PE10:
13739 case FLASH_5720VENDOR_M_ST_M45PE10:
13740 case FLASH_5720VENDOR_A_ST_M25PE10:
13741 case FLASH_5720VENDOR_A_ST_M45PE10:
13742 case FLASH_5720VENDOR_M_ST_M25PE20:
13743 case FLASH_5720VENDOR_M_ST_M45PE20:
13744 case FLASH_5720VENDOR_A_ST_M25PE20:
13745 case FLASH_5720VENDOR_A_ST_M45PE20:
13746 case FLASH_5720VENDOR_M_ST_M25PE40:
13747 case FLASH_5720VENDOR_M_ST_M45PE40:
13748 case FLASH_5720VENDOR_A_ST_M25PE40:
13749 case FLASH_5720VENDOR_A_ST_M45PE40:
13750 case FLASH_5720VENDOR_M_ST_M25PE80:
13751 case FLASH_5720VENDOR_M_ST_M45PE80:
13752 case FLASH_5720VENDOR_A_ST_M25PE80:
13753 case FLASH_5720VENDOR_A_ST_M45PE80:
13754 case FLASH_5720VENDOR_ST_25USPT:
13755 case FLASH_5720VENDOR_ST_45USPT:
13756 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013757 tg3_flag_set(tp, NVRAM_BUFFERED);
13758 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013759
13760 switch (nvmpinstrp) {
13761 case FLASH_5720VENDOR_M_ST_M25PE20:
13762 case FLASH_5720VENDOR_M_ST_M45PE20:
13763 case FLASH_5720VENDOR_A_ST_M25PE20:
13764 case FLASH_5720VENDOR_A_ST_M45PE20:
13765 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13766 break;
13767 case FLASH_5720VENDOR_M_ST_M25PE40:
13768 case FLASH_5720VENDOR_M_ST_M45PE40:
13769 case FLASH_5720VENDOR_A_ST_M25PE40:
13770 case FLASH_5720VENDOR_A_ST_M45PE40:
13771 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13772 break;
13773 case FLASH_5720VENDOR_M_ST_M25PE80:
13774 case FLASH_5720VENDOR_M_ST_M45PE80:
13775 case FLASH_5720VENDOR_A_ST_M25PE80:
13776 case FLASH_5720VENDOR_A_ST_M45PE80:
13777 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13778 break;
13779 default:
13780 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13781 break;
13782 }
13783 break;
13784 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013785 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013786 return;
13787 }
13788
13789 tg3_nvram_get_pagesize(tp, nvcfg1);
13790 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013791 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013792}
13793
Linus Torvalds1da177e2005-04-16 15:20:36 -070013794/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013795static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013796{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013797 tw32_f(GRC_EEPROM_ADDR,
13798 (EEPROM_ADDR_FSM_RESET |
13799 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13800 EEPROM_ADDR_CLKPERD_SHIFT)));
13801
Michael Chan9d57f012006-12-07 00:23:25 -080013802 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013803
13804 /* Enable seeprom accesses. */
13805 tw32_f(GRC_LOCAL_CTRL,
13806 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13807 udelay(100);
13808
13809 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13810 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013811 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013812
Michael Chanec41c7d2006-01-17 02:40:55 -080013813 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013814 netdev_warn(tp->dev,
13815 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013816 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013817 return;
13818 }
Michael Chane6af3012005-04-21 17:12:05 -070013819 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013820
Matt Carlson989a9d22007-05-05 11:51:05 -070013821 tp->nvram_size = 0;
13822
Michael Chan361b4ac2005-04-21 17:11:21 -070013823 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13824 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013825 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13826 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013827 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013828 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013830 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013831 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13832 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013833 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13834 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013835 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013836 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013837 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013838 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013840 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013841 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13842 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013843 else
13844 tg3_get_nvram_info(tp);
13845
Matt Carlson989a9d22007-05-05 11:51:05 -070013846 if (tp->nvram_size == 0)
13847 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013848
Michael Chane6af3012005-04-21 17:12:05 -070013849 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013850 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013851
13852 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013853 tg3_flag_clear(tp, NVRAM);
13854 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013855
13856 tg3_get_eeprom_size(tp);
13857 }
13858}
13859
Linus Torvalds1da177e2005-04-16 15:20:36 -070013860struct subsys_tbl_ent {
13861 u16 subsys_vendor, subsys_devid;
13862 u32 phy_id;
13863};
13864
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013865static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013866 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013867 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013868 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013869 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013870 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013871 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013872 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013873 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13874 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13875 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013876 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013877 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013878 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013879 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13880 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013882 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, 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_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013886 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013888 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013889
13890 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013891 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013892 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013893 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013894 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013895 { TG3PCI_SUBVENDOR_ID_3COM,
13896 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13897 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013898 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013899 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013900 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013901
13902 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013903 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013904 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013905 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013906 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013907 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013908 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013909 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013910 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013911
13912 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013913 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013914 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013915 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013916 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013917 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13918 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13919 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013920 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013921 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013922 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923
13924 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013925 { TG3PCI_SUBVENDOR_ID_IBM,
13926 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013927};
13928
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013929static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013930{
13931 int i;
13932
13933 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13934 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13935 tp->pdev->subsystem_vendor) &&
13936 (subsys_id_to_phy_id[i].subsys_devid ==
13937 tp->pdev->subsystem_device))
13938 return &subsys_id_to_phy_id[i];
13939 }
13940 return NULL;
13941}
13942
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013943static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013944{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013945 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013946
Matt Carlson79eb6902010-02-17 15:17:03 +000013947 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013948 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13949
Gary Zambranoa85feb82007-05-05 11:52:19 -070013950 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13952 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013953
Michael Chanb5d37722006-09-27 16:06:21 -070013954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013955 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013956 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13957 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013958 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013959 val = tr32(VCPU_CFGSHDW);
13960 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013961 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013962 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013963 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013964 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013965 device_set_wakeup_enable(&tp->pdev->dev, true);
13966 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013967 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013968 }
13969
Linus Torvalds1da177e2005-04-16 15:20:36 -070013970 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13971 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13972 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013973 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013974 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975
13976 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13977 tp->nic_sram_data_cfg = nic_cfg;
13978
13979 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13980 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013981 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13982 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13983 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 (ver > 0) && (ver < 0x100))
13985 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13986
Matt Carlsona9daf362008-05-25 23:49:44 -070013987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13988 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13989
Linus Torvalds1da177e2005-04-16 15:20:36 -070013990 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13991 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13992 eeprom_phy_serdes = 1;
13993
13994 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13995 if (nic_phy_id != 0) {
13996 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13997 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13998
13999 eeprom_phy_id = (id1 >> 16) << 10;
14000 eeprom_phy_id |= (id2 & 0xfc00) << 16;
14001 eeprom_phy_id |= (id2 & 0x03ff) << 0;
14002 } else
14003 eeprom_phy_id = 0;
14004
Michael Chan7d0c41e2005-04-21 17:06:20 -070014005 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070014006 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000014007 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014008 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000014009 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014010 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070014011 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070014012
Joe Perches63c3a662011-04-26 08:12:10 +000014013 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070014014 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
14015 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070014016 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014017 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
14018
14019 switch (led_cfg) {
14020 default:
14021 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
14022 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14023 break;
14024
14025 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
14026 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14027 break;
14028
14029 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
14030 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070014031
14032 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
14033 * read on some older 5700/5701 bootcode.
14034 */
14035 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14036 ASIC_REV_5700 ||
14037 GET_ASIC_REV(tp->pci_chip_rev_id) ==
14038 ASIC_REV_5701)
14039 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
14040
Linus Torvalds1da177e2005-04-16 15:20:36 -070014041 break;
14042
14043 case SHASTA_EXT_LED_SHARED:
14044 tp->led_ctrl = LED_CTRL_MODE_SHARED;
14045 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
14046 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
14047 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14048 LED_CTRL_MODE_PHY_2);
14049 break;
14050
14051 case SHASTA_EXT_LED_MAC:
14052 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
14053 break;
14054
14055 case SHASTA_EXT_LED_COMBO:
14056 tp->led_ctrl = LED_CTRL_MODE_COMBO;
14057 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
14058 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
14059 LED_CTRL_MODE_PHY_2);
14060 break;
14061
Stephen Hemminger855e1112008-04-16 16:37:28 -070014062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014063
14064 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14065 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
14066 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
14067 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
14068
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014069 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
14070 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080014071
Michael Chan9d26e212006-12-07 00:21:14 -080014072 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014074 if ((tp->pdev->subsystem_vendor ==
14075 PCI_VENDOR_ID_ARIMA) &&
14076 (tp->pdev->subsystem_device == 0x205a ||
14077 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000014078 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080014079 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014080 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
14081 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080014082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014083
14084 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000014085 tg3_flag_set(tp, ENABLE_ASF);
14086 if (tg3_flag(tp, 5750_PLUS))
14087 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014089
14090 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014091 tg3_flag(tp, 5750_PLUS))
14092 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080014093
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014094 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070014095 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000014096 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014097
Joe Perches63c3a662011-04-26 08:12:10 +000014098 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014099 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014100 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000014101 device_set_wakeup_enable(&tp->pdev->dev, true);
14102 }
Matt Carlson0527ba32007-10-10 18:03:30 -070014103
Linus Torvalds1da177e2005-04-16 15:20:36 -070014104 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014105 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106
14107 /* serdes signal pre-emphasis in register 0x590 set by */
14108 /* bootcode if bit 18 is set */
14109 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014110 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070014111
Joe Perches63c3a662011-04-26 08:12:10 +000014112 if ((tg3_flag(tp, 57765_PLUS) ||
14113 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14114 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080014115 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014116 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080014117
Joe Perches63c3a662011-04-26 08:12:10 +000014118 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000014119 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014120 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070014121 u32 cfg3;
14122
14123 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
14124 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000014125 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070014126 }
Matt Carlsona9daf362008-05-25 23:49:44 -070014127
Matt Carlson14417062010-02-17 15:16:59 +000014128 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000014129 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070014130 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014131 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070014132 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000014133 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080014135done:
Joe Perches63c3a662011-04-26 08:12:10 +000014136 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014137 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000014139 else
14140 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070014141}
14142
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014143static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014144{
14145 int i;
14146 u32 val;
14147
14148 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
14149 tw32(OTP_CTRL, cmd);
14150
14151 /* Wait for up to 1 ms for command to execute. */
14152 for (i = 0; i < 100; i++) {
14153 val = tr32(OTP_STATUS);
14154 if (val & OTP_STATUS_CMD_DONE)
14155 break;
14156 udelay(10);
14157 }
14158
14159 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
14160}
14161
14162/* Read the gphy configuration from the OTP region of the chip. The gphy
14163 * configuration is a 32-bit value that straddles the alignment boundary.
14164 * We do two 32-bit reads and then shift and merge the results.
14165 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014166static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014167{
14168 u32 bhalf_otp, thalf_otp;
14169
14170 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
14171
14172 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
14173 return 0;
14174
14175 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
14176
14177 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14178 return 0;
14179
14180 thalf_otp = tr32(OTP_READ_DATA);
14181
14182 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
14183
14184 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
14185 return 0;
14186
14187 bhalf_otp = tr32(OTP_READ_DATA);
14188
14189 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
14190}
14191
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014192static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000014193{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000014194 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014195
14196 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
14197 adv |= ADVERTISED_1000baseT_Half |
14198 ADVERTISED_1000baseT_Full;
14199
14200 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14201 adv |= ADVERTISED_100baseT_Half |
14202 ADVERTISED_100baseT_Full |
14203 ADVERTISED_10baseT_Half |
14204 ADVERTISED_10baseT_Full |
14205 ADVERTISED_TP;
14206 else
14207 adv |= ADVERTISED_FIBRE;
14208
14209 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000014210 tp->link_config.speed = SPEED_UNKNOWN;
14211 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014212 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000014213 tp->link_config.active_speed = SPEED_UNKNOWN;
14214 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000014215
14216 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000014217}
14218
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014219static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070014220{
14221 u32 hw_phy_id_1, hw_phy_id_2;
14222 u32 hw_phy_id, hw_phy_id_masked;
14223 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014224
Matt Carlsone256f8a2011-03-09 16:58:24 +000014225 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000014226 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000014227 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
14228
Michael Chan8151ad52012-07-29 19:15:41 +000014229 if (tg3_flag(tp, ENABLE_APE)) {
14230 switch (tp->pci_fn) {
14231 case 0:
14232 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
14233 break;
14234 case 1:
14235 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
14236 break;
14237 case 2:
14238 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
14239 break;
14240 case 3:
14241 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
14242 break;
14243 }
14244 }
14245
Joe Perches63c3a662011-04-26 08:12:10 +000014246 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014247 return tg3_phy_init(tp);
14248
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010014250 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014251 */
14252 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000014253 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000014254 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255 } else {
14256 /* Now read the physical PHY_ID from the chip and verify
14257 * that it is sane. If it doesn't look good, we fall back
14258 * to either the hard-coded table based PHY_ID and failing
14259 * that the value found in the eeprom area.
14260 */
14261 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
14262 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
14263
14264 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
14265 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
14266 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
14267
Matt Carlson79eb6902010-02-17 15:17:03 +000014268 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014269 }
14270
Matt Carlson79eb6902010-02-17 15:17:03 +000014271 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014272 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000014273 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014274 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070014275 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014276 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000014278 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070014279 /* Do nothing, phy ID already set up in
14280 * tg3_get_eeprom_hw_cfg().
14281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282 } else {
14283 struct subsys_tbl_ent *p;
14284
14285 /* No eeprom signature? Try the hardcoded
14286 * subsys device table.
14287 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014288 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014289 if (!p)
14290 return -ENODEV;
14291
14292 tp->phy_id = p->phy_id;
14293 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014294 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014295 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014296 }
14297 }
14298
Matt Carlsona6b68da2010-12-06 08:28:52 +000014299 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000014300 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
14302 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000014303 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
14304 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
14305 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014306 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14307
Matt Carlsone256f8a2011-03-09 16:58:24 +000014308 tg3_phy_init_link_config(tp);
14309
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014310 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014311 !tg3_flag(tp, ENABLE_APE) &&
14312 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014313 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314
14315 tg3_readphy(tp, MII_BMSR, &bmsr);
14316 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14317 (bmsr & BMSR_LSTATUS))
14318 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014319
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320 err = tg3_phy_reset(tp);
14321 if (err)
14322 return err;
14323
Matt Carlson42b64a42011-05-19 12:12:49 +000014324 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014326 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014327 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14328 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329
14330 tg3_writephy(tp, MII_BMCR,
14331 BMCR_ANENABLE | BMCR_ANRESTART);
14332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 }
14334
14335skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014336 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337 err = tg3_init_5401phy_dsp(tp);
14338 if (err)
14339 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014340
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341 err = tg3_init_5401phy_dsp(tp);
14342 }
14343
Linus Torvalds1da177e2005-04-16 15:20:36 -070014344 return err;
14345}
14346
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014347static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014348{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014349 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014350 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014351 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014352 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014353
Matt Carlson535a4902011-07-20 10:20:56 +000014354 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014355 if (!vpd_data)
14356 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357
Matt Carlson535a4902011-07-20 10:20:56 +000014358 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014359 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014361
14362 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14363 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14364 i += PCI_VPD_LRDT_TAG_SIZE;
14365
Matt Carlson535a4902011-07-20 10:20:56 +000014366 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014367 goto out_not_found;
14368
Matt Carlson184b8902010-04-05 10:19:25 +000014369 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14370 PCI_VPD_RO_KEYWORD_MFR_ID);
14371 if (j > 0) {
14372 len = pci_vpd_info_field_size(&vpd_data[j]);
14373
14374 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14375 if (j + len > block_end || len != 4 ||
14376 memcmp(&vpd_data[j], "1028", 4))
14377 goto partno;
14378
14379 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14380 PCI_VPD_RO_KEYWORD_VENDOR0);
14381 if (j < 0)
14382 goto partno;
14383
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)
14388 goto partno;
14389
14390 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014391 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014392 }
14393
14394partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014395 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14396 PCI_VPD_RO_KEYWORD_PARTNO);
14397 if (i < 0)
14398 goto out_not_found;
14399
14400 len = pci_vpd_info_field_size(&vpd_data[i]);
14401
14402 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14403 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014404 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014405 goto out_not_found;
14406
14407 memcpy(tp->board_part_number, &vpd_data[i], len);
14408
Linus Torvalds1da177e2005-04-16 15:20:36 -070014409out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014410 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014411 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014412 return;
14413
14414out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000014415 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014416 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014418 strcpy(tp->board_part_number, "BCM5717");
14419 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14420 strcpy(tp->board_part_number, "BCM5718");
14421 else
14422 goto nomatch;
14423 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14424 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14425 strcpy(tp->board_part_number, "BCM57780");
14426 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14427 strcpy(tp->board_part_number, "BCM57760");
14428 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14429 strcpy(tp->board_part_number, "BCM57790");
14430 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14431 strcpy(tp->board_part_number, "BCM57788");
14432 else
14433 goto nomatch;
14434 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14435 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14436 strcpy(tp->board_part_number, "BCM57761");
14437 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14438 strcpy(tp->board_part_number, "BCM57765");
14439 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14440 strcpy(tp->board_part_number, "BCM57781");
14441 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14442 strcpy(tp->board_part_number, "BCM57785");
14443 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14444 strcpy(tp->board_part_number, "BCM57791");
14445 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14446 strcpy(tp->board_part_number, "BCM57795");
14447 else
14448 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000014449 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
14450 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14451 strcpy(tp->board_part_number, "BCM57762");
14452 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14453 strcpy(tp->board_part_number, "BCM57766");
14454 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14455 strcpy(tp->board_part_number, "BCM57782");
14456 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14457 strcpy(tp->board_part_number, "BCM57786");
14458 else
14459 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000014460 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014461 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014462 } else {
14463nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014464 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014466}
14467
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014468static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014469{
14470 u32 val;
14471
Matt Carlsone4f34112009-02-25 14:25:00 +000014472 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014473 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014474 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014475 val != 0)
14476 return 0;
14477
14478 return 1;
14479}
14480
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014481static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014482{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014483 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014484 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014485 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014486
14487 if (tg3_nvram_read(tp, 0xc, &offset) ||
14488 tg3_nvram_read(tp, 0x4, &start))
14489 return;
14490
14491 offset = tg3_nvram_logical_addr(tp, offset);
14492
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014493 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014494 return;
14495
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014496 if ((val & 0xfc000000) == 0x0c000000) {
14497 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014498 return;
14499
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014500 if (val == 0)
14501 newver = true;
14502 }
14503
Matt Carlson75f99362010-04-05 10:19:24 +000014504 dst_off = strlen(tp->fw_ver);
14505
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014506 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014507 if (TG3_VER_SIZE - dst_off < 16 ||
14508 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014509 return;
14510
14511 offset = offset + ver_offset - start;
14512 for (i = 0; i < 16; i += 4) {
14513 __be32 v;
14514 if (tg3_nvram_read_be32(tp, offset + i, &v))
14515 return;
14516
Matt Carlson75f99362010-04-05 10:19:24 +000014517 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014518 }
14519 } else {
14520 u32 major, minor;
14521
14522 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14523 return;
14524
14525 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14526 TG3_NVM_BCVER_MAJSFT;
14527 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014528 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14529 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014530 }
14531}
14532
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014533static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014534{
14535 u32 val, major, minor;
14536
14537 /* Use native endian representation */
14538 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14539 return;
14540
14541 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14542 TG3_NVM_HWSB_CFG1_MAJSFT;
14543 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14544 TG3_NVM_HWSB_CFG1_MINSFT;
14545
14546 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14547}
14548
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014549static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014550{
14551 u32 offset, major, minor, build;
14552
Matt Carlson75f99362010-04-05 10:19:24 +000014553 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014554
14555 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14556 return;
14557
14558 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14559 case TG3_EEPROM_SB_REVISION_0:
14560 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14561 break;
14562 case TG3_EEPROM_SB_REVISION_2:
14563 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14564 break;
14565 case TG3_EEPROM_SB_REVISION_3:
14566 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14567 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014568 case TG3_EEPROM_SB_REVISION_4:
14569 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14570 break;
14571 case TG3_EEPROM_SB_REVISION_5:
14572 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14573 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014574 case TG3_EEPROM_SB_REVISION_6:
14575 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14576 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014577 default:
14578 return;
14579 }
14580
Matt Carlsone4f34112009-02-25 14:25:00 +000014581 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014582 return;
14583
14584 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14585 TG3_EEPROM_SB_EDH_BLD_SHFT;
14586 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14587 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14588 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14589
14590 if (minor > 99 || build > 26)
14591 return;
14592
Matt Carlson75f99362010-04-05 10:19:24 +000014593 offset = strlen(tp->fw_ver);
14594 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14595 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014596
14597 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014598 offset = strlen(tp->fw_ver);
14599 if (offset < TG3_VER_SIZE - 1)
14600 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014601 }
14602}
14603
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014604static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014605{
14606 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014607 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014608
14609 for (offset = TG3_NVM_DIR_START;
14610 offset < TG3_NVM_DIR_END;
14611 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014612 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014613 return;
14614
14615 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14616 break;
14617 }
14618
14619 if (offset == TG3_NVM_DIR_END)
14620 return;
14621
Joe Perches63c3a662011-04-26 08:12:10 +000014622 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014623 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014624 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014625 return;
14626
Matt Carlsone4f34112009-02-25 14:25:00 +000014627 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014628 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014629 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014630 return;
14631
14632 offset += val - start;
14633
Matt Carlsonacd9c112009-02-25 14:26:33 +000014634 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014635
Matt Carlsonacd9c112009-02-25 14:26:33 +000014636 tp->fw_ver[vlen++] = ',';
14637 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014638
14639 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014640 __be32 v;
14641 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014642 return;
14643
Al Virob9fc7dc2007-12-17 22:59:57 -080014644 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014645
Matt Carlsonacd9c112009-02-25 14:26:33 +000014646 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14647 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014648 break;
14649 }
14650
Matt Carlsonacd9c112009-02-25 14:26:33 +000014651 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14652 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014653 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014654}
14655
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014656static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014657{
Matt Carlson7fd76442009-02-25 14:27:20 +000014658 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014659
14660 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14661 if (apedata != APE_SEG_SIG_MAGIC)
14662 return;
14663
14664 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14665 if (!(apedata & APE_FW_STATUS_READY))
14666 return;
14667
Michael Chan165f4d12012-07-16 16:23:59 +000014668 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14669 tg3_flag_set(tp, APE_HAS_NCSI);
14670}
14671
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014672static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014673{
14674 int vlen;
14675 u32 apedata;
14676 char *fwtype;
14677
Matt Carlson7fd76442009-02-25 14:27:20 +000014678 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14679
Michael Chan165f4d12012-07-16 16:23:59 +000014680 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014681 fwtype = "NCSI";
Michael Chan165f4d12012-07-16 16:23:59 +000014682 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014683 fwtype = "DASH";
14684
Matt Carlson7fd76442009-02-25 14:27:20 +000014685 vlen = strlen(tp->fw_ver);
14686
Matt Carlsonecc79642010-08-02 11:26:01 +000014687 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14688 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014689 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14690 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14691 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14692 (apedata & APE_FW_VERSION_BLDMSK));
14693}
14694
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014695static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014696{
14697 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014698 bool vpd_vers = false;
14699
14700 if (tp->fw_ver[0] != 0)
14701 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014702
Joe Perches63c3a662011-04-26 08:12:10 +000014703 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014704 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000014705 return;
14706 }
14707
Matt Carlsonacd9c112009-02-25 14:26:33 +000014708 if (tg3_nvram_read(tp, 0, &val))
14709 return;
14710
14711 if (val == TG3_EEPROM_MAGIC)
14712 tg3_read_bc_ver(tp);
14713 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14714 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014715 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14716 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014717
Michael Chan165f4d12012-07-16 16:23:59 +000014718 if (tg3_flag(tp, ENABLE_ASF)) {
14719 if (tg3_flag(tp, ENABLE_APE)) {
14720 tg3_probe_ncsi(tp);
14721 if (!vpd_vers)
14722 tg3_read_dash_ver(tp);
14723 } else if (!vpd_vers) {
14724 tg3_read_mgmtfw_ver(tp);
14725 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014726 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014727
14728 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014729}
14730
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014731static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14732{
Joe Perches63c3a662011-04-26 08:12:10 +000014733 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014734 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014735 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014736 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014737 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014738 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014739}
14740
Matt Carlson41434702011-03-09 16:58:22 +000014741static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014742 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14743 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14744 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14745 { },
14746};
14747
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014748static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014749{
14750 struct pci_dev *peer;
14751 unsigned int func, devnr = tp->pdev->devfn & ~7;
14752
14753 for (func = 0; func < 8; func++) {
14754 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14755 if (peer && peer != tp->pdev)
14756 break;
14757 pci_dev_put(peer);
14758 }
14759 /* 5704 can be configured in single-port mode, set peer to
14760 * tp->pdev in that case.
14761 */
14762 if (!peer) {
14763 peer = tp->pdev;
14764 return peer;
14765 }
14766
14767 /*
14768 * We don't need to keep the refcount elevated; there's no way
14769 * to remove one half of this device without removing the other
14770 */
14771 pci_dev_put(peer);
14772
14773 return peer;
14774}
14775
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014776static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000014777{
14778 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14779 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14780 u32 reg;
14781
14782 /* All devices that use the alternate
14783 * ASIC REV location have a CPMU.
14784 */
14785 tg3_flag_set(tp, CPMU_PRESENT);
14786
14787 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000014788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000014789 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14790 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14791 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14792 reg = TG3PCI_GEN2_PRODID_ASICREV;
14793 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14796 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14797 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14798 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14799 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14800 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14801 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14802 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14803 reg = TG3PCI_GEN15_PRODID_ASICREV;
14804 else
14805 reg = TG3PCI_PRODID_ASICREV;
14806
14807 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14808 }
14809
14810 /* Wrong chip ID in 5752 A0. This code can be removed later
14811 * as A0 is not in production.
14812 */
14813 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14814 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14815
Michael Chan79d49692012-11-05 14:26:29 +000014816 if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
14817 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
14818
Matt Carlson42b123b2012-02-13 15:20:13 +000014819 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14820 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14821 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14822 tg3_flag_set(tp, 5717_PLUS);
14823
14824 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14825 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14826 tg3_flag_set(tp, 57765_CLASS);
14827
14828 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14829 tg3_flag_set(tp, 57765_PLUS);
14830
14831 /* Intentionally exclude ASIC_REV_5906 */
14832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14834 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14835 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14838 tg3_flag(tp, 57765_PLUS))
14839 tg3_flag_set(tp, 5755_PLUS);
14840
14841 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14843 tg3_flag_set(tp, 5780_CLASS);
14844
14845 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14848 tg3_flag(tp, 5755_PLUS) ||
14849 tg3_flag(tp, 5780_CLASS))
14850 tg3_flag_set(tp, 5750_PLUS);
14851
14852 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14853 tg3_flag(tp, 5750_PLUS))
14854 tg3_flag_set(tp, 5705_PLUS);
14855}
14856
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014857static bool tg3_10_100_only_device(struct tg3 *tp,
14858 const struct pci_device_id *ent)
14859{
14860 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
14861
14862 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14863 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14864 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14865 return true;
14866
14867 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
14868 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
14869 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
14870 return true;
14871 } else {
14872 return true;
14873 }
14874 }
14875
14876 return false;
14877}
14878
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +000014879static int tg3_get_invariants(struct tg3 *tp, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014880{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014881 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882 u32 pci_state_reg, grc_misc_cfg;
14883 u32 val;
14884 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014885 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014886
Linus Torvalds1da177e2005-04-16 15:20:36 -070014887 /* Force memory write invalidate off. If we leave it on,
14888 * then on 5700_BX chips we have to enable a workaround.
14889 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14890 * to match the cacheline size. The Broadcom driver have this
14891 * workaround but turns MWI off all the times so never uses
14892 * it. This seems to suggest that the workaround is insufficient.
14893 */
14894 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14895 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14896 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14897
Matt Carlson16821282011-07-13 09:27:28 +000014898 /* Important! -- Make sure register accesses are byteswapped
14899 * correctly. Also, for those chips that require it, make
14900 * sure that indirect register accesses are enabled before
14901 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014902 */
14903 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14904 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014905 tp->misc_host_ctrl |= (misc_ctrl_reg &
14906 MISC_HOST_CTRL_CHIPREV);
14907 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14908 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014909
Matt Carlson42b123b2012-02-13 15:20:13 +000014910 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014911
Michael Chan68929142005-08-09 20:17:14 -070014912 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14913 * we need to disable memory and use config. cycles
14914 * only to access all registers. The 5702/03 chips
14915 * can mistakenly decode the special cycles from the
14916 * ICH chipsets as memory write cycles, causing corruption
14917 * of register and memory space. Only certain ICH bridges
14918 * will drive special cycles with non-zero data during the
14919 * address phase which can fall within the 5703's address
14920 * range. This is not an ICH bug as the PCI spec allows
14921 * non-zero address during special cycles. However, only
14922 * these ICH bridges are known to drive non-zero addresses
14923 * during special cycles.
14924 *
14925 * Since special cycles do not cross PCI bridges, we only
14926 * enable this workaround if the 5703 is on the secondary
14927 * bus of these ICH bridges.
14928 */
14929 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14930 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14931 static struct tg3_dev_id {
14932 u32 vendor;
14933 u32 device;
14934 u32 rev;
14935 } ich_chipsets[] = {
14936 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14937 PCI_ANY_ID },
14938 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14939 PCI_ANY_ID },
14940 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14941 0xa },
14942 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14943 PCI_ANY_ID },
14944 { },
14945 };
14946 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14947 struct pci_dev *bridge = NULL;
14948
14949 while (pci_id->vendor != 0) {
14950 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14951 bridge);
14952 if (!bridge) {
14953 pci_id++;
14954 continue;
14955 }
14956 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014957 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014958 continue;
14959 }
14960 if (bridge->subordinate &&
14961 (bridge->subordinate->number ==
14962 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014963 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014964 pci_dev_put(bridge);
14965 break;
14966 }
14967 }
14968 }
14969
Matt Carlson6ff6f812011-05-19 12:12:54 +000014970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba12008-04-19 18:12:33 -070014971 static struct tg3_dev_id {
14972 u32 vendor;
14973 u32 device;
14974 } bridge_chipsets[] = {
14975 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14976 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14977 { },
14978 };
14979 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14980 struct pci_dev *bridge = NULL;
14981
14982 while (pci_id->vendor != 0) {
14983 bridge = pci_get_device(pci_id->vendor,
14984 pci_id->device,
14985 bridge);
14986 if (!bridge) {
14987 pci_id++;
14988 continue;
14989 }
14990 if (bridge->subordinate &&
14991 (bridge->subordinate->number <=
14992 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070014993 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba12008-04-19 18:12:33 -070014994 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014995 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba12008-04-19 18:12:33 -070014996 pci_dev_put(bridge);
14997 break;
14998 }
14999 }
15000 }
15001
Michael Chan4a29cc22006-03-19 13:21:12 -080015002 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
15003 * DMA addresses > 40-bit. This bridge may have other additional
15004 * 57xx devices behind it in some 4-port NIC designs for example.
15005 * Any tg3 device found behind the bridge will also need the 40-bit
15006 * DMA workaround.
15007 */
Matt Carlson42b123b2012-02-13 15:20:13 +000015008 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015009 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070015010 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000015011 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080015012 struct pci_dev *bridge = NULL;
15013
15014 do {
15015 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
15016 PCI_DEVICE_ID_SERVERWORKS_EPB,
15017 bridge);
15018 if (bridge && bridge->subordinate &&
15019 (bridge->subordinate->number <=
15020 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070015021 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080015022 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015023 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080015024 pci_dev_put(bridge);
15025 break;
15026 }
15027 } while (bridge);
15028 }
Michael Chan4cf78e42005-07-25 12:29:19 -070015029
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000015031 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070015032 tp->pdev_peer = tg3_find_peer(tp);
15033
Matt Carlson507399f2009-11-13 13:03:37 +000015034 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000015035 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000015036 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000015037 else if (tg3_flag(tp, 57765_PLUS))
15038 tg3_flag_set(tp, HW_TSO_3);
15039 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015040 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015041 tg3_flag_set(tp, HW_TSO_2);
15042 else if (tg3_flag(tp, 5750_PLUS)) {
15043 tg3_flag_set(tp, HW_TSO_1);
15044 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
15046 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000015047 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015048 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15049 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
15050 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015051 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000015052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
15053 tp->fw_needed = FIRMWARE_TG3TSO5;
15054 else
15055 tp->fw_needed = FIRMWARE_TG3TSO;
15056 }
15057
Matt Carlsondabc5c62011-05-19 12:12:52 +000015058 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015059 if (tg3_flag(tp, HW_TSO_1) ||
15060 tg3_flag(tp, HW_TSO_2) ||
15061 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015062 tp->fw_needed) {
15063 /* For firmware TSO, assume ASF is disabled.
15064 * We'll disable TSO later if we discover ASF
15065 * is enabled in tg3_get_eeprom_hw_cfg().
15066 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000015067 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015068 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000015069 tg3_flag_clear(tp, TSO_CAPABLE);
15070 tg3_flag_clear(tp, TSO_BUG);
15071 tp->fw_needed = NULL;
15072 }
15073
15074 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
15075 tp->fw_needed = FIRMWARE_TG3;
15076
Matt Carlson507399f2009-11-13 13:03:37 +000015077 tp->irq_max = 1;
15078
Joe Perches63c3a662011-04-26 08:12:10 +000015079 if (tg3_flag(tp, 5750_PLUS)) {
15080 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015081 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
15082 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
15083 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
15084 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
15085 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000015086 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070015087
Joe Perches63c3a662011-04-26 08:12:10 +000015088 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070015089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015090 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070015091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015092
Joe Perches63c3a662011-04-26 08:12:10 +000015093 if (tg3_flag(tp, 57765_PLUS)) {
15094 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000015095 tp->irq_max = TG3_IRQ_MAX_VECS;
15096 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015097 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000015098
Michael Chan91024262012-09-28 07:12:38 +000015099 tp->txq_max = 1;
15100 tp->rxq_max = 1;
15101 if (tp->irq_max > 1) {
15102 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
15103 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
15104
15105 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15107 tp->txq_max = tp->irq_max - 1;
15108 }
15109
Matt Carlsonb7abee62012-06-07 12:56:54 +000015110 if (tg3_flag(tp, 5755_PLUS) ||
15111 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000015112 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015113
Matt Carlsone31aa982011-07-27 14:20:53 +000015114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000015115 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000015116
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000015117 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15118 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000015120 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000015121
Joe Perches63c3a662011-04-26 08:12:10 +000015122 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000015123 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000015124 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000015125
Joe Perches63c3a662011-04-26 08:12:10 +000015126 if (!tg3_flag(tp, 5705_PLUS) ||
15127 tg3_flag(tp, 5780_CLASS) ||
15128 tg3_flag(tp, USE_JUMBO_BDFLAG))
15129 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070015130
Matt Carlson52f44902008-11-21 17:17:04 -080015131 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15132 &pci_state_reg);
15133
Jon Mason708ebb3a2011-06-27 12:56:50 +000015134 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015135 u16 lnkctl;
15136
Joe Perches63c3a662011-04-26 08:12:10 +000015137 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080015138
Jiang Liu0f49bfb2012-08-20 13:28:20 -060015139 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015140 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000015141 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
15142 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000015143 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000015144 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000015145 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080015146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015147 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000015148 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
15149 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000015150 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000015151 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000015152 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080015153 }
Matt Carlson52f44902008-11-21 17:17:04 -080015154 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000015155 /* BCM5785 devices are effectively PCIe devices, and should
15156 * follow PCIe codepaths, but do not have a PCIe capabilities
15157 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000015158 */
Joe Perches63c3a662011-04-26 08:12:10 +000015159 tg3_flag_set(tp, PCI_EXPRESS);
15160 } else if (!tg3_flag(tp, 5705_PLUS) ||
15161 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080015162 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
15163 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000015164 dev_err(&tp->pdev->dev,
15165 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080015166 return -EIO;
15167 }
15168
15169 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000015170 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080015171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015172
Michael Chan399de502005-10-03 14:02:39 -070015173 /* If we have an AMD 762 or VIA K8T800 chipset, write
15174 * reordering to the mailbox registers done by the host
15175 * controller can cause major troubles. We read back from
15176 * every mailbox register write to force the writes to be
15177 * posted to the chip in order.
15178 */
Matt Carlson41434702011-03-09 16:58:22 +000015179 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000015180 !tg3_flag(tp, PCI_EXPRESS))
15181 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070015182
Matt Carlson69fc4052008-12-21 20:19:57 -080015183 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
15184 &tp->pci_cacheline_sz);
15185 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15186 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
15188 tp->pci_lat_timer < 64) {
15189 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080015190 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
15191 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 }
15193
Matt Carlson16821282011-07-13 09:27:28 +000015194 /* Important! -- It is critical that the PCI-X hw workaround
15195 * situation is decided before the first MMIO register access.
15196 */
Matt Carlson52f44902008-11-21 17:17:04 -080015197 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
15198 /* 5700 BX chips need to have their TX producer index
15199 * mailboxes written twice to workaround a bug.
15200 */
Joe Perches63c3a662011-04-26 08:12:10 +000015201 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070015202
Matt Carlson52f44902008-11-21 17:17:04 -080015203 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204 *
15205 * The workaround is to use indirect register accesses
15206 * for all chip writes not to mailbox registers.
15207 */
Joe Perches63c3a662011-04-26 08:12:10 +000015208 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015209 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015210
Joe Perches63c3a662011-04-26 08:12:10 +000015211 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015212
15213 /* The chip can have it's power management PCI config
15214 * space registers clobbered due to this bug.
15215 * So explicitly force the chip into D0 here.
15216 */
Matt Carlson9974a352007-10-07 23:27:28 -070015217 pci_read_config_dword(tp->pdev,
15218 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015219 &pm_reg);
15220 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
15221 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070015222 pci_write_config_dword(tp->pdev,
15223 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224 pm_reg);
15225
15226 /* Also, force SERR#/PERR# in PCI command. */
15227 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15228 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
15229 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15230 }
15231 }
15232
Linus Torvalds1da177e2005-04-16 15:20:36 -070015233 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015234 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015235 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000015236 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237
15238 /* Chip-specific fixup from Broadcom driver */
15239 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
15240 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
15241 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
15242 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
15243 }
15244
Michael Chan1ee582d2005-08-09 20:16:46 -070015245 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070015246 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015247 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070015248 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070015249 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070015250 tp->write32_tx_mbox = tg3_write32;
15251 tp->write32_rx_mbox = tg3_write32;
15252
15253 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000015254 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070015255 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015256 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015257 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070015258 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
15259 /*
15260 * Back to back register writes can cause problems on these
15261 * chips, the workaround is to read back all reg writes
15262 * except those to mailbox regs.
15263 *
15264 * See tg3_write_indirect_reg32().
15265 */
Michael Chan1ee582d2005-08-09 20:16:46 -070015266 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070015267 }
15268
Joe Perches63c3a662011-04-26 08:12:10 +000015269 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070015270 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000015271 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070015272 tp->write32_rx_mbox = tg3_write_flush_reg32;
15273 }
Michael Chan20094932005-08-09 20:16:32 -070015274
Joe Perches63c3a662011-04-26 08:12:10 +000015275 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070015276 tp->read32 = tg3_read_indirect_reg32;
15277 tp->write32 = tg3_write_indirect_reg32;
15278 tp->read32_mbox = tg3_read_indirect_mbox;
15279 tp->write32_mbox = tg3_write_indirect_mbox;
15280 tp->write32_tx_mbox = tg3_write_indirect_mbox;
15281 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15282
15283 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015284 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015285
15286 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15287 pci_cmd &= ~PCI_COMMAND_MEMORY;
15288 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15289 }
Michael Chanb5d37722006-09-27 16:06:21 -070015290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15291 tp->read32_mbox = tg3_read32_mbox_5906;
15292 tp->write32_mbox = tg3_write32_mbox_5906;
15293 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15294 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15295 }
Michael Chan68929142005-08-09 20:17:14 -070015296
Michael Chanbbadf502006-04-06 21:46:34 -070015297 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015298 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070015299 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070015300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015301 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015302
Matt Carlson16821282011-07-13 09:27:28 +000015303 /* The memory arbiter has to be enabled in order for SRAM accesses
15304 * to succeed. Normally on powerup the tg3 chip firmware will make
15305 * sure it is enabled, but other entities such as system netboot
15306 * code might disable it.
15307 */
15308 val = tr32(MEMARB_MODE);
15309 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15310
Matt Carlson9dc5e342011-11-04 09:15:02 +000015311 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
15312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
15313 tg3_flag(tp, 5780_CLASS)) {
15314 if (tg3_flag(tp, PCIX_MODE)) {
15315 pci_read_config_dword(tp->pdev,
15316 tp->pcix_cap + PCI_X_STATUS,
15317 &val);
15318 tp->pci_fn = val & 0x7;
15319 }
15320 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
15321 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15322 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15323 NIC_SRAM_CPMUSTAT_SIG) {
15324 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
15325 tp->pci_fn = tp->pci_fn ? 1 : 0;
15326 }
15327 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
15329 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15330 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15331 NIC_SRAM_CPMUSTAT_SIG) {
15332 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15333 TG3_CPMU_STATUS_FSHFT_5719;
15334 }
Matt Carlson69f11c92011-07-13 09:27:30 +000015335 }
15336
Michael Chan7d0c41e2005-04-21 17:06:20 -070015337 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015338 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015339 * determined before calling tg3_set_power_state() so that
15340 * we know whether or not to switch out of Vaux power.
15341 * When the flag is set, it means that GPIO1 is used for eeprom
15342 * write protect and also implies that it is a LOM where GPIOs
15343 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015344 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015345 tg3_get_eeprom_hw_cfg(tp);
15346
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015347 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15348 tg3_flag_clear(tp, TSO_CAPABLE);
15349 tg3_flag_clear(tp, TSO_BUG);
15350 tp->fw_needed = NULL;
15351 }
15352
Joe Perches63c3a662011-04-26 08:12:10 +000015353 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015354 /* Allow reads and writes to the
15355 * APE register and memory space.
15356 */
15357 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015358 PCISTATE_ALLOW_APE_SHMEM_WR |
15359 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015360 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15361 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015362
15363 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015364 }
15365
Matt Carlson16821282011-07-13 09:27:28 +000015366 /* Set up tp->grc_local_ctrl before calling
15367 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15368 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015369 * It is also used as eeprom write protect on LOMs.
15370 */
15371 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015373 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015374 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15375 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015376 /* Unused GPIO3 must be driven as output on 5752 because there
15377 * are no pull-up resistors on unused GPIO pins.
15378 */
15379 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
15380 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015381
Matt Carlson321d32a2008-11-21 17:22:19 -080015382 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000015383 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015384 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015385 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15386
Matt Carlson8d519ab2009-04-20 06:58:01 +000015387 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15388 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015389 /* Turn off the debug UART. */
15390 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015391 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015392 /* Keep VMain power. */
15393 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15394 GRC_LCLCTRL_GPIO_OUTPUT0;
15395 }
15396
Matt Carlson16821282011-07-13 09:27:28 +000015397 /* Switch out of Vaux if it is a NIC */
15398 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015399
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400 /* Derive initial jumbo mode from MTU assigned in
15401 * ether_setup() via the alloc_etherdev() call
15402 */
Joe Perches63c3a662011-04-26 08:12:10 +000015403 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15404 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015405
15406 /* Determine WakeOnLan speed to use. */
15407 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15408 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
15409 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
15410 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015411 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015412 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015413 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015414 }
15415
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015416 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015417 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015418
Linus Torvalds1da177e2005-04-16 15:20:36 -070015419 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15421 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015422 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070015423 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015424 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15425 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15426 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015427
15428 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
15429 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015430 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015431 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015432 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015433
Joe Perches63c3a662011-04-26 08:12:10 +000015434 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015435 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080015436 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015437 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015438 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070015439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070015440 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070015441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
15442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015443 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15444 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015445 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015446 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015447 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015448 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015449 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015452 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15453 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
15454 tp->phy_otp = tg3_read_otp_phycfg(tp);
15455 if (tp->phy_otp == 0)
15456 tp->phy_otp = TG3_OTP_DEFAULT;
15457 }
15458
Joe Perches63c3a662011-04-26 08:12:10 +000015459 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015460 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15461 else
15462 tp->mi_mode = MAC_MI_MODE_BASE;
15463
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
15466 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
15467 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15468
Matt Carlson4d958472011-04-20 07:57:35 +000015469 /* Set these bits to enable statistics workaround. */
15470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15471 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
15472 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
15473 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15474 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15475 }
15476
Matt Carlson321d32a2008-11-21 17:22:19 -080015477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015479 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015480
Matt Carlson158d7ab2008-05-29 01:37:54 -070015481 err = tg3_mdio_init(tp);
15482 if (err)
15483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015484
15485 /* Initialize data/descriptor byte/word swapping. */
15486 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000015487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15488 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15489 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15490 GRC_MODE_B2HRX_ENABLE |
15491 GRC_MODE_HTX2B_ENABLE |
15492 GRC_MODE_HOST_STACKUP);
15493 else
15494 val &= GRC_MODE_HOST_STACKUP;
15495
Linus Torvalds1da177e2005-04-16 15:20:36 -070015496 tw32(GRC_MODE, val | tp->grc_mode);
15497
15498 tg3_switch_clocks(tp);
15499
15500 /* Clear this out for sanity. */
15501 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15502
15503 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15504 &pci_state_reg);
15505 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015506 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015507 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
15508
15509 if (chiprevid == CHIPREV_ID_5701_A0 ||
15510 chiprevid == CHIPREV_ID_5701_B0 ||
15511 chiprevid == CHIPREV_ID_5701_B2 ||
15512 chiprevid == CHIPREV_ID_5701_B5) {
15513 void __iomem *sram_base;
15514
15515 /* Write some dummy words into the SRAM status block
15516 * area, see if it reads back correctly. If the return
15517 * value is bad, force enable the PCIX workaround.
15518 */
15519 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15520
15521 writel(0x00000000, sram_base);
15522 writel(0x00000000, sram_base + 4);
15523 writel(0xffffffff, sram_base + 4);
15524 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015525 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526 }
15527 }
15528
15529 udelay(50);
15530 tg3_nvram_init(tp);
15531
15532 grc_misc_cfg = tr32(GRC_MISC_CFG);
15533 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15534
Linus Torvalds1da177e2005-04-16 15:20:36 -070015535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
15536 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15537 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015538 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539
Joe Perches63c3a662011-04-26 08:12:10 +000015540 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000015541 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015542 tg3_flag_set(tp, TAGGED_STATUS);
15543 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015544 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15545 HOSTCC_MODE_CLRTICK_TXBD);
15546
15547 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15548 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15549 tp->misc_host_ctrl);
15550 }
15551
Matt Carlson3bda1252008-08-15 14:08:22 -070015552 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015553 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015554 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015555 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015556 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015557
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015558 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015559 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015560
15561 err = tg3_phy_probe(tp);
15562 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015563 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015564 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015565 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015566 }
15567
Matt Carlson184b8902010-04-05 10:19:25 +000015568 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015569 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015570
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015571 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15572 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015573 } else {
15574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015575 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015576 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015577 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578 }
15579
15580 /* 5700 {AX,BX} chips have a broken status block link
15581 * change bit implementation, so we must use the
15582 * status register in those cases.
15583 */
15584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015585 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015586 else
Joe Perches63c3a662011-04-26 08:12:10 +000015587 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588
15589 /* The led_ctrl is set during tg3_phy_probe, here we might
15590 * have to force the link status polling mechanism based
15591 * upon subsystem IDs.
15592 */
15593 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070015594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015595 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15596 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015597 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015598 }
15599
15600 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015601 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015602 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015603 else
Joe Perches63c3a662011-04-26 08:12:10 +000015604 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015605
Eric Dumazet9205fd92011-11-18 06:47:01 +000015606 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015607 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015609 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015610 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015611#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015612 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015613#endif
15614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015615
Matt Carlson2c49a442010-09-30 10:34:35 +000015616 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15617 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015618 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15619
Matt Carlson2c49a442010-09-30 10:34:35 +000015620 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015621
15622 /* Increment the rx prod index on the rx std ring by at most
15623 * 8 for these chips to workaround hw errata.
15624 */
15625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
15626 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
15627 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
15628 tp->rx_std_max_post = 8;
15629
Joe Perches63c3a662011-04-26 08:12:10 +000015630 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015631 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15632 PCIE_PWR_MGMT_L1_THRESH_MSK;
15633
Linus Torvalds1da177e2005-04-16 15:20:36 -070015634 return err;
15635}
15636
David S. Miller49b6e95f2007-03-29 01:38:42 -070015637#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015638static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015639{
15640 struct net_device *dev = tp->dev;
15641 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015642 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015643 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015644 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015645
David S. Miller49b6e95f2007-03-29 01:38:42 -070015646 addr = of_get_property(dp, "local-mac-address", &len);
15647 if (addr && len == 6) {
15648 memcpy(dev->dev_addr, addr, 6);
15649 memcpy(dev->perm_addr, dev->dev_addr, 6);
15650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015651 }
15652 return -ENODEV;
15653}
15654
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015655static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015656{
15657 struct net_device *dev = tp->dev;
15658
15659 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070015660 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015661 return 0;
15662}
15663#endif
15664
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015665static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015666{
15667 struct net_device *dev = tp->dev;
15668 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015669 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015670
David S. Miller49b6e95f2007-03-29 01:38:42 -070015671#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015672 if (!tg3_get_macaddr_sparc(tp))
15673 return 0;
15674#endif
15675
15676 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015678 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015679 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15680 mac_offset = 0xcc;
15681 if (tg3_nvram_lock(tp))
15682 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15683 else
15684 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015685 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015686 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015687 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015688 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015689 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000015690 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015691 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015692
15693 /* First try to get it from MAC address mailbox. */
15694 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15695 if ((hi >> 16) == 0x484b) {
15696 dev->dev_addr[0] = (hi >> 8) & 0xff;
15697 dev->dev_addr[1] = (hi >> 0) & 0xff;
15698
15699 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15700 dev->dev_addr[2] = (lo >> 24) & 0xff;
15701 dev->dev_addr[3] = (lo >> 16) & 0xff;
15702 dev->dev_addr[4] = (lo >> 8) & 0xff;
15703 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015704
Michael Chan008652b2006-03-27 23:14:53 -080015705 /* Some old bootcode may report a 0 MAC address in SRAM */
15706 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15707 }
15708 if (!addr_ok) {
15709 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015710 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015711 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015712 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015713 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15714 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015715 }
15716 /* Finally just fetch it out of the MAC control regs. */
15717 else {
15718 hi = tr32(MAC_ADDR_0_HIGH);
15719 lo = tr32(MAC_ADDR_0_LOW);
15720
15721 dev->dev_addr[5] = lo & 0xff;
15722 dev->dev_addr[4] = (lo >> 8) & 0xff;
15723 dev->dev_addr[3] = (lo >> 16) & 0xff;
15724 dev->dev_addr[2] = (lo >> 24) & 0xff;
15725 dev->dev_addr[1] = hi & 0xff;
15726 dev->dev_addr[0] = (hi >> 8) & 0xff;
15727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728 }
15729
15730 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015731#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015732 if (!tg3_get_default_macaddr_sparc(tp))
15733 return 0;
15734#endif
15735 return -EINVAL;
15736 }
John W. Linville2ff43692005-09-12 14:44:20 -070015737 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015738 return 0;
15739}
15740
David S. Miller59e6b432005-05-18 22:50:10 -070015741#define BOUNDARY_SINGLE_CACHELINE 1
15742#define BOUNDARY_MULTI_CACHELINE 2
15743
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015744static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015745{
15746 int cacheline_size;
15747 u8 byte;
15748 int goal;
15749
15750 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
15751 if (byte == 0)
15752 cacheline_size = 1024;
15753 else
15754 cacheline_size = (int) byte * 4;
15755
15756 /* On 5703 and later chips, the boundary bits have no
15757 * effect.
15758 */
15759 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15760 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015761 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070015762 goto out;
15763
15764#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
15765 goal = BOUNDARY_MULTI_CACHELINE;
15766#else
15767#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
15768 goal = BOUNDARY_SINGLE_CACHELINE;
15769#else
15770 goal = 0;
15771#endif
15772#endif
15773
Joe Perches63c3a662011-04-26 08:12:10 +000015774 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015775 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
15776 goto out;
15777 }
15778
David S. Miller59e6b432005-05-18 22:50:10 -070015779 if (!goal)
15780 goto out;
15781
15782 /* PCI controllers on most RISC systems tend to disconnect
15783 * when a device tries to burst across a cache-line boundary.
15784 * Therefore, letting tg3 do so just wastes PCI bandwidth.
15785 *
15786 * Unfortunately, for PCI-E there are only limited
15787 * write-side controls for this, and thus for reads
15788 * we will still get the disconnects. We'll also waste
15789 * these PCI cycles for both read and write for chips
15790 * other than 5700 and 5701 which do not implement the
15791 * boundary bits.
15792 */
Joe Perches63c3a662011-04-26 08:12:10 +000015793 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015794 switch (cacheline_size) {
15795 case 16:
15796 case 32:
15797 case 64:
15798 case 128:
15799 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15800 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15801 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15802 } else {
15803 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15804 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15805 }
15806 break;
15807
15808 case 256:
15809 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15810 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15811 break;
15812
15813 default:
15814 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15815 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15816 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015817 }
Joe Perches63c3a662011-04-26 08:12:10 +000015818 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015819 switch (cacheline_size) {
15820 case 16:
15821 case 32:
15822 case 64:
15823 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15824 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15825 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15826 break;
15827 }
15828 /* fallthrough */
15829 case 128:
15830 default:
15831 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15832 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15833 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015834 }
David S. Miller59e6b432005-05-18 22:50:10 -070015835 } else {
15836 switch (cacheline_size) {
15837 case 16:
15838 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15839 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15840 DMA_RWCTRL_WRITE_BNDRY_16);
15841 break;
15842 }
15843 /* fallthrough */
15844 case 32:
15845 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15846 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15847 DMA_RWCTRL_WRITE_BNDRY_32);
15848 break;
15849 }
15850 /* fallthrough */
15851 case 64:
15852 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15853 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15854 DMA_RWCTRL_WRITE_BNDRY_64);
15855 break;
15856 }
15857 /* fallthrough */
15858 case 128:
15859 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15860 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15861 DMA_RWCTRL_WRITE_BNDRY_128);
15862 break;
15863 }
15864 /* fallthrough */
15865 case 256:
15866 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15867 DMA_RWCTRL_WRITE_BNDRY_256);
15868 break;
15869 case 512:
15870 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15871 DMA_RWCTRL_WRITE_BNDRY_512);
15872 break;
15873 case 1024:
15874 default:
15875 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15876 DMA_RWCTRL_WRITE_BNDRY_1024);
15877 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015878 }
David S. Miller59e6b432005-05-18 22:50:10 -070015879 }
15880
15881out:
15882 return val;
15883}
15884
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015885static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
15886 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015887{
15888 struct tg3_internal_buffer_desc test_desc;
15889 u32 sram_dma_descs;
15890 int i, ret;
15891
15892 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15893
15894 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15895 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15896 tw32(RDMAC_STATUS, 0);
15897 tw32(WDMAC_STATUS, 0);
15898
15899 tw32(BUFMGR_MODE, 0);
15900 tw32(FTQ_RESET, 0);
15901
15902 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15903 test_desc.addr_lo = buf_dma & 0xffffffff;
15904 test_desc.nic_mbuf = 0x00002100;
15905 test_desc.len = size;
15906
15907 /*
15908 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15909 * the *second* time the tg3 driver was getting loaded after an
15910 * initial scan.
15911 *
15912 * Broadcom tells me:
15913 * ...the DMA engine is connected to the GRC block and a DMA
15914 * reset may affect the GRC block in some unpredictable way...
15915 * The behavior of resets to individual blocks has not been tested.
15916 *
15917 * Broadcom noted the GRC reset will also reset all sub-components.
15918 */
15919 if (to_device) {
15920 test_desc.cqid_sqid = (13 << 8) | 2;
15921
15922 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15923 udelay(40);
15924 } else {
15925 test_desc.cqid_sqid = (16 << 8) | 7;
15926
15927 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15928 udelay(40);
15929 }
15930 test_desc.flags = 0x00000005;
15931
15932 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15933 u32 val;
15934
15935 val = *(((u32 *)&test_desc) + i);
15936 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15937 sram_dma_descs + (i * sizeof(u32)));
15938 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15939 }
15940 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15941
Matt Carlson859a588792010-04-05 10:19:28 +000015942 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015943 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015944 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015945 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015946
15947 ret = -ENODEV;
15948 for (i = 0; i < 40; i++) {
15949 u32 val;
15950
15951 if (to_device)
15952 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15953 else
15954 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15955 if ((val & 0xffff) == sram_dma_descs) {
15956 ret = 0;
15957 break;
15958 }
15959
15960 udelay(100);
15961 }
15962
15963 return ret;
15964}
15965
David S. Millerded73402005-05-23 13:59:47 -070015966#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015967
Matt Carlson41434702011-03-09 16:58:22 +000015968static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015969 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15970 { },
15971};
15972
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015973static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015974{
15975 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015976 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015977 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015978
Matt Carlson4bae65c2010-11-24 08:31:52 +000015979 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15980 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015981 if (!buf) {
15982 ret = -ENOMEM;
15983 goto out_nofree;
15984 }
15985
15986 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15987 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15988
David S. Miller59e6b432005-05-18 22:50:10 -070015989 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015990
Joe Perches63c3a662011-04-26 08:12:10 +000015991 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015992 goto out;
15993
Joe Perches63c3a662011-04-26 08:12:10 +000015994 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015995 /* DMA read watermark not used on PCIE */
15996 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015997 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015998 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15999 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016000 tp->dma_rwctrl |= 0x003f0000;
16001 else
16002 tp->dma_rwctrl |= 0x003f000f;
16003 } else {
16004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
16006 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080016007 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016008
Michael Chan4a29cc22006-03-19 13:21:12 -080016009 /* If the 5704 is behind the EPB bridge, we can
16010 * do the less restrictive ONE_DMA workaround for
16011 * better performance.
16012 */
Joe Perches63c3a662011-04-26 08:12:10 +000016013 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080016014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16015 tp->dma_rwctrl |= 0x8000;
16016 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016017 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
16018
Michael Chan49afdeb2007-02-13 12:17:03 -080016019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
16020 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070016021 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080016022 tp->dma_rwctrl |=
16023 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
16024 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
16025 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070016026 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
16027 /* 5780 always in PCIX mode */
16028 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070016029 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
16030 /* 5714 always in PCIX mode */
16031 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016032 } else {
16033 tp->dma_rwctrl |= 0x001b000f;
16034 }
16035 }
16036
16037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
16038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
16039 tp->dma_rwctrl &= 0xfffffff0;
16040
16041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
16042 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
16043 /* Remove this if it causes problems for some boards. */
16044 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
16045
16046 /* On 5700/5701 chips, we need to set this bit.
16047 * Otherwise the chip will issue cacheline transactions
16048 * to streamable DMA memory with not all the byte
16049 * enables turned on. This is an error on several
16050 * RISC PCI controllers, in particular sparc64.
16051 *
16052 * On 5703/5704 chips, this bit has been reassigned
16053 * a different meaning. In particular, it is used
16054 * on those chips to enable a PCI-X workaround.
16055 */
16056 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
16057 }
16058
16059 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16060
16061#if 0
16062 /* Unneeded, already done by tg3_get_invariants. */
16063 tg3_switch_clocks(tp);
16064#endif
16065
Linus Torvalds1da177e2005-04-16 15:20:36 -070016066 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
16067 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
16068 goto out;
16069
David S. Miller59e6b432005-05-18 22:50:10 -070016070 /* It is best to perform DMA test with maximum write burst size
16071 * to expose the 5700/5701 write DMA bug.
16072 */
16073 saved_dma_rwctrl = tp->dma_rwctrl;
16074 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16075 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16076
Linus Torvalds1da177e2005-04-16 15:20:36 -070016077 while (1) {
16078 u32 *p = buf, i;
16079
16080 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
16081 p[i] = i;
16082
16083 /* Send the buffer to the chip. */
16084 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
16085 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000016086 dev_err(&tp->pdev->dev,
16087 "%s: Buffer write failed. err = %d\n",
16088 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016089 break;
16090 }
16091
16092#if 0
16093 /* validate data reached card RAM correctly. */
16094 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16095 u32 val;
16096 tg3_read_mem(tp, 0x2100 + (i*4), &val);
16097 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000016098 dev_err(&tp->pdev->dev,
16099 "%s: Buffer corrupted on device! "
16100 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016101 /* ret = -ENODEV here? */
16102 }
16103 p[i] = 0;
16104 }
16105#endif
16106 /* Now read it back. */
16107 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
16108 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000016109 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
16110 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016111 break;
16112 }
16113
16114 /* Verify it. */
16115 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
16116 if (p[i] == i)
16117 continue;
16118
David S. Miller59e6b432005-05-18 22:50:10 -070016119 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16120 DMA_RWCTRL_WRITE_BNDRY_16) {
16121 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016122 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
16123 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16124 break;
16125 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000016126 dev_err(&tp->pdev->dev,
16127 "%s: Buffer corrupted on read back! "
16128 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016129 ret = -ENODEV;
16130 goto out;
16131 }
16132 }
16133
16134 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
16135 /* Success. */
16136 ret = 0;
16137 break;
16138 }
16139 }
David S. Miller59e6b432005-05-18 22:50:10 -070016140 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
16141 DMA_RWCTRL_WRITE_BNDRY_16) {
16142 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070016143 * now look for chipsets that are known to expose the
16144 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070016145 */
Matt Carlson41434702011-03-09 16:58:22 +000016146 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016147 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
16148 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000016149 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070016150 /* Safe to use the calculated DMA boundary. */
16151 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000016152 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070016153
David S. Miller59e6b432005-05-18 22:50:10 -070016154 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
16155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016156
16157out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000016158 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016159out_nofree:
16160 return ret;
16161}
16162
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016163static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016164{
Joe Perches63c3a662011-04-26 08:12:10 +000016165 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000016166 tp->bufmgr_config.mbuf_read_dma_low_water =
16167 DEFAULT_MB_RDMA_LOW_WATER_5705;
16168 tp->bufmgr_config.mbuf_mac_rx_low_water =
16169 DEFAULT_MB_MACRX_LOW_WATER_57765;
16170 tp->bufmgr_config.mbuf_high_water =
16171 DEFAULT_MB_HIGH_WATER_57765;
16172
16173 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16174 DEFAULT_MB_RDMA_LOW_WATER_5705;
16175 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16176 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
16177 tp->bufmgr_config.mbuf_high_water_jumbo =
16178 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000016179 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070016180 tp->bufmgr_config.mbuf_read_dma_low_water =
16181 DEFAULT_MB_RDMA_LOW_WATER_5705;
16182 tp->bufmgr_config.mbuf_mac_rx_low_water =
16183 DEFAULT_MB_MACRX_LOW_WATER_5705;
16184 tp->bufmgr_config.mbuf_high_water =
16185 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070016186 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
16187 tp->bufmgr_config.mbuf_mac_rx_low_water =
16188 DEFAULT_MB_MACRX_LOW_WATER_5906;
16189 tp->bufmgr_config.mbuf_high_water =
16190 DEFAULT_MB_HIGH_WATER_5906;
16191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016192
Michael Chanfdfec1722005-07-25 12:31:48 -070016193 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16194 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
16195 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16196 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
16197 tp->bufmgr_config.mbuf_high_water_jumbo =
16198 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
16199 } else {
16200 tp->bufmgr_config.mbuf_read_dma_low_water =
16201 DEFAULT_MB_RDMA_LOW_WATER;
16202 tp->bufmgr_config.mbuf_mac_rx_low_water =
16203 DEFAULT_MB_MACRX_LOW_WATER;
16204 tp->bufmgr_config.mbuf_high_water =
16205 DEFAULT_MB_HIGH_WATER;
16206
16207 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
16208 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
16209 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
16210 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
16211 tp->bufmgr_config.mbuf_high_water_jumbo =
16212 DEFAULT_MB_HIGH_WATER_JUMBO;
16213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016214
16215 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
16216 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
16217}
16218
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016219static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016220{
Matt Carlson79eb6902010-02-17 15:17:03 +000016221 switch (tp->phy_id & TG3_PHY_ID_MASK) {
16222 case TG3_PHY_ID_BCM5400: return "5400";
16223 case TG3_PHY_ID_BCM5401: return "5401";
16224 case TG3_PHY_ID_BCM5411: return "5411";
16225 case TG3_PHY_ID_BCM5701: return "5701";
16226 case TG3_PHY_ID_BCM5703: return "5703";
16227 case TG3_PHY_ID_BCM5704: return "5704";
16228 case TG3_PHY_ID_BCM5705: return "5705";
16229 case TG3_PHY_ID_BCM5750: return "5750";
16230 case TG3_PHY_ID_BCM5752: return "5752";
16231 case TG3_PHY_ID_BCM5714: return "5714";
16232 case TG3_PHY_ID_BCM5780: return "5780";
16233 case TG3_PHY_ID_BCM5755: return "5755";
16234 case TG3_PHY_ID_BCM5787: return "5787";
16235 case TG3_PHY_ID_BCM5784: return "5784";
16236 case TG3_PHY_ID_BCM5756: return "5722/5756";
16237 case TG3_PHY_ID_BCM5906: return "5906";
16238 case TG3_PHY_ID_BCM5761: return "5761";
16239 case TG3_PHY_ID_BCM5718C: return "5718C";
16240 case TG3_PHY_ID_BCM5718S: return "5718S";
16241 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000016242 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000016243 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000016244 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070016245 case 0: return "serdes";
16246 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070016247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016248}
16249
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016250static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070016251{
Joe Perches63c3a662011-04-26 08:12:10 +000016252 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016253 strcpy(str, "PCI Express");
16254 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000016255 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070016256 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
16257
16258 strcpy(str, "PCIX:");
16259
16260 if ((clock_ctrl == 7) ||
16261 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
16262 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
16263 strcat(str, "133MHz");
16264 else if (clock_ctrl == 0)
16265 strcat(str, "33MHz");
16266 else if (clock_ctrl == 2)
16267 strcat(str, "50MHz");
16268 else if (clock_ctrl == 4)
16269 strcat(str, "66MHz");
16270 else if (clock_ctrl == 6)
16271 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070016272 } else {
16273 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000016274 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070016275 strcat(str, "66MHz");
16276 else
16277 strcat(str, "33MHz");
16278 }
Joe Perches63c3a662011-04-26 08:12:10 +000016279 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070016280 strcat(str, ":32-bit");
16281 else
16282 strcat(str, ":64-bit");
16283 return str;
16284}
16285
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016286static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016287{
16288 struct ethtool_coalesce *ec = &tp->coal;
16289
16290 memset(ec, 0, sizeof(*ec));
16291 ec->cmd = ETHTOOL_GCOALESCE;
16292 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16293 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16294 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16295 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16296 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16297 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16298 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16299 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16300 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16301
16302 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16303 HOSTCC_MODE_CLRTICK_TXBD)) {
16304 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16305 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16306 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16307 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16308 }
Michael Chand244c892005-07-05 14:42:33 -070016309
Joe Perches63c3a662011-04-26 08:12:10 +000016310 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016311 ec->rx_coalesce_usecs_irq = 0;
16312 ec->tx_coalesce_usecs_irq = 0;
16313 ec->stats_block_coalesce_usecs = 0;
16314 }
David S. Miller15f98502005-05-18 22:49:26 -070016315}
16316
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016317static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016318 const struct pci_device_id *ent)
16319{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016320 struct net_device *dev;
16321 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016322 int i, err, pm_cap;
16323 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016324 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016325 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016326 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016327
Joe Perches05dbe002010-02-17 19:44:19 +000016328 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016329
16330 err = pci_enable_device(pdev);
16331 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016332 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016333 return err;
16334 }
16335
Linus Torvalds1da177e2005-04-16 15:20:36 -070016336 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16337 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016338 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016339 goto err_out_disable_pdev;
16340 }
16341
16342 pci_set_master(pdev);
16343
16344 /* Find power-management capability. */
16345 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16346 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016347 dev_err(&pdev->dev,
16348 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016349 err = -EIO;
16350 goto err_out_free_res;
16351 }
16352
Matt Carlson16821282011-07-13 09:27:28 +000016353 err = pci_set_power_state(pdev, PCI_D0);
16354 if (err) {
16355 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16356 goto err_out_free_res;
16357 }
16358
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016359 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016360 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016361 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016362 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016363 }
16364
Linus Torvalds1da177e2005-04-16 15:20:36 -070016365 SET_NETDEV_DEV(dev, &pdev->dev);
16366
Linus Torvalds1da177e2005-04-16 15:20:36 -070016367 tp = netdev_priv(dev);
16368 tp->pdev = pdev;
16369 tp->dev = dev;
16370 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016371 tp->rx_mode = TG3_DEF_RX_MODE;
16372 tp->tx_mode = TG3_DEF_TX_MODE;
Nithin Nayak Sujir9c13cb82013-01-14 17:10:59 +000016373 tp->irq_sync = 1;
Matt Carlson8ef21422008-05-02 16:47:53 -070016374
Linus Torvalds1da177e2005-04-16 15:20:36 -070016375 if (tg3_debug > 0)
16376 tp->msg_enable = tg3_debug;
16377 else
16378 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16379
16380 /* The word/byte swap controls here control register access byte
16381 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16382 * setting below.
16383 */
16384 tp->misc_host_ctrl =
16385 MISC_HOST_CTRL_MASK_PCI_INT |
16386 MISC_HOST_CTRL_WORD_SWAP |
16387 MISC_HOST_CTRL_INDIR_ACCESS |
16388 MISC_HOST_CTRL_PCISTATE_RW;
16389
16390 /* The NONFRM (non-frame) byte/word swap controls take effect
16391 * on descriptor entries, anything which isn't packet data.
16392 *
16393 * The StrongARM chips on the board (one for tx, one for rx)
16394 * are running in big-endian mode.
16395 */
16396 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16397 GRC_MODE_WSWAP_NONFRM_DATA);
16398#ifdef __BIG_ENDIAN
16399 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16400#endif
16401 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016402 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016403 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016404
Matt Carlsond5fe4882008-11-21 17:20:32 -080016405 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016406 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016407 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016408 err = -ENOMEM;
16409 goto err_out_free_dev;
16410 }
16411
Matt Carlsonc9cab242011-07-13 09:27:27 +000016412 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16413 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16414 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16415 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16416 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016418 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16419 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
16420 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
16421 tg3_flag_set(tp, ENABLE_APE);
16422 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16423 if (!tp->aperegs) {
16424 dev_err(&pdev->dev,
16425 "Cannot map APE registers, aborting\n");
16426 err = -ENOMEM;
16427 goto err_out_iounmap;
16428 }
16429 }
16430
Linus Torvalds1da177e2005-04-16 15:20:36 -070016431 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16432 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016433
Linus Torvalds1da177e2005-04-16 15:20:36 -070016434 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016435 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016436 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016437 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016438
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016439 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016440 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016441 dev_err(&pdev->dev,
16442 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016443 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016444 }
16445
Michael Chan4a29cc22006-03-19 13:21:12 -080016446 /* The EPB bridge inside 5714, 5715, and 5780 and any
16447 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016448 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16449 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16450 * do DMA address check in tg3_start_xmit().
16451 */
Joe Perches63c3a662011-04-26 08:12:10 +000016452 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016453 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016454 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016455 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016456#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016457 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016458#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016459 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016460 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016461
16462 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016463 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016464 err = pci_set_dma_mask(pdev, dma_mask);
16465 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016466 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016467 err = pci_set_consistent_dma_mask(pdev,
16468 persist_dma_mask);
16469 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016470 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16471 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016472 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016473 }
16474 }
16475 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016476 if (err || dma_mask == DMA_BIT_MASK(32)) {
16477 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016478 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016479 dev_err(&pdev->dev,
16480 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016481 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016482 }
16483 }
16484
Michael Chanfdfec1722005-07-25 12:31:48 -070016485 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016486
Matt Carlson0da06062011-05-19 12:12:53 +000016487 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16488
16489 /* 5700 B0 chips do not support checksumming correctly due
16490 * to hardware bugs.
16491 */
16492 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
16493 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16494
16495 if (tg3_flag(tp, 5755_PLUS))
16496 features |= NETIF_F_IPV6_CSUM;
16497 }
16498
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016499 /* TSO is on by default on chips that support hardware TSO.
16500 * Firmware TSO on older chips gives lower performance, so it
16501 * is off by default, but can be enabled using ethtool.
16502 */
Joe Perches63c3a662011-04-26 08:12:10 +000016503 if ((tg3_flag(tp, HW_TSO_1) ||
16504 tg3_flag(tp, HW_TSO_2) ||
16505 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016506 (features & NETIF_F_IP_CSUM))
16507 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016508 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016509 if (features & NETIF_F_IPV6_CSUM)
16510 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016511 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000016512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070016513 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
16514 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000016515 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000016516 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016517 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016519
Matt Carlsond542fe22011-05-19 16:02:43 +000016520 dev->features |= features;
16521 dev->vlan_features |= features;
16522
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016523 /*
16524 * Add loopback capability only for a subset of devices that support
16525 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16526 * loopback for the remaining devices.
16527 */
16528 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
16529 !tg3_flag(tp, CPMU_PRESENT))
16530 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016531 features |= NETIF_F_LOOPBACK;
16532
Matt Carlson0da06062011-05-19 12:12:53 +000016533 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016534
Linus Torvalds1da177e2005-04-16 15:20:36 -070016535 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016536 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016537 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016538 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016539 tp->rx_pending = 63;
16540 }
16541
Linus Torvalds1da177e2005-04-16 15:20:36 -070016542 err = tg3_get_device_address(tp);
16543 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016544 dev_err(&pdev->dev,
16545 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016546 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016547 }
16548
Matt Carlsonc88864d2007-11-12 21:07:01 -080016549 /*
16550 * Reset chip in case UNDI or EFI driver did not shutdown
16551 * DMA self test will enable WDMAC and we'll see (spurious)
16552 * pending DMA on the PCI bus at that point.
16553 */
16554 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16555 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16556 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16557 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16558 }
16559
16560 err = tg3_test_dma(tp);
16561 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016562 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016563 goto err_out_apeunmap;
16564 }
16565
Matt Carlson78f90dc2009-11-13 13:03:42 +000016566 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16567 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16568 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016569 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016570 struct tg3_napi *tnapi = &tp->napi[i];
16571
16572 tnapi->tp = tp;
16573 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16574
16575 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016576 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016577 intmbx += 0x8;
16578 else
16579 intmbx += 0x4;
16580
16581 tnapi->consmbox = rcvmbx;
16582 tnapi->prodmbox = sndmbx;
16583
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016584 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016585 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016586 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016587 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016588
Joe Perches63c3a662011-04-26 08:12:10 +000016589 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016590 break;
16591
16592 /*
16593 * If we support MSIX, we'll be using RSS. If we're using
16594 * RSS, the first vector only handles link interrupts and the
16595 * remaining vectors handle rx and tx interrupts. Reuse the
16596 * mailbox values for the next iteration. The values we setup
16597 * above are still useful for the single vectored mode.
16598 */
16599 if (!i)
16600 continue;
16601
16602 rcvmbx += 0x8;
16603
16604 if (sndmbx & 0x4)
16605 sndmbx -= 0x4;
16606 else
16607 sndmbx += 0xc;
16608 }
16609
Matt Carlsonc88864d2007-11-12 21:07:01 -080016610 tg3_init_coal(tp);
16611
Michael Chanc49a1562006-12-17 17:07:29 -080016612 pci_set_drvdata(pdev, dev);
16613
Matt Carlsonfb4ce8a2012-12-03 19:37:00 +000016614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
16615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
16616 tg3_flag_set(tp, PTP_CAPABLE);
16617
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016618 if (tg3_flag(tp, 5717_PLUS)) {
16619 /* Resume a low-power mode */
16620 tg3_frob_aux_power(tp, false);
16621 }
16622
Matt Carlson21f76382012-02-22 12:35:21 +000016623 tg3_timer_init(tp);
16624
Linus Torvalds1da177e2005-04-16 15:20:36 -070016625 err = register_netdev(dev);
16626 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016627 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016628 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016629 }
16630
Joe Perches05dbe002010-02-17 19:44:19 +000016631 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16632 tp->board_part_number,
16633 tp->pci_chip_rev_id,
16634 tg3_bus_string(tp, str),
16635 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016636
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016637 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016638 struct phy_device *phydev;
16639 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016640 netdev_info(dev,
16641 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016642 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016643 } else {
16644 char *ethtype;
16645
16646 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16647 ethtype = "10/100Base-TX";
16648 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16649 ethtype = "1000Base-SX";
16650 else
16651 ethtype = "10/100/1000Base-T";
16652
Matt Carlson5129c3a2010-04-05 10:19:23 +000016653 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016654 "(WireSpeed[%d], EEE[%d])\n",
16655 tg3_phy_string(tp), ethtype,
16656 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16657 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016658 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016659
Joe Perches05dbe002010-02-17 19:44:19 +000016660 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016661 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016662 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016663 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016664 tg3_flag(tp, ENABLE_ASF) != 0,
16665 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016666 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16667 tp->dma_rwctrl,
16668 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16669 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016670
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016671 pci_save_state(pdev);
16672
Linus Torvalds1da177e2005-04-16 15:20:36 -070016673 return 0;
16674
Matt Carlson0d3031d2007-10-10 18:02:43 -070016675err_out_apeunmap:
16676 if (tp->aperegs) {
16677 iounmap(tp->aperegs);
16678 tp->aperegs = NULL;
16679 }
16680
Linus Torvalds1da177e2005-04-16 15:20:36 -070016681err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016682 if (tp->regs) {
16683 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016684 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016686
16687err_out_free_dev:
16688 free_netdev(dev);
16689
Matt Carlson16821282011-07-13 09:27:28 +000016690err_out_power_down:
16691 pci_set_power_state(pdev, PCI_D3hot);
16692
Linus Torvalds1da177e2005-04-16 15:20:36 -070016693err_out_free_res:
16694 pci_release_regions(pdev);
16695
16696err_out_disable_pdev:
16697 pci_disable_device(pdev);
16698 pci_set_drvdata(pdev, NULL);
16699 return err;
16700}
16701
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016702static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016703{
16704 struct net_device *dev = pci_get_drvdata(pdev);
16705
16706 if (dev) {
16707 struct tg3 *tp = netdev_priv(dev);
16708
Jesper Juhle3c55302012-04-09 22:50:15 +020016709 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016710
Matt Carlsondb219972011-11-04 09:15:03 +000016711 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016712
David S. Miller1805b2f2011-10-24 18:18:09 -040016713 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016714 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016715 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016716 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016717
Linus Torvalds1da177e2005-04-16 15:20:36 -070016718 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016719 if (tp->aperegs) {
16720 iounmap(tp->aperegs);
16721 tp->aperegs = NULL;
16722 }
Michael Chan68929142005-08-09 20:17:14 -070016723 if (tp->regs) {
16724 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016725 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016727 free_netdev(dev);
16728 pci_release_regions(pdev);
16729 pci_disable_device(pdev);
16730 pci_set_drvdata(pdev, NULL);
16731 }
16732}
16733
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016734#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016735static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016736{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016737 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016738 struct net_device *dev = pci_get_drvdata(pdev);
16739 struct tg3 *tp = netdev_priv(dev);
16740 int err;
16741
16742 if (!netif_running(dev))
16743 return 0;
16744
Matt Carlsondb219972011-11-04 09:15:03 +000016745 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016746 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016747 tg3_netif_stop(tp);
16748
Matt Carlson21f76382012-02-22 12:35:21 +000016749 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016750
David S. Millerf47c11e2005-06-24 20:18:35 -070016751 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016752 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070016753 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016754
16755 netif_device_detach(dev);
16756
David S. Millerf47c11e2005-06-24 20:18:35 -070016757 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070016758 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000016759 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070016760 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016761
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016762 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016763 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016764 int err2;
16765
David S. Millerf47c11e2005-06-24 20:18:35 -070016766 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016767
Joe Perches63c3a662011-04-26 08:12:10 +000016768 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016769 err2 = tg3_restart_hw(tp, 1);
16770 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070016771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016772
Matt Carlson21f76382012-02-22 12:35:21 +000016773 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016774
16775 netif_device_attach(dev);
16776 tg3_netif_start(tp);
16777
Michael Chanb9ec6c12006-07-25 16:37:27 -070016778out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016779 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016780
16781 if (!err2)
16782 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016783 }
16784
16785 return err;
16786}
16787
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016788static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016789{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016790 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016791 struct net_device *dev = pci_get_drvdata(pdev);
16792 struct tg3 *tp = netdev_priv(dev);
16793 int err;
16794
16795 if (!netif_running(dev))
16796 return 0;
16797
Linus Torvalds1da177e2005-04-16 15:20:36 -070016798 netif_device_attach(dev);
16799
David S. Millerf47c11e2005-06-24 20:18:35 -070016800 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016801
Joe Perches63c3a662011-04-26 08:12:10 +000016802 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016803 err = tg3_restart_hw(tp, 1);
16804 if (err)
16805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016806
Matt Carlson21f76382012-02-22 12:35:21 +000016807 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016808
Linus Torvalds1da177e2005-04-16 15:20:36 -070016809 tg3_netif_start(tp);
16810
Michael Chanb9ec6c12006-07-25 16:37:27 -070016811out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016812 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016813
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016814 if (!err)
16815 tg3_phy_start(tp);
16816
Michael Chanb9ec6c12006-07-25 16:37:27 -070016817 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016818}
16819
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016820static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016821#define TG3_PM_OPS (&tg3_pm_ops)
16822
16823#else
16824
16825#define TG3_PM_OPS NULL
16826
16827#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016828
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016829/**
16830 * tg3_io_error_detected - called when PCI error is detected
16831 * @pdev: Pointer to PCI device
16832 * @state: The current pci connection state
16833 *
16834 * This function is called after a PCI bus error affecting
16835 * this device has been detected.
16836 */
16837static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16838 pci_channel_state_t state)
16839{
16840 struct net_device *netdev = pci_get_drvdata(pdev);
16841 struct tg3 *tp = netdev_priv(netdev);
16842 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16843
16844 netdev_info(netdev, "PCI I/O error detected\n");
16845
16846 rtnl_lock();
16847
16848 if (!netif_running(netdev))
16849 goto done;
16850
16851 tg3_phy_stop(tp);
16852
16853 tg3_netif_stop(tp);
16854
Matt Carlson21f76382012-02-22 12:35:21 +000016855 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016856
16857 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016858 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016859
16860 netif_device_detach(netdev);
16861
16862 /* Clean up software state, even if MMIO is blocked */
16863 tg3_full_lock(tp, 0);
16864 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16865 tg3_full_unlock(tp);
16866
16867done:
16868 if (state == pci_channel_io_perm_failure)
16869 err = PCI_ERS_RESULT_DISCONNECT;
16870 else
16871 pci_disable_device(pdev);
16872
16873 rtnl_unlock();
16874
16875 return err;
16876}
16877
16878/**
16879 * tg3_io_slot_reset - called after the pci bus has been reset.
16880 * @pdev: Pointer to PCI device
16881 *
16882 * Restart the card from scratch, as if from a cold-boot.
16883 * At this point, the card has exprienced a hard reset,
16884 * followed by fixups by BIOS, and has its config space
16885 * set up identically to what it was at cold boot.
16886 */
16887static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16888{
16889 struct net_device *netdev = pci_get_drvdata(pdev);
16890 struct tg3 *tp = netdev_priv(netdev);
16891 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16892 int err;
16893
16894 rtnl_lock();
16895
16896 if (pci_enable_device(pdev)) {
16897 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16898 goto done;
16899 }
16900
16901 pci_set_master(pdev);
16902 pci_restore_state(pdev);
16903 pci_save_state(pdev);
16904
16905 if (!netif_running(netdev)) {
16906 rc = PCI_ERS_RESULT_RECOVERED;
16907 goto done;
16908 }
16909
16910 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016911 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016912 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016913
16914 rc = PCI_ERS_RESULT_RECOVERED;
16915
16916done:
16917 rtnl_unlock();
16918
16919 return rc;
16920}
16921
16922/**
16923 * tg3_io_resume - called when traffic can start flowing again.
16924 * @pdev: Pointer to PCI device
16925 *
16926 * This callback is called when the error recovery driver tells
16927 * us that its OK to resume normal operation.
16928 */
16929static void tg3_io_resume(struct pci_dev *pdev)
16930{
16931 struct net_device *netdev = pci_get_drvdata(pdev);
16932 struct tg3 *tp = netdev_priv(netdev);
16933 int err;
16934
16935 rtnl_lock();
16936
16937 if (!netif_running(netdev))
16938 goto done;
16939
16940 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016941 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016942 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016943 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016944 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016945 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16946 goto done;
16947 }
16948
16949 netif_device_attach(netdev);
16950
Matt Carlson21f76382012-02-22 12:35:21 +000016951 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016952
16953 tg3_netif_start(tp);
16954
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016955 tg3_full_unlock(tp);
16956
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016957 tg3_phy_start(tp);
16958
16959done:
16960 rtnl_unlock();
16961}
16962
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070016963static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016964 .error_detected = tg3_io_error_detected,
16965 .slot_reset = tg3_io_slot_reset,
16966 .resume = tg3_io_resume
16967};
16968
Linus Torvalds1da177e2005-04-16 15:20:36 -070016969static struct pci_driver tg3_driver = {
16970 .name = DRV_MODULE_NAME,
16971 .id_table = tg3_pci_tbl,
16972 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016973 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016974 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016975 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016976};
16977
16978static int __init tg3_init(void)
16979{
Jeff Garzik29917622006-08-19 17:48:59 -040016980 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016981}
16982
16983static void __exit tg3_cleanup(void)
16984{
16985 pci_unregister_driver(&tg3_driver);
16986}
16987
16988module_init(tg3_init);
16989module_exit(tg3_cleanup);